Initial version of Lifx lightbulb controller in C++ for Fidelius
[iotcloud.git] / version2 / src / RPi / LifxLightBulb.h
1 #ifndef _LIFXLIGHTBULB_H__
2 #define _LIFXLIGHTBULB_H__
3 #include <iostream>
4 #include <fstream>
5 #include <atomic>
6 #include <mutex>
7 #include <thread>
8 #include <chrono>
9
10 #include <string.h>
11 #include <time.h>
12
13 #include "LightBulb.h"
14 //#include "Socket.h"
15 #include "IoTSet.h"
16 #include "IoTUDP.h"
17 #include "IoTDeviceAddress.h"
18
19 // Helper classes for LifxLightBulb
20 #include "LifxHeader.h"
21 #include "BulbColor.h"
22 #include "DeviceStateGroup.h"
23 #include "DeviceStateHostFirmware.h"
24 #include "DeviceStateHostInfo.h"
25 #include "DeviceStateInfo.h"
26 #include "DeviceStateLocation.h"
27 #include "DeviceStateService.h"
28 #include "DeviceStateVersion.h"
29 #include "DeviceStateWifiFirmware.h"
30 #include "DeviceStateWifiInfo.h"
31 #include "LightState.h"
32
33
34 using namespace std;
35
36 // Driver LifxLightBulb
37 // Implemented based on LightBulb virtual class (interface)
38
39 //std::atomic
40 //std::atomic<bool> didAlreadyInit(false);
41 //std::atomic<bool> didGetBulbVersion(false);
42
43 class LifxLightBulb : public LightBulb
44 {
45         private:
46                 // Constants
47                 const static int64_t GET_BULB_VERSION_RESEND_WAIT_SECONDS = 10;
48
49                 // Variables
50                 IoTUDP *communicationSocket;
51                 char bulbMacAddress[8];
52                 //TODO:
53                 //static Semaphore socketMutex = new Semaphore(1);
54                 bool sendSocketFlag = false;
55
56                 // Current Bulb Values
57                 int currentHue = 0;
58                 int currentSaturation = 0;
59                 int currentBrightness = 65535;
60                 int currentTemperature = 9000;
61                 bool bulbIsOn = false;
62
63                 //std::atomic
64                 //atomic<bool> didAlreadyInit;
65                 bool didAlreadyInit;
66                 //atomic<bool> didGetBulbVersion;
67                 bool didGetBulbVersion;
68
69                 // Mutex locks
70                 mutex socketMutex;
71                 mutex settingBulbColorMutex;
72                 mutex settingBulbTemperatureMutex;
73                 mutex bulbStateMutex;
74
75                 // color and temperature ranges for the bulbs
76                 int hueLowerBound = 0;
77                 int hueUpperBound = 0;
78                 int saturationLowerBound = 0;
79                 int saturationUpperBound = 0;
80                 int brightnessLowerBound = 0;
81                 int brightnessUpperBound = 0;
82                 int temperatureLowerBound = 2500;
83                 int temperatureUpperBound = 9000;
84
85                 // Check if a state change was requested, used to poll the bulb for if the bulb did
86                 // preform the requested state change
87                 bool stateDidChange = false;
88
89                 // Device address
90                 IoTSet<void*>* lb_addresses;    // IoTSet<IoTDeviceAddress*>* lb_addresses
91
92                 // Logging
93                 ofstream log;
94         public:
95
96                 // Constructor
97                 LifxLightBulb();
98                 LifxLightBulb(uint8_t* ipAddress, string macAddress, int srcPort);
99                 //LifxLightBulb(IoTSet<IoTDeviceAddress*>* _devAddress, string macAddress);
100                 LifxLightBulb(IoTSet<void*>* _devAddress, string macAddress);
101                 ~LifxLightBulb();
102                 // Initialize the lightbulb
103                 void init();
104                 void turnOff();
105                 void turnOn();
106                 double getHue();
107                 double getSaturation();
108                 double getBrightness();
109                 int getTemperature();
110                 double getHueRangeLowerBound();
111                 double getHueRangeUpperBound();
112                 double getSaturationRangeLowerBound();
113                 double getSaturationRangeUpperBound();
114                 double getBrightnessRangeLowerBound();
115                 double getBrightnessRangeUpperBound();
116                 int getTemperatureRangeLowerBound();
117                 int getTemperatureRangeUpperBound();
118                 void setTemperature(int _temperature);
119                 void setColor(double _hue, double _saturation, double _brightness);
120                 bool getState();
121
122         private:
123                 // Private functions
124                 // Communication helpers
125                 void receivedPacket(char* packetData);
126                 void sendPacket(char* packetData, int len);
127                 // Worker function which runs the while loop for receiving data from the bulb.
128                 // Is blocking.
129                 void workerFunction(LifxLightBulb* llb);
130                 //  Sending
131                 //  Device Messages
132                 void sendGetServicePacket();
133                 void sendGetHostInfoPacket();
134                 void sendGetHostFirmwarePacket();
135                 void sendGetWifiInfoPacket();
136                 void sendGetWifiFirmwarePacket();
137                 void sendGetPowerPacket();
138                 void sendSetPowerPacket(int level);
139                 void sendGetLabelPacket();
140                 void sendSetLabelPacket(string label);
141                 void sendGetVersionPacket();
142                 void sendGetInfoPacket();
143                 void sendGetLocationPacket();
144                 void sendGetGroupPacket();
145                 //  Sending
146                 //  Light Messages
147                 void sendGetLightStatePacket();
148                 void sendSetLightColorPacket(BulbColor* bulbColor, long duration);
149                 void sendGetLightPowerPacket();
150                 void sendSetLightPowerPacket(int level, long duration);
151                 void sendEchoRequestPacket(char data[64]);
152                 // Receiving
153                 // Device Messages
154                 DeviceStateService* parseDeviceStateServiceMessage(char* payloadData);
155                 DeviceStateHostInfo* parseDeviceStateHostInfoMessage(char* payloadData);
156                 DeviceStateHostFirmware* parseDeviceStateHostFirmwareMessage(char* payloadData);
157                 DeviceStateWifiInfo* parseDeviceStateWifiInfoMessage(char* payloadData);
158                 DeviceStateWifiFirmware* parseDeviceStateWifiFirmwareMessage(char* payloadData);
159                 int parseStatePowerMessage(char* payloadData);
160                 DeviceStateVersion* parseDeviceStateVersionMessage(char* payloadData);
161                 DeviceStateInfo* parseDeviceStateInfoMessage(char* payloadData);
162                 DeviceStateLocation* parseDeviceStateLocationMessage(char* payloadData);
163                 DeviceStateGroup* parseDeviceStateGroupMessage(char* payloadData);
164                 // Receiving
165                 // Light Messages
166                 LightState* parseLightStateMessage(char* payloadData);
167                 int parseLightStatePowerMessage(char* payloadData);
168                 // Private Handlers
169                 void handleStateVersionMessageReceived(char* payloadData);
170                 void handleLightStateMessageReceived(char* payloadData);
171 };
172
173 #endif