-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathpthread_sqlite.c
1159 lines (1091 loc) · 29.3 KB
/
pthread_sqlite.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "pthread_sqlite.h"
#include "sqlite3.h"
#define TEMPERATURE_MAX 1
#define TEMPERATURE_MIN 2
#define HUMIDITY_MAX 3
#define HUMIDITY_MIN 4
#define ILLUMINATION_MAX 5
#define ILLUMINATION_MIN 6
#define TELEPHONE_NUM 7
#define MODE 8
#define IFNOCICE 9
#define INTERVAL 10
#define UPDATE_PERIOD 11
#define USER_NAME 12
#define PASSWORD 13
float callback_val[15];
char callback_str[20];
extern int storageNum;
extern int goodsKinds;
extern struct storage_info storage_no_s;
extern struct env_info_clien_addr env_info_clien_addr_s;
extern struct env_info_clien_addr all_info_RT;
extern struct sqlite_operation sqlite_operation_s;
extern slinklist slinkHead, slinkTail;
/*
**回到函数,查询之后的信息都通过
*/
static int callback_getenv (void * para, int n_column, char ** column_value, char ** column_name )
{
int i;
#if DEBUG_SQLITE
printf ("n_column = %d\n", n_column);
#endif
for (i=0; i<n_column; i++)
{
if (n_column-1 == i)
{
strcpy (callback_str, column_value[i]);
#if DEBUG_SQLITE
printf ("%s = %s\n", column_name[i], callback_str);
#endif
}
else
{
callback_val[i] = (float)atof(column_value[i]);
#if DEBUG_SQLITE
printf ("%s = %f\n", column_name[i], callback_val[i]);
#endif
}
}
#if DEBUG_SQLITE
printf ("**********************\n");
#endif
return 0;
}
static int callback_getenv_s (void * para, int n_column, char ** column_value, char ** column_name )
{
strcpy (callback_str, column_value[0]);
#if DEBUG_SQLITE
printf ("%s = %s\n", column_name[0], callback_str);
#endif
return 0;
}
/*
**创建数据库表
*/
int Create_table (void)
{
char sql[1024];
sqlite3 *db;
char *err_msg = 0;
int recode;
recode = sqlite3_open (SQLITE_OPEN, &db);
if (recode != SQLITE_OK)
{
printf ("Can't Open Database:%s!\n",sqlite3_errmsg(db));
sqlite3_close (db);
return 1;
}
else
{
#if DEBUG_SQLITE
printf ("open OK!\n");
#endif
sprintf (sql,"create table env(dev_no int not null,\
temperatureMax float not null,\
temperatureMin float not null,\
humidityMax float not null,\
humidityMin float not null,\
illuminationMax float not null,\
illuminationMin float not null,\
telephoneNum varchar(11) not null,\
mode int not null,\
ifnocice int not null,\
interval int not null,\
updatePeriod int not null,\
username varchar(20) not null,\
password int not null);");
recode = sqlite3_exec (db,sql,0,0,&err_msg);
if (recode != SQLITE_OK)
{
printf ("Error:%s",err_msg);
sqlite3_close (db);
return 1;
}
else
{
#if DEBUG_SQLITE
printf ("creat env OK!\n");
#endif
}
sprintf (sql,"create table collect_env(dev_no32 int not null,\
temperatureVal float not null,\
humidityVal float not null,\
illuminationVal float not null,\
envtime varchar(10) not null);");
recode = sqlite3_exec (db,sql,0,0,&err_msg);
if (recode != SQLITE_OK)
{
printf ("Error:%s",err_msg);
sqlite3_close (db);
return 1;
}
else
{
#if DEBUG_SQLITE
printf ("creat collect_env OK!\n");
#endif
Init_table_env ();
#if DEBUG_SQLITE
printf ("collect_env init OK!\n");
#endif
}
sprintf (sql,"create table goods(dev_no int not null,\
goodsId int not null,\
goodsCount int not null,\
goodsTime varchar(10) not null);");
recode =sqlite3_exec (db,sql,0,0,&err_msg);
if (recode != SQLITE_OK)
{
printf("Error:%s",err_msg);
sqlite3_close(db);
return 1;
}
else
{
#if DEBUG_SQLITE
printf ("creat goods OK!\n");
#endif
sqlite3_close(db);
return 0;
}
}
}
/*
**初始化环境表,
*/
int Init_table_env (void)
{
char sql[1024];
sqlite3 *db;
char *err_msg = 0;
int recode;
int i;
for (i=1; i<=2; i++)
{
sprintf (sql,"insert into env values('%d',25.4,10.5,55.8,10.8,\
258,129,'12345678901',1,\
2,3,4,'binea',123456);", i);
recode = sqlite3_open (SQLITE_OPEN, &db);
if (recode != SQLITE_OK)
{
printf ("Can't Open Database:%s!\n",sqlite3_errmsg(db));
sqlite3_close (db);
return 1;
}
else
{
#if DEBUG_SQLITE
printf ("open OK!\n");
#endif
recode = sqlite3_exec (db,sql,0,0,&err_msg);
if (recode != SQLITE_OK)
{
printf ("Error:%s",err_msg);
sqlite3_close (db);
return 1;
}
else
{
#if DEBUG_SQLITE
printf ("insert env OK!\n");
#endif
sqlite3_close (db);
}
}
}
return 0;
}
/*
**更新env参数的SQLite指令选择函数
**这个函数中只能插入int类型的值
*/
int setEnv (float val, int dev_no_t, int no)
{
char sql[1024];
switch (no)
{
case 1:
sprintf (sql, "update env set temperatureMax = '%f' where dev_no = '%d';", val, dev_no_t);
break;
case 2:
sprintf (sql, "update env set temperatureMin = '%f' where dev_no = '%d';", val, dev_no_t);
break;
case 3:
sprintf (sql, "update env set humidityMax = '%f' where dev_no = '%d';", val, dev_no_t);
break;
case 4:
sprintf (sql, "update env set humidityMin = '%f' where dev_no = '%d';", val, dev_no_t);
break;
case 5:
sprintf (sql, "update env set illuminationMax = '%f' where dev_no = '%d';", val, dev_no_t);
break;
case 6:
sprintf (sql, "update env set illuminationMin = '%f' where dev_no = '%d';", val, dev_no_t);
break;
#if 0
case 7:
sprintf (sql, "update env set telephoneNum = '%f';", val);
break;
case 8:
sprintf (sql, "update env set mode = '%d';", (int)val);
break;
case 9:
sprintf (sql, "update env set ifnocice = '%d';", (int)val);
break;
case 10:
sprintf (sql, "update env set interval = '%d';", (int)val);
break;
case 11:
sprintf (sql, "update env set updatePeriod = '%d';", (int)val);
break;
// case 12:
// sprintf (sql, "update env set username = '%f';", val);
break;
case 13:
sprintf (sql, "update env set password = '%d';", (int)val);
break;
#endif
}
sqlite3 *db;
char *err_msg = 0;
int recode;
recode = sqlite3_open (SQLITE_OPEN, &db);
if (recode != SQLITE_OK)
{
printf ("Can't Open Database:%s!\n", sqlite3_errmsg(db));
sqlite3_close (db);
return 1;
}
else
{
#if DEBUG_SQLITE
printf ("open OK!\n");
#endif
recode=sqlite3_exec (db, sql, 0, 0, &err_msg);
if (recode != SQLITE_OK)
{
printf ("Error:%s", err_msg);
sqlite3_close (db);
return 1;
}
else
{
#if DEBUG_SQLITE
printf ("Insert OK!\n");
#endif
sqlite3_close (db);
return 0;
}
}
}
/*
**更新env参数的SQLite指令选择函数
**这个函数中只能插入字符串类型的值
*/
int setEnv_s (char *val, int no)
{
char sql[1024];
switch(no)
{
case 7:
sprintf (sql, "update env set telephoneNum = '%s';", val);
case 12:
sprintf (sql, "update env set username = '%s';", val);
}
sqlite3 *db;
char *err_msg = 0;
int recode;
recode = sqlite3_open (SQLITE_OPEN, &db);
if (recode != SQLITE_OK)
{
printf ("Can't Open Database:%s!\n",sqlite3_errmsg(db));
sqlite3_close (db);
return 1;
}
else
{
#if DEBUG_SQLITE
printf ("open OK!\n");
#endif
recode = sqlite3_exec (db, sql, 0, 0, &err_msg);
if (recode != SQLITE_OK)
{
printf ("Error:%s", err_msg);
sqlite3_close (db);
return 1;
}
else
{
#if DEBUG_SQLITE
printf ("Insert OK!\n");
#endif
sqlite3_close (db);
return 0;
}
}
}
/*
**获得env参数的SQLite指令选择函数
*/
int getEnv_select (int storageNum_t, int no)
{
char sql[1024];
sqlite3 *db;
char *err_msg = 0;
int recode;
switch (no)
{
case 1:
sprintf (sql, "select temperatureMax from env where dev_no = '%d';", storageNum_t);
break;
case 2:
sprintf (sql, "select temperatureMin from env where dev_no = '%d';", storageNum_t);
break;
case 3:
sprintf (sql, "select humidityMax from env where dev_no = '%d';", storageNum_t);
break;
case 4:
sprintf (sql, "select humidityMin from env where dev_no = '%d';", storageNum_t);
break;
case 5:
sprintf (sql, "select illuminationMax from env where dev_no = '%d';", storageNum_t);
break;
case 6:
sprintf (sql, "select illuminationMin from env where dev_no = '%d';", storageNum_t);
break;
#if 0
case 7:
sprintf (sql, "select telephoneNum from env;");
break;
case 8:
sprintf (sql, "select mode from env;");
break;
case 9:
sprintf (sql, "select ifnocice from env;");
break;
case 10:
sprintf (sql, "select interval from env;");
break;
case 11:
sprintf (sql, "select updatePeriod from env;");
break;
case 12:
sprintf (sql, "select username from env;");
break;
case 13:
sprintf (sql, "select password from env;");
break;
#endif
}
recode = sqlite3_open (SQLITE_OPEN, &db);
if (recode != SQLITE_OK)
{
printf ("Can't Open Database:%s!\n", sqlite3_errmsg(db));
sqlite3_close (db);
return 1;
}
else
{
#if DEBUG_SQLITE
printf ("open OK!\n");
#endif
if ((7 == no) || (12 == no))
{
recode = sqlite3_exec (db, sql, callback_getenv_s, 0, &err_msg);
}
else
{
recode = sqlite3_exec (db, sql, callback_getenv, 0, &err_msg);
}
if (recode != SQLITE_OK)
{
printf ("Error:%s", err_msg);
sqlite3_close (db);
return 1;
}
else
{
#if DEBUG_SQLITE
printf ("get env OK!\n");
#endif
sqlite3_close (db);
return 0;
}
}
}
/*
**更新env参数
*/
int updateEnv (struct env_info_clien_addr env_info_clien_addr_t, int storageNum_t)
{
setEnv (env_info_clien_addr_t.storage_no[storageNum_t].temperatureMAX, storageNum_t, TEMPERATURE_MAX);
setEnv (env_info_clien_addr_t.storage_no[storageNum_t].temperatureMIN, storageNum_t, TEMPERATURE_MIN);
setEnv (env_info_clien_addr_t.storage_no[storageNum_t].humidityMAX, storageNum_t, HUMIDITY_MAX);
setEnv (env_info_clien_addr_t.storage_no[storageNum_t].humidityMIN, storageNum_t, HUMIDITY_MIN);
setEnv (env_info_clien_addr_t.storage_no[storageNum_t].illuminationMAX, storageNum_t, ILLUMINATION_MAX);
setEnv (env_info_clien_addr_t.storage_no[storageNum_t].illuminationMIN, storageNum_t, ILLUMINATION_MIN);
// setEnv_s (storage_no_t->telephoneNum, TELEPHONE_NUM);
// setEnv (storage_no_t->mode, MODE);
// setEnv (storage_no_t->ifnocice, IFNOCICE);
// setEnv (storage_no_t->interval, INTERVAL);
// setEnv (storage_no_t->updatePeriod, UPDATE_PERIOD);
// setEnv_s (storage_no_t->username, USER_NAME);
// setEnv (storage_no_t->password, PASSWORD);
return 0;
}
/*
**获得env参数
*/
int getEnv (struct env_info_clien_addr *env_info_clien_addr_t, int storageNum_t)
{
getEnv_select (storageNum_t, TEMPERATURE_MAX);
env_info_clien_addr_t->storage_no[storageNum_t].temperatureMAX = callback_val[0];
all_info_RT.storage_no[storageNum_t].temperatureMAX = callback_val[0];
getEnv_select (storageNum_t, TEMPERATURE_MIN);
env_info_clien_addr_t->storage_no[storageNum_t].temperatureMIN = callback_val[0];
all_info_RT.storage_no[storageNum_t].temperatureMIN = callback_val[0];
getEnv_select (storageNum_t, HUMIDITY_MAX);
env_info_clien_addr_t->storage_no[storageNum_t].humidityMAX = callback_val[0];
all_info_RT.storage_no[storageNum_t].humidityMAX = callback_val[0];
getEnv_select (storageNum_t, HUMIDITY_MIN);
env_info_clien_addr_t->storage_no[storageNum_t].humidityMIN = callback_val[0];
all_info_RT.storage_no[storageNum_t].humidityMIN = callback_val[0];
getEnv_select (storageNum_t, ILLUMINATION_MAX);
env_info_clien_addr_t->storage_no[storageNum_t].illuminationMAX = callback_val[0];
all_info_RT.storage_no[storageNum_t].illuminationMAX = callback_val[0];
getEnv_select (storageNum_t, ILLUMINATION_MIN);
env_info_clien_addr_t->storage_no[storageNum_t].illuminationMIN = callback_val[0];
all_info_RT.storage_no[storageNum_t].illuminationMIN = callback_val[0];
#if 0
getEnv_select (TELEPHONE_NUM);
strcpy(storage_info_t->telephoneNum, callback_str);
getEnv_select (MODE);
storage_info_t->mode = (int)callback_val[0];
getEnv_select (IFNOCICE);
storage_info_t->ifnocice = (int)callback_val[0];
getEnv_select (INTERVAL);
storage_info_t->interval = (int)callback_val[0];
getEnv_select (UPDATE_PERIOD);
storage_info_t->updatePeriod = (int)callback_val[0];
getEnv_select (USER_NAME);
strcpy(storage_info_t->username, callback_str);
getEnv_select (PASSWORD);
storage_info_t->password = (int)callback_val[0];
#endif
return 0;
}
int insertCollect_env (struct env_info_clien_addr env_info_clien_addr_t, int storage_num)
{
#if DEBUG_SQLITE
printf ("insertCollect_env ok\n");
#endif
#if 1
sqlite3 *db;
char *err_msg = 0;
int recode;
char sql[1024];
sprintf (sql, "insert into collect_env values(%d,%f,%f,%f,'%s');",
storage_num,
env_info_clien_addr_t.storage_no[storage_num].temperature,
env_info_clien_addr_t.storage_no[storage_num].humidity,
env_info_clien_addr_t.storage_no[storage_num].illumination,
env_info_clien_addr_t.storage_no[storage_num].samplingTime);
recode = sqlite3_open(SQLITE_OPEN, &db);
if (recode != SQLITE_OK)
{
printf ("Can't Open Database:%s!\n", sqlite3_errmsg(db));
sqlite3_close (db);
return 1;
}
else
{
#if DEBUG_SQLITE
printf ("open OK!\n");
#endif
recode = sqlite3_exec (db, sql, 0, 0, &err_msg);
if (recode != SQLITE_OK)
{
printf ("Error:%s", err_msg);
sqlite3_close (db);
return 1;
}
else
{
#if DEBUG_SQLITE
printf ("Insert collect_env OK!\n");
#endif
sqlite3_close (db);
return 0;
}
}
#endif
}
int getCollect_env (char itime_t[], int storage_num_t)
{
#if DEBUG_SQLITE
printf ("getCollect_env ok\n");
#endif
sqlite3 *db;
char *err_msg = 0;
int recode;
recode = sqlite3_open (SQLITE_OPEN, &db);
char sql[1024];
sprintf (sql, "select * from collect_env where envtime = '%s' and dev_no = %d;", itime_t, storage_num_t);
if (recode != SQLITE_OK)
{
printf ("Can't Open Database:%s!\n", sqlite3_errmsg(db));
sqlite3_close (db);
return 1;
}
else
{
#if DEBUG_SQLITE
printf ("open OK!\n");
#endif
recode = sqlite3_exec (db, sql, callback_getenv, 0, &err_msg);
if (recode != SQLITE_OK)
{
printf ("Error:%s", err_msg);
sqlite3_close (db);
return 1;
}
else
{
#if DEBUG_SQLITE
printf ("select collect_env OK!\n");
#endif
sqlite3_close (db);
}
env_info_clien_addr_s.storage_no[storage_num_t].temperature = callback_val[1];
env_info_clien_addr_s.storage_no[storage_num_t].humidity = callback_val[2];
env_info_clien_addr_s.storage_no[storage_num_t].illumination = callback_val[3];
strcpy(env_info_clien_addr_s.storage_no[storage_num_t].samplingTime, callback_str);
all_info_RT.storage_no[storage_num_t].temperature = callback_val[1];
all_info_RT.storage_no[storage_num_t].humidity = callback_val[2];
all_info_RT.storage_no[storage_num_t].illumination = callback_val[3];
strcpy(all_info_RT.storage_no[storage_num_t].samplingTime, callback_str);
#if 1
printf ("dev_no:%d temperature = %0.1f\n", storage_num_t, env_info_clien_addr_s.storage_no[storage_num_t].temperature);
printf ("dev_no:%d humidity = %0.1f\n", storage_num_t, env_info_clien_addr_s.storage_no[storage_num_t].humidity);
printf ("dev_no:%d illumination = %0.1f\n", storage_num_t, env_info_clien_addr_s.storage_no[storage_num_t].illumination);
printf ("dev_no:%d time = %s\n", storage_num_t, env_info_clien_addr_s.storage_no[storage_num_t].samplingTime);
printf ("-----------------------------\n");
#endif
}
return 0;
}
int getCollect_Current_env (int storage_num_t)
{
#if DEBUG_SQLITE
printf ("getCollect_env ok\n");
#endif
sqlite3 *db;
char *err_msg = 0;
int recode;
recode = sqlite3_open (SQLITE_OPEN, &db);
char sql[1024];
sprintf (sql, "select * from collect_env where dev_no = %d order by envtime desc limit 1", storage_num_t);
if (recode != SQLITE_OK)
{
printf ("Can't Open Database:%s!\n", sqlite3_errmsg(db));
sqlite3_close (db);
return 1;
}
else
{
#if DEBUG_SQLITE
printf ("open OK!\n");
#endif
recode = sqlite3_exec (db, sql, callback_getenv, 0, &err_msg);
if (recode != SQLITE_OK)
{
printf ("Error:%s", err_msg);
sqlite3_close (db);
return 1;
}
else
{
#if DEBUG_SQLITE
printf ("select collect_env OK!\n");
#endif
sqlite3_close (db);
}
env_info_clien_addr_s.storage_no[storage_num_t].temperature = callback_val[1];
env_info_clien_addr_s.storage_no[storage_num_t].humidity = callback_val[2];
env_info_clien_addr_s.storage_no[storage_num_t].illumination = callback_val[3];
strcpy(env_info_clien_addr_s.storage_no[storage_num_t].samplingTime, callback_str);
all_info_RT.storage_no[storage_num_t].temperature = callback_val[1];
all_info_RT.storage_no[storage_num_t].humidity = callback_val[2];
all_info_RT.storage_no[storage_num_t].illumination = callback_val[3];
strcpy(all_info_RT.storage_no[storage_num_t].samplingTime, callback_str);
#if 1
printf ("dev_no:%d temperature = %0.1f\n", storage_num_t, env_info_clien_addr_s.storage_no[storage_num_t].temperature);
printf ("dev_no:%d humidity = %0.1f\n", storage_num_t, env_info_clien_addr_s.storage_no[storage_num_t].humidity);
printf ("dev_no:%d illumination = %0.1f\n", storage_num_t, env_info_clien_addr_s.storage_no[storage_num_t].illumination);
printf ("dev_no:%d time = %s\n", storage_num_t, env_info_clien_addr_s.storage_no[storage_num_t].samplingTime);
printf ("-----------------------------\n");
#endif
}
return 0;
}
int insertGoods (struct env_info_clien_addr env_info_clien_addr_t, int storageNum_t, int goodsKinds_t)
{
#if DEBUG_SQLITE
printf ("insertGoods ok\n");
printf ("storageNum = %d, goodsKinds = %d\n",storageNum_t,goodsKinds_t);
#endif
sqlite3 *db;
char *err_msg = 0;
int recode;
char sql[1024];
sprintf (sql, "insert into goods values(%d,%d,%d,'%s');",
storageNum_t,
env_info_clien_addr_t.storage_no[storageNum_t].goods_info[goodsKinds_t].goods_type,
env_info_clien_addr_t.storage_no[storageNum_t].goods_info[goodsKinds_t].goods_count,
env_info_clien_addr_t.storage_no[storageNum_t].samplingTime);
recode = sqlite3_open (SQLITE_OPEN, &db);
if (recode != SQLITE_OK)
{
printf ("Can't Open Database:%s!\n", sqlite3_errmsg(db));
sqlite3_close (db);
return 1;
}
else
{
#if DEBUG_SQLITE
printf ("open OK!\n");
#endif
recode = sqlite3_exec (db, sql, 0, 0, &err_msg);
if (recode != SQLITE_OK)
{
printf ("Error:%s", err_msg);
sqlite3_close (db);
return 1;
}
else
{
#if DEBUG_SQLITE
printf ("Insert goods OK!\n");
#endif
sqlite3_close (db);
}
}
return 0;
}
int deleteGoods (int storageNum_t, int goodsKinds_t)
{
#if DEBUG_SQLITE
printf ("deleteGoods OK\n");
#endif
sqlite3 *db;
char *err_msg = 0;
int recode;
recode = sqlite3_open (SQLITE_OPEN, &db);
char sql[1024];
sprintf (sql, "delete from goods where dev_no = '%d' and goodsId = '%d';", storageNum_t, goodsKinds_t);
if (recode != SQLITE_OK)
{
printf ("Can't Open Database:%s!\n", sqlite3_errmsg(db));
sqlite3_close (db);
return 1;
}
else
{
#if DEBUG_SQLITE
printf ("open OK!\n");
#endif
recode = sqlite3_exec (db, sql, 0, 0, &err_msg);
if (recode != SQLITE_OK)
{
printf ("Error:%s", err_msg);
sqlite3_close (db);
return 1;
}
else
{
#if DEBUG_SQLITE
printf ("delete goods OK!\n");
#endif
sqlite3_close (db);
}
}
return 0;
}
int getGoods (int storageNum_t, int goodsKinds_t)
{
#if DEBUG_SQLITE
printf ("getGoods ok\n");
#endif
sqlite3 *db;
char *err_msg = 0;
int recode;
recode = sqlite3_open (SQLITE_OPEN, &db);
char sql[1024];
sprintf (sql, "select * from goods where dev_no = '%d' and goodsId = '%d';", storageNum_t, goodsKinds_t);
if (recode != SQLITE_OK)
{
printf ("Can't Open Database:%s!\n", sqlite3_errmsg(db));
sqlite3_close (db);
return 1;
}
else
{
#if DEBUG_SQLITE
printf ("open OK!\n");
#endif
recode = sqlite3_exec (db, sql, callback_getenv, 0, &err_msg);
if (recode != SQLITE_OK)
{
printf ("Error:%s", err_msg);
sqlite3_close (db);
return 1;
}
else
{
#if DEBUG_SQLITE
printf ("select goods OK!\n");
#endif
sqlite3_close (db);
}
env_info_clien_addr_s.storage_no[storageNum_t].goods_info[goodsKinds_t].goods_type = (int)callback_val[1];
env_info_clien_addr_s.storage_no[storageNum_t].goods_info[goodsKinds_t].goods_count = (int)callback_val[2];
all_info_RT.storage_no[storageNum_t].goods_info[goodsKinds_t].goods_type = (int)callback_val[1];
all_info_RT.storage_no[storageNum_t].goods_info[goodsKinds_t].goods_count = (int)callback_val[2];
#if DEBUG_SQLITE
printf ("goods_s.goods_type = %d\n", env_info_clien_addr_s.storage_no[storageNum_t].goods_info[goodsKinds_t].goods_type);
printf ("goods_s.goods_count = %d\n", env_info_clien_addr_s.storage_no[storageNum_t].goods_info[goodsKinds_t].goods_count);
printf ("---------------------------------\n");
#endif
}
return 0;
}
int getAllGoods (void)
{
#if DEBUG_SQLITE
printf ("getAllGoods ok\n");
#endif
return 0;
}
int viewGoods (int storageNum_t, int goodsKinds_t)
{
#if DEBUG_SQLITE
printf ("viewGoods ok\n");
#endif
sqlite3 *db;
char *err_msg = 0;
int recode;
recode = sqlite3_open (SQLITE_OPEN, &db);
char sql[1024];
sprintf (sql, "select * from goods where dev_no = '%d' and goodsId = '%d';", storageNum_t, goodsKinds_t);
if (recode != SQLITE_OK)
{
printf ("Can't Open Database:%s!\n", sqlite3_errmsg(db));
sqlite3_close (db);
return 1;
}
else
{
#if DEBUG_SQLITE
printf ("open OK!\n");
#endif
recode = sqlite3_exec (db, sql, callback_getenv, 0, &err_msg);
if (recode != SQLITE_OK)
{
printf ("Error:%s", err_msg);
sqlite3_close (db);
return 1;
}
else
{
#if DEBUG_SQLITE
printf ("view goods OK!\n");
#endif
sqlite3_close (db);
}
}
return (int)callback_val[2];
}
int addGoods (struct env_info_clien_addr env_info_clien_addr_t, int storageNum_t, int goodsKinds_t)
{
#if DEBUG_SQLITE
printf ("addGoods ok\n");
printf ("storageNum = %d, goodsKinds = %d\n",storageNum_t,goodsKinds_t);
#endif
sqlite3 *db;
char *err_msg = 0;
int recode, addNum = 0;
char sql[1024];
addNum = env_info_clien_addr_t.storage_no[storageNum_t].goods_info[goodsKinds_t].goods_count;
sprintf (sql, "select * from goods where dev_no = '%d' and goodsId = '%d';", storageNum_t, goodsKinds_t);
recode = sqlite3_open (SQLITE_OPEN, &db);
if (recode != SQLITE_OK)
{
printf ("Can't Open Database:%s!\n", sqlite3_errmsg(db));
sqlite3_close (db);
return 1;
}
else
{
#if DEBUG_SQLITE
printf ("open OK!\n");
#endif
recode = sqlite3_exec (db, sql, callback_getenv, 0, &err_msg);
if (recode != SQLITE_OK)
{
printf ("Error:%s", err_msg);
sqlite3_close (db);
return 1;
}
else
{
#if DEBUG_SQLITE
printf ("add select goods OK!\n");
#endif
sqlite3_close (db);
}
env_info_clien_addr_s.storage_no[storageNum_t].goods_info[goodsKinds_t].goods_type = (int)callback_val[1];
env_info_clien_addr_s.storage_no[storageNum_t].goods_info[goodsKinds_t].goods_count = (int)callback_val[2];
printf ("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!%d, %d\n", env_info_clien_addr_s.storage_no[storageNum_t].goods_info[goodsKinds_t].goods_type, env_info_clien_addr_s.storage_no[storageNum_t].goods_info[goodsKinds_t].goods_count);
}
sprintf (sql, "update goods set goodsCount = '%d', goodsTime = '%s' where dev_no = '%d' and goodsId = '%d';",
env_info_clien_addr_s.storage_no[storageNum_t].goods_info[goodsKinds_t].goods_count + addNum,
env_info_clien_addr_s.storage_no[storageNum_t].samplingTime,
storageNum_t,
env_info_clien_addr_s.storage_no[storageNum_t].goods_info[goodsKinds_t].goods_type);
recode = sqlite3_open (SQLITE_OPEN, &db);
if (recode != SQLITE_OK)
{
printf ("Can't Open Database:%s!\n", sqlite3_errmsg(db));
sqlite3_close (db);
return 1;
}
else
{
#if DEBUG_SQLITE
printf ("open OK!\n");
#endif
recode = sqlite3_exec (db, sql, 0, 0, &err_msg);
if (recode != SQLITE_OK)
{
printf ("Error:%s", err_msg);
sqlite3_close (db);
return 1;
}
else
{
#if DEBUG_SQLITE
printf ("add goods OK!\n");
#endif
printf ("~~~~~~~~~~~~~~%d, %d\n", env_info_clien_addr_s.storage_no[storageNum_t].goods_info[goodsKinds_t].goods_type, env_info_clien_addr_s.storage_no[storageNum_t].goods_info[goodsKinds_t].goods_count + addNum);
sqlite3_close (db);
}
}
return 0;
}
int reduceGoods (struct env_info_clien_addr env_info_clien_addr_t, int storageNum_t, int goodsKinds_t)
{
#if DEBUG_SQLITE
printf ("reduceGoods ok\n");
printf ("storageNum = %d, goodsKinds = %d\n",storageNum_t,goodsKinds_t);
#endif
sqlite3 *db;
char *err_msg = 0;
int recode, addNum = 0;
char sql[1024];
addNum = env_info_clien_addr_t.storage_no[storageNum_t].goods_info[goodsKinds_t].goods_count,
sprintf (sql, "select * from goods where dev_no = '%d' and goodsId = '%d';", storageNum_t, goodsKinds_t);
recode = sqlite3_open (SQLITE_OPEN, &db);
if (recode != SQLITE_OK)
{
printf ("Can't Open Database:%s!\n", sqlite3_errmsg(db));
sqlite3_close (db);
return 1;
}
else
{
#if DEBUG_SQLITE
printf ("open OK!\n");
#endif
recode = sqlite3_exec (db, sql, callback_getenv, 0, &err_msg);
if (recode != SQLITE_OK)
{
printf ("Error:%s", err_msg);
sqlite3_close (db);
return 1;
}
else
{
#if DEBUG_SQLITE
printf ("Reduce select goods OK!\n");
#endif
sqlite3_close (db);
}
env_info_clien_addr_s.storage_no[storageNum_t].goods_info[goodsKinds_t].goods_type = (int)callback_val[1];
env_info_clien_addr_s.storage_no[storageNum_t].goods_info[goodsKinds_t].goods_count = (int)callback_val[2];
}
sprintf (sql, "update goods set goodsCount = '%d', goodsTime = '%s' where dev_no = '%d' and goodsId = '%d';",
(env_info_clien_addr_s.storage_no[storageNum_t].goods_info[goodsKinds_t].goods_count - addNum),
env_info_clien_addr_s.storage_no[storageNum_t].samplingTime,
storageNum_t,
env_info_clien_addr_s.storage_no[storageNum_t].goods_info[goodsKinds_t].goods_type);
recode = sqlite3_open (SQLITE_OPEN, &db);
if (recode != SQLITE_OK)
{
printf ("Can't Open Database:%s!\n", sqlite3_errmsg(db));
sqlite3_close (db);
return 1;
}
else
{