Repository navigation
Expand file tree
/
Copy pathValueSchedule.cpp
More file actions
327 lines (285 loc) · 9.87 KB
/
Copy pathValueSchedule.cpp
File metadata and controls
327 lines (285 loc) · 9.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
//-----------------------------------------------------------------------------
//
// ValueSchedule.cpp
//
// A one day schedule for the Climate Control Schedule command class
//
// Copyright (c) 2010 Mal Lansell <openzwave@lansell.org>
//
// SOFTWARE NOTICE AND LICENSE
//
// This file is part of OpenZWave.
//
// OpenZWave is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// OpenZWave is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenZWave. If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
#include <sstream>
#include <limits.h>
#include "tinyxml.h"
#include "value_classes/ValueSchedule.h"
#include "Msg.h"
#include "platform/Log.h"
namespace OpenZWave
{
namespace Internal
{
namespace VC
{
//-----------------------------------------------------------------------------
// <ValueSchedule::ValueSchedule>
// Constructor
//-----------------------------------------------------------------------------
ValueSchedule::ValueSchedule(uint32 const _homeId, uint8 const _nodeId, ValueID::ValueGenre const _genre, uint8 const _commandClassId, uint8 const _instance, uint16 const _index, string const& _label, string const& _units, bool const _readOnly, bool const _writeOnly, uint8 const _pollIntensity) :
Value(_homeId, _nodeId, _genre, _commandClassId, _instance, _index, ValueID::ValueType_Schedule, _label, _units, _readOnly, _writeOnly, false, _pollIntensity), m_numSwitchPoints(0)
{
}
//-----------------------------------------------------------------------------
// <ValueSchedule::ValueSchedule>
// Constructor
//-----------------------------------------------------------------------------
ValueSchedule::ValueSchedule() :
Value(), m_numSwitchPoints(0)
{
}
//-----------------------------------------------------------------------------
// <ValueSchedule::ReadXML>
// Apply settings from XML
//-----------------------------------------------------------------------------
void ValueSchedule::ReadXML(uint32 const _homeId, uint8 const _nodeId, uint8 const _commandClassId, TiXmlElement const* _valueElement)
{
Value::ReadXML(_homeId, _nodeId, _commandClassId, _valueElement);
// Read in the switch points
TiXmlElement const* child = _valueElement->FirstChildElement();
while (child)
{
char const* str = child->Value();
if (str)
{
if (!strcmp(str, "SwitchPoint"))
{
int intVal;
uint8 hours = 0;
if (TIXML_SUCCESS == child->QueryIntAttribute("hours", &intVal))
{
hours = (uint8) intVal;
}
uint8 minutes = 0;
if (TIXML_SUCCESS == child->QueryIntAttribute("minutes", &intVal))
{
minutes = (uint8) intVal;
}
int8 setback = 0;
if (TIXML_SUCCESS == child->QueryIntAttribute("setback", &intVal))
{
setback = (int8) intVal;
}
SetSwitchPoint(hours, minutes, setback);
}
}
child = child->NextSiblingElement();
}
}
//-----------------------------------------------------------------------------
// <ValueSchedule::WriteXML>
// Write ourselves to an XML document
//-----------------------------------------------------------------------------
void ValueSchedule::WriteXML(TiXmlElement* _valueElement)
{
Value::WriteXML(_valueElement);
for (uint8 i = 0; i < GetNumSwitchPoints(); ++i)
{
uint8 hours;
uint8 minutes;
int8 setback;
if (GetSwitchPoint(i, &hours, &minutes, &setback))
{
char str[8];
TiXmlElement* switchPointElement = new TiXmlElement("SwitchPoint");
_valueElement->LinkEndChild(switchPointElement);
snprintf(str, sizeof(str), "%d", hours);
switchPointElement->SetAttribute("hours", str);
snprintf(str, sizeof(str), "%d", minutes);
switchPointElement->SetAttribute("minutes", str);
snprintf(str, sizeof(str), "%d", setback);
switchPointElement->SetAttribute("setback", str);
}
}
}
//-----------------------------------------------------------------------------
// <ValueSchedule::Set>
// Set a new value in the device
//-----------------------------------------------------------------------------
bool ValueSchedule::Set()
{
// Set the value in the device.
// TODO this needs to be checked to make sure it works as intended
return Value::Set();
}
//-----------------------------------------------------------------------------
// <ValueSchedule::OnValueRefreshed>
// A value in a device has been refreshed
//-----------------------------------------------------------------------------
void ValueSchedule::OnValueRefreshed()
{
/* Value::VerifyRefreshedValue doesn't handle Schedule Properly yet, but still do this so we can do it eventually */
switch (VerifyRefreshedValue((void*) "Schedule", (void*) "Schedule", (void*) "Schedule", ValueID::ValueType_Schedule))
{
case 0: // value hasn't changed, nothing to do
break;
case 1: // value has changed (not confirmed yet), save _value in m_valueCheck
break;
case 2: // value has changed (confirmed), save _value in m_value
break;
case 3: // all three values are different, so wait for next refresh to try again
break;
}
}
//-----------------------------------------------------------------------------
// <ValueSchedule::SetSwitchPoint>
// A value in a device has changed
//-----------------------------------------------------------------------------
bool ValueSchedule::SetSwitchPoint(uint8 const _hours, uint8 const _minutes, int8 const _setback)
{
// Find where to insert this switch point. They must be sorted by ascending time value.
uint8 i;
uint8 insertAt = 0;
for (i = 0; i < m_numSwitchPoints; ++i)
{
if (m_switchPoints[i].m_hours == _hours)
{
if (m_switchPoints[i].m_minutes == _minutes)
{
// There is already a switch point with this time, so we
// just update its setback value
m_switchPoints[i].m_setback = _setback;
return true;
}
if (m_switchPoints[i].m_minutes > _minutes)
{
break;
}
}
else if (m_switchPoints[i].m_hours > _hours)
{
break;
}
++insertAt;
}
if (m_numSwitchPoints >= 9)
{
// The schedule is full
return false;
}
// Shuffle any later switch points out of the way
for (i = m_numSwitchPoints; i > insertAt; --i)
{
m_switchPoints[i].m_hours = m_switchPoints[i - 1].m_hours;
m_switchPoints[i].m_minutes = m_switchPoints[i - 1].m_minutes;
m_switchPoints[i].m_setback = m_switchPoints[i - 1].m_setback;
}
// Insert the new switch point
m_switchPoints[insertAt].m_hours = _hours;
m_switchPoints[insertAt].m_minutes = _minutes;
m_switchPoints[insertAt].m_setback = _setback;
++m_numSwitchPoints;
return true;
}
//-----------------------------------------------------------------------------
// <ValueSchedule::RemoveSwitchPoint>
// A value in a device has changed
//-----------------------------------------------------------------------------
bool ValueSchedule::RemoveSwitchPoint(uint8 const _idx)
{
if (_idx >= m_numSwitchPoints)
{
// _idx is out of range
return false;
}
// Shuffle any later switch points down to fill the gap
for (uint8 i = _idx; i < (m_numSwitchPoints - 1); ++i)
{
m_switchPoints[i].m_hours = m_switchPoints[i + 1].m_hours;
m_switchPoints[i].m_minutes = m_switchPoints[i + 1].m_minutes;
m_switchPoints[i].m_setback = m_switchPoints[i + 1].m_setback;
}
--m_numSwitchPoints;
return true;
}
//-----------------------------------------------------------------------------
// <ValueSchedule::GetSwitchPoint>
// Get the values of a switch point
//-----------------------------------------------------------------------------
bool ValueSchedule::GetSwitchPoint(uint8 const _idx, uint8* o_hours, uint8* o_minutes, int8* o_setback) const
{
if (_idx >= m_numSwitchPoints)
{
// _idx is out of range
return false;
}
if (o_hours)
{
*o_hours = m_switchPoints[_idx].m_hours;
}
if (o_minutes)
{
*o_minutes = m_switchPoints[_idx].m_minutes;
}
if (o_setback)
{
*o_setback = m_switchPoints[_idx].m_setback;
}
return true;
}
//-----------------------------------------------------------------------------
// <ValueSchedule::FindSwitchPoint>
// Get the index of the switch point at the specified time
//-----------------------------------------------------------------------------
bool ValueSchedule::FindSwitchPoint(uint8 const _hours, uint8 const _minutes, uint8* o_idx) const
{
for (uint8 i = 0; i < m_numSwitchPoints; ++i)
{
if (m_switchPoints[i].m_hours == _hours)
{
if (m_switchPoints[i].m_minutes == _minutes)
{
// Found a match
if (o_idx)
{
*o_idx = i;
}
return true;
}
if (m_switchPoints[i].m_minutes > _minutes)
{
// Gone past any possible match
return false;
}
}
else if (m_switchPoints[i].m_hours > _hours)
{
// Gone past any possible match
return false;
}
}
// No match found
return false;
}
std::string const ValueSchedule::GetAsString() const
{
/* we should actuall find a way to return the arrays of switchpoints nicely */
return "SwitchPoint";
}
} // namespace VC
} // namespace Internal
} // namespace OpenZWave