-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProximitySwitch.cpp
108 lines (86 loc) · 2.05 KB
/
ProximitySwitch.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <fstream>
#include <regex>
#include "lightbulb.h"
using namespace std;
bool check() { //Returns true is 192.168.1.5 responds to ping
string result = executeSystemCommand("ping 192.168.1.5 -c 3 -W 1 -q", false);
regex r(".*, (\\d+) received.*");
smatch m;
regex_search(result, m, r);
if (m.size() == 2) {
return stoi(m[1])>0;
}
else {
cout << "Regex match error." << endl;
return false;
}
}
int main() {
ifstream ifile;
ifile.open("lastCheck.txt");
bool lastCheck = false;
if (ifile) {
ifile >> lastCheck;
ifile.close();
}
else {
cout << "Error: couldn't open lastCheck.txt. Exiting." << endl;
return 0;
}
bool currentCheck = check();
if (lastCheck != currentCheck) {
if (currentCheck) {
//turn lights on
cout << "Checking lights to see if they are off..." << endl;
Lightbulb bulb1("192.168.1.24");
Lightbulb bulb2("192.168.1.25");
Lightbulb lamp("192.168.1.26");
cout << "Turning on lights..." << endl;
if (bulb1.getConnected()) {
bulb1.setAllowTimeouts(false);
bulb1.turnOn();
}
if (bulb2.getConnected()) {
bulb2.setAllowTimeouts(false);
bulb2.turnOn();
}
if (lamp.getConnected()) {
lamp.setAllowTimeouts(false);
lamp.turnOn();
}
cout << "...finished." << endl;
}
else {
//turn lights off
cout << "Checking lights to see if they are on..." << endl;
Lightbulb bulb1("192.168.1.24");
Lightbulb bulb2("192.168.1.25");
Lightbulb lamp("192.168.1.26");
cout << "Turning off lights..." << endl;
if (bulb1.getConnected()) {
bulb1.setAllowTimeouts(false);
bulb1.turnOff();
}
if (bulb2.getConnected()) {
bulb2.setAllowTimeouts(false);
bulb2.turnOff();
}
if (lamp.getConnected()) {
lamp.setAllowTimeouts(false);
lamp.turnOff();
}
cout << "...finished." << endl;
}
ofstream ofile("lastCheck.txt");
if (ofile) {
ofile << currentCheck;
}
else {
cout << "Error writing new lastCheck value." << endl;
}
}
else {
cout << "No change in device status. Exiting." << endl;
}
}