Completing Lifx driver that uses Particle cloud; Particle phone app provides a nice...
[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 class LifxLightBulb : public LightBulb
40 {
41         private:
42                 // Constants
43                 const static int64_t GET_BULB_VERSION_RESEND_WAIT_SECONDS = 10;
44
45                 // Variables
46                 IoTUDP *communicationSocket;
47                 char bulbMacAddress[8];
48                 //TODO:
49                 bool sendSocketFlag = false;
50
51                 // Current Bulb Values
52                 int currentHue = 0;
53                 int currentSaturation = 0;
54                 int currentBrightness = 65535;
55                 int currentTemperature = 9000;
56                 bool bulbIsOn = false;
57
58                 bool didAlreadyInit = false;
59                 bool didGetBulbVersion = false;
60
61                 // Mutex locks
62                 mutex socketMutex;
63                 mutex settingBulbColorMutex;
64                 mutex settingBulbTemperatureMutex;
65                 mutex bulbStateMutex;
66
67                 // color and temperature ranges for the bulbs
68                 int hueLowerBound = 0;
69                 int hueUpperBound = 0;
70                 int saturationLowerBound = 0;
71                 int saturationUpperBound = 0;
72                 int brightnessLowerBound = 0;
73                 int brightnessUpperBound = 0;
74                 int temperatureLowerBound = 2500;
75                 int temperatureUpperBound = 9000;
76
77                 // Check if a state change was requested, used to poll the bulb for if the bulb did
78                 // preform the requested state change
79                 bool stateDidChange = false;
80
81                 // Device address
82                 IoTSet<void*>* lb_addresses;    // IoTSet<IoTDeviceAddress*>* lb_addresses
83
84                 // Logging
85                 ofstream log;
86         public:
87
88                 // Constructor
89                 LifxLightBulb();
90                 LifxLightBulb(IPAddress ipAddress, char* macAddress, int srcPort);
91                 ~LifxLightBulb();
92                 // Initialize the lightbulb
93                 void init();
94                 void turnOff();
95                 void turnOn();
96                 double getHue();
97                 double getSaturation();
98                 double getBrightness();
99                 int getTemperature();
100                 double getHueRangeLowerBound();
101                 double getHueRangeUpperBound();
102                 double getSaturationRangeLowerBound();
103                 double getSaturationRangeUpperBound();
104                 double getBrightnessRangeLowerBound();
105                 double getBrightnessRangeUpperBound();
106                 int getTemperatureRangeLowerBound();
107                 int getTemperatureRangeUpperBound();
108                 void setTemperature(int _temperature);
109                 void setColor(double _hue, double _saturation, double _brightness);
110                 bool getState();
111
112         private:
113                 // Private functions
114                 // Communication helpers
115                 void receivedPacket(char* packetData);
116                 void sendPacket(char* packetData, int len);
117                 // Worker function which runs the while loop for receiving data from the bulb.
118                 // Is blocking.
119                 void workerFunction(LifxLightBulb* llb);
120                 //  Sending
121                 //  Device Messages
122                 void sendGetServicePacket();
123                 void sendGetHostInfoPacket();
124                 void sendGetHostFirmwarePacket();
125                 void sendGetWifiInfoPacket();
126                 void sendGetWifiFirmwarePacket();
127                 void sendGetPowerPacket();
128                 void sendSetPowerPacket(int level);
129                 void sendGetLabelPacket();
130                 void sendSetLabelPacket(string label);
131                 void sendGetVersionPacket();
132                 void sendGetInfoPacket();
133                 void sendGetLocationPacket();
134                 void sendGetGroupPacket();
135                 //  Sending
136                 //  Light Messages
137                 void sendGetLightStatePacket();
138                 void sendSetLightColorPacket(BulbColor* bulbColor, long duration);
139                 void sendGetLightPowerPacket();
140                 void sendSetLightPowerPacket(int level, long duration);
141                 void sendEchoRequestPacket(char data[64]);
142                 // Receiving
143                 // Device Messages
144                 DeviceStateService* parseDeviceStateServiceMessage(char* payloadData);
145                 DeviceStateHostInfo* parseDeviceStateHostInfoMessage(char* payloadData);
146                 DeviceStateHostFirmware* parseDeviceStateHostFirmwareMessage(char* payloadData);
147                 DeviceStateWifiInfo* parseDeviceStateWifiInfoMessage(char* payloadData);
148                 DeviceStateWifiFirmware* parseDeviceStateWifiFirmwareMessage(char* payloadData);
149                 int parseStatePowerMessage(char* payloadData);
150                 DeviceStateVersion* parseDeviceStateVersionMessage(char* payloadData);
151                 DeviceStateInfo* parseDeviceStateInfoMessage(char* payloadData);
152                 DeviceStateLocation* parseDeviceStateLocationMessage(char* payloadData);
153                 DeviceStateGroup* parseDeviceStateGroupMessage(char* payloadData);
154                 // Receiving
155                 // Light Messages
156                 LightState* parseLightStateMessage(char* payloadData);
157                 int parseLightStatePowerMessage(char* payloadData);
158                 // Private Handlers
159                 void handleStateVersionMessageReceived(char* payloadData);
160                 void handleLightStateMessageReceived(char* payloadData);
161 };
162
163 #endif