1 //-----------------------------------------------------------------------------
2 //
3 //	SwitchToggleBinary.cpp
4 //
5 //	Implementation of the Z-Wave COMMAND_CLASS_SWITCH_TOGGLE_BINARY
6 //
7 //	Copyright (c) 2010 Mal Lansell <openzwave@lansell.org>
8 //
9 //	SOFTWARE NOTICE AND LICENSE
10 //
11 //	This file is part of OpenZWave.
12 //
13 //	OpenZWave is free software: you can redistribute it and/or modify
14 //	it under the terms of the GNU Lesser General Public License as published
15 //	by the Free Software Foundation, either version 3 of the License,
16 //	or (at your option) any later version.
17 //
18 //	OpenZWave is distributed in the hope that it will be useful,
19 //	but WITHOUT ANY WARRANTY; without even the implied warranty of
20 //	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 //	GNU Lesser General Public License for more details.
22 //
23 //	You should have received a copy of the GNU Lesser General Public License
24 //	along with OpenZWave.  If not, see <http://www.gnu.org/licenses/>.
25 //
26 //-----------------------------------------------------------------------------
27 
28 #include "command_classes/CommandClasses.h"
29 #include "command_classes/SwitchToggleBinary.h"
30 #include "Defs.h"
31 #include "Msg.h"
32 #include "Driver.h"
33 #include "Node.h"
34 #include "platform/Log.h"
35 
36 #include "value_classes/ValueBool.h"
37 
38 using namespace OpenZWave;
39 
40 enum SwitchToggleBinaryCmd
41 {
42 	SwitchToggleBinaryCmd_Set		= 0x01,
43 	SwitchToggleBinaryCmd_Get		= 0x02,
44 	SwitchToggleBinaryCmd_Report	= 0x03
45 };
46 
47 //-----------------------------------------------------------------------------
48 // <SwitchToggleBinary::RequestState>
49 // Request current state from the device
50 //-----------------------------------------------------------------------------
RequestState(uint32 const _requestFlags,uint8 const _instance,Driver::MsgQueue const _queue)51 bool SwitchToggleBinary::RequestState
52 (
53 	uint32 const _requestFlags,
54 	uint8 const _instance,
55 	Driver::MsgQueue const _queue
56 )
57 {
58 	if( _requestFlags & RequestFlag_Dynamic )
59 	{
60 		return RequestValue( _requestFlags, 0, _instance, _queue );
61 	}
62 
63 	return false;
64 }
65 
66 //-----------------------------------------------------------------------------
67 // <SwitchToggleBinary::RequestValue>
68 // Request current value from the device
69 //-----------------------------------------------------------------------------
RequestValue(uint32 const _requestFlags,uint8 const _dummy1,uint8 const _instance,Driver::MsgQueue const _queue)70 bool SwitchToggleBinary::RequestValue
71 (
72 	uint32 const _requestFlags,
73 	uint8 const _dummy1,	// = 0 (not used)
74 	uint8 const _instance,
75 	Driver::MsgQueue const _queue
76 )
77 {
78 	if ( IsGetSupported() )
79 	{
80 		Msg* msg = new Msg( "SwitchToggleBinaryCmd_Get", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true, true, FUNC_ID_APPLICATION_COMMAND_HANDLER, GetCommandClassId() );
81 		msg->SetInstance( this, _instance );
82 		msg->Append( GetNodeId() );
83 		msg->Append( 2 );
84 		msg->Append( GetCommandClassId() );
85 		msg->Append( SwitchToggleBinaryCmd_Get );
86 		msg->Append( GetDriver()->GetTransmitOptions() );
87 		GetDriver()->SendMsg( msg, _queue );
88 		return true;
89 	} else {
90 		Log::Write(  LogLevel_Info, GetNodeId(), "SwitchToggleBinaryCmd_Get Not Supported on this node");
91 	}
92 	return false;
93 }
94 
95 //-----------------------------------------------------------------------------
96 // <SwitchToggleBinary::HandleMsg>
97 // Handle a message from the Z-Wave network
98 //-----------------------------------------------------------------------------
HandleMsg(uint8 const * _data,uint32 const _length,uint32 const _instance)99 bool SwitchToggleBinary::HandleMsg
100 (
101 	uint8 const* _data,
102 	uint32 const _length,
103 	uint32 const _instance	// = 1
104 )
105 {
106 	if( SwitchToggleBinaryCmd_Report == (SwitchToggleBinaryCmd)_data[0] )
107 	{
108 		Log::Write( LogLevel_Info, GetNodeId(), "Received SwitchToggleBinary report: %s", _data[1] ? "On" : "Off" );
109 
110 		if( ValueBool* value = static_cast<ValueBool*>( GetValue( _instance, 0 ) ) )
111 		{
112 			value->OnValueRefreshed( _data[1] != 0 );
113 			value->Release();
114 		}
115 		return true;
116 	}
117 
118 	return false;
119 }
120 
121 //-----------------------------------------------------------------------------
122 // <SwitchToggleBinary::SetValue>
123 // Toggle the state of the switch
124 //-----------------------------------------------------------------------------
SetValue(Value const & _value)125 bool SwitchToggleBinary::SetValue
126 (
127 	Value const& _value
128 )
129 {
130 	Log::Write( LogLevel_Info, GetNodeId(), "SwitchToggleBinary::Set - Toggling the state" );
131 	Msg* msg = new Msg( "SwitchToggleBinaryCmd_Set", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true );
132 	msg->SetInstance( this, _value.GetID().GetInstance() );
133 	msg->Append( GetNodeId() );
134 	msg->Append( 2 );
135 	msg->Append( GetCommandClassId() );
136 	msg->Append( SwitchToggleBinaryCmd_Set );
137 	msg->Append( GetDriver()->GetTransmitOptions() );
138 	GetDriver()->SendMsg( msg, Driver::MsgQueue_Send );
139 	return true;
140 }
141 
142 //-----------------------------------------------------------------------------
143 // <SwitchToggleBinary::CreateVars>
144 // Create the values managed by this command class
145 //-----------------------------------------------------------------------------
CreateVars(uint8 const _instance)146 void SwitchToggleBinary::CreateVars
147 (
148 	uint8 const _instance
149 )
150 {
151 	if( Node* node = GetNodeUnsafe() )
152 	{
153 	  	node->CreateValueBool( ValueID::ValueGenre_User, GetCommandClassId(), _instance, 0, "Toggle Switch", "", false, false, false, 0 );
154 	}
155 }
156 
157 
158