forked from HowieXue/Linux_Gateway-ioT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpthread_analysis.c~
383 lines (341 loc) · 8.84 KB
/
pthread_analysis.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#include "data_global.h"
#include "link_list.h"
#include "sqlite_link_list.h"
struct getEnvMsg
{
unsigned char sto_no;
unsigned char tem[2];
unsigned char hum[2];
unsigned char x;
unsigned char y;
unsigned char z;
unsigned int ill;
unsigned int battery;
unsigned int adc;
};
struct getGoodsMsg
{
unsigned char sto_no;
unsigned char io;
unsigned char goodsno;
unsigned char goodsnum;
};
extern linklist linkHead;
extern linklist slinkHead;
extern pthread_mutex_t mutex_linklist;
extern pthread_mutex_t mutex_slinklist;
extern pthread_mutex_t mutex_analysis;
extern pthread_mutex_t mutex_global;
extern pthread_mutex_t mutex_sms;
extern pthread_mutex_t mutex_buzzer;
extern pthread_cond_t cond_analysis;
extern pthread_cond_t cond_sqlite;
extern pthread_cond_t cond_refresh;
extern pthread_cond_t cond_buzzer;
extern pthread_cond_t cond_sms;
char tem_alarm_status[STORAGE_NUM] = {0};
char hum_alarm_status[STORAGE_NUM] = {0};
char ill_alarm_status[STORAGE_NUM] = {0};
char beep_status[STORAGE_NUM] = {0};
extern unsigned char dev_sms_mask;
extern int msgid;
extern int dev_buzzer_mask;
extern struct env_info_clien_addr all_info_RT;
float dota_atof (char unitl)
{
if (unitl > 100)
{
return unitl / 1000.0;
}
else if (unitl > 10)
{
return unitl / 100.0;
}
else
{
return unitl / 10.0;
}
}
int dota_atoi (const char * cDecade)
{
int result = 0;
if (' ' != cDecade[0])
{
result = (cDecade[0] - 48) * 10;
}
result += cDecade[1] - 48;
return result;
}
float dota_adc (unsigned int ratio)
{
return ((ratio * 3.6) / 1024);
}
int storageAllgood (int sto_no)
{
if ((tem_alarm_status[sto_no] + hum_alarm_status[sto_no] + ill_alarm_status[sto_no]) == 0)
{
return 0;
}
return 1;
}
int checkEnv (int sto_no, struct storage_info *p)
{
char flag = 0;
static char a8_beep_status = 0;
if (0 == tem_alarm_status[sto_no])
{
if (p->temperature > p->temperatureMAX)
{
sendMsgQueue (MSG_LED, MSG_LED_TEM_ON);
sendMsgQueue (MSG_M0, MSG_M0_FAN_ON3 | sto_no << 6);
p->fan_status = 3;
tem_alarm_status[sto_no] = 2;
flag = 1;
}
else if (p->temperature < p->temperatureMIN)
{
sendMsgQueue (MSG_LED, MSG_LED_TEM_ON);
sendMsgQueue (MSG_M0, MSG_M0_FAN_OFF | sto_no << 6);
p->fan_status = 0;
tem_alarm_status[sto_no] = 1;
flag = 1;
}
if (flag)
{
pthread_mutex_lock (&mutex_sms);
dev_sms_mask = SMS_TEM;
pthread_mutex_unlock (&mutex_sms);
pthread_cond_signal (&cond_sms);
flag = 0;
if (beep_status[sto_no] == 0)
{
beep_status[sto_no] = 1;
sendMsgQueue (MSG_M0, MSG_M0_BEEP_ON | sto_no << 6);
}
if (a8_beep_status == 0)
{
a8_beep_status = 1;
sendMsgQueue (MSG_BEEP, MSG_M0_BEEP_ON);
}
}
}
else
{
if (p->temperature < p->temperatureMAX && p->temperature > p->temperatureMIN)
{
sendMsgQueue (MSG_LED, MSG_LED_TEM_OFF);
sendMsgQueue (MSG_M0, MSG_M0_FAN_OFF | sto_no << 6);
p->fan_status = 0;
tem_alarm_status[sto_no] = 0;
if (!storageAllgood (sto_no))
{
beep_status[sto_no] = 0;
sendMsgQueue (MSG_M0, MSG_M0_BEEP_OFF | sto_no << 6);
a8_beep_status = 0;
sendMsgQueue (MSG_BEEP, MSG_BEEP_OFF);
}
}
}
if (0 == hum_alarm_status[sto_no])
{
if (p->humidity > p->humidityMAX)
{
sendMsgQueue (MSG_LED, MSG_LED_HUM_ON);
sendMsgQueue (MSG_M0, MSG_M0_FAN_ON3 | sto_no << 6);
p->fan_status = 3;
hum_alarm_status[sto_no] = 2;
flag = 1;
}
else if (p->humidity < p->humidityMIN)
{
sendMsgQueue (MSG_LED, MSG_LED_HUM_ON);
sendMsgQueue (MSG_M0, MSG_M0_FAN_OFF | sto_no << 6);
p->fan_status = 0;
hum_alarm_status[sto_no] = 1;
flag = 1;
}
if (flag)
{
pthread_mutex_lock (&mutex_sms);
dev_sms_mask = SMS_HUM;
pthread_mutex_unlock (&mutex_sms);
pthread_cond_signal (&cond_sms);
flag = 0;
if (beep_status[sto_no] == 0)
{
beep_status[sto_no] = 1;
sendMsgQueue (MSG_M0, MSG_M0_BEEP_ON | sto_no << 6);
}
if (a8_beep_status == 0)
{
a8_beep_status = 1;
sendMsgQueue (MSG_BEEP, MSG_M0_BEEP_ON);
}
}
}
else
{
if (p->humidity < p->humidityMAX && p->humidity > p->humidityMIN)
{
sendMsgQueue (MSG_LED, MSG_LED_HUM_OFF);
sendMsgQueue (MSG_M0, MSG_M0_FAN_OFF | sto_no << 6);
p->fan_status = 0;
hum_alarm_status[sto_no] = 0;
if (!storageAllgood (sto_no))
{
beep_status[sto_no] = 0;
sendMsgQueue (MSG_M0, MSG_M0_BEEP_OFF | sto_no << 6);
a8_beep_status = 0;
sendMsgQueue (MSG_BEEP, MSG_BEEP_OFF);
}
}
}
if (0 == ill_alarm_status[sto_no])
{
if (p->illumination > p->illuminationMAX)
{
sendMsgQueue (MSG_LED, MSG_LED_ILL_ON);
sendMsgQueue (MSG_M0, MSG_M0_LED_OFF | sto_no << 6);
p->led_status = 0;
ill_alarm_status[sto_no] = 2;
flag = 1;
}
else if (p->illumination < p->illuminationMIN)
{
sendMsgQueue (MSG_LED, MSG_LED_ILL_ON);
sendMsgQueue (MSG_M0, MSG_M0_LED_ON | sto_no << 6);
p->led_status = 1;
ill_alarm_status[sto_no] = 1;
flag = 1;
}
if (flag)
{
pthread_mutex_lock (&mutex_sms);
dev_sms_mask = SMS_ILL;
pthread_mutex_unlock (&mutex_sms);
pthread_cond_signal (&cond_sms);
flag = 0;
if (beep_status[sto_no] == 0)
{
beep_status[sto_no] = 1;
sendMsgQueue (MSG_M0, MSG_M0_BEEP_ON | sto_no << 6);
}
if (a8_beep_status == 0)
{
a8_beep_status = 1;
sendMsgQueue (MSG_BEEP, MSG_M0_BEEP_ON);
}
}
}
else if (ill_alarm_status[sto_no])
{
if (p->illumination < p->illuminationMAX && p->illumination > p->illuminationMIN)
{
sendMsgQueue (MSG_LED, MSG_LED_ILL_OFF);
sendMsgQueue (MSG_M0, MSG_M0_LED_OFF | sto_no << 6);
p->led_status = 0;
ill_alarm_status[sto_no] = 0;
if (!storageAllgood (sto_no))
{
beep_status[sto_no] = 0;
sendMsgQueue (MSG_M0, MSG_M0_BEEP_OFF | sto_no << 6);
a8_beep_status = 0;
sendMsgQueue (MSG_BEEP, MSG_BEEP_OFF);
}
}
}
return 0;
}
void getEnvPackage (link_datatype *buf)
{
struct getEnvMsg pack;
memcpy (&pack, buf->text, 20);
int sto_no = pack.sto_no;
pthread_mutex_lock (&mutex_global);
struct storage_info current = all_info_RT.storage_no[sto_no];
pthread_mutex_unlock (&mutex_global);
current.storage_status = 1;
current.x = pack.x;
current.y = pack.y;
current.z = pack.z;
current.temperature = pack.tem[0] + dota_atof (pack.tem[1]);
current.humidity = pack.hum[0] + dota_atof (pack.hum[1]);
current.illumination = pack.ill;
current.battery = dota_adc (pack.battery);
current.adc = dota_adc (pack.adc);
printf ("no = %d tem = %0.2f hum = %0.2f ill = %0.2f battery = %0.2f adc = %0.2f x = %d y = %d z = %d\n", sto_no,
current.temperature, current.humidity, current.illumination, current.battery, current.adc, current.x, current.y, current.z);
checkEnv (sto_no, ¤t);
pthread_mutex_lock (&mutex_global);
all_info_RT.storage_no[sto_no] = current;
pthread_mutex_lock (&mutex_slinklist);
sqlite_InsertLinknode (COLLECT_INSERTER, all_info_RT, sto_no, 0);//0,0分别是仓库号和货物种类号
pthread_mutex_unlock (&mutex_slinklist);
pthread_mutex_unlock (&mutex_global);
pthread_cond_signal (&cond_refresh);
pthread_cond_signal (&cond_sqlite);
return ;
}
void getGoodsPackage (link_datatype *buf)
{
struct getGoodsMsg pack;
memcpy (&pack, buf->text, 16);
int sto_no = pack.sto_no;
pthread_mutex_lock (&mutex_global);
struct storage_info current = all_info_RT.storage_no[sto_no];
pthread_mutex_unlock (&mutex_global);
current.storage_status = 1;
current.goods_info[pack.goodsno].goods_type = pack.goodsno;
current.goods_info[pack.goodsno].goods_count = pack.goodsnum;
printf ("sto_no = %d, io = %c goods_type = %d, goods_num = %d\n", sto_no, pack.io, current.goods_info[pack.goodsno].goods_type, current.goods_info[pack.goodsno].goods_count);
pthread_mutex_lock (&mutex_global);
all_info_RT.storage_no[sto_no] = current;
pthread_mutex_lock (&mutex_slinklist);
if (pack.io == 'I')
{
sqlite_InsertLinknode (GOODS_ADD, all_info_RT, sto_no, pack.goodsno);//0,0分别是仓库号和货物种类号
}
else if (pack.io == 'O')
{
sqlite_InsertLinknode (GOODS_REDUCE, all_info_RT, sto_no, pack.goodsno);//0,0分别是仓库号和货物种类号
}
pthread_mutex_unlock (&mutex_slinklist);
pthread_mutex_unlock (&mutex_global);
pthread_cond_signal (&cond_refresh);
pthread_cond_signal (&cond_sqlite);
return ;
}
void *pthread_analysis (void *arg)
{
linklist node;
link_datatype buf;
printf ("pthread_analysis is ok\n");
while (1)
{
pthread_mutex_lock (&mutex_analysis);
pthread_cond_wait (&cond_analysis, &mutex_analysis);
pthread_mutex_unlock (&mutex_analysis);
// printf ("wake pthread_analysis wake up\n");
while (1)
{
pthread_mutex_lock (&mutex_linklist);
if ((node = GetLinknode (linkHead)) == NULL)
{
pthread_mutex_unlock (&mutex_linklist);
break;
}
buf = node->data;
free (node);
pthread_mutex_unlock (&mutex_linklist);
if ('e' == buf.msg_type)
{
getEnvPackage (&buf);
}
else if ('r' == buf.msg_type)
{
getGoodsPackage (&buf);
}
}
}
return 0;
}