-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathDev_Light.cpp
230 lines (214 loc) · 5.48 KB
/
Dev_Light.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/**
* @file Dev_Light.cpp
*
* Light 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_Light.h"
const char *Light::STATE[] = {"light", NULL};
const char *Light::VALUE_ON[] = {"on", NULL};
const char *Light::VALUE_OFF[] = {"off", NULL};
/**
* Constructor of Light.
*
* @param net - the pointer to object of subclass of NetInterface.
* @param light_pin - the gpio pin number to control the light.
* HIGH level means on and LOW to off.
*/
Light::Light(NetInterface *net, uint16_t light_pin)
:IoTgo(net)
{
this->light_pin = light_pin;
pinMode(this->light_pin, OUTPUT);
}
/**
* Read the state from server and turn on/off the light.
*
* 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 Light::sync(void)
{
int32_t state = 0;
int32_t ret = 0;
ret = getState(&state);
if (state == LIGHT_STATE_ON)
{
digitalWrite(light_pin, HIGH);
}
else if (state == LIGHT_STATE_OFF)
{
digitalWrite(light_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 LIGHT_STATE_OFF
* and @ref LIGHT_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.
*
* @par Usage: a snippet following
* @code
* ret = obj.getState(&state);
* if (state == LIGHT_STATE_ON)
* {
* digitalWrite(light_pin, HIGH);
* }
* else if (state == LIGHT_STATE_OFF)
* {
* digitalWrite(light_pin, LOW);
* }
* else
* {
* // do something else
* }
* @endcode
*
* @see LightState Light::sync
*/
int32_t Light::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, "\"light\":\"on\"");
str_state_off = strstr(response, "\"light\":\"off\"");
if (str_error && str_params)
{
if (str_state_on && !str_state_off)
{
*state = LIGHT_STATE_ON;
return 0;
}
else if (!str_state_on && str_state_off)
{
*state = LIGHT_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 LIGHT_STATE_ON or LIGHT_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 Light::setState(int32_t state)
{
const char **value;
const char *response;
char *str_error;
if (state == LIGHT_STATE_ON)
{
value = VALUE_ON;
}
else if (state == LIGHT_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 light 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 Light::on(void)
{
int32_t ret;
ret = setState(LIGHT_STATE_ON) ;
if (ret == 0)
{
digitalWrite(light_pin, HIGH);
}
return ret;
}
/**
* Turn off the light 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 Light::off(void)
{
int32_t ret;
ret = setState(LIGHT_STATE_OFF) ;
if (ret == 0)
{
digitalWrite(light_pin, LOW);
}
return ret;
}