Cleaning up drivers/Cpp, Cpp/Lifxtest, virtuals, and iotrmi/C++ (revisiting the C...
[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
9 // External create, destroy, and init functions
10 extern "C" void* createLifxtest(void** params) {
11         // Arguments: IoTSet<void*>* lifx_light_bulb, IoTSet<void*>* lab_room, IoTRelation<void*,void*> roomLightRelation
12         return new Lifxtest((IoTSet<void*>*) params[0], (IoTSet<void*>*) params[1], (IoTRelation<void*,void*>*) params[2]);
13 }
14
15
16 extern "C" void destroyLifxtest(void* t) {
17         Lifxtest* lt = (Lifxtest*) t;
18         delete lt;
19 }
20
21
22 extern "C" void initLifxtest(void* t) {
23         Lifxtest* lt = (Lifxtest*) t;
24         lt->init();
25 }
26
27
28 // Empty constructor (for testing)
29 Lifxtest::Lifxtest() {
30 }
31
32
33 // Constructor with 2 IoTSet and 1 IoTRelation objects
34 Lifxtest::Lifxtest(IoTSet<void*>* _lifx_light_bulb, IoTSet<void*>* _lab_room, IoTRelation<void*,void*>* _roomLightRelation) {
35
36         log.open("/home/iotuser/iot2/bin/iotruntime/log/Lifxtest_object_cpp.log");
37         lifx_light_bulb = _lifx_light_bulb;
38         lab_room = _lab_room;
39         roomLightRelation = _roomLightRelation;
40         log << "lifx_light_bulb initialized!" << endl;
41 }
42
43 Lifxtest::~Lifxtest() {
44 }
45
46 void Lifxtest::init() {
47
48         unordered_set<void*>* bulbSet = lifx_light_bulb->values();
49         unordered_set<void*>* roomSet = lab_room->values();
50         unordered_multimap<void*,void*>* roomLightRel = roomLightRelation->values();
51         log << "Size of map: " << roomLightRel->size() << endl;
52         
53         for (auto itr = roomSet->begin(); itr != roomSet->end(); ++itr) {
54                 RoomSmart* rs = (RoomSmart*) *itr;
55                 log << "Now turning on lights in room with ID: " << rs->getRoomID() << endl;
56                 auto itrLight = roomLightRel->find(rs);
57
58                 if (itrLight == roomLightRel->end())
59                         log << "No matching light! - should not get here at all!" << endl;
60                 else {
61                         log << "Getting LightBulb!" << endl;
62                         LightBulbTest* lifx = (LightBulbTest*) itrLight->second;
63                         log << "Executing init!" << endl;
64                         lifx->init();
65                         for (int i = 0; i < 10; i++) {
66                                 lifx->turnOff();
67                                 log << "Turning off!" << endl;
68                                 this_thread::sleep_for (chrono::milliseconds(1000));
69                                 lifx->turnOn();
70                                 log << "Turning on!" << i << endl;
71                                 this_thread::sleep_for (chrono::milliseconds(1000));
72                         }
73                         /*      Note: The bottom part has not yet been heavily tested and
74                         this might get the execution stuck at some point.
75                         We are suspecting that this is due to the complex interaction
76                         of the multiple threads that we have.
77                         for (int i = 2500; i < 9000; i += 100) {
78                                 log << "Adjusting Temp: " << i << endl;
79                                 lifx->setTemperature(i);
80                                 this_thread::sleep_for (chrono::milliseconds(100));
81                         }
82
83                         for (int i = 9000; i > 2500; i -= 100) {
84                                 log << "Adjusting Temp: " << i << endl;
85                                 lifx->setTemperature(i);
86                                 this_thread::sleep_for (chrono::milliseconds(100));
87                         }
88                         
89                         double hue = lifx->getHue();
90                         double saturation = lifx->getSaturation();
91
92                         for (int i = 100; i > 0; i -= 10) {
93                                 log << "Adjusting Brightness: " << i << endl;
94                                 lifx->setColor(hue, saturation, i);
95                                 this_thread::sleep_for (chrono::milliseconds(500));
96                         }
97
98                         for (int i = 0; i < 100; i += 10) {
99                                 log << "Adjusting Brightness: " << i << endl;
100                                 lifx->setColor(hue, saturation, i);
101                                 this_thread::sleep_for (chrono::milliseconds(500));
102                         }*/
103                         //lifx->turnOff();
104                 }
105                 log << "End of one LightBulb!" << endl << endl;
106         }
107                 
108         log << "End of iteration.. closing!" << endl;
109         log.close();
110         //while(true) { }       // Testing infinite loop - will need to do "pkill IoTSlave"
111 }