1 #ifndef _LIFXLIGHTBULB_HPP__
2 #define _LIFXLIGHTBULB_HPP__
12 #include "LightBulb.hpp"
14 #include "IoTRMIUtil.hpp"
17 #include "IoTDeviceAddress.hpp"
18 #include "Iterator.hpp"
20 // Helper classes for LifxLightBulb
21 #include "LifxHeader.hpp"
22 #include "BulbColor.hpp"
23 #include "DeviceStateGroup.hpp"
24 #include "DeviceStateHostFirmware.hpp"
25 #include "DeviceStateHostInfo.hpp"
26 #include "DeviceStateInfo.hpp"
27 #include "DeviceStateLocation.hpp"
28 #include "DeviceStateService.hpp"
29 #include "DeviceStateVersion.hpp"
30 #include "DeviceStateWifiFirmware.hpp"
31 #include "DeviceStateWifiInfo.hpp"
32 #include "LightState.hpp"
37 // Driver LifxLightBulb
38 // Implemented based on LightBulb virtual class (interface)
41 std::atomic<bool> didAlreadyInit(false);
42 std::atomic<bool> didGetBulbVersion(false);
44 class LifxLightBulb : public LightBulb
48 const static int64_t GET_BULB_VERSION_RESEND_WAIT_SECONDS = 10;
51 IoTUDP *communicationSocket;
52 char bulbMacAddress[8];
54 //static Semaphore socketMutex = new Semaphore(1);
55 bool sendSocketFlag = false;
57 // Current Bulb Values
59 int currentSaturation = 0;
60 int currentBrightness = 65535;
61 int currentTemperature = 9000;
62 bool bulbIsOn = false;
65 atomic<bool> didAlreadyInit;
66 atomic<bool> didGetBulbVersion;
70 mutex settingBulbColorMutex;
71 mutex settingBulbTemperatureMutex;
74 // color and temperature ranges for the bulbs
75 int hueLowerBound = 0;
76 int hueUpperBound = 0;
77 int saturationLowerBound = 0;
78 int saturationUpperBound = 0;
79 int brightnessLowerBound = 0;
80 int brightnessUpperBound = 0;
81 int temperatureLowerBound = 2500;
82 int temperatureUpperBound = 9000;
84 // Check if a state change was requested, used to poll the bulb for if the bulb did
85 // preform the requested state change
86 bool stateDidChange = false;
89 IoTSet<IoTDeviceAddress*> lb_addresses;
95 LifxLightBulb(IoTSet<IoTDeviceAddress*> _devAddress, string macAddress);
97 // Initialize the lightbulb
102 double getSaturation();
103 double getBrightness();
104 int getTemperature();
105 double getHueRangeLowerBound();
106 double getHueRangeUpperBound();
107 double getSaturationRangeLowerBound();
108 double getSaturationRangeUpperBound();
109 double getBrightnessRangeLowerBound();
110 double getBrightnessRangeUpperBound();
111 int getTemperatureRangeLowerBound();
112 int getTemperatureRangeUpperBound();
113 void setTemperature(int _temperature);
114 void setColor(double _hue, double _saturation, double _brightness);
119 // Communication helpers
120 void receivedPacket(char* packetData);
121 void sendPacket(char* packetData, int len);
122 // Worker function which runs the while loop for receiving data from the bulb.
124 void workerFunction(LifxLightBulb* llb);
127 void sendGetServicePacket();
128 void sendGetHostInfoPacket();
129 void sendGetHostFirmwarePacket();
130 void sendGetWifiInfoPacket();
131 void sendGetWifiFirmwarePacket();
132 void sendGetPowerPacket();
133 void sendSetPowerPacket(int level);
134 void sendGetLabelPacket();
135 void sendSetLabelPacket(string label);
136 void sendGetVersionPacket();
137 void sendGetInfoPacket();
138 void sendGetLocationPacket();
139 void sendGetGroupPacket();
142 void sendGetLightStatePacket();
143 void sendSetLightColorPacket(BulbColor* bulbColor, long duration);
144 void sendGetLightPowerPacket();
145 void sendSetLightPowerPacket(int level, long duration);
146 void sendEchoRequestPacket(char data[64]);
149 DeviceStateService* parseDeviceStateServiceMessage(char* payloadData);
150 DeviceStateHostInfo* parseDeviceStateHostInfoMessage(char* payloadData);
151 DeviceStateHostFirmware* parseDeviceStateHostFirmwareMessage(char* payloadData);
152 DeviceStateWifiInfo* parseDeviceStateWifiInfoMessage(char* payloadData);
153 DeviceStateWifiFirmware* parseDeviceStateWifiFirmwareMessage(char* payloadData);
154 int parseStatePowerMessage(char* payloadData);
155 DeviceStateVersion* parseDeviceStateVersionMessage(char* payloadData);
156 DeviceStateInfo* parseDeviceStateInfoMessage(char* payloadData);
157 DeviceStateLocation* parseDeviceStateLocationMessage(char* payloadData);
158 DeviceStateGroup* parseDeviceStateGroupMessage(char* payloadData);
161 LightState* parseLightStateMessage(char* payloadData);
162 int parseLightStatePowerMessage(char* payloadData);
164 void handleStateVersionMessageReceived(char* payloadData);
165 void handleLightStateMessageReceived(char* payloadData);