forked from OpenZWave/open-zwave
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCentralScene.cpp
More file actions
369 lines (346 loc) · 13.2 KB
/
Copy pathCentralScene.cpp
File metadata and controls
369 lines (346 loc) · 13.2 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
//-----------------------------------------------------------------------------
//
// CentralScene.cpp
//
// Implementation of the Z-Wave COMMAND_CLASS_CENTRAL_SCENE
//
// Copyright (c) 2012 Greg Satz <satz@iranger.com>
//
// 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 "command_classes/CommandClasses.h"
#include "command_classes/CentralScene.h"
#include "Defs.h"
#include "Msg.h"
#include "Node.h"
#include "Driver.h"
#include "platform/Log.h"
#include "value_classes/ValueInt.h"
#include "value_classes/ValueButton.h"
#include "value_classes/ValueByte.h"
#include "tinyxml.h"
namespace OpenZWave
{
namespace Internal
{
namespace CC
{
enum CentralSceneCmd
{
CentralSceneCmd_Capability_Get = 0x01,
CentralSceneCmd_Capability_Report = 0x02,
CentralSceneCmd_Set = 0x03
};
enum CentralScene_ValueID_Index
{
};
enum CentralScene_KeyAttributesMask
{
CentralSceneMask_KeyPressed1time = 0x01,
CentralSceneMask_KeyReleased = 0x02,
CentralSceneMask_HeldDown = 0x04,
CentralSceneMask_KeyPressed2times = 0x08,
CentralSceneMask_KeyPressed3times = 0x10,
CentralSceneMask_KeyPressed4times = 0x20,
CentralSceneMask_KeyPressed5times = 0x40,
CentralSceneMask_reserved = 0x80,
};
enum CentralScene_KeyAttributes
{
CentralScene_KeyAttributes_KeyPressed1time = 0,
CentralScene_KeyAttributes_KeyReleased = 1,
CentralScene_KeyAttributes_KeyHeldDown = 2,
CentralScene_KeyAttributes_KeyPressed2times = 3,
CentralScene_KeyAttributes_KeyPressed3times = 4,
CentralScene_KeyAttributes_KeyPressed4times = 5,
CentralScene_KeyAttributes_KeyPressed5times = 6,
CentralScene_KeyAttributes_reserved = 7,
};
static char const* c_CentralScene_KeyAttributes[] =
{ "Inactive", "Pressed 1 Time", "Key Released", "Key Held down", "Pressed 2 Times", "Pressed 3 Times", "Pressed 4 Times", "Pressed 5 Times" };
//-----------------------------------------------------------------------------
// <CentralScene::CentralScene>
// Constructor
//-----------------------------------------------------------------------------
CentralScene::CentralScene(uint32 const _homeId, uint8 const _nodeId) :
CommandClass(_homeId, _nodeId), m_slowrefresh(false), m_sequence(0)
{
m_dom.EnableFlag(STATE_FLAG_CS_SCENECOUNT, 0);
m_dom.EnableFlag(STATE_FLAG_CS_CLEARTIMEOUT, 1000);
Timer::SetDriver(GetDriver());
SetStaticRequest(StaticRequest_Values);
}
//-----------------------------------------------------------------------------
// <CentralScene::RequestState>
// Request current state from the device
//-----------------------------------------------------------------------------
bool CentralScene::RequestState(uint32 const _requestFlags, uint8 const _instance, Driver::MsgQueue const _queue)
{
bool requests = false;
if ((_requestFlags & RequestFlag_Static) && HasStaticRequest(StaticRequest_Values))
{
requests = RequestValue(_requestFlags, CentralSceneCmd_Capability_Get, _instance, _queue);
}
return requests;
}
//-----------------------------------------------------------------------------
// <CentralScene::RequestValue>
// Request current value from the device
//-----------------------------------------------------------------------------
bool CentralScene::RequestValue(uint32 const _requestFlags, uint16 const _what, uint8 const _instance, Driver::MsgQueue const _queue)
{
if (_what == CentralSceneCmd_Capability_Get)
{
Msg* msg = new Msg("CentralSceneCmd_Capability_Get", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true, true, FUNC_ID_APPLICATION_COMMAND_HANDLER, GetCommandClassId());
msg->SetInstance(this, _instance);
msg->Append(GetNodeId());
msg->Append(2);
msg->Append(GetCommandClassId());
msg->Append(CentralSceneCmd_Capability_Get);
msg->Append(GetDriver()->GetTransmitOptions());
GetDriver()->SendMsg(msg, _queue);
}
return true;
}
//-----------------------------------------------------------------------------
// <CentralScene::RequestValue>
// Request current value from the device
//-----------------------------------------------------------------------------
bool CentralScene::SetValue(Internal::VC::Value const& _value)
{
if ((ValueID::ValueType_Int == _value.GetID().GetType()) && (_value.GetID().GetIndex() == ValueID_Index_CentralScene::ClearSceneTimeout))
{
Internal::VC::ValueInt const *value = static_cast<Internal::VC::ValueInt const *>(&_value);
m_dom.SetFlagInt(STATE_FLAG_CS_CLEARTIMEOUT, value->GetValue());
return true;
}
return false;
}
//-----------------------------------------------------------------------------
// <CentralScene::HandleMsg>
// Handle a message from the Z-Wave network
//-----------------------------------------------------------------------------
bool CentralScene::HandleMsg(uint8 const* _data, uint32 const _length, uint32 const _instance // = 1
)
{
if (CentralSceneCmd_Set == (CentralSceneCmd) _data[0])
{
// Central Scene Set received so send notification
/* if the sequence number is the same as what we have received previously this is a retried packet */
if (m_sequence == _data[1])
{
Log::Write(LogLevel_Warning, GetNodeId(), "Received Duplicated Scene Notification. Dropping...");
return true;
}
m_sequence = _data[1];
uint8 keyAttribute = (_data[2] & 0x07);
uint8 sceneID = _data[3];
Log::Write(LogLevel_Info, GetNodeId(), "Received Central Scene set from node %d: scene id=%d with key Attribute %d. Sending event notification.", GetNodeId(), sceneID, keyAttribute);
if (Internal::VC::ValueList* value = static_cast<Internal::VC::ValueList*>(GetValue(_instance, sceneID)))
{
/* plus one, as we have our own "inactive" entry at index 0 */
value->OnValueRefreshed(keyAttribute + 1);
value->Release();
/* Start up a Timer to set this back to Inactive */
Log::Write(LogLevel_Info, GetNodeId(), "Automatically Clearing Scene %d in %dms", sceneID, m_dom.GetFlagInt(STATE_FLAG_CS_CLEARTIMEOUT));
if (m_TimersSet.find(sceneID) == m_TimersSet.end())
{
m_TimersSet.insert(std::pair<uint32, uint32>(sceneID, _instance));
}
else
{
/* clear the Old Timer */
TimerDelEvent(sceneID);
/* no need to pop it off the list, as we will add it again below */
}
TimerThread::TimerCallback callback = bind(&CentralScene::ClearScene, this, sceneID);
TimerSetEvent(m_dom.GetFlagInt(STATE_FLAG_CS_CLEARTIMEOUT), callback, sceneID);
}
else
{
Log::Write(LogLevel_Warning, GetNodeId(), "No ValueID created for Scene %d", sceneID);
return false;
}
return true;
}
else if (CentralSceneCmd_Capability_Report == (CentralSceneCmd) _data[0])
{
/* Create a Number of ValueID's based on the STATE_FLAG_CS_SCENECOUNT variable
* We prefer what the Config File specifies rather than what is returned by
* the Device...
*/
int scenecount = _data[1];
if (m_dom.GetFlagByte(STATE_FLAG_CS_SCENECOUNT) == 0)
{
m_dom.SetFlagByte(STATE_FLAG_CS_SCENECOUNT, scenecount);
}
bool identical = true; //version 1 does not know this, so set it to true.
if (GetVersion() >= 2)
{
identical = _data[2] & 0x01;
Log::Write(LogLevel_Detail, GetNodeId(), "CentralScene: all scenes identical? %i", identical);
if (GetVersion() >= 3)
m_slowrefresh = (_data[2] & 0x80) == 1 ? true : false;
}
if (Internal::VC::ValueInt* value = static_cast<Internal::VC::ValueInt*>(GetValue(_instance, ValueID_Index_CentralScene::SceneCount)))
{
value->OnValueRefreshed(m_dom.GetFlagByte(STATE_FLAG_CS_SCENECOUNT));
value->Release();
}
else
{
Log::Write(LogLevel_Warning, GetNodeId(), "Can't find ValueID for SceneCount");
}
for (int sceneID = 1; sceneID <= m_dom.GetFlagByte(STATE_FLAG_CS_SCENECOUNT); sceneID++)
{
if (GetVersion() == 1)
{
// version 1 does not tell us which keyAttributes are supported, but only single press, released and held down are supported, so add these 3
if (Node* node = GetNodeUnsafe())
{
vector<Internal::VC::ValueList::Item> items;
for (unsigned int i = 0; i < 4; i++)
{
Internal::VC::ValueList::Item item;
item.m_label = c_CentralScene_KeyAttributes[i];
item.m_value = i;
items.push_back(item);
}
char lbl[64];
snprintf(lbl, 64, "Scene %d", sceneID);
node->CreateValueList(ValueID::ValueGenre_User, GetCommandClassId(), _instance, sceneID, lbl, "", true, false, 3, items, 0, 0);
}
}
if (GetVersion() >= 2)
{
if (identical)
{
int keyAttributes = _data[3];
createSupportedKeyAttributesValues(keyAttributes, sceneID, _instance);
}
else
{
int keyAttributes = _data[2 + sceneID];
createSupportedKeyAttributesValues(keyAttributes, sceneID, _instance);
}
}
}
}
return false;
}
//-----------------------------------------------------------------------------
// <CentralScene::CreateVars>
// Create the values managed by this command class
//-----------------------------------------------------------------------------
void CentralScene::CreateVars(uint8 const _instance)
{
if (Node* node = GetNodeUnsafe())
{
node->CreateValueInt(ValueID::ValueGenre_User, GetCommandClassId(), _instance, ValueID_Index_CentralScene::SceneCount, "Scene Count", "", true, false, 0, 0);
node->CreateValueInt(ValueID::ValueGenre_Config, GetCommandClassId(), _instance, ValueID_Index_CentralScene::ClearSceneTimeout, "Scene Reset Timeout", "", false, false, m_dom.GetFlagInt(STATE_FLAG_CS_CLEARTIMEOUT), 0);
}
}
void CentralScene::createSupportedKeyAttributesValues(uint8 keyAttributes, uint8 sceneNumber, uint8 instance)
{
if (Node* node = GetNodeUnsafe())
{
vector<Internal::VC::ValueList::Item> items;
{
Internal::VC::ValueList::Item item;
item.m_label = c_CentralScene_KeyAttributes[0];
item.m_value = 0;
items.push_back(item);
}
if (keyAttributes & CentralSceneMask_KeyPressed1time)
{
Internal::VC::ValueList::Item item;
item.m_label = c_CentralScene_KeyAttributes[1];
item.m_value = 1;
items.push_back(item);
}
if (keyAttributes & CentralSceneMask_KeyReleased)
{
Internal::VC::ValueList::Item item;
item.m_label = c_CentralScene_KeyAttributes[2];
item.m_value = 2;
items.push_back(item);
}
if (keyAttributes & CentralSceneMask_HeldDown)
{
Internal::VC::ValueList::Item item;
item.m_label = c_CentralScene_KeyAttributes[3];
item.m_value = 3;
items.push_back(item);
}
if (keyAttributes & CentralSceneMask_KeyPressed2times)
{
Internal::VC::ValueList::Item item;
item.m_label = c_CentralScene_KeyAttributes[4];
item.m_value = 4;
items.push_back(item);
}
if (keyAttributes & CentralSceneMask_KeyPressed3times)
{
Internal::VC::ValueList::Item item;
item.m_label = c_CentralScene_KeyAttributes[5];
item.m_value = 5;
items.push_back(item);
}
if (keyAttributes & CentralSceneMask_KeyPressed4times)
{
Internal::VC::ValueList::Item item;
item.m_label = c_CentralScene_KeyAttributes[6];
item.m_value = 6;
items.push_back(item);
}
if (keyAttributes & CentralSceneMask_KeyPressed5times)
{
Internal::VC::ValueList::Item item;
item.m_label = c_CentralScene_KeyAttributes[7];
item.m_value = 7;
items.push_back(item);
}
char lbl[64];
snprintf(lbl, 64, "Scene %d", sceneNumber);
node->CreateValueList(ValueID::ValueGenre_User, GetCommandClassId(), instance, sceneNumber, lbl, "", true, false, (uint8_t) (items.size() & 0xFF), items, 0, 0);
}
}
void CentralScene::ClearScene(uint32 sceneID)
{
uint8 _instance;
if (m_TimersSet.find(sceneID) != m_TimersSet.end())
{
_instance = m_TimersSet.at(sceneID);
}
else
{
Log::Write(LogLevel_Warning, "Can't find Timer in TimerSet List");
return;
}
if (Internal::VC::ValueList* value = static_cast<Internal::VC::ValueList*>(GetValue(_instance, sceneID)))
{
/* plus one, as we have our own "inactive" entry at index 0 */
value->OnValueRefreshed(0);
value->Release();
}
m_TimersSet.erase(sceneID);
}
} // namespace CC
} // namespace Internal
} // namespace OpenZWave