1 #ifndef _LIFXLIGHTBULB_HPP__
2 #define _LIFXLIGHTBULB_HPP__
13 #include "LightBulb.hpp"
15 #include "IoTRMIUtil.hpp"
18 #include "IoTDeviceAddress.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<void*>* lb_addresses; // IoTSet<IoTDeviceAddress*>* lb_addresses
97 //LifxLightBulb(IoTSet<IoTDeviceAddress*>* _devAddress, string macAddress);
98 LifxLightBulb(IoTSet<void*>* _devAddress, string macAddress);
100 // Initialize the lightbulb
105 double getSaturation();
106 double getBrightness();
107 int getTemperature();
108 double getHueRangeLowerBound();
109 double getHueRangeUpperBound();
110 double getSaturationRangeLowerBound();
111 double getSaturationRangeUpperBound();
112 double getBrightnessRangeLowerBound();
113 double getBrightnessRangeUpperBound();
114 int getTemperatureRangeLowerBound();
115 int getTemperatureRangeUpperBound();
116 void setTemperature(int _temperature);
117 void setColor(double _hue, double _saturation, double _brightness);
122 // Communication helpers
123 void receivedPacket(char* packetData);
124 void sendPacket(char* packetData, int len);
125 // Worker function which runs the while loop for receiving data from the bulb.
127 void workerFunction(LifxLightBulb* llb);
130 void sendGetServicePacket();
131 void sendGetHostInfoPacket();
132 void sendGetHostFirmwarePacket();
133 void sendGetWifiInfoPacket();
134 void sendGetWifiFirmwarePacket();
135 void sendGetPowerPacket();
136 void sendSetPowerPacket(int level);
137 void sendGetLabelPacket();
138 void sendSetLabelPacket(string label);
139 void sendGetVersionPacket();
140 void sendGetInfoPacket();
141 void sendGetLocationPacket();
142 void sendGetGroupPacket();
145 void sendGetLightStatePacket();
146 void sendSetLightColorPacket(BulbColor* bulbColor, long duration);
147 void sendGetLightPowerPacket();
148 void sendSetLightPowerPacket(int level, long duration);
149 void sendEchoRequestPacket(char data[64]);
152 DeviceStateService* parseDeviceStateServiceMessage(char* payloadData);
153 DeviceStateHostInfo* parseDeviceStateHostInfoMessage(char* payloadData);
154 DeviceStateHostFirmware* parseDeviceStateHostFirmwareMessage(char* payloadData);
155 DeviceStateWifiInfo* parseDeviceStateWifiInfoMessage(char* payloadData);
156 DeviceStateWifiFirmware* parseDeviceStateWifiFirmwareMessage(char* payloadData);
157 int parseStatePowerMessage(char* payloadData);
158 DeviceStateVersion* parseDeviceStateVersionMessage(char* payloadData);
159 DeviceStateInfo* parseDeviceStateInfoMessage(char* payloadData);
160 DeviceStateLocation* parseDeviceStateLocationMessage(char* payloadData);
161 DeviceStateGroup* parseDeviceStateGroupMessage(char* payloadData);
164 LightState* parseLightStateMessage(char* payloadData);
165 int parseLightStatePowerMessage(char* payloadData);
167 void handleStateVersionMessageReceived(char* payloadData);
168 void handleLightStateMessageReceived(char* payloadData);