-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
286 lines (253 loc) · 9.52 KB
/
server.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
#include <stdio.h>
#include <mysql.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include "cJSON.h"
#define DATABASE "agricultural_system"
#define TABLE_HISTORY "history"
#define TABLE_VALUE "value"
#define PORT 8002
#define VER "8.0.27"
int parse_json_recv(const char* buf, char* id, float* temp, float *humi, char* motor, char* sw);
void finish_with_error(MYSQL *con);
int get_value(MYSQL *con, const char* table, const char* field, const char* id, char* buf);
int insert_value(MYSQL *con, const char* table, char* id, float temp, float humi, char* motor, char* sw);
int main(){
MYSQL *con = NULL;
char id[20] = {0};
char buf[20] = {0};
char sql[100];
/* 存放终端信息 */
float temp = 0.0;
float humi = 0.0;
char motor[10]={0};
char sw[10]={0};
/* 存放阈值 */
float temp_max, temp_min;
float humi_max, humi_min;
/* UDP scoket */
int server_sock_fd;
struct sockaddr_in server_addr, client_addr;
char udp_buf[100];
int nbytes = 0;
socklen_t len = 0;
printf("************************************************\n");
printf("* *\n");
printf("* 欢迎登陆UDP的数据采集服务端 *\n");
printf("* *\n");
printf("* *\n");
printf("* 作者:yangzhaung UDP监听端口:%-4d *\n", PORT);
printf("* 数据库:MySQL 版本:v%3s *\n", VER);
printf("* *\n");
printf("************************************************\n");
/* 初始化MYSQL变量并连接数据库 */
con = mysql_init(NULL);
if(con == NULL){
printf("MySQL init fail.\n");
fprintf(stderr,"%s\n",mysql_error(con));
return -1;
}
if(NULL == mysql_real_connect(con,"localhost","root","123456",DATABASE,3306,NULL,0)){
printf("MySQL connect fail.\n");
fprintf(stderr,"%s\n",mysql_error(con));
mysql_close(con);
return -1;
}
printf("[System]成功连接到数据库:%s!\n", DATABASE);
/* 创建Server Socket */
server_sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (server_sock_fd < 0){
printf("[System]服务端Socket创建失败\n");
return -1;
}
/* 绑定ip和端口 */
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(PORT);//指定端口号
bind(server_sock_fd, (struct sockaddr *) &server_addr, sizeof(server_addr));
printf("[System]系统已启动监听端口%d!\n", PORT);
while(1){
printf("************************************************\n");
len = sizeof(client_addr);
nbytes = recvfrom(server_sock_fd, udp_buf, 100, 0, (struct sockaddr *)&client_addr, &len);
udp_buf[nbytes] = '\0';
printf("[UDP Server]接收到来自数据终端(%s)的消息:%s\n", inet_ntoa(client_addr.sin_addr), udp_buf);
//使用CJSON解析提取终端上报数据
if(-1 == parse_json_recv(udp_buf, id, &temp, &humi, motor, sw)){
printf("[CJSON]解析终端数据失败!\n");
return -1;
}
/* 插入历史记录 */
if(-1 != insert_value(con, TABLE_HISTORY, id, temp, humi, motor, sw)){
printf("[MySQL]终端[%10s]插入一条记录!\n", id);
}
else{
printf("[MySQL]终端[%10s]插入记录失败!\n", id);
return -1;
}
/* 查询此终端当前阈值 */
//查询当前温度最大阈值
if(-1 == get_value(con, TABLE_VALUE, "temp_max", id, buf)){
printf("[MySQL][终端%s]最大温度阈值查询失败!\n", id);
return -1;
}
temp_max = atof(buf);
//查询当前温度最小阈值
if(-1 == get_value(con, TABLE_VALUE, "temp_min", id, buf)){
printf("[MySQL][终端%s]最小温度阈值查询失败!\n", id);
return -1;
}
temp_min = atof(buf);
//查询当前湿度最大阈值
if(-1 == get_value(con, TABLE_VALUE, "humi_max", id, buf)){
printf("[MySQL][终端%s]最大湿度阈值查询失败!\n", id);
return -1;
}
humi_max = atof(buf);
//查询当前湿度最小阈值
if(-1 == get_value(con, TABLE_VALUE, "humi_min", id, buf)){
printf("[MySQL][终端%s]最小温度阈值查询失败!\n", id);
return -1;
}
humi_min = atof(buf);
printf("[MySQL]当前终端[%10s]的设定阈值为:temp_max=%2.1f,temp_min=%2.1f,humi_max=%2.1f,humi_min=%2.1f\n",id,temp_max,temp_min,humi_max,humi_min);
/* 报警判断 */
if(temp > temp_max){
sprintf(udp_buf, "motoron");
sendto(server_sock_fd,udp_buf,strlen(udp_buf),0,(struct sockaddr *)&client_addr,len);
printf("[UDP Server]系统已经向控制终端发送电机开启指令!\n");
}
else if(temp < temp_min){
sprintf(udp_buf, "motoroff");
sendto(server_sock_fd,udp_buf,strlen(udp_buf),0,(struct sockaddr *)&client_addr,len);
printf("[UDP Server]系统已经向控制终端发送电机关闭指令!\n");
}
if(humi > humi_max){
sprintf(udp_buf, "switchon");
sendto(server_sock_fd,udp_buf,strlen(udp_buf),0,(struct sockaddr *)&client_addr,len);
printf("[UDP Server]系统已经向控制终端发送水阀关闭指令!\n");
}
else if(humi < humi_min){
sprintf(udp_buf, "switchoff");
sendto(server_sock_fd,udp_buf,strlen(udp_buf),0,(struct sockaddr *)&client_addr,len);
printf("[UDP Server]系统已经向控制终端发送水阀开启指令!\n");
}
else{
sprintf(udp_buf, "normal");
sendto(server_sock_fd,udp_buf,strlen(udp_buf),0,(struct sockaddr *)&client_addr,len);
printf("[UDP Server]系统正常运行!\n");
}
}
mysql_close(con);
return 0;
}
/**
* 使用cJSON从接收到数据中提取终端ID、温度、湿度、电机状态、开关状态
* @param buf 需要处理的数据(json格式)
* @param id 存放提取出的用户ID
* @param
* @return 解析成功返回0,解析失败返回-1
*/
int parse_json_recv(const char* buf, char* id, float* temp, float *humi, char* motor, char* sw){
cJSON* cjson_all = NULL;
cJSON* cjson_id = NULL;
cJSON* cjson_temp = NULL;
cJSON* cjson_humi = NULL;
cJSON* cjson_motor = NULL;
cJSON* cjson_switch = NULL;
/* 解析整段JSON数据 */
cjson_all = cJSON_Parse(buf);
if(cjson_all == NULL){
return -1;
}
/* 根据键值提取终端ID */
cjson_id = cJSON_GetObjectItem(cjson_all, "id");
strcpy(id, cjson_id->valuestring);
/* 根据键值提取终端温度 */
cjson_temp = cJSON_GetObjectItem(cjson_all, "temp");
*temp = (float)cjson_temp->valuedouble;
/* 根据键值提取终端湿度 */
cjson_humi = cJSON_GetObjectItem(cjson_all, "humi");
*humi = (float)cjson_humi->valuedouble;
/* 根据键值提取终端电机状态 */
cjson_motor = cJSON_GetObjectItem(cjson_all, "motor");
strcpy(motor, cjson_motor->valuestring);
/* 根据键值提取终端开关状态 */
cjson_switch = cJSON_GetObjectItem(cjson_all, "sw");
strcpy(sw, cjson_switch->valuestring);
/* 释放cjson占用内存 */
cJSON_Delete(cjson_all);
return 0;
}
/**
* 语句执行出错处理
* @param con 需要处理的MYSQL变量
* @return -1;
*/
void finish_with_error(MYSQL *con){
fprintf(stderr,"%s\n",mysql_error(con));
mysql_close(con);
}
/**
* 从数据库中查询用户信息
* @param con 成功连接的MySQL变量
* @param table 需要查询的表
* @param id 用户id
* @param buf 存放查询结果的缓冲区
* @return 成功返回0,失败则返回-1
*/
int get_value(MYSQL *con, const char* table, const char* field, const char* id, char* buf){
char sql[100];
MYSQL_RES *result = NULL;
MYSQL_ROW row;
//构造完整sql语句
sprintf(sql, "select %s from %s where id=%11s;", field, table, id);
//查询
if(mysql_query(con,sql)){
finish_with_error(con);
return -1;
}
//获取并存储查询结果
result = mysql_store_result(con);
if(NULL == result){
finish_with_error(con);
return -1;
}
//根据行数查询数据
if(row = mysql_fetch_row(result)){
if(row[0] != NULL){
strcpy(buf, row[0]);
}
else{
strcpy(buf, "NULL");
}
//printf("用户%s的用电类型为:%s\n", id, );
}
mysql_free_result(result);
return 0;
}
/**
* 向数据库中插入信息
* @param con 成功连接的MySQL变量
* @param table 需要更新的表
* @param id 用户id
* @param buf 存放待更新数据的缓冲区
* @return 成功返回0,失败则返回-1
*/
int insert_value(MYSQL *con, const char* table, char* id, float temp, float humi, char* motor, char* sw){
char sql[100];
//构造完整sql语句
sprintf(sql, "insert into %s (id,temp,humi,motor,switch) values(%10s,%2.1f,%2.1f,\"%3s\",\"%3s\")", table, id, temp, humi, motor,sw);
//执行
if(mysql_query(con,sql)){
finish_with_error(con);
return -1;
}
return 0;
}