openzwave-sys 0.1.1

FFI Library to the opensource OpenZwave library
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
//-----------------------------------------------------------------------------
//
//	Scene.cpp
//
//	A collection of ValueIDs to be used together.
//
//	Copyright (c) 2011 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 <cstring>
#include "Manager.h"
#include "platform/Log.h"
#include "value_classes/Value.h"
#include "value_classes/ValueID.h"
#include "Scene.h"
#include "Options.h"

#include "tinyxml.h"

using namespace OpenZWave;

uint32 const c_sceneVersion = 1;

//-----------------------------------------------------------------------------
// Statics
//-----------------------------------------------------------------------------
uint8		Scene::s_sceneCnt = 0;
Scene*		Scene::s_scenes[256] = { 0 };

//-----------------------------------------------------------------------------
// <Scene::Scene>
// Constructor
//-----------------------------------------------------------------------------
Scene::Scene
( 
	uint8 const _sceneId
):
	m_sceneId( _sceneId ),
	m_label( "" )
{
	s_scenes[_sceneId] = this;
	s_sceneCnt++;
}

//-----------------------------------------------------------------------------
// <Scene::~Scene>
// Destructor
//-----------------------------------------------------------------------------
Scene::~Scene
(
)
{
	while( !m_values.empty() )
	{
		SceneStorage* ss = m_values.back();
		m_values.pop_back();
		delete ss;
	}

	s_sceneCnt--;
	s_scenes[m_sceneId] = NULL;
}

//-----------------------------------------------------------------------------
// <Scene::WriteXML>
// Write ourselves to an XML document
//-----------------------------------------------------------------------------
void Scene::WriteXML
(
	string const& _name
)
{
	char str[16];

	// Create a new XML document to contain the driver configuration
	TiXmlDocument doc;
	TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "utf-8", "" );
	TiXmlElement* scenesElement = new TiXmlElement( "Scenes" );
	doc.LinkEndChild( decl );
	doc.LinkEndChild( scenesElement );

	scenesElement->SetAttribute( "xmlns", "http://code.google.com/p/open-zwave/" );

	snprintf( str, sizeof(str), "%d", c_sceneVersion );
	scenesElement->SetAttribute( "version", str);

	for( int i = 1; i < 256; i++ )
	{
		if( s_scenes[i] == NULL )
		{
			continue;
		}

		TiXmlElement* sceneElement = new TiXmlElement( "Scene" );

		snprintf( str, sizeof(str), "%d", i );
		sceneElement->SetAttribute( "id", str );
		sceneElement->SetAttribute( "label", s_scenes[i]->m_label.c_str() );

		for( vector<SceneStorage*>::iterator vt = s_scenes[i]->m_values.begin(); vt != s_scenes[i]->m_values.end(); ++vt )
		{
			TiXmlElement* valueElement = new TiXmlElement( "Value" );

			snprintf( str, sizeof(str), "0x%.8x", (*vt)->m_id.GetHomeId() );
			valueElement->SetAttribute( "homeId", str );

			snprintf( str, sizeof(str), "%d", (*vt)->m_id.GetNodeId() );
			valueElement->SetAttribute( "nodeId", str );

			valueElement->SetAttribute( "genre", Value::GetGenreNameFromEnum((*vt)->m_id.GetGenre()) );

			snprintf( str, sizeof(str), "%d", (*vt)->m_id.GetCommandClassId() );
			valueElement->SetAttribute( "commandClassId", str );

			snprintf( str, sizeof(str), "%d", (*vt)->m_id.GetInstance() );
			valueElement->SetAttribute( "instance", str );

			snprintf( str, sizeof(str), "%d", (*vt)->m_id.GetIndex() );
			valueElement->SetAttribute( "index", str );

			valueElement->SetAttribute( "type", Value::GetTypeNameFromEnum((*vt)->m_id.GetType()) );

			TiXmlText* textElement = new TiXmlText( (*vt)->m_value.c_str() );
			valueElement->LinkEndChild( textElement );

			sceneElement->LinkEndChild( valueElement );
		}

		scenesElement->LinkEndChild( sceneElement );
	}

	string userPath;
	Options::Get()->GetOptionAsString( "UserPath", &userPath );

	string filename =  userPath + _name;

	doc.SaveFile( filename.c_str() );
}

//-----------------------------------------------------------------------------
// <Scene::ReadScene>
// Read scene configuration from an XML document
//-----------------------------------------------------------------------------
bool Scene::ReadScenes
(
)
{
	int32 intVal;
	char const* str;

	// Load the XML document that contains the driver configuration
	string userPath;
	Options::Get()->GetOptionAsString( "UserPath", &userPath );
	
	string filename =  userPath + "zwscene.xml";

	TiXmlDocument doc;
	if( !doc.LoadFile( filename.c_str(), TIXML_ENCODING_UTF8 ) )
	{
		return false;
	}

	TiXmlElement const* scenesElement = doc.RootElement();

	// Version
	if( TIXML_SUCCESS == scenesElement->QueryIntAttribute( "version", &intVal ) )
	{
		if( (uint32)intVal != c_sceneVersion )
		{
			Log::Write( LogLevel_Alert, "Driver::ReadScenes - %s is from an older version of OpenZWave and cannot be loaded.", filename.c_str() );
			return false;
		}
	}
	else
	{
		Log::Write( LogLevel_Alert, "Driver::ReadScenes - %s is from an older version of OpenZWave and cannot be loaded.", filename.c_str() );
		return false;
	}

	TiXmlElement const* sceneElement = scenesElement->FirstChildElement();
	while( sceneElement )
	{
		Scene* scene = NULL;

		if( TIXML_SUCCESS == sceneElement->QueryIntAttribute( "id", &intVal ) )
		{
			scene = new Scene( (uint8)intVal );
		}

		if( scene == NULL )
		{
			continue;
		}

		str = sceneElement->Attribute( "label" );
		if( str )
		{
			scene->m_label = str;
		}

		// Read the ValueId for this scene
		TiXmlElement const* valueElement = sceneElement->FirstChildElement();
		while( valueElement )
		{
			char const* elementName = valueElement->Value();
			if( elementName && !strcmp( elementName, "Value" ) )
			{
				uint32 homeId = 0;
				str = valueElement->Attribute( "homeId" );
				if( str )
				{
					char *p;
					homeId = (uint32)strtol( str, &p, 0 );
				}
				uint8 nodeId = 0;
				if (TIXML_SUCCESS == valueElement->QueryIntAttribute( "nodeId", &intVal ) )
				{
					nodeId = intVal;
				}
				ValueID::ValueGenre genre = Value::GetGenreEnumFromName( valueElement->Attribute( "genre" ) );
				uint8 commandClassId = 0;
				if (TIXML_SUCCESS == valueElement->QueryIntAttribute( "commandClassId", &intVal ) )
				{
					commandClassId = intVal;
				}
				uint8 instance = 0;
				if (TIXML_SUCCESS == valueElement->QueryIntAttribute( "instance", &intVal ) )
				{
					instance = intVal;
				}
				uint8 index = 0;
				if (TIXML_SUCCESS == valueElement->QueryIntAttribute( "index", &intVal ) )
				{
					index = intVal;
				}
				ValueID::ValueType type = Value::GetTypeEnumFromName( valueElement->Attribute( "type" ) );
				char const* data = valueElement->GetText();

				scene->m_values.push_back( new SceneStorage( ValueID(homeId, nodeId, genre, commandClassId, instance, index, type), data ) );
			}

			valueElement = valueElement->NextSiblingElement();
		}
		sceneElement = sceneElement->NextSiblingElement();
	}
	return true;
}

//-----------------------------------------------------------------------------
// <Scene::Get>
// Return the Scene object given the Scene Id
//-----------------------------------------------------------------------------
Scene* Scene::Get
(
	uint8 const _sceneId
)
{
	if ( s_scenes[_sceneId] != NULL )
	{
		return s_scenes[_sceneId];
	}
	return NULL;
}

//-----------------------------------------------------------------------------
// <Scene::GetAllScenes>
// Return an array of uint8 of used Scene IDs and the count
//-----------------------------------------------------------------------------
uint8 Scene::GetAllScenes
(
	uint8** _sceneIds
)
{
	if( s_sceneCnt > 0 )
	{
		*_sceneIds = new uint8[s_sceneCnt];
		int j = 0;
		for( int i = 1; i < 256; i++ )
		{
			if (s_scenes[i] != NULL)
			{
				(*_sceneIds)[j++] = s_scenes[i]->m_sceneId;
			}
		}
	}
	return s_sceneCnt;
}

//-----------------------------------------------------------------------------
// <Scene::AddValue>
// Add a ValueID and a string to the scene.
//-----------------------------------------------------------------------------
bool Scene::AddValue
(
	ValueID const& _valueId,
	string const& _value
)
{
	m_values.push_back( new SceneStorage( _valueId, _value ) );
	return true;
}

//-----------------------------------------------------------------------------
// <Scene::RemoveValue>
// Remove the first ValueID found
//-----------------------------------------------------------------------------
bool Scene::RemoveValue
(
	ValueID const& _valueId
)
{
	for( vector<SceneStorage*>::iterator it = m_values.begin(); it != m_values.end(); ++it )
	{
		if( (*it)->m_id == _valueId )
		{
			delete *it;
			m_values.erase( it );
			return true;
		}
	}
	return false;
}

//-----------------------------------------------------------------------------
// <Scene::RemoveValues>
// Remove all ValueIDs from given Home ID 
//-----------------------------------------------------------------------------
void Scene::RemoveValues
(
	uint32 const _homeId
)
{
 again:
	for( vector<SceneStorage*>::iterator it = m_values.begin(); it != m_values.end(); ++it )
	{
		if( (*it)->m_id.GetHomeId() == _homeId )
		{
			delete *it;
			m_values.erase( it );
			goto again;
		}
	}
	// If the scene is now empty, delete it.
	if( m_values.empty() )
	{
		delete this;
	}
}

//-----------------------------------------------------------------------------
// <Scene::RemoveValues>
// Remove all ValueIDs from given Home ID and node ID
//-----------------------------------------------------------------------------
void Scene::RemoveValues
(
	uint32 const _homeId,
	uint8 const _nodeId
)
{
	for( int i = 1; i < 256; i++ )
	{
		Scene *scene = Scene::Get( i );
		if( scene != NULL )
		{
		again:
			for( vector<SceneStorage*>::iterator it = scene->m_values.begin(); it != scene->m_values.end(); ++it )
			{
				if( (*it)->m_id.GetHomeId() == _homeId && (*it)->m_id.GetNodeId() == _nodeId )
				{
					delete *it;
					scene->m_values.erase( it );
					goto again;
				}
			}
			// If the scene is now empty, delete it.
			if( scene->m_values.empty() )
			{
				delete scene;
			}
		}
	}
}

//-----------------------------------------------------------------------------
// <Scene::GetValues>
// Return all ValueIDs for the given scene.
//-----------------------------------------------------------------------------
int Scene::GetValues
(
	vector<ValueID>* o_value
)
{
	int size = (int) m_values.size();
	if( size > 0 )
	{
		for( vector<SceneStorage*>::iterator it = m_values.begin(); it != m_values.end(); ++it )
		{
			o_value->push_back( (*it)->m_id );
		}
	}
	return size;
}

//-----------------------------------------------------------------------------
// <Scene::GetValue>
// Return a ValueID's value as string
//-----------------------------------------------------------------------------
bool Scene::GetValue
(
	ValueID const& _valueId,
	string* o_value
)
{
	for( vector<SceneStorage*>::iterator it = m_values.begin(); it != m_values.end(); ++it )
	{
		if( (*it)->m_id == _valueId )
		{
			*o_value = (*it)->m_value;
			return true;
		} 
	}
	return false;
}

//-----------------------------------------------------------------------------
// <Scene::SetValue>
// Set a ValueID's value as string
//-----------------------------------------------------------------------------
bool Scene::SetValue
(
	ValueID const& _valueId,
	string const& _value
)
{
	for( vector<SceneStorage*>::iterator it = m_values.begin(); it != m_values.end(); ++it )
	{
		if( (*it)->m_id == _valueId )
		{
			(*it)->m_value = _value;
			return true;
		} 
	}
	return false;
}

//-----------------------------------------------------------------------------
// <Scene::Activate>
// Execute scene activation by running each ValueId/value
//-----------------------------------------------------------------------------
bool Scene::Activate
(
)
{
	bool res = true;
	for( vector<SceneStorage*>::iterator it = m_values.begin(); it != m_values.end(); ++it )
	{
		if ( !Manager::Get()->SetValue( (*it)->m_id, (*it)->m_value ) )
		{
			res = false;
		}
	}
	return res;
}