1 #ifndef _LIFXLIGHTBULB_HPP__
2 #define _LIFXLIGHTBULB_HPP__
13 #include "LightBulb.hpp"
15 #include "IoTRMIUtil.hpp"
18 #include "IoTDeviceAddress.hpp"
19 #include "Iterator.hpp"
21 // Helper classes for LifxLightBulb
22 #include "LifxHeader.hpp"
23 #include "BulbColor.hpp"
24 #include "DeviceStateGroup.hpp"
25 #include "DeviceStateHostFirmware.hpp"
26 #include "DeviceStateHostInfo.hpp"
27 #include "DeviceStateInfo.hpp"
28 #include "DeviceStateLocation.hpp"
29 #include "DeviceStateService.hpp"
30 #include "DeviceStateVersion.hpp"
31 #include "DeviceStateWifiFirmware.hpp"
32 #include "DeviceStateWifiInfo.hpp"
33 #include "LightState.hpp"
38 // Driver LifxLightBulb
39 // Implemented based on LightBulb virtual class (interface)
42 std::atomic<bool> didAlreadyInit(false);
43 std::atomic<bool> didGetBulbVersion(false);
45 class LifxLightBulb : public LightBulb
49 const static int64_t GET_BULB_VERSION_RESEND_WAIT_SECONDS = 10;
52 IoTUDP *communicationSocket;
53 char bulbMacAddress[8];
55 //static Semaphore socketMutex = new Semaphore(1);
56 bool sendSocketFlag = false;
58 // Current Bulb Values
60 int currentSaturation = 0;
61 int currentBrightness = 65535;
62 int currentTemperature = 9000;
63 bool bulbIsOn = false;
66 atomic<bool> didAlreadyInit;
67 atomic<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(IoTSet<IoTDeviceAddress*>* _devAddress, string macAddress);
99 LifxLightBulb(IoTSet<void*>* _devAddress, string macAddress);
101 // Initialize the lightbulb
106 double getSaturation();
107 double getBrightness();
108 int getTemperature();
109 double getHueRangeLowerBound();
110 double getHueRangeUpperBound();
111 double getSaturationRangeLowerBound();
112 double getSaturationRangeUpperBound();
113 double getBrightnessRangeLowerBound();
114 double getBrightnessRangeUpperBound();
115 int getTemperatureRangeLowerBound();
116 int getTemperatureRangeUpperBound();
117 void setTemperature(int _temperature);
118 void setColor(double _hue, double _saturation, double _brightness);
123 // Communication helpers
124 void receivedPacket(char* packetData);
125 void sendPacket(char* packetData, int len);
126 // Worker function which runs the while loop for receiving data from the bulb.
128 void workerFunction(LifxLightBulb* llb);
131 void sendGetServicePacket();
132 void sendGetHostInfoPacket();
133 void sendGetHostFirmwarePacket();
134 void sendGetWifiInfoPacket();
135 void sendGetWifiFirmwarePacket();
136 void sendGetPowerPacket();
137 void sendSetPowerPacket(int level);
138 void sendGetLabelPacket();
139 void sendSetLabelPacket(string label);
140 void sendGetVersionPacket();
141 void sendGetInfoPacket();
142 void sendGetLocationPacket();
143 void sendGetGroupPacket();
146 void sendGetLightStatePacket();
147 void sendSetLightColorPacket(BulbColor* bulbColor, long duration);
148 void sendGetLightPowerPacket();
149 void sendSetLightPowerPacket(int level, long duration);
150 void sendEchoRequestPacket(char data[64]);
153 DeviceStateService* parseDeviceStateServiceMessage(char* payloadData);
154 DeviceStateHostInfo* parseDeviceStateHostInfoMessage(char* payloadData);
155 DeviceStateHostFirmware* parseDeviceStateHostFirmwareMessage(char* payloadData);
156 DeviceStateWifiInfo* parseDeviceStateWifiInfoMessage(char* payloadData);
157 DeviceStateWifiFirmware* parseDeviceStateWifiFirmwareMessage(char* payloadData);
158 int parseStatePowerMessage(char* payloadData);
159 DeviceStateVersion* parseDeviceStateVersionMessage(char* payloadData);
160 DeviceStateInfo* parseDeviceStateInfoMessage(char* payloadData);
161 DeviceStateLocation* parseDeviceStateLocationMessage(char* payloadData);
162 DeviceStateGroup* parseDeviceStateGroupMessage(char* payloadData);
165 LightState* parseLightStateMessage(char* payloadData);
166 int parseLightStatePowerMessage(char* payloadData);
168 void handleStateVersionMessageReceived(char* payloadData);
169 void handleLightStateMessageReceived(char* payloadData);