/
CentralScene.cpp
233 lines (210 loc) · 6.85 KB
/
CentralScene.cpp
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
//-----------------------------------------------------------------------------
//
// 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 "tinyxml.h"
using namespace OpenZWave;
enum CentralSceneCmd
{
CentralSceneCmd_Capability_Get = 0x01,
CentralSceneCmd_Capability_Report = 0x02,
CentralSceneCmd_Set = 0x03
};
enum CentralScene_ValueID_Index
{
CentralScene_Count = 0x00
};
//-----------------------------------------------------------------------------
// <CentralScene::CentralScene>
// Constructor
//-----------------------------------------------------------------------------
CentralScene::CentralScene
(
uint32 const _homeId,
uint8 const _nodeId
):
CommandClass( _homeId, _nodeId ),
m_scenecount(0)
{
Log::Write(LogLevel_Info, GetNodeId(), "CentralScene - Created %d", HasStaticRequest( StaticRequest_Values ));
}
//-----------------------------------------------------------------------------
// <CentralScene::RequestState>
// Request current state from the device
//-----------------------------------------------------------------------------
bool CentralScene::RequestState
(
uint32 const _requestFlags,
uint8 const _instance,
Driver::MsgQueue const _queue
)
{
Log::Write(LogLevel_Info, GetNodeId(), "CentralScene RequestState: %d", _requestFlags);
bool requests = false;
if( ( _requestFlags & RequestFlag_AfterMark ))
{
requests = RequestValue( _requestFlags, CentralSceneCmd_Capability_Get, _instance, _queue );
} else {
Log::Write(LogLevel_Info, GetNodeId(), "CentralScene: Not a StaticRequest");
}
return requests;
}
//-----------------------------------------------------------------------------
// <CentralScene::RequestValue>
// Request current value from the device
//-----------------------------------------------------------------------------
bool CentralScene::RequestValue
(
uint32 const _requestFlags,
uint8 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( _what );
msg->Append( GetDriver()->GetTransmitOptions() );
GetDriver()->SendMsg( msg, _queue );
}
return true;
}
//-----------------------------------------------------------------------------
// <CentralScene::ReadXML>
// Class specific configuration
//-----------------------------------------------------------------------------
void CentralScene::ReadXML
(
TiXmlElement const* _ccElement
)
{
int32 intVal;
CommandClass::ReadXML( _ccElement );
if( TIXML_SUCCESS == _ccElement->QueryIntAttribute( "scenecount", &intVal ) )
{
m_scenecount = intVal;
}
}
//-----------------------------------------------------------------------------
// <CentralScene::WriteXML>
// Class specific configuration
//-----------------------------------------------------------------------------
void CentralScene::WriteXML
(
TiXmlElement* _ccElement
)
{
char str[32];
CommandClass::WriteXML( _ccElement );
snprintf( str, sizeof(str), "%d", m_scenecount );
_ccElement->SetAttribute( "scenecount", str);
}
//-----------------------------------------------------------------------------
// <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
int32 when;
if( _data[2] == 0 )
when = 0;
else if( _data[2] <= 0x7F )
when = _data[2];
else if( _data[2] <= 0xFE )
when = 60 * _data[2];
else
when = 0;
Log::Write( LogLevel_Info, GetNodeId(), "Received Central Scene set from node %d: scene id=%d in %d seconds. Sending event notification.", GetNodeId(), _data[3], when);
if( ValueInt* value = static_cast<ValueInt*>( GetValue( _instance, _data[3] ) ) )
{
value->OnValueRefreshed( when );
value->Release();
} else {
Log::Write( LogLevel_Warning, GetNodeId(), "No ValueID created for Scene %d", _data[3]);
return false;
}
return true;
} else if (CentralSceneCmd_Capability_Report == (CentralSceneCmd)_data[0]) {
/* Create a Number of ValueID's based on the m_scenecount variable
* We prefer what the Config File specifies rather than what is returned by
* the Device...
*/
int scenecount = _data[1];
if (m_scenecount != 0)
m_scenecount = scenecount;
if ( ValueInt* value = static_cast<ValueInt*>( GetValue( _instance, CentralScene_Count)))
{
value->OnValueRefreshed(m_scenecount);
value->Release();
} else {
Log::Write( LogLevel_Warning, GetNodeId(), "Can't find ValueID for SceneCount");
}
if( Node* node = GetNodeUnsafe() )
{
char lbl[64];
for (int i = 1; i <= m_scenecount; i++) {
snprintf(lbl, 64, "Scene %d", i);
node->CreateValueInt(ValueID::ValueGenre_User, GetCommandClassId(), _instance, i, lbl, "", true, false, 0, 0 );
}
} else {
Log::Write(LogLevel_Info, GetNodeId(), "CentralScene: Can't find Node!");
}
}
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_System, GetCommandClassId(), _instance, CentralScene_Count, "Scene Count", "", true, false, 0, 0 );
}
}