Adjustments and clean-ups for Makefile for C++ benchmark and drivers
[iot2.git] / benchmarks / Cpp / Lifxtest / Lifxtest.cpp
1 #include <iostream>
2 #include <chrono>
3 #include <thread>
4
5 #include "Lifxtest.hpp"
6 #include "LifxLightBulb.cpp"
7 #include "LabRoom.cpp"
8 #include "Iterator.hpp"
9
10
11 // External create, destroy, and init functions
12 extern "C" void* createLifxtest(void** params) {
13         // Arguments: IoTSet<void*>* lifx_light_bulb
14         //return new Lifxtest((IoTSet<void*>*) params[0]);
15         return new Lifxtest((IoTSet<void*>*) params[0], (IoTSet<void*>*) params[1], (IoTRelation<void*,void*>*) params[2]);
16 }
17
18
19 extern "C" void destroyLifxtest(void* t) {
20         Lifxtest* lt = (Lifxtest*) t;
21         delete lt;
22 }
23
24
25 extern "C" void initLifxtest(void* t) {
26         Lifxtest* lt = (Lifxtest*) t;
27         lt->init();
28 }
29
30
31 // Empty constructor (for testing)
32 Lifxtest::Lifxtest() {
33         log.open("Lifxtest_object_cpp.log");
34         log << "lifx_light_bulb initialized!" << endl;
35 }
36
37
38 // Constructor with only 1 IoTSet object (lifx_light_bulb)
39 Lifxtest::Lifxtest(IoTSet<void*>* _lifx_light_bulb) {
40
41         log.open("Lifxtest_object_cpp.log");
42         lifx_light_bulb = _lifx_light_bulb;
43         log << "lifx_light_bulb initialized!" << endl;
44 }
45
46
47 // Constructor with 2 IoTSet and 1 IoTRelation objects
48 Lifxtest::Lifxtest(IoTSet<void*>* _lifx_light_bulb, IoTSet<void*>* _lab_room, IoTRelation<void*,void*>* _roomLightRelation) {
49
50         log.open("Lifxtest_object_cpp.log");
51         lifx_light_bulb = _lifx_light_bulb;
52         lab_room = _lab_room;
53         roomLightRelation = _roomLightRelation;
54         log << "lifx_light_bulb initialized!" << endl;
55 }
56
57
58 // Constructor with void** argument
59 Lifxtest::Lifxtest(void** args) {
60
61         log.open("Lifxtest_object_cpp.log");
62         lifx_light_bulb = (IoTSet<void*>*) args[0];
63         log << "lifx_light_bulb initialized!" << endl;
64 }
65
66
67 Lifxtest::~Lifxtest() {
68 }
69
70
71 /*void Lifxtest::init() {
72
73         unordered_set<void*>* bulbSet = lifx_light_bulb->values();
74         //for (unordered_set<void*>::const_iterator itr = bulbSet->begin(); itr != bulbSet->end(); ++itr) {
75         log << "Get into Lifxtest init()!" << endl;
76         for (auto itr = bulbSet->begin(); itr != bulbSet->end(); ++itr) {
77                 log << "Iteration init()!" << endl;
78                 //LightBulb* lifx = (LightBulb*) *itr;
79                 LightBulbTest* lifx = (LightBulbTest*) *itr;
80                 log << "Getting object!" << endl;
81                 lifx->init();
82                 log << "Executing init!" << endl;
83                 this_thread::sleep_for (chrono::milliseconds(1000));
84
85                 for (int i = 0; i < 10; i++) {
86
87                         lifx->init();
88 //                      lifx->turnOff();
89                         //cout << "Turning off!" << endl;
90                         log << "Turning off!" << endl;
91                         this_thread::sleep_for (chrono::milliseconds(1000));
92 //                      lifx->turnOn();
93                         //cout << "Turning on!" << endl;
94                         log << "Turning on!" << i << endl;
95                         this_thread::sleep_for (chrono::milliseconds(1000));
96                 }
97                 
98 */
99 /*              for (int i = 2500; i < 9000; i += 100) {
100                         //cout << "Adjusting Temp: " << i << endl;
101                         log << "Adjusting Temp: " << i << endl;
102                         lifx->setTemperature(i);
103                         this_thread::sleep_for (chrono::milliseconds(100));
104                 }
105
106                 for (int i = 9000; i > 2500; i -= 100) {
107                         //cout << "Adjusting Temp: " << i << endl;
108                         log << "Adjusting Temp: " << i << endl;
109                         lifx->setTemperature(i);
110                         this_thread::sleep_for (chrono::milliseconds(100));
111                 }
112
113                 for (int i = 100; i > 0; i -= 10) {
114                         //cout << "Adjusting Brightness: " << i << endl;
115                         log << "Adjusting Brightness: " << i << endl;
116                         lifx->setColor(lifx->getHue(), lifx->getSaturation(), i);
117                         this_thread::sleep_for (chrono::milliseconds(500));
118                 }
119
120                 for (int i = 0; i < 100; i += 10) {
121                         //cout << "Adjusting Brightness: " << i << endl;
122                         log << "Adjusting Brightness: " << i << endl;
123                         lifx->setColor(lifx->getHue(), lifx->getSaturation(), i);
124                         this_thread::sleep_for (chrono::milliseconds(500));
125                 }
126                 lifx->turnOff();
127         }
128
129         log << "End of iteration.. closing!" << endl;
130         log.close();
131         //while(true) { }       // Testing infinite loop - will need to do "pkill IoTSlave"
132 }*/
133
134
135 void Lifxtest::init() {
136
137         unordered_set<void*>* bulbSet = lifx_light_bulb->values();
138         unordered_set<void*>* roomSet = lab_room->values();
139         unordered_multimap<void*,void*>* roomLightRel = roomLightRelation->values();
140         log << "Size of map: " << roomLightRel->size() << endl;
141         //for (unordered_set<void*>::const_iterator itr = bulbSet->begin(); itr != bulbSet->end(); ++itr) {
142         for (auto itr = roomSet->begin(); itr != roomSet->end(); ++itr) {
143                 log << "Getting Room!" << endl;
144                 //Room* rs = (Room*) *itr;
145                 RoomSmart* rs = (RoomSmart*) *itr;
146                 log << "Getting Room! ID: " << rs->getRoomID() << endl;
147                 auto itrLight = roomLightRel->find(rs);
148
149                 if (itrLight == roomLightRel->end())
150                         log << "No match!" << endl;
151                 //while (itrLight != roomLightRel->end()) {
152                 else {
153                         //LightBulb* lifx = (LightBulb*) itrLight->second;
154                         log << "Getting LightBulb!" << endl;
155                         LightBulbTest* lifx = (LightBulbTest*) itrLight->second;
156                         log << "Executing init!" << endl;
157                         lifx->init();
158                         for (int i = 0; i < 10; i++) {
159                                 lifx->turnOff();
160                                 //cout << "Turning off!" << endl;
161                                 log << "Turning off!" << endl;
162                                 this_thread::sleep_for (chrono::milliseconds(1000));
163                                 lifx->turnOn();
164                                 //cout << "Turning on!" << endl;
165                                 log << "Turning on!" << i << endl;
166                                 this_thread::sleep_for (chrono::milliseconds(1000));
167                         }
168                         for (int i = 2500; i < 9000; i += 100) {
169                                 //cout << "Adjusting Temp: " << i << endl;
170                                 log << "Adjusting Temp: " << i << endl;
171                                 lifx->setTemperature(i);
172                                 this_thread::sleep_for (chrono::milliseconds(100));
173                         }
174
175                         for (int i = 9000; i > 2500; i -= 100) {
176                                 //cout << "Adjusting Temp: " << i << endl;
177                                 log << "Adjusting Temp: " << i << endl;
178                                 lifx->setTemperature(i);
179                                 this_thread::sleep_for (chrono::milliseconds(100));
180                         }
181
182                         for (int i = 100; i > 0; i -= 10) {
183                                 //cout << "Adjusting Brightness: " << i << endl;
184                                 log << "Adjusting Brightness: " << i << endl;
185                                 lifx->setColor(lifx->getHue(), lifx->getSaturation(), i);
186                                 this_thread::sleep_for (chrono::milliseconds(500));
187                         }
188
189                         for (int i = 0; i < 100; i += 10) {
190                                 //cout << "Adjusting Brightness: " << i << endl;
191                                 log << "Adjusting Brightness: " << i << endl;
192                                 lifx->setColor(lifx->getHue(), lifx->getSaturation(), i);
193                                 this_thread::sleep_for (chrono::milliseconds(500));
194                         }
195                         //++itrLight;
196                 }
197                 log << "End of one LightBulb!" << endl << endl;
198         }
199                 
200         log << "End of iteration.. closing!" << endl;
201         log.close();
202         //while(true) { }       // Testing infinite loop - will need to do "pkill IoTSlave"
203 }
204
205 /*
206 int main(int argc, char *argv[])
207 {
208         // LightBulb #1
209         string macAddress1 = "D073D5128E300000";
210         string devIPAddress1 = "192.168.2.126";
211         IoTDeviceAddress* devAddress1 = new IoTDeviceAddress(devIPAddress1, 12345, 56700, false, false);
212         unordered_set<void*>* myset1 = new unordered_set<void*>();
213         myset1->insert(devAddress1);
214         IoTSet<void*>* setDevAddress1 = new IoTSet<void*>(myset1);
215         LifxLightBulb *llb1 = new LifxLightBulb(setDevAddress1, macAddress1);
216         //cout << "Generated LifxLightBulb object!" << endl;
217
218         // LightBulb #2
219         string macAddress2 = "D073D50241DA0000";
220         string devIPAddress2 = "192.168.2.232";
221         IoTDeviceAddress* devAddress2 = new IoTDeviceAddress(devIPAddress2, 12346, 56700, false, false);
222         unordered_set<void*>* myset2 = new unordered_set<void*>();
223         myset2->insert(devAddress2);
224         IoTSet<void*>* setDevAddress2 = new IoTSet<void*>(myset2);
225         LifxLightBulb *llb2 = new LifxLightBulb(setDevAddress2, macAddress2);
226
227         // Set of lightbulbs
228         unordered_set<void*>* setLb = new unordered_set<void*>();
229         setLb->insert(llb1);
230         setLb->insert(llb2);
231         IoTSet<void*>* lbSet = new IoTSet<void*>(setLb);
232
233         // Set of rooms
234         LabRoom *lr1 = new LabRoom();
235         LabRoom *lr2 = new LabRoom();
236         unordered_set<void*>* setLR = new unordered_set<void*>();
237         setLR->insert(lr1);
238         setLR->insert(lr2);
239         IoTSet<void*>* lrSet = new IoTSet<void*>(setLR);
240
241         pair<void*,void*>* pair1 = new pair<void*,void*>(lr1, llb1);
242         pair<void*,void*>* pair2 = new pair<void*,void*>(lr2, llb2);
243         unordered_multimap<void*,void*>* mmap = new unordered_multimap<void*,void*>();
244         mmap->insert(*pair1);
245         mmap->insert(*pair2);
246         IoTRelation<void*,void*>* rlRel = new IoTRelation<void*,void*>(mmap);
247
248         //void* args[1];
249         //args[0] = (void*) lbSet;
250         //Lifxtest *lt = new Lifxtest(args);
251         Lifxtest *lt = new Lifxtest(lbSet, lrSet, rlRel);
252         lt->init();
253
254         //delete llb1;
255         //delete llb2;
256         delete devAddress1;
257         delete devAddress2;
258         delete myset1;
259         delete myset2;
260         delete setDevAddress1;
261         delete setDevAddress2;
262         delete setLb;
263         delete lbSet;
264         delete lr1;
265         delete lr2;
266         delete lrSet;
267         delete pair1;
268         delete pair2;
269
270         return 0;
271 }*/
272
273