You are on page 1of 3

Ex.

No: 3
CONTROL AND MONITOR IOT
Date: 05.9.23 DEVICES

Aim:
You are developing a Dart program to control and monitor IoT devices in a
smart home. The program should manage devices, their status, and allow users to
interact with them through commands. You need to implement a system to represent
IoT devices and handle user commands.

Program Code:
import 'dart:io';

class IOT {
List<String> name = [];
List<String> status = [];
}
class IOTController {
IOT i = IOT();
void add() {
stdout.write("Enter the name of device:");
i.name.add(stdin.readLineSync() ?? "");
stdout.write("Enter the status of device:");
i.status.add(stdin.readLineSync() ?? "");
}
void remove() {
dis();
int idx = -1;
stdout.write("Enter the name of device to remove:");
idx = i.name.indexOf(stdin.readLineSync() ?? "");
i.name.removeAt(idx);
i.status.removeAt(idx);
}
void toggledevice() {
dis();
int idx = -1;
stdout.write("Enter the name of device to toggle:");

1
idx = i.name.indexOf(stdin.readLineSync() ?? "");
if (i.status[idx] == 'ON')
i.status[idx] = 'OFF';
else
i.status[idx] = 'ON';
}
void dis() {
print("Device List:");
for (int j = 0; j < i.name.length; j++) {
print(i.name[j] + "=>" + i.status[j]);
}
}
void dis_on() {
print("Power on device List:");
for (int j = 0; j < i.name.length; j++) {
if (i.status[j] == "ON") print(i.name[j] + "=>" + i.status[j]);
}
}
}
void main() {
IOTController a = IOTController();
int t = 0;
while (t == 0) {
print(
"\n1.Add a device\n2.Remove a device\n3.ToggleDevice\n4.Display all devices\
n5.Display ON devices\n6.Exit\n");
stdout.write("Choose an Option:");
var option = stdin.readLineSync() ?? "";
if (option == '1') a.add();
if (option == '2') a.remove();
if (option == '3') a.toggledevice();
if (option == '4') a.dis();
if (option == '5') a.dis_on();
if (option == '6') t = 1;
}
print("You have exited the program.");
}

2
Output:

Result:
The Dart program to control and monitor IoT devices in a smart home has
been implemented successfully.

You might also like