-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathuser_function.c
340 lines (288 loc) · 11.9 KB
/
user_function.c
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include "appmain.h"
#include "user_gpio.h"
#include "cJSON/cJSON.h"
//#include "user_ota.h"
//#include "user_mqtt_client.h"
#include "user_udp.h"
#include <string.h>
uint32_t last_time = 0;
void user_function_set_last_time( )
{
//last_time = UpTicks( );
}
bool json_plug_analysis( int udp_flag, unsigned char x, cJSON * pJsonRoot, cJSON * pJsonSend );
bool json_plug_task_analysis(unsigned char x, unsigned char y, cJSON * pJsonRoot, cJSON * pJsonSend );
void user_send( int udp_flag, char *s )
{
//if ( udp_flag || !user_mqtt_isconnect( ) )
user_udp_send( s ); //发送数据
// else
//user_mqtt_send( s );
}
void USER_FUNC user_function_cmd_received( int udp_flag, char *pusrdata )
{
unsigned char i;
bool update_user_config_flag = false; //标志位,记录最后是否需要更新储存的数据
bool return_flag = true; //为true时返回json结果,否则不返回
cJSON * pJsonRoot = cJSON_Parse( pusrdata );
if ( !pJsonRoot )
{
u_printf( "this is not a json data:\r\n%s\r\n", pusrdata );
return;
}else{
u_printf( "this is a json data:\r\n%s\r\n", pusrdata );
}
//解析UDP命令device report(MQTT同样回复命令)
cJSON *p_cmd = cJSON_GetObjectItem( pJsonRoot, "cmd" );
if ( p_cmd && cJSON_IsString( p_cmd ) && strcmp( p_cmd->valuestring, "device report" ) == 0 )
{
cJSON *pRoot = cJSON_CreateObject( );
cJSON_AddStringToObject( pRoot, "name", deviceid );
cJSON_AddStringToObject( pRoot, "mac", strMac );
cJSON_AddNumberToObject( pRoot, "type", TYPE );
cJSON_AddStringToObject( pRoot, "type_name", TYPE_NAME );
cJSON_AddStringToObject( pRoot, "ip", strIp );
char *s = cJSON_Print( pRoot );
user_send( udp_flag, s ); //发送数据
u_printf( "udp_flag:%d, send json pRoot: %s\r\n",udp_flag, s );
hfmem_free( (void *) s );
cJSON_Delete( pRoot );
// cJSON_Delete(p_cmd);
}
//以下为解析命令部分
cJSON *p_name = cJSON_GetObjectItem( pJsonRoot, "name" );
cJSON *p_mac = cJSON_GetObjectItem( pJsonRoot, "mac" );
//开始正式处理所有命令
if ( (p_name && cJSON_IsString( p_name ) ) //name
|| (p_mac && cJSON_IsString( p_mac ) && strcmp( p_mac->valuestring, strMac ) == 0) //mac
)
{
cJSON *json_send = cJSON_CreateObject( );
cJSON_AddStringToObject( json_send, "mac", strMac );
//解析版本
cJSON *p_version = cJSON_GetObjectItem( pJsonRoot, "version" );
if ( p_version )
{
u_printf("version:%s",VERSION);
cJSON_AddStringToObject( json_send, "version", VERSION );
}
//解析主机setting-----------------------------------------------------------------
cJSON *p_setting = cJSON_GetObjectItem( pJsonRoot, "setting" );
if ( p_setting )
{
//解析ota
cJSON *p_ota = cJSON_GetObjectItem( p_setting, "ota" );
if ( p_ota )
{
if ( cJSON_IsString( p_ota ) ){}
//user_ota_start( p_ota->valuestring, NULL );
}
cJSON *json_setting_send = cJSON_CreateObject( );
//设置设备名称/deviceid
cJSON *p_setting_name = cJSON_GetObjectItem( p_setting, "name" );
if ( p_setting_name && cJSON_IsString( p_setting_name ) )
{
update_user_config_flag = true;
//u_printf("u_config.plug.name:%s\n",u_config.plug[1].name);
sprintf( deviceid, p_setting_name->valuestring );
//u_printf("p_setting_name->valuestring:%s\n",deviceid);
//
}
//设置mqtt ip
cJSON *p_mqtt_ip = cJSON_GetObjectItem( p_setting, "mqtt_uri" );
if ( p_mqtt_ip && cJSON_IsString( p_mqtt_ip ) )
{
update_user_config_flag = true;
sprintf( u_config.mqtt_ip, p_mqtt_ip->valuestring );
}
//设置mqtt port
cJSON *p_mqtt_port = cJSON_GetObjectItem( p_setting, "mqtt_port" );
if ( p_mqtt_port && cJSON_IsNumber( p_mqtt_port ) )
{
update_user_config_flag = true;
u_config.mqtt_port = p_mqtt_port->valueint;
}
//设置mqtt user
cJSON *p_mqtt_user = cJSON_GetObjectItem( p_setting, "mqtt_user" );
if ( p_mqtt_user && cJSON_IsString( p_mqtt_user ) )
{
update_user_config_flag = true;
sprintf( u_config.mqtt_user, p_mqtt_user->valuestring );
}
//设置mqtt password
cJSON *p_mqtt_password = cJSON_GetObjectItem( p_setting, "mqtt_password" );
if ( p_mqtt_password && cJSON_IsString( p_mqtt_password ) )
{
update_user_config_flag = true;
sprintf( u_config.mqtt_password, p_mqtt_password->valuestring );
}
//开发返回数据
//返回设备ota
if ( p_ota ) cJSON_AddStringToObject( json_setting_send, "ota", p_ota->valuestring );
//返回设备名称/deviceid
if ( p_setting_name ) cJSON_AddStringToObject( json_setting_send, "name", deviceid);
//返回mqtt ip
if ( p_mqtt_ip ) cJSON_AddStringToObject( json_setting_send, "mqtt_uri", u_config.mqtt_ip );
//返回mqtt port
if ( p_mqtt_port ) cJSON_AddNumberToObject( json_setting_send, "mqtt_port", u_config.mqtt_port );
//返回mqtt user
if ( p_mqtt_user ) cJSON_AddStringToObject( json_setting_send, "mqtt_user", u_config.mqtt_user );
//返回mqtt password
if ( p_mqtt_password ) cJSON_AddStringToObject( json_setting_send, "mqtt_password", u_config.mqtt_password );
cJSON_AddItemToObject( json_send, "setting", json_setting_send );
}
//解析plug-----------------------------------------------------------------
for ( i = 0; i < PLUG_NUM; i++ )
{
if ( json_plug_analysis( udp_flag, i, pJsonRoot, json_send ) )
update_user_config_flag = true;
}
cJSON_AddStringToObject( json_send, "name", deviceid );
if ( return_flag == true )
{
char *json_str = cJSON_Print( json_send );
//u_printf( "[user_function_cmd_received] pRoot: %s\r\n", json_str );
user_send( udp_flag, json_str ); //发送数据
hfmem_free( (void *) json_str );
}
cJSON_Delete( json_send );
}
if ( update_user_config_flag )
{
//mico_system_context_update( sys_config );
update_user_config_flag = false;
}
cJSON_Delete( pJsonRoot );
}
/*
*解析处理定时任务json
*udp_flag:发送udp/mqtt标志位,此处修改插座开关状态时,需要实时更新给domoticz
*x:插座编号
*/
bool json_plug_analysis( int udp_flag, unsigned char x, cJSON * pJsonRoot, cJSON * pJsonSend )
{
if ( !pJsonRoot ) return false;
if ( !pJsonSend ) return false;
char i;
bool return_flag = false;
char plug_str[] = "plug_X";
plug_str[5] = x + '0';
cJSON *p_plug = cJSON_GetObjectItem( pJsonRoot, plug_str );
if ( !p_plug ) return_flag = false;
cJSON *json_plug_send = cJSON_CreateObject( );
//u_printf("plug_str: %s\n",plug_str);
//解析plug on------------------------------------------------------
if ( p_plug )
{
cJSON *p_plug_on = cJSON_GetObjectItem( p_plug, "on" );
if ( p_plug_on )
{
if ( cJSON_IsNumber( p_plug_on ) )
{
user_relay_set( x, p_plug_on->valueint );
u_printf("[json_plug_analysis] p_plug_on->valueint:%d",p_plug_on->valueint);
return_flag = true;
}
//user_mqtt_send_plug_state(x);
}
//解析plug中setting项目----------------------------------------------
cJSON *p_plug_setting = cJSON_GetObjectItem( p_plug, "setting" );
if ( p_plug_setting )
{
cJSON *json_plug_setting_send = cJSON_CreateObject( );
//解析plug中setting中name----------------------------------------
cJSON *p_plug_setting_name = cJSON_GetObjectItem( p_plug_setting, "name" );
if ( p_plug_setting_name )
{
if ( cJSON_IsString( p_plug_setting_name ) )
{
return_flag = true;
u_printf("u_config.plug[%s].name;%s\n",x,u_config.plug[x].name);
sprintf( u_config.plug[x].name, p_plug_setting_name->valuestring );
//user_mqtt_hass_auto(x);
}
cJSON_AddStringToObject( json_plug_setting_send, "name", u_config.plug[x].name );
}
//解析plug中setting中task----------------------------------------
for ( i = 0; i < PLUG_TIME_TASK_NUM; i++ )
{
if ( json_plug_task_analysis( x, i, p_plug_setting, json_plug_setting_send ) )
return_flag = true;
}
cJSON_AddItemToObject( json_plug_send, "setting", json_plug_setting_send );
}
}
// cJSON *p_nvalue = cJSON_GetObjectItem( pJsonRoot, "nvalue" );
// if ( p_plug || p_nvalue )
cJSON_AddNumberToObject( json_plug_send, "on", u_config.plug[x].on );
cJSON_AddItemToObject( pJsonSend, plug_str, json_plug_send );
return return_flag;
}
/*
*解析处理定时任务json
*x:插座编号 y:任务编号
*/
bool json_plug_task_analysis( unsigned char x, unsigned char y, cJSON * pJsonRoot, cJSON * pJsonSend )
{
if ( !pJsonRoot ) return false;
bool return_flag = false;
char plug_task_str[] = "task_X";
plug_task_str[5] = y + '0';
cJSON *p_plug_task = cJSON_GetObjectItem( pJsonRoot, plug_task_str );
if ( !p_plug_task ) return false;
cJSON *json_plug_task_send = cJSON_CreateObject( );
cJSON *p_plug_task_hour = cJSON_GetObjectItem( p_plug_task, "hour" );
cJSON *p_plug_task_minute = cJSON_GetObjectItem( p_plug_task, "minute" );
cJSON *p_plug_task_repeat = cJSON_GetObjectItem( p_plug_task, "repeat" );
cJSON *p_plug_task_action = cJSON_GetObjectItem( p_plug_task, "action" );
cJSON *p_plug_task_on = cJSON_GetObjectItem( p_plug_task, "on" );
if ( p_plug_task_hour && p_plug_task_minute && p_plug_task_repeat &&
p_plug_task_action
&& p_plug_task_on )
{
if ( cJSON_IsNumber( p_plug_task_hour )
&& cJSON_IsNumber( p_plug_task_minute )
&& cJSON_IsNumber( p_plug_task_repeat )
&& cJSON_IsNumber( p_plug_task_action )
&& cJSON_IsNumber( p_plug_task_on )
)
{
return_flag = true;
u_config.plug[x].task[y].hour = p_plug_task_hour->valueint;
u_config.plug[x].task[y].minute = p_plug_task_minute->valueint;
u_config.plug[x].task[y].repeat = p_plug_task_repeat->valueint;
u_config.plug[x].task[y].action = p_plug_task_action->valueint;
u_config.plug[x].task[y].on = p_plug_task_on->valueint;
}
}
cJSON_AddNumberToObject( json_plug_task_send, "hour", u_config.plug[x].task[y].hour );
cJSON_AddNumberToObject( json_plug_task_send, "minute", u_config.plug[x].task[y].minute );
cJSON_AddNumberToObject( json_plug_task_send, "repeat", u_config.plug[x].task[y].repeat );
cJSON_AddNumberToObject( json_plug_task_send, "action", u_config.plug[x].task[y].action );
cJSON_AddNumberToObject( json_plug_task_send, "on", u_config.plug[x].task[y].on );
cJSON_AddItemToObject( pJsonSend, plug_task_str, json_plug_task_send );
u_printf("plug task set done. plug_task_str:%s\n",plug_task_str);
return return_flag;
}
unsigned char strtohex( char a, char b )
{
if ( a >= 0x30 && a <= 0x39 )
a -= 0x30;
else if ( a >= 0x41 && a <= 0x46 )
{
a = a + 10 - 0x41;
} else if ( a >= 0x61 && a <= 0x66 )
{
a = a + 10 - 0x61;
}
if ( b >= 0x30 && b <= 0x39 )
b -= 0x30;
else if ( b >= 0x41 && b <= 0x46 )
{
b = b + 10 - 0x41;
} else if ( b >= 0x61 && b <= 0x66 )
{
b = b + 10 - 0x61;
}
return a * 16 + b;
}