-
Notifications
You must be signed in to change notification settings - Fork 27
/
Dev_Switch.cpp
211 lines (195 loc) · 5.08 KB
/
Dev_Switch.cpp
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
/**
* @file Dev_Switch.cpp
*
* Switch API of IoTgo (iotgo.iteadstudio.com)
*
* @author Wu Pengfei (email:<[email protected]>)
* @date 2014/11/11
* @copyright
* Copyright (C) 2013-2014 ITEAD Intelligent Systems Co., Ltd. \n
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*/
#include "Dev_Switch.h"
const char *Switch::STATE[] = {"switch", NULL};
const char *Switch::VALUE_ON[] = {"on", NULL};
const char *Switch::VALUE_OFF[] = {"off", NULL};
/**
* Constructor.
*
* @param net - the pointer to object of subclass of NetInterface.
* @param sw_pin - the gpio pin to control the Switch device, HIGH for
* on and LOW for off.
*/
Switch::Switch(NetInterface *net, uint16_t sw_pin)
:IoTgo(net)
{
this->sw_pin = sw_pin;
pinMode(this->sw_pin, OUTPUT);
}
/**
* Read the state from server and turn on/off the switch.
*
* We always assume that the state from server is newest.
*
* @retval 0 - success
* @retval ERR_NO_RESPONSE - no response package from server.
* @retval ERR_NO_EXPECT - no state information expected in response package from server.
*/
int32_t Switch::sync(void)
{
int32_t state = 0;
int32_t ret = 0;
ret = getState(&state);
if (state == SWITCH_STATE_ON)
{
digitalWrite(sw_pin, HIGH);
}
else if (state == SWITCH_STATE_OFF)
{
digitalWrite(sw_pin, LOW);
}
else
{
DebugSerial.print("\ngetState() error: ret = ");
DebugSerial.println(ret);
}
return ret;
}
/**
* Read state from server.
*
* @param state - the pointer to store the state which is one of
* @ref SWITCH_STATE_OFF and @ref SWITCH_STATE_ON.
*
* @retval 0 - success.
* @retval ERR_INVALID_PARAMETER - state is invalid.
* @retval ERR_NO_RESPONSE - no response package from server.
* @retval ERR_NO_EXPECT - no state information expected in response package from server.
*/
int32_t Switch::getState(int32_t *state)
{
const char *response;
char *str_error;
char *str_params;
char *str_state_on;
char *str_state_off;
if (state == NULL)
{
return ERR_INVALID_PARAMETER;
}
response = query(STATE); /* {"error":0,"params":{"state":"on"}} {"error":0,"params":{"state":"off"}}*/
if (response != NULL)
{
str_error = strstr(response, "\"error\":0");
str_params = strstr(response, "\"params\":");
str_state_on = strstr(response, "\"switch\":\"on\"");
str_state_off = strstr(response, "\"switch\":\"off\"");
if (str_error && str_params)
{
if (str_state_on && !str_state_off)
{
*state = SWITCH_STATE_ON;
return 0;
}
else if (!str_state_on && str_state_off)
{
*state = SWITCH_STATE_OFF;
return 0;
}
else
{
return ERR_NO_EXPECT;
}
}
else
{
return ERR_NO_EXPECT;
}
}
else
{
return ERR_NO_RESPONSE;
}
}
/**
* Update the specific state to server.
*
* @param state - the state you want to update, only SWITCH_STATE_ON or SWITCH_STATE_OFF.
*
* @retval 0 - success
* @retval ERR_INVALID_PARAMETER - state is invalid value.
* @retval ERR_NO_RESPONSE - no response package from server.
* @retval ERR_NO_EXPECT - no state information expected in response package from server.
*/
int32_t Switch::setState(int32_t state)
{
const char **value;
const char *response;
char *str_error;
if (state == SWITCH_STATE_ON)
{
value = VALUE_ON;
}
else if (state == SWITCH_STATE_OFF)
{
value = VALUE_OFF;
}
else
{
return ERR_INVALID_PARAMETER;
}
response = update(STATE, value);
if (response != NULL)
{
str_error = strstr(response, "\"error\":0");
if (str_error)
{
return 0;
}
else
{
return ERR_NO_EXPECT;
}
}
else
{
return ERR_NO_RESPONSE;
}
}
/**
* Turn on the switch and update the state to server.
*
* @retval 0 - success
* @retval ERR_NO_RESPONSE - no response package from server.
* @retval ERR_NO_EXPECT - no state information expected in response package from server.
*/
int32_t Switch::on(void)
{
int32_t ret;
ret = setState(SWITCH_STATE_ON) ;
if (ret == 0)
{
digitalWrite(sw_pin, HIGH);
}
return ret;
}
/**
* Turn off the switch and update the state to server.
*
* @retval 0 - success
* @retval ERR_NO_RESPONSE - no response package from server.
* @retval ERR_NO_EXPECT - no state information expected in response package from server.
*/
int32_t Switch::off(void)
{
int32_t ret;
ret = setState(SWITCH_STATE_OFF) ;
if (ret == 0)
{
digitalWrite(sw_pin, LOW);
}
return ret;
}