1 #ifndef _LIFXLIGHTBULB_H__
2 #define _LIFXLIGHTBULB_H__
13 #include "LightBulb.h"
17 #include "IoTDeviceAddress.h"
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"
36 // Driver LifxLightBulb
37 // Implemented based on LightBulb virtual class (interface)
40 //std::atomic<bool> didAlreadyInit(false);
41 //std::atomic<bool> didGetBulbVersion(false);
43 class LifxLightBulb : public LightBulb
47 const static int64_t GET_BULB_VERSION_RESEND_WAIT_SECONDS = 10;
50 IoTUDP *communicationSocket;
51 char bulbMacAddress[8];
53 //static Semaphore socketMutex = new Semaphore(1);
54 bool sendSocketFlag = false;
56 // Current Bulb Values
58 int currentSaturation = 0;
59 int currentBrightness = 65535;
60 int currentTemperature = 9000;
61 bool bulbIsOn = false;
64 //atomic<bool> didAlreadyInit;
66 //atomic<bool> didGetBulbVersion;
67 bool didGetBulbVersion;
71 mutex settingBulbColorMutex;
72 mutex settingBulbTemperatureMutex;
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;
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;
90 IoTSet<void*>* lb_addresses; // IoTSet<IoTDeviceAddress*>* lb_addresses
98 LifxLightBulb(uint8_t* ipAddress, string macAddress, int srcPort);
99 //LifxLightBulb(IoTSet<IoTDeviceAddress*>* _devAddress, string macAddress);
100 LifxLightBulb(IoTSet<void*>* _devAddress, string macAddress);
102 // Initialize the lightbulb
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);
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.
129 void workerFunction(LifxLightBulb* llb);
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();
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]);
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);
166 LightState* parseLightStateMessage(char* payloadData);
167 int parseLightStatePowerMessage(char* payloadData);
169 void handleStateVersionMessageReceived(char* payloadData);
170 void handleLightStateMessageReceived(char* payloadData);