1 //-----------------------------------------------------------------------------
2 //
3 //	ThermostatOperatingState.cpp
4 //
5 //	Implementation of the Z-Wave COMMAND_CLASS_THERMOSTAT_OPERATING_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/ThermostatOperatingState.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 ThermostatOperatingStateCmd
41 {
42 	ThermostatOperatingStateCmd_Get				= 0x02,
43 	ThermostatOperatingStateCmd_Report			= 0x03
44 };
45 
46 static char const* c_stateName[] =
47 {
48 	"Idle",
49 	"Heating",
50 	"Cooling",
51 	"Fan Only",
52 	"Pending Heat",
53 	"Pending Cool",
54 	"Vent / Economizer",
55 	"State 07",				// Undefined states.  May be used in the future.
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 // <ThermostatOperatingState::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 ThermostatOperatingState::RequestState
71 (
72 	uint32 const _requestFlags,
73 	uint8 const _instance,
74 	Driver::MsgQueue const _queue
75 )
76 {
77 	if( _requestFlags & RequestFlag_Dynamic )
78 	{
79 		return RequestValue( _requestFlags, 0, _instance, _queue );
80 	}
81 	return false;
82 }
83 
84 //-----------------------------------------------------------------------------
85 // <ThermostatOperatingState::RequestValue>
86 // Get a thermostat mode value from the device
87 //-----------------------------------------------------------------------------
RequestValue(uint32 const _requestFlags,uint8 const _dummy1,uint8 const _instance,Driver::MsgQueue const _queue)88 bool ThermostatOperatingState::RequestValue
89 (
90 	uint32 const _requestFlags,
91 	uint8 const _dummy1,	// = 0 (not used)
92 	uint8 const _instance,
93 	Driver::MsgQueue const _queue
94 )
95 {
96 	if ( IsGetSupported() )
97 	{
98 		Msg* msg = new Msg( "ThermostatOperatingStateCmd_Get", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true, true, FUNC_ID_APPLICATION_COMMAND_HANDLER, GetCommandClassId() );
99 		msg->SetInstance( this, _instance );
100 		msg->Append( GetNodeId() );
101 		msg->Append( 2 );
102 		msg->Append( GetCommandClassId() );
103 		msg->Append( ThermostatOperatingStateCmd_Get );
104 		msg->Append( GetDriver()->GetTransmitOptions() );
105 		GetDriver()->SendMsg( msg, _queue );
106 		return true;
107 	} else {
108 		Log::Write(  LogLevel_Info, GetNodeId(), "ThermostatOperatingStateCmd_Get Not Supported on this node");
109 	}
110 	return false;
111 }
112 
113 //-----------------------------------------------------------------------------
114 // <ThermostatOperatingState::HandleMsg>
115 // Handle a message from the Z-Wave network
116 //-----------------------------------------------------------------------------
HandleMsg(uint8 const * _data,uint32 const _length,uint32 const _instance)117 bool ThermostatOperatingState::HandleMsg
118 (
119 	uint8 const* _data,
120 	uint32 const _length,
121 	uint32 const _instance	// = 1
122 )
123 {
124 	if( ThermostatOperatingStateCmd_Report == (ThermostatOperatingStateCmd)_data[0] )
125 	{
126 		// We have received the thermostat operating state from the Z-Wave device
127 		if( ValueString* valueString = static_cast<ValueString*>( GetValue( _instance, 0 ) ) )
128 		{
129 			/* no need bounds checking on c_stateName here, as it can only be 1 Byte anyway */
130 			valueString->OnValueRefreshed( c_stateName[_data[1]&0x0f] );
131 			valueString->Release();
132 			Log::Write( LogLevel_Info, GetNodeId(), "Received thermostat operating state: %s", valueString->GetValue().c_str() );
133 		}
134 		return true;
135 	}
136 
137 	return false;
138 }
139 
140 //-----------------------------------------------------------------------------
141 // <ThermostatOperatingState::CreateVars>
142 // Create the values managed by this command class
143 //-----------------------------------------------------------------------------
CreateVars(uint8 const _instance)144 void ThermostatOperatingState::CreateVars
145 (
146 	uint8 const _instance
147 )
148 {
149 	if( Node* node = GetNodeUnsafe() )
150 	{
151 	  	node->CreateValueString( ValueID::ValueGenre_User, GetCommandClassId(), _instance, 0, "Operating State", "", true, false, c_stateName[0], 0 );
152 	}
153 }
154 
155