-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreply.c
More file actions
144 lines (118 loc) · 5.08 KB
/
Copy pathreply.c
File metadata and controls
144 lines (118 loc) · 5.08 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "tcp.h"
#include "json.h"
#include "reply.h"
#include "gpio.h"
char send_buf[1024];
void reply_heart_beat()
{
json_object *my_object = json_object_new_object();
json_object_object_add(my_object, "method",json_object_new_string("update"));
json_object_object_add(my_object, "gatewayNo",json_object_new_string(devno));
json_object_object_add(my_object, "userkey", json_object_new_string(userid));
strcpy(send_buf, json_object_to_json_string(my_object));
strcat(send_buf,"&^!");
// printf("my_object.to_string()=%s\n",send_buf);
send(sockfd,send_buf,strlen(send_buf),0);
json_object_put(my_object);
}
void replay_sta(int success,char *msg)
{
json_object *my_object = json_object_new_object();
json_object *sub_object=json_object_new_object();
json_object_object_add(sub_object, "successful",json_object_new_boolean(success));
json_object_object_add(sub_object, "message",json_object_new_string(msg));
json_object_object_add(my_object, "method",json_object_new_string("response"));
json_object_object_add(my_object, "result",sub_object);
strcpy(send_buf, json_object_to_json_string(my_object));
strcat(send_buf,"&^!");
// printf("my_object.to_string()=%s\n",send_buf);
send(sockfd,send_buf,strlen(send_buf),0);
json_object_put(my_object);
json_object_put(sub_object);
}
void replay_sensor_list()
{
json_object *my_object = json_object_new_object();
json_object *my_array = json_object_new_array();
json_object *sub_object=json_object_new_object();
json_object *dev0_object=json_object_new_object();
json_object *dev1_object=json_object_new_object();
json_object *dev2_object=json_object_new_object();
json_object *dev3_object=json_object_new_object();
json_object_object_add(dev0_object, "id",json_object_new_string("sw0"));
if(gpio_get(P8_12)==0)
json_object_object_add(dev0_object, "value",json_object_new_string("1"));
else
json_object_object_add(dev0_object, "value",json_object_new_string("0"));
json_object_object_add(dev1_object, "id",json_object_new_string("sw1"));
if(gpio_get(P8_14)==0)
json_object_object_add(dev1_object, "value",json_object_new_string("1"));
else
json_object_object_add(dev1_object, "value",json_object_new_string("0"));
json_object_object_add(dev2_object, "id",json_object_new_string("sw2"));
if(gpio_get(P8_16)==0)
json_object_object_add(dev2_object, "value",json_object_new_string("1"));
else
json_object_object_add(dev2_object, "value",json_object_new_string("0"));
json_object_object_add(dev3_object, "id",json_object_new_string("sw3"));
if(gpio_get(P8_18)==0)
json_object_object_add(dev3_object, "value",json_object_new_string("1"));
else
json_object_object_add(dev3_object, "value",json_object_new_string("0"));
json_object_array_add(my_array,dev0_object);
json_object_array_add(my_array,dev1_object);
json_object_array_add(my_array,dev2_object);
json_object_array_add(my_array,dev3_object);
json_object_object_add(sub_object, "successful",json_object_new_boolean(1));
json_object_object_add(sub_object, "message",json_object_new_string("ok"));
json_object_object_add(sub_object, "data",my_array);
json_object_object_add(my_object, "method",json_object_new_string("response"));
json_object_object_add(my_object, "result",sub_object);
strcpy(send_buf, json_object_to_json_string(my_object));
strcat(send_buf,"&^!");
// printf("my_object.to_string()=%s\n",send_buf);
send(sockfd,send_buf,strlen(send_buf),0);
json_object_put(my_object);
json_object_put(my_array);
json_object_put(sub_object);
json_object_put(dev0_object);
json_object_put(dev1_object);
json_object_put(dev2_object);
json_object_put(dev3_object);
}
void reply_update(int fd)
{
char tmp_buf[8];
int16_t temp=1,press=1;
// temp=mpl3115_get_temp(fd);
// press=mpl3115_get_press(fd);
json_object *my_object = json_object_new_object();
json_object *sensor_array = json_object_new_array();
json_object *t1_object=json_object_new_object();
json_object *p1_object=json_object_new_object();
sprintf(tmp_buf,"%.1f",temp/10.0);
json_object_object_add(t1_object,"Name",json_object_new_string("T1"));
json_object_object_add(t1_object,"Value",json_object_new_string(tmp_buf));
sprintf(tmp_buf,"%.1f",press/10.0);
json_object_object_add(p1_object,"Name",json_object_new_string("P1"));
json_object_object_add(p1_object,"Value",json_object_new_string(tmp_buf));
json_object_array_add(sensor_array,t1_object);
json_object_array_add(sensor_array,p1_object);
json_object_object_add(my_object, "method",json_object_new_string("upload"));
json_object_object_add(my_object, "data",sensor_array);
strcpy(send_buf, json_object_to_json_string(my_object));
strcat(send_buf,"&^!");
// printf("my_object.to_string()=%s\n",send_buf);
send(sockfd,send_buf,strlen(send_buf),0);
json_object_put(my_object);
json_object_put(sensor_array);
json_object_put(t1_object);
json_object_put(p1_object);
}