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)
39 class LifxLightBulb : public LightBulb
43 const static int64_t GET_BULB_VERSION_RESEND_WAIT_SECONDS = 10;
46 IoTUDP *communicationSocket;
47 char bulbMacAddress[8];
49 bool sendSocketFlag = false;
51 // Current Bulb Values
53 int currentSaturation = 0;
54 int currentBrightness = 65535;
55 int currentTemperature = 9000;
56 bool bulbIsOn = false;
58 bool didAlreadyInit = false;
59 bool didGetBulbVersion = false;
63 mutex settingBulbColorMutex;
64 mutex settingBulbTemperatureMutex;
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;
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;
82 IoTSet<void*>* lb_addresses; // IoTSet<IoTDeviceAddress*>* lb_addresses
90 LifxLightBulb(IPAddress ipAddress, char* macAddress, int srcPort);
92 // Initialize the lightbulb
97 double getSaturation();
98 double getBrightness();
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);
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.
119 void workerFunction(LifxLightBulb* llb);
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();
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]);
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);
156 LightState* parseLightStateMessage(char* payloadData);
157 int parseLightStatePowerMessage(char* payloadData);
159 void handleStateVersionMessageReceived(char* payloadData);
160 void handleLightStateMessageReceived(char* payloadData);