1 //-----------------------------------------------------------------------------
2 //
3 //	ThermostatFanState.cpp
4 //
5 //	Implementation of the Z-Wave COMMAND_CLASS_THERMOSTAT_FAN_STATE
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/ThermostatFanState.h"
30 #include "Defs.h"
31 #include "Msg.h"
32 #include "Node.h"
33 #include "Driver.h"
34 #include "platform/Log.h"
35 
36 #include "value_classes/ValueString.h"
37 
38 using namespace OpenZWave;
39 
40 enum ThermostatFanStateCmd
41 {
42 	ThermostatFanStateCmd_Get				= 0x02,
43 	ThermostatFanStateCmd_Report			= 0x03
44 };
45 
46 static char const* c_stateName[] =
47 {
48 	"Idle",
49 	"Running",
50 	"Running High",
51 	"State 03",			// Undefined states.  May be used in the future.
52 	"State 04",
53 	"State 05",
54 	"State 06",
55 	"State 07",
56 	"State 08",
57 	"State 09",
58 	"State 10",
59 	"State 11",
60 	"State 12",
61 	"State 13",
62 	"State 14",
63 	"State 15",
64 };
65 
66 //-----------------------------------------------------------------------------
67 // <ThermostatFanState::RequestState>
68 // Get the static thermostat mode details from the device
69 //-----------------------------------------------------------------------------
RequestState(uint32 const _requestFlags,uint8 const _instance,Driver::MsgQueue const _queue)70 bool ThermostatFanState::RequestState
71 (
72 	uint32 const _requestFlags,
73 	uint8 const _instance,
74 	Driver::MsgQueue const _queue
75 )
76 {
77 	if( _requestFlags & RequestFlag_Dynamic )
78 	{
79 		// Request the current state
80 		return RequestValue( _requestFlags, 0, _instance, _queue );
81 	}
82 	return false;
83 }
84 
85 //-----------------------------------------------------------------------------
86 // <ThermostatFanState::RequestValue>
87 // Get the thermostat fan state details from the device
88 //-----------------------------------------------------------------------------
RequestValue(uint32 const _requestFlags,uint8 const _dummy1,uint8 const _instance,Driver::MsgQueue const _queue)89 bool ThermostatFanState::RequestValue
90 (
91 	uint32 const _requestFlags,
92 	uint8 const _dummy1,	// = 0 (not used)
93 	uint8 const _instance,
94 	Driver::MsgQueue const _queue
95 )
96 {
97 	if ( IsGetSupported() )
98 	{
99 		// Request the current state
100 		Msg* msg = new Msg( "ThermostatFanStateCmd_Get", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true, true, FUNC_ID_APPLICATION_COMMAND_HANDLER, GetCommandClassId() );
101 		msg->SetInstance( this, _instance );
102 		msg->Append( GetNodeId() );
103 		msg->Append( 2 );
104 		msg->Append( GetCommandClassId() );
105 		msg->Append( ThermostatFanStateCmd_Get );
106 		msg->Append( GetDriver()->GetTransmitOptions() );
107 		GetDriver()->SendMsg( msg, _queue );
108 		return true;
109 	} else {
110 		Log::Write(  LogLevel_Info, GetNodeId(), "ThermostatFanStateCmd_Get Not Supported on this node");
111 	}
112 	return false;
113 }
114 
115 //-----------------------------------------------------------------------------
116 // <ThermostatFanState::HandleMsg>
117 // Handle a message from the Z-Wave network
118 //-----------------------------------------------------------------------------
HandleMsg(uint8 const * _data,uint32 const _length,uint32 const _instance)119 bool ThermostatFanState::HandleMsg
120 (
121 	uint8 const* _data,
122 	uint32 const _length,
123 	uint32 const _instance	// = 1
124 )
125 {
126 	if( ThermostatFanStateCmd_Report == (ThermostatFanStateCmd)_data[0] )
127 	{
128 		// We have received the thermostat fan state from the Z-Wave device
129 		if( ValueString* valueString = static_cast<ValueString*>( GetValue( _instance, 0 ) ) )
130 		{
131 			/* No need bounds checking as the state can only be a single byte - No larger than our Char array anyway */
132 			uint8 state = (_data[1]&0x0f);
133 
134 			valueString->OnValueRefreshed( c_stateName[state] );
135 			valueString->Release();
136 			Log::Write( LogLevel_Info, GetNodeId(), "Received thermostat fan state: %s", valueString->GetValue().c_str() );
137 		}
138 		return true;
139 	}
140 
141 	return false;
142 }
143 
144 //-----------------------------------------------------------------------------
145 // <ThermostatFanState::CreateVars>
146 // Create the values managed by this command class
147 //-----------------------------------------------------------------------------
CreateVars(uint8 const _instance)148 void ThermostatFanState::CreateVars
149 (
150 	uint8 const _instance
151 )
152 {
153 	if( Node* node = GetNodeUnsafe() )
154 	{
155 	  	node->CreateValueString( ValueID::ValueGenre_User, GetCommandClassId(), _instance, 0, "Fan State", "", true, false, c_stateName[0], 0 );
156 	}
157 }
158 
159