1 #ifndef _LIFXLIGHTBULB_HPP__
2 #define _LIFXLIGHTBULB_HPP__
14 #include "LightBulb.hpp"
16 #include "IoTRMIUtil.hpp"
19 #include "IoTDeviceAddress.hpp"
20 #include "Iterator.hpp"
22 // Helper classes for LifxLightBulb
23 #include "LifxHeader.hpp"
24 #include "BulbColor.hpp"
25 #include "DeviceStateGroup.hpp"
26 #include "DeviceStateHostFirmware.hpp"
27 #include "DeviceStateHostInfo.hpp"
28 #include "DeviceStateInfo.hpp"
29 #include "DeviceStateLocation.hpp"
30 #include "DeviceStateService.hpp"
31 #include "DeviceStateVersion.hpp"
32 #include "DeviceStateWifiFirmware.hpp"
33 #include "DeviceStateWifiInfo.hpp"
34 #include "LightState.hpp"
39 // Driver LifxLightBulb
40 // Implemented based on LightBulb virtual class (interface)
43 std::atomic<bool> didAlreadyInit(false);
44 std::atomic<bool> didGetBulbVersion(false);
46 class LifxLightBulb : public LightBulb
50 const static int64_t GET_BULB_VERSION_RESEND_WAIT_SECONDS = 10;
53 IoTUDP *communicationSocket;
54 char bulbMacAddress[8];
56 //static Semaphore socketMutex = new Semaphore(1);
57 bool sendSocketFlag = false;
59 // Current Bulb Values
61 int currentSaturation = 0;
62 int currentBrightness = 65535;
63 int currentTemperature = 9000;
64 bool bulbIsOn = false;
67 atomic<bool> didAlreadyInit;
68 atomic<bool> didGetBulbVersion;
72 mutex settingBulbColorMutex;
73 mutex settingBulbTemperatureMutex;
76 // color and temperature ranges for the bulbs
77 int hueLowerBound = 0;
78 int hueUpperBound = 0;
79 int saturationLowerBound = 0;
80 int saturationUpperBound = 0;
81 int brightnessLowerBound = 0;
82 int brightnessUpperBound = 0;
83 int temperatureLowerBound = 2500;
84 int temperatureUpperBound = 9000;
86 // Check if a state change was requested, used to poll the bulb for if the bulb did
87 // preform the requested state change
88 bool stateDidChange = false;
91 IoTSet<IoTDeviceAddress*> lb_addresses;
97 LifxLightBulb(IoTSet<IoTDeviceAddress*> _devAddress, string macAddress);
99 // Initialize the lightbulb
104 double getSaturation();
105 double getBrightness();
106 int getTemperature();
107 double getHueRangeLowerBound();
108 double getHueRangeUpperBound();
109 double getSaturationRangeLowerBound();
110 double getSaturationRangeUpperBound();
111 double getBrightnessRangeLowerBound();
112 double getBrightnessRangeUpperBound();
113 int getTemperatureRangeLowerBound();
114 int getTemperatureRangeUpperBound();
115 void setTemperature(int _temperature);
116 void setColor(double _hue, double _saturation, double _brightness);
121 // Communication helpers
122 void receivedPacket(char* packetData);
123 void sendPacket(char* packetData, int len);
124 // Worker function which runs the while loop for receiving data from the bulb.
126 void workerFunction(LifxLightBulb* llb);
129 void sendGetServicePacket();
130 void sendGetHostInfoPacket();
131 void sendGetHostFirmwarePacket();
132 void sendGetWifiInfoPacket();
133 void sendGetWifiFirmwarePacket();
134 void sendGetPowerPacket();
135 void sendSetPowerPacket(int level);
136 void sendGetLabelPacket();
137 void sendSetLabelPacket(string label);
138 void sendGetVersionPacket();
139 void sendGetInfoPacket();
140 void sendGetLocationPacket();
141 void sendGetGroupPacket();
144 void sendGetLightStatePacket();
145 void sendSetLightColorPacket(BulbColor* bulbColor, long duration);
146 void sendGetLightPowerPacket();
147 void sendSetLightPowerPacket(int level, long duration);
148 void sendEchoRequestPacket(char data[64]);
151 DeviceStateService* parseDeviceStateServiceMessage(char* payloadData);
152 DeviceStateHostInfo* parseDeviceStateHostInfoMessage(char* payloadData);
153 DeviceStateHostFirmware* parseDeviceStateHostFirmwareMessage(char* payloadData);
154 DeviceStateWifiInfo* parseDeviceStateWifiInfoMessage(char* payloadData);
155 DeviceStateWifiFirmware* parseDeviceStateWifiFirmwareMessage(char* payloadData);
156 int parseStatePowerMessage(char* payloadData);
157 DeviceStateVersion* parseDeviceStateVersionMessage(char* payloadData);
158 DeviceStateInfo* parseDeviceStateInfoMessage(char* payloadData);
159 DeviceStateLocation* parseDeviceStateLocationMessage(char* payloadData);
160 DeviceStateGroup* parseDeviceStateGroupMessage(char* payloadData);
163 LightState* parseLightStateMessage(char* payloadData);
164 int parseLightStatePowerMessage(char* payloadData);
166 void handleStateVersionMessageReceived(char* payloadData);
167 void handleLightStateMessageReceived(char* payloadData);