1 //-----------------------------------------------------------------------------
2 //
3 // TimeParameters.cpp
4 //
5 // Implementation of the Z-Wave COMMAND_CLASS_TIME_PARAMETERS
6 //
7 // Copyright (c) 2014 Justin Hammond <Justin@dynam.ac>
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 <time.h>
29 #include "command_classes/CommandClasses.h"
30 #include "command_classes/TimeParameters.h"
31 #include "Defs.h"
32 #include "Msg.h"
33 #include "Node.h"
34 #include "Driver.h"
35 #include "platform/Log.h"
36
37 #include "value_classes/ValueButton.h"
38 #include "value_classes/ValueString.h"
39
40 using namespace OpenZWave;
41
42 enum TimeParametersCmd
43 {
44 TimeParametersCmd_Set = 0x01,
45 TimeParametersCmd_Get = 0x02,
46 TimeParametersCmd_Report = 0x03
47 };
48
49 enum
50 {
51 TimeParametersIndex_Date = 0,
52 TimeParametersIndex_Time,
53 TimeParametersIndex_Set,
54 TimeParametersIndex_Refresh
55 };
56
57
58 //-----------------------------------------------------------------------------
59 // <DoorLockLogging::DoorLockLogging>
60 // Constructor
61 //-----------------------------------------------------------------------------
TimeParameters(uint32 const _homeId,uint8 const _nodeId)62 TimeParameters::TimeParameters
63 (
64 uint32 const _homeId,
65 uint8 const _nodeId
66 ):
67 CommandClass( _homeId, _nodeId )
68 {
69 SetStaticRequest( StaticRequest_Values );
70 }
71
72
73 //-----------------------------------------------------------------------------
74 // <TimeParameters::RequestState>
75 // Request current state from the device
76 //-----------------------------------------------------------------------------
RequestState(uint32 const _requestFlags,uint8 const _instance,Driver::MsgQueue const _queue)77 bool TimeParameters::RequestState
78 (
79 uint32 const _requestFlags,
80 uint8 const _instance,
81 Driver::MsgQueue const _queue
82 )
83 {
84 if( ( _requestFlags & RequestFlag_Static ) && HasStaticRequest( StaticRequest_Values ) )
85 {
86 return RequestValue( _requestFlags, 0, _instance, _queue );
87 }
88
89 return false;
90 }
91
92 //-----------------------------------------------------------------------------
93 // <TimeParameters::RequestValue>
94 // Request current value from the device
95 //-----------------------------------------------------------------------------
RequestValue(uint32 const _requestFlags,uint8 const _dummy1,uint8 const _instance,Driver::MsgQueue const _queue)96 bool TimeParameters::RequestValue
97 (
98 uint32 const _requestFlags,
99 uint8 const _dummy1, // = 0 (not used)
100 uint8 const _instance,
101 Driver::MsgQueue const _queue
102 )
103 {
104 if ( IsGetSupported() )
105 {
106 Msg* msg = new Msg( "TimeParametersCmd_Get", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true, true, FUNC_ID_APPLICATION_COMMAND_HANDLER, GetCommandClassId() );
107 msg->SetInstance( this, _instance );
108 msg->Append( GetNodeId() );
109 msg->Append( 2 );
110 msg->Append( GetCommandClassId() );
111 msg->Append( TimeParametersCmd_Get );
112 msg->Append( GetDriver()->GetTransmitOptions() );
113 GetDriver()->SendMsg( msg, _queue );
114 return true;
115 } else {
116 Log::Write( LogLevel_Info, GetNodeId(), "TimeParametersCmd_Get Not Supported on this node");
117 }
118 return false;
119 }
120
121 //-----------------------------------------------------------------------------
122 // <TimeParameters::HandleMsg>
123 // Handle a message from the Z-Wave network
124 //-----------------------------------------------------------------------------
HandleMsg(uint8 const * _data,uint32 const _length,uint32 const _instance)125 bool TimeParameters::HandleMsg
126 (
127 uint8 const* _data,
128 uint32 const _length,
129 uint32 const _instance // = 1
130 )
131 {
132 if (TimeParametersCmd_Report == (TimeParametersCmd)_data[0])
133 {
134 uint16 year = (_data[1] << 8) + (_data[2] & 0xFF);
135 uint8 month = (_data[3] & 0x0F);
136 uint8 day = (_data[4] & 0x1F);
137 uint8 hour = (_data[5] & 0x1F);
138 uint8 minute = (_data[6] & 0x3F);
139 uint8 second = (_data[7] & 0x3F);
140
141 Log::Write( LogLevel_Info, GetNodeId(), "Received TimeParameters report: %02d/%02d/%04d %02d:%02d:%02d", (int)day, (int)month, (int)year, (int)hour, (int)minute, (int)second);
142 if( ValueString* value = static_cast<ValueString*>( GetValue( _instance, TimeParametersIndex_Date ) ) )
143 {
144 char msg[512];
145 snprintf(msg, sizeof(msg), "%02d/%02d/%04d", (int)day, (int)month, (int)year);
146 value->OnValueRefreshed( msg );
147 value->Release();
148 }
149 if( ValueString* value = static_cast<ValueString*>( GetValue( _instance, TimeParametersIndex_Time ) ) )
150 {
151 char msg[512];
152 snprintf(msg, sizeof(msg), "%02d:%02d:%02d", (int)hour, (int)minute, (int)second);
153 value->OnValueRefreshed( msg );
154 value->Release();
155 }
156 ClearStaticRequest( StaticRequest_Values );
157 return true;
158 }
159
160 return false;
161 }
162
163 //-----------------------------------------------------------------------------
164 // <TimeParameters::SetValue>
165 // Set a value in the Z-Wave device
166 //-----------------------------------------------------------------------------
SetValue(Value const & _value)167 bool TimeParameters::SetValue
168 (
169 Value const& _value
170 )
171 {
172 bool ret = false;
173
174 uint8 instance = _value.GetID().GetInstance();
175
176 if ( (ValueID::ValueType_Button == _value.GetID().GetType()) && (_value.GetID().GetIndex() == TimeParametersIndex_Set) )
177 {
178 time_t rawtime;
179 struct tm *timeinfo;
180 time(&rawtime);
181 #ifdef WINAPI_FAMILY_APP
182 #pragma warning(push)
183 #pragma warning(disable:4996)
184 #endif
185 timeinfo = localtime(&rawtime);
186 #ifdef WINAPI_FAMILY_APP
187 #pragma warning(pop)
188 #endif
189 Msg* msg = new Msg( "TimeParametersCmd_Set", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true, true, FUNC_ID_APPLICATION_COMMAND_HANDLER, GetCommandClassId() );
190 msg->SetInstance( this, instance );
191 msg->Append( GetNodeId() );
192 msg->Append( 9 );
193 msg->Append( GetCommandClassId() );
194 msg->Append( TimeParametersCmd_Set );
195 /* Year 1 */
196 msg->Append( ((timeinfo->tm_year + 1900)>> 8) & 0xFF);
197 /* Year 2 */
198 msg->Append( ((timeinfo->tm_year + 1900) & 0xFF));
199 /* Month */
200 msg->Append( (timeinfo->tm_mon & 0x0F)+1);
201 /* Day */
202 msg->Append( (timeinfo->tm_mday & 0x1F));
203 /* Hour */
204 msg->Append( (timeinfo->tm_hour & 0x1F));
205 /* Minute */
206 msg->Append( (timeinfo->tm_min & 0x3F));
207 /* Second */
208 msg->Append( (timeinfo->tm_sec & 0x3F));
209 msg->Append( GetDriver()->GetTransmitOptions() );
210 GetDriver()->SendMsg( msg, Driver::MsgQueue_Send );
211
212
213 /* Refresh after we send updated date/time */
214 SetStaticRequest( StaticRequest_Values );
215 ret = RequestValue( RequestFlag_Static, 0, instance, Driver::MsgQueue_Query );
216 }
217 if ( (ValueID::ValueType_Button == _value.GetID().GetType()) && (_value.GetID().GetIndex() == TimeParametersIndex_Refresh) )
218 {
219 SetStaticRequest( StaticRequest_Values );
220 ret = RequestValue( RequestFlag_Static, 0, instance, Driver::MsgQueue_Query );
221 }
222
223 return ret;
224 }
225
226 //-----------------------------------------------------------------------------
227 // <TimeParameters::CreateVars>
228 // Create the values managed by this command class
229 //-----------------------------------------------------------------------------
CreateVars(uint8 const _instance)230 void TimeParameters::CreateVars
231 (
232 uint8 const _instance
233 )
234 {
235 if( Node* node = GetNodeUnsafe() )
236 {
237 node->CreateValueString( ValueID::ValueGenre_System, GetCommandClassId(), _instance, TimeParametersIndex_Date, "Date", "", true, false, "", 0 );
238 node->CreateValueString( ValueID::ValueGenre_System, GetCommandClassId(), _instance, TimeParametersIndex_Time, "Time", "", true, false, "", 0 );
239 node->CreateValueButton( ValueID::ValueGenre_System, GetCommandClassId(), _instance, TimeParametersIndex_Set, "Set Date/Time", 0);
240 node->CreateValueButton( ValueID::ValueGenre_System, GetCommandClassId(), _instance, TimeParametersIndex_Refresh, "Refresh Date/Time", 0);
241
242 }
243 }
244