1 #include "stdafx.h"
2 #include "PhilipsHueSensors.h"
3 #include "../../main/mainworker.h"
4 #include <json/json.h>
5 
CPHSensorState(const Json::Value & state,const Json::Value & capabilities)6 CPHSensorState::CPHSensorState(const Json::Value& state, const Json::Value& capabilities)
7 {
8 	m_lastupdated = (state["lastupdated"] != Json::Value::null) ? state["lastupdated"].asString() : "none";
9 	m_buttonevent = (state["buttonevent"] != Json::Value::null) ? state["buttonevent"].asInt() : 0;
10 	m_presence = (state["presence"] != Json::Value::null) ? state["presence"].asBool() : false;
11 	m_temperature = (state["temperature"] != Json::Value::null) ? state["temperature"].asInt() : 0;
12 	m_lightlevel = (state["lightlevel"] != Json::Value::null) ? state["lightlevel"].asInt() : 0;
13 	m_dark = (state["dark"] != Json::Value::null) ? state["dark"].asBool() : false;
14 	m_daylight = (state["daylight"] != Json::Value::null) ? state["daylight"].asBool() : false;
15 
16 	m_button_nr = m_buttonevent;
17 	if (m_buttonevent > 0)
18 		ExtractButtonInfo(state, capabilities);
19 }
20 
ExtractButtonInfo(const Json::Value & state,const Json::Value & capabilities)21 void CPHSensorState::ExtractButtonInfo(const Json::Value& state, const Json::Value& capabilities)
22 {
23 	Json::Value inputs = capabilities["inputs"];
24 	m_button_count = inputs.size();
25 	for (int buttonidx = 0; buttonidx < m_button_count; buttonidx++)
26 	{
27 		Json::Value events = inputs[buttonidx]["events"];
28 		unsigned int event_count = events.size();
29 		for (unsigned int eventidx = 0; eventidx < event_count; eventidx++)
30 		{
31 			std::string eventtypestring = events[eventidx]["eventtype"].asString();
32 			if (eventtypestring == EVENT_LONG_RELEASE)
33 				m_button_can_long_press = true;
34 
35 			int eventid = events[eventidx]["buttonevent"].asInt();
36 			if (eventid == m_buttonevent)
37 			{
38 				m_button_nr = buttonidx;
39 				m_button_valid = true;
40 
41 				if (event_count == 1)
42 					m_button_eventtype = SensorZLLSwitchEventSimple;
43 				else if (EVENT_INITIAL_PRESS == eventtypestring)
44 					m_button_eventtype = SensorZLLSwitchEventInitialPress;
45 				else if (EVENT_REPEAT == eventtypestring)
46 					m_button_eventtype = SensorZLLSwitchEventRepeat;
47 				else if (EVENT_SHORT_RELEASE == eventtypestring)
48 					m_button_eventtype = SensorZLLSwitchEventShortRelease;
49 				else if (EVENT_LONG_RELEASE == eventtypestring)
50 					m_button_eventtype = SensorZLLSwitchEventLongRelease;
51 				else
52 					m_button_valid = false;
53 
54 			}
55 		}
56 	}
57 }
58 
GetSelectorLevel(const CPHSensorState & previous_state)59 int32_t CPHSensorState::GetSelectorLevel(const CPHSensorState& previous_state)
60 {
61 	if (m_button_valid == false)
62 		return -1;
63 
64 	if (SensorZLLSwitchEventInitialPress == m_button_eventtype)
65 		return -1;
66 
67 	int32_t selectorLevel = m_button_nr * 10;
68 	if (m_button_can_long_press)
69 		selectorLevel *= 2;
70 	selectorLevel += 10;
71 
72 	if (SensorZLLSwitchEventRepeat == m_button_eventtype || SensorZLLSwitchEventLongRelease == m_button_eventtype)
73 	{
74 		if (previous_state.m_button_nr == m_button_nr &&
75 			previous_state.m_button_eventtype == SensorZLLSwitchEventRepeat)
76 		{
77 			return -1; //It's a repeat of a long press.
78 		}
79 		selectorLevel += 10;
80 	}
81 
82 	return selectorLevel;
83 }
84 
GetButtonOptions()85 std::map<std::string, std::string> CPHSensorState::GetButtonOptions()
86 {
87 	std::map<std::string, std::string> options;
88 	options["SelectorStyle"] = "1";
89 	options["LevelOffHidden"] = "true";
90 	options["LevelNames"] = "Off";
91 	for (int buttonidx = 0; buttonidx < m_button_count; buttonidx++)
92 	{
93 		if (m_button_can_long_press)
94 		{
95 			options["LevelNames"] += "|Button " + std::to_string(buttonidx + 1) + " short";
96 			options["LevelNames"] += "|Button " + std::to_string(buttonidx + 1) + " long";
97 		}
98 		else
99 		{
100 			options["LevelNames"] += "|Button " + std::to_string(buttonidx + 1);
101 		}
102 	}
103 	return options;
104 }
105 
operator ==(const CPHSensorState & other)106 bool CPHSensorState::operator==(const CPHSensorState& other)
107 {
108 	return (m_lastupdated == other.m_lastupdated &&
109 		m_buttonevent == other.m_buttonevent &&
110 		m_presence == other.m_presence &&
111 		m_temperature == other.m_temperature &&
112 		m_lightlevel == other.m_lightlevel &&
113 		m_dark == other.m_dark &&
114 		m_daylight == other.m_daylight);
115 }
116 
CPHSensorConfig(const Json::Value & config)117 CPHSensorConfig::CPHSensorConfig(const Json::Value& config)
118 {
119 	m_on = config["on"].asBool();
120 	m_reachable =		(config["reachable"] != Json::Value::null)		? config["reachable"].asBool()		: true;
121 	m_battery =			(config["battery"] != Json::Value::null)		? config["battery"].asInt()			: 255;
122 	m_tholddark =		(config["tholddark"] != Json::Value::null)		? config["tholddark"].asInt()		: 0;
123 	m_tholdoffset =		(config["tholdoffset"] != Json::Value::null)	? config["tholdoffset"].asInt()		: 0;
124 	m_configured =		(config["configured"] != Json::Value::null)		? config["configured"].asBool()		: true;
125 	m_sunriseoffset =	(config["sunriseoffset"] != Json::Value::null)	? config["sunriseoffset"].asInt()	: 0;
126 	m_sunsetoffset =	(config["sunsetoffset"] != Json::Value::null)	? config["sunsetoffset"].asInt()	: 0;
127 }
128 
129 
CPHSensor(const Json::Value & sensor)130 CPHSensor::CPHSensor(const Json::Value& sensor) : m_state(sensor["state"], sensor["capabilities"]), m_config(sensor["config"])
131 {
132 	m_name = sensor["name"].asString();
133 	m_type = sensor["type"].asString();
134 	m_model_id = sensor["modelid"].asString();
135 	m_manufacturer_name = sensor["manufacturername"].asString();
136 	m_sw_version = sensor["swversion"].asString();
137 	m_uniqueid = (sensor["uniqueid"] != Json::Value::null) ? sensor["uniqueid"].asString() : "";
138 }
139