main.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. #include <stdio.h>
  2. #include <json-c/json.h>
  3. #include "../ytCore/yt_unit_shm.h"
  4. #include <string.h>
  5. #include <sqlite3.h>
  6. #include <time.h>
  7. #include <sys/time.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #define CMD_GET_REALTIME_STATUS 1
  11. #define CMD_GET_HISTORY_VALUE 2
  12. #define CMD_GET_COMM_PARAMS 3
  13. #define CMD_SET_COMM_PARAMS 4
  14. #define CMD_GET_SPNO_PARAMS 5
  15. #define CMD_SET_SPNO_PARAMS 6
  16. #define CMD_QRY_IOT_STATUS 16
  17. #define CMD_CTL 64
  18. YT_UNIT_SHM *ytShm;
  19. bool load_shm()
  20. {
  21. int shmid;
  22. key_t key;
  23. if((key = ftok(SHM_PATH,(int)SHM_PORT))==-1)
  24. return false;
  25. if((shmid = shmget(key,sizeof(YT_UNIT_SHM),IPC_CREAT|0666))==-1)
  26. return false;
  27. ytShm = (YT_UNIT_SHM *)shmat(shmid,NULL,0);
  28. return true;
  29. }
  30. void get_realtime_status(json_object *q_body, json_object *body)
  31. {
  32. int commid,ncomm=0,comm_alarm=0,pno_alarm=0,alarmL1=0,alarmL2=0,total_count=0;
  33. char tmp[256];
  34. json_object *comm_list = json_object_new_array();
  35. for(commid=0;commid<4;commid++){
  36. if(ytShm->commList.comm[commid].ENABLED==0x01){
  37. int count=0,add;
  38. time_t t;
  39. struct tm *loc_time;
  40. json_object *o_comm = json_object_new_object();
  41. json_object *pno_list = json_object_new_array();
  42. ncomm++;
  43. for(add=0;add<256;add++){
  44. if((ytShm->sPointList.sPoint[commid][add].ENABLED==0x01)
  45. &&(ytShm->sPointList.sPoint[commid][add].PNO_TYPE>0)
  46. &&(ytShm->sPointList.sPoint[commid][add].PNO_TYPE<3)
  47. ){
  48. count++;
  49. json_object *spno = json_object_new_object();
  50. sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][add].COMM_IDX);
  51. json_object_object_add(spno,"COMM",json_object_new_string(tmp));
  52. sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][add].BUS_ADD);
  53. json_object_object_add(spno,"ADDR",json_object_new_string(tmp));
  54. json_object_object_add(spno,"NAME",json_object_new_string(ytShm->sPointList.sPoint[commid][add].NAME));
  55. sprintf(tmp,"%ld",ytShm->sPointList.sPoint[commid][add].SUB_TYPE);
  56. json_object_object_add(spno,"SUB_TYPE",json_object_new_string(tmp));
  57. sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][add].PNO_TYPE);
  58. json_object_object_add(spno,"PNO_TYPE",json_object_new_string(tmp));
  59. if(ytShm->spStatusList.spStatus[commid][add].ALARM_STATUS>0){
  60. pno_alarm++;
  61. if(ytShm->spStatusList.spStatus[commid][add].ALARM_STATUS==2)
  62. alarmL2++;
  63. else if(ytShm->spStatusList.spStatus[commid][add].ALARM_STATUS==1)
  64. alarmL1++;
  65. }
  66. sprintf(tmp,"%d",ytShm->spStatusList.spStatus[commid][add].ALARM_STATUS);
  67. json_object_object_add(spno,"STATUS",json_object_new_string(tmp));
  68. if(ytShm->sPointList.sPoint[commid][add].PNO_TYPE==1){
  69. if(ytShm->spStatusList.spStatus[commid][add].REALTIME_VALUE==0)
  70. json_object_object_add(spno,"VALUE",json_object_new_string("0"));
  71. else
  72. json_object_object_add(spno,"VALUE",json_object_new_string("1"));
  73. }else{
  74. sprintf(tmp,"%.03f",ytShm->spStatusList.spStatus[commid][add].REALTIME_VALUE);
  75. json_object_object_add(spno,"VALUE",json_object_new_string(tmp));
  76. }
  77. t = ytShm->spStatusList.spStatus[commid][add].REALTIME_TIMESTAMP;
  78. loc_time = localtime(&t);
  79. sprintf(tmp,"%04d-%02d-%02d %02d:%02d:%02d",
  80. loc_time->tm_year+1900,
  81. loc_time->tm_mon+1,
  82. loc_time->tm_mday,
  83. loc_time->tm_hour,
  84. loc_time->tm_min,
  85. loc_time->tm_sec);
  86. json_object_object_add(spno,"UPDATETIME",json_object_new_string(tmp));
  87. sprintf(tmp,"%d",(ytShm->currentTime.TIMESTAMP-ytShm->spStatusList.spStatus[commid][add].REALTIME_TIMESTAMP));
  88. json_object_object_add(spno,"DLYTIME",json_object_new_string(tmp));
  89. json_object_array_add(pno_list,spno);
  90. }
  91. }
  92. total_count += count;
  93. sprintf(tmp,"%d",ytShm->commList.comm[commid].IDX);
  94. json_object_object_add(o_comm,"IDX",json_object_new_string(tmp));
  95. json_object_object_add(o_comm,"PNOLIST",pno_list);
  96. if(ytShm->commStatusList.commStatus[commid].STATUS!=0)
  97. comm_alarm++;
  98. sprintf(tmp,"%d",ytShm->commStatusList.commStatus[commid].STATUS);
  99. json_object_object_add(o_comm,"STATUS",json_object_new_string(tmp));
  100. json_object_array_add(comm_list,o_comm);
  101. }
  102. }
  103. json_object_object_add(body,"COMMLIST",comm_list);
  104. }
  105. void get_history_value(json_object *q_body, json_object *body)
  106. {
  107. json_object *CommId=NULL, *BusAdd=NULL;
  108. json_object_object_foreach(q_body, key, val){
  109. if(strcmp("COMM",key)==0)
  110. CommId = val;
  111. else if(strcmp("ADDR",key)==0)
  112. BusAdd = val;
  113. }
  114. if((CommId!=NULL)&&(BusAdd!=NULL)){
  115. sqlite3 *db;
  116. time_t t;
  117. struct tm *tm_loc;
  118. char **azResult, *errMsg, sql[1024],tmp[256];
  119. int nrow,ncol,i;
  120. int commid = atoi(json_object_get_string(CommId));
  121. int busadd = atoi(json_object_get_string(BusAdd));
  122. sprintf(tmp,"%d",commid);
  123. json_object_object_add(body,"COMM",json_object_new_string(tmp));
  124. sprintf(tmp,"%d",busadd);
  125. json_object_object_add(body,"ADDR",json_object_new_string(tmp));
  126. if((commid>=0)&&(commid<4)&&(busadd>=0)&&(busadd<256)){
  127. if(sqlite3_open(REC_DB,&db)==SQLITE_OK){
  128. sprintf(sql,"select REALTIME_VALUE, AVE_VALUE, REALTIME_TIMESTAMP, MAX_VALUE, MAX_TIMESTAMP, MIN_VALUE, MIN_TIMESTAMP from yt_rec_pno where COMMID=%s and BUSADD=%s",
  129. json_object_get_string(CommId), json_object_get_string(BusAdd));
  130. // printf("sql:%s<br>",sql);
  131. json_object *value_list = json_object_new_array();
  132. if(sqlite3_get_table(db,sql,&azResult,&nrow,&ncol,&errMsg)==SQLITE_OK){
  133. if((nrow>0)&&(ncol>=7)){
  134. for(i=1;i<(nrow+1);i++){
  135. json_object *value = json_object_new_object();
  136. sprintf(tmp,"%s",azResult[i*ncol+0]);
  137. json_object_object_add(value,"REALTIME_VALUE",json_object_new_string(tmp));
  138. sprintf(tmp,"%s",azResult[i*ncol+1]);
  139. json_object_object_add(value,"AVE_VALUE",json_object_new_string(tmp));
  140. t = atoll(azResult[i*ncol+2])&0xffffffff;
  141. tm_loc = localtime(&t);
  142. sprintf(tmp,"%04d-%02d-%02d %02d:%02d:%02d",
  143. tm_loc->tm_year,
  144. tm_loc->tm_mon+1,
  145. tm_loc->tm_mday,
  146. tm_loc->tm_hour,
  147. tm_loc->tm_min,
  148. tm_loc->tm_sec);
  149. json_object_object_add(value,"REALTIME_TIMESTAMP",json_object_new_string(tmp));
  150. sprintf(tmp,"%s",azResult[i*ncol+3]);
  151. json_object_object_add(value,"MAX_VALUE",json_object_new_string(tmp));
  152. t = atoll(azResult[i*ncol+4])&0xffffffff;
  153. tm_loc = localtime(&t);
  154. sprintf(tmp,"%04d-%02d-%02d %02d:%02d:%02d",
  155. tm_loc->tm_year,
  156. tm_loc->tm_mon+1,
  157. tm_loc->tm_mday,
  158. tm_loc->tm_hour,
  159. tm_loc->tm_min,
  160. tm_loc->tm_sec);
  161. json_object_object_add(value,"MAX_TIMESTAMP",json_object_new_string(tmp));
  162. sprintf(tmp,"%s",azResult[i*ncol+5]);
  163. json_object_object_add(value,"MIN_VALUE",json_object_new_string(tmp));
  164. t = atoll(azResult[i*ncol+6])&0xffffffff;
  165. tm_loc = localtime(&t);
  166. sprintf(tmp,"%04d-%02d-%02d %02d:%02d:%02d",
  167. tm_loc->tm_year,
  168. tm_loc->tm_mon+1,
  169. tm_loc->tm_mday,
  170. tm_loc->tm_hour,
  171. tm_loc->tm_min,
  172. tm_loc->tm_sec);
  173. json_object_object_add(value,"MIN_TIMESTAMP",json_object_new_string(tmp));
  174. json_object_array_add(value_list,value);
  175. }
  176. json_object_object_add(body,"VALUELIST",value_list);
  177. }else{
  178. json_object_object_add(body,"ErrMsg",json_object_new_string("table struct error"));
  179. }
  180. sqlite3_free_table(azResult);
  181. }else{
  182. json_object_object_add(body,"ErrMsg",json_object_new_string("db gettable failed"));
  183. sqlite3_free(errMsg);
  184. }
  185. sqlite3_close(db);
  186. }else{
  187. json_object_object_add(body,"ErrMsg",json_object_new_string("db open failed"));
  188. }
  189. json_object *pno_param = json_object_new_object();
  190. sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][busadd].ENABLED);
  191. json_object_object_add(pno_param,"ENABLED",json_object_new_string(tmp));
  192. json_object_object_add(pno_param,"NAME",json_object_new_string(ytShm->sPointList.sPoint[commid][busadd].NAME));
  193. sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][busadd].PNO_TYPE);
  194. json_object_object_add(pno_param,"PNO_TYPE",json_object_new_string(tmp));
  195. sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][busadd].PNO_ALARM);
  196. json_object_object_add(pno_param,"PNO_ALARM",json_object_new_string(tmp));
  197. sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][busadd].PNO_VALUE);
  198. json_object_object_add(pno_param,"PNO_VALUE",json_object_new_string(tmp));
  199. sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][busadd].PNO_DLY);
  200. json_object_object_add(pno_param,"PNO_DLY",json_object_new_string(tmp));
  201. sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][busadd].PNO_AD);
  202. json_object_object_add(pno_param,"PNO_AD",json_object_new_string(tmp));
  203. sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][busadd].OFFSET);
  204. json_object_object_add(pno_param,"OFFSET",json_object_new_string(tmp));
  205. sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][busadd].OFFSET_FLAG);
  206. json_object_object_add(pno_param,"OFFSET_FLAG",json_object_new_string(tmp));
  207. sprintf(tmp,"%.03f",ytShm->sPointList.sPoint[commid][busadd].PNO_MAX);
  208. json_object_object_add(pno_param,"PNO_MAX",json_object_new_string(tmp));
  209. sprintf(tmp,"%.03f",ytShm->sPointList.sPoint[commid][busadd].PNO_MIN);
  210. json_object_object_add(pno_param,"PNO_MIN",json_object_new_string(tmp));
  211. sprintf(tmp,"%.03f",ytShm->sPointList.sPoint[commid][busadd].ALARM_H);
  212. json_object_object_add(pno_param,"ALARM_H",json_object_new_string(tmp));
  213. sprintf(tmp,"%.03f",ytShm->sPointList.sPoint[commid][busadd].ALARM_L);
  214. json_object_object_add(pno_param,"ALARM_L",json_object_new_string(tmp));
  215. json_object_object_add(body,"PARAMS",pno_param);
  216. }else{
  217. json_object_object_add(body,"ErrMsg",json_object_new_string("query out of size"));
  218. }
  219. }else{
  220. json_object_object_add(body,"ErrMsg",json_object_new_string("query params error"));
  221. }
  222. }
  223. void get_comm_params(json_object *q_body, json_object *body)
  224. {
  225. json_object *CommId=NULL;
  226. json_object_object_foreach(q_body, key, val){
  227. if(strcmp("COMM",key)==0)
  228. CommId = val;
  229. }
  230. if((CommId!=NULL)){
  231. char tmp[256];
  232. int commid = atoi(json_object_get_string(CommId));
  233. sprintf(tmp,"%d",commid);
  234. json_object_object_add(body,"COMM",json_object_new_string(tmp));
  235. if((commid>=0)&&(commid<4)){
  236. json_object *comm_param = json_object_new_object();
  237. sprintf(tmp,"%d",ytShm->commList.comm[commid].IDX);
  238. json_object_object_add(comm_param,"IDX",json_object_new_string(tmp));
  239. sprintf(tmp,"%d",ytShm->commList.comm[commid].ENABLED);
  240. json_object_object_add(comm_param,"ENABLED",json_object_new_string(tmp));
  241. sprintf(tmp,"%d",ytShm->commList.comm[commid].CommType);
  242. json_object_object_add(comm_param,"COMMTYPE",json_object_new_string(tmp));
  243. json_object_object_add(comm_param,"PATH",json_object_new_string(ytShm->commList.comm[commid].PATH));
  244. json_object_object_add(body,"PARAMS",comm_param);
  245. }else{
  246. json_object_object_add(body,"ErrMsg",json_object_new_string("query out of size"));
  247. }
  248. }else{
  249. json_object_object_add(body,"ErrMsg",json_object_new_string("query params error"));
  250. }
  251. }
  252. void set_comm_params(json_object *q_body, json_object *body)
  253. {
  254. json_object *CommId=NULL, *CommParams=NULL;
  255. json_object_object_foreach(q_body, key, val){
  256. if(strcmp("COMM",key)==0)
  257. CommId = val;
  258. else if(strcmp("PARAMS",key)==0)
  259. CommParams = val;
  260. }
  261. if((CommId!=NULL)&&(CommParams!=NULL)){
  262. char tmp[256];
  263. int commid = atoi(json_object_get_string(CommId));
  264. sprintf(tmp,"%d",commid);
  265. json_object_object_add(body,"COMM",json_object_new_string(tmp));
  266. if((commid>=0)&&(commid<4)){
  267. json_object *Idx=NULL, *Enabled=NULL, *CommType=NULL, *Path=NULL;
  268. json_object_object_foreach(CommParams, key, val){
  269. if(strcmp("IDX",key)==0)
  270. Idx = val;
  271. else if(strcmp("ENABLED",key)==0)
  272. Enabled = val;
  273. else if(strcmp("COMMTYPE",key)==0)
  274. CommType = val;
  275. else if(strcmp("PATH",key)==0)
  276. Path = val;
  277. }
  278. if((Idx!=NULL)
  279. &&(json_object_get_int(Idx)==commid)
  280. &&(Enabled!=NULL)
  281. &&(CommType!=NULL)
  282. &&(Path!=NULL))
  283. {
  284. int enabled = json_object_get_int(Enabled);
  285. int commtype = json_object_get_int(CommType);
  286. char path[512];
  287. sprintf(path,"%s",json_object_get_string(Path));
  288. if((enabled>=0)&&(enabled<2)&&(commtype>=0)&&(strlen(path)<255)){
  289. ytShm->commList.comm[commid].IDX = commid&0xff;
  290. ytShm->commList.comm[commid].ENABLED = enabled&0xff;
  291. ytShm->commList.comm[commid].CommType = commtype&0xff;
  292. sprintf(ytShm->commList.comm[commid].PATH,"%s",path);
  293. ytShm->commList.comm[commid].UNSAVED =0x01;
  294. json_object_object_add(body,"SETUP",json_object_new_string("TRUE"));
  295. }else{
  296. json_object_object_add(body,"SETUP",json_object_new_string("FALSE"));
  297. json_object_object_add(body,"ErrMsg",json_object_new_string("set params out of size"));
  298. }
  299. }else{
  300. json_object_object_add(body,"SETUP",json_object_new_string("FALSE"));
  301. json_object_object_add(body,"ErrMsg",json_object_new_string("set params error"));
  302. }
  303. }else{
  304. json_object_object_add(body,"SETUP",json_object_new_string("FALSE"));
  305. json_object_object_add(body,"ErrMsg",json_object_new_string("query out of size"));
  306. }
  307. }else{
  308. json_object_object_add(body,"SETUP",json_object_new_string("FALSE"));
  309. json_object_object_add(body,"ErrMsg",json_object_new_string("query params error"));
  310. }
  311. }
  312. void get_spno_params(json_object *q_body, json_object *body)
  313. {
  314. json_object *CommId=NULL, *Addr=NULL;
  315. json_object_object_foreach(q_body, key, val){
  316. if(strcmp("COMM",key)==0)
  317. CommId = val;
  318. else if(strcmp("ADDR",key)==0)
  319. Addr = val;
  320. }
  321. if((CommId!=NULL)&&(Addr!=NULL)){
  322. char tmp[256];
  323. int commid = json_object_get_int(CommId);
  324. int addr = json_object_get_int(Addr);
  325. sprintf(tmp,"%d",commid);
  326. json_object_object_add(body,"COMM",json_object_new_string(tmp));
  327. sprintf(tmp,"%d",addr);
  328. json_object_object_add(body,"ADDR",json_object_new_string(tmp));
  329. if((commid>=0)&&(commid<4)
  330. &&(addr>=0)&&(addr<256)){
  331. json_object *params = json_object_new_object();
  332. sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][addr].COMM_IDX);
  333. json_object_object_add(params,"COMM_IDX",json_object_new_string(tmp));
  334. sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][addr].BUS_ADD);
  335. json_object_object_add(params,"BUS_ADD",json_object_new_string(tmp));
  336. sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][addr].ENABLED);
  337. json_object_object_add(params,"ENABLED",json_object_new_string(tmp));
  338. json_object_object_add(params,"NAME",json_object_new_string(ytShm->sPointList.sPoint[commid][addr].NAME));
  339. sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][addr].PNO_TYPE);
  340. json_object_object_add(params,"PNO_TYPE",json_object_new_string(tmp));
  341. sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][addr].PNO_ALARM);
  342. json_object_object_add(params,"PNO_ALARM",json_object_new_string(tmp));
  343. sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][addr].PNO_VALUE);
  344. json_object_object_add(params,"PNO_VALUE",json_object_new_string(tmp));
  345. sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][addr].PNO_DLY);
  346. json_object_object_add(params,"PNO_DLY",json_object_new_string(tmp));
  347. sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][addr].PNO_AD);
  348. json_object_object_add(params,"PNO_AD",json_object_new_string(tmp));
  349. sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][addr].OFFSET);
  350. json_object_object_add(params,"OFFSET",json_object_new_string(tmp));
  351. sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][addr].OFFSET_FLAG);
  352. json_object_object_add(params,"OFFSET_FLAG",json_object_new_string(tmp));
  353. sprintf(tmp,"%.03f",ytShm->sPointList.sPoint[commid][addr].PNO_MAX);
  354. json_object_object_add(params,"PNO_MAX",json_object_new_string(tmp));
  355. sprintf(tmp,"%.03f",ytShm->sPointList.sPoint[commid][addr].PNO_MIN);
  356. json_object_object_add(params,"PNO_MIN",json_object_new_string(tmp));
  357. sprintf(tmp,"%.03f",ytShm->sPointList.sPoint[commid][addr].ALARM_H);
  358. json_object_object_add(params,"ALARM_H",json_object_new_string(tmp));
  359. sprintf(tmp,"%.03f",ytShm->sPointList.sPoint[commid][addr].ALARM_L);
  360. json_object_object_add(params,"ALARM_L",json_object_new_string(tmp));
  361. sprintf(tmp,"%ld",ytShm->sPointList.sPoint[commid][addr].SUB_TYPE);
  362. json_object_object_add(params,"SUB_TYPE",json_object_new_string(tmp));
  363. json_object_object_add(body,"PARAMS",params);
  364. }else{
  365. json_object_object_add(body,"ErrMsg",json_object_new_string("query out of size"));
  366. }
  367. }else{
  368. json_object_object_add(body,"ErrMsg",json_object_new_string("query params error"));
  369. }
  370. }
  371. void set_spno_params(json_object *q_body, json_object *body)
  372. {
  373. json_object *CommId=NULL, *Addr=NULL, *Params=NULL;
  374. json_object_object_foreach(q_body, key, val){
  375. if(strcmp("COMM",key)==0)
  376. CommId = val;
  377. else if(strcmp("ADDR",key)==0)
  378. Addr = val;
  379. else if(strcmp("PARAMS",key)==0)
  380. Params = val;
  381. }
  382. if((CommId!=NULL)&&(Addr!=NULL)
  383. &&(Params!=NULL)){
  384. char tmp[256];
  385. int commid = json_object_get_int(CommId);
  386. int addr = json_object_get_int(Addr);
  387. sprintf(tmp,"%d",commid);
  388. json_object_object_add(body,"COMM",json_object_new_string(tmp));
  389. sprintf(tmp,"%d",addr);
  390. json_object_object_add(body,"ADDR",json_object_new_string(tmp));
  391. if((commid>=0)&&(commid<4)
  392. &&(addr>=0)&&(addr<256)){
  393. json_object *CommIdx=NULL, *BusAdd=NULL, *Enabled=NULL, *Name=NULL;
  394. json_object *PnoType=NULL, *PnoAlarm=NULL, *PnoValue=NULL, *PnoDly=NULL;
  395. json_object *PnoAd=NULL, *Offset=NULL, *OffsetFlag=NULL, *PnoMax=NULL;
  396. json_object *PnoMin=NULL, *AlarmH=NULL, *AlarmL=NULL, *SubType=NULL;
  397. json_object_object_foreach(Params, key, val){
  398. if(strcmp("COMM_IDX",key)==0)
  399. CommIdx = val;
  400. else if(strcmp("BUS_ADD",key)==0)
  401. BusAdd = val;
  402. else if(strcmp("ENABLED",key)==0)
  403. Enabled = val;
  404. else if(strcmp("NAME",key)==0)
  405. Name = val;
  406. else if(strcmp("PNO_TYPE",key)==0)
  407. PnoType = val;
  408. else if(strcmp("PNO_ALARM",key)==0)
  409. PnoAlarm = val;
  410. else if(strcmp("PNO_VALUE",key)==0)
  411. PnoValue = val;
  412. else if(strcmp("PNO_DLY",key)==0)
  413. PnoDly = val;
  414. else if(strcmp("PNO_AD",key)==0)
  415. PnoAd = val;
  416. else if(strcmp("OFFSET",key)==0)
  417. Offset = val;
  418. else if(strcmp("OFFSET_FLAG",key)==0)
  419. OffsetFlag = val;
  420. else if(strcmp("PNO_MAX",key)==0)
  421. PnoMax = val;
  422. else if(strcmp("PNO_MIN",key)==0)
  423. PnoMin = val;
  424. else if(strcmp("ALARM_H",key)==0)
  425. AlarmH = val;
  426. else if(strcmp("ALARM_L",key)==0)
  427. AlarmL = val;
  428. else if(strcmp("SUB_TYPE",key)==0)
  429. SubType = val;
  430. }
  431. if((CommIdx!=NULL)&&(BusAdd!=NULL)&&(Enabled!=NULL)
  432. &&(Name!=NULL)&&(PnoType!=NULL)&&(PnoAlarm!=NULL)
  433. &&(PnoValue!=NULL)&&(PnoDly!=NULL)&&(PnoAd!=NULL)
  434. &&(Offset!=NULL)&&(OffsetFlag!=NULL)&&(PnoMax!=NULL)
  435. &&(PnoMin!=NULL)&&(AlarmH!=NULL)&&(AlarmL!=NULL)
  436. &&(SubType!=NULL)){
  437. int commidx = atoi(json_object_get_string(CommIdx));
  438. int busadd = atoi(json_object_get_string(BusAdd));
  439. int enabled = atoi(json_object_get_string(Enabled));
  440. int pnotype = atoi(json_object_get_string(PnoType));
  441. int pnoalarm = atoi(json_object_get_string(PnoAlarm));
  442. int pnovalue = atoi(json_object_get_string(PnoValue));
  443. int pnodly = atoi(json_object_get_string(PnoDly));
  444. int pnoad = atoi(json_object_get_string(PnoAd));
  445. int offset = atoi(json_object_get_string(Offset));
  446. int offset_flag = atoi(json_object_get_string(OffsetFlag));
  447. float pnomax = atof(json_object_get_string(PnoMax));
  448. float pnomin = atof(json_object_get_string(PnoMin));
  449. float alarmh = atof(json_object_get_string(AlarmH));
  450. float alarml = atof(json_object_get_string(AlarmL));
  451. uint subtype = (atoll(json_object_get_string(SubType)))&0xffffffff;
  452. if((commidx==commid)&&(busadd==addr)&&(enabled>=0)&&(enabled<2)
  453. &&(pnotype>=0)&&(pnotype<5)&&(pnoalarm>=0)&&(pnoalarm<3)
  454. &&(pnovalue>=0)&&(pnovalue<4)&&(pnodly>=0)&&(pnodly<256)
  455. &&(pnoad>=0)&&(pnoad<17)&&(offset>=0)&&(offset<256)
  456. &&(offset_flag>=0)&&(offset_flag<2)&&(strlen(json_object_get_string(Name))<=220)){
  457. ytShm->sPointList.sPoint[commid][addr].COMM_IDX = commidx;
  458. ytShm->sPointList.sPoint[commid][addr].BUS_ADD = busadd;
  459. ytShm->sPointList.sPoint[commid][addr].ENABLED = enabled;
  460. sprintf(ytShm->sPointList.sPoint[commid][addr].NAME,"%s",json_object_get_string(Name));
  461. ytShm->sPointList.sPoint[commid][addr].PNO_TYPE = pnotype;
  462. ytShm->sPointList.sPoint[commid][addr].PNO_ALARM = pnoalarm;
  463. ytShm->sPointList.sPoint[commid][addr].PNO_VALUE = pnovalue;
  464. ytShm->sPointList.sPoint[commid][addr].PNO_DLY = pnodly;
  465. ytShm->sPointList.sPoint[commid][addr].PNO_AD = pnoad;
  466. ytShm->sPointList.sPoint[commid][addr].OFFSET = offset;
  467. ytShm->sPointList.sPoint[commid][addr].OFFSET_FLAG = offset_flag;
  468. ytShm->sPointList.sPoint[commid][addr].PNO_MAX = pnomax;
  469. ytShm->sPointList.sPoint[commid][addr].PNO_MIN = pnomin;
  470. ytShm->sPointList.sPoint[commid][addr].ALARM_H = alarmh;
  471. ytShm->sPointList.sPoint[commid][addr].ALARM_L = alarml;
  472. ytShm->sPointList.sPoint[commid][addr].SUB_TYPE = subtype;
  473. ytShm->sPointList.sPoint[commid][addr].UNSAVED = 0x01;
  474. json_object_object_add(body,"SETUP",json_object_new_string("TRUE"));
  475. }else{
  476. json_object_object_add(body,"SETUP",json_object_new_string("FALSE"));
  477. json_object_object_add(body,"ErrMsg",json_object_new_string("set params out of size"));
  478. }
  479. }else{
  480. json_object_object_add(body,"SETUP",json_object_new_string("FALSE"));
  481. json_object_object_add(body,"ErrMsg",json_object_new_string("set params error"));
  482. }
  483. }else{
  484. json_object_object_add(body,"SETUP",json_object_new_string("FALSE"));
  485. json_object_object_add(body,"ErrMsg",json_object_new_string("query out of size"));
  486. }
  487. }else{
  488. json_object_object_add(body,"SETUP",json_object_new_string("FALSE"));
  489. json_object_object_add(body,"ErrMsg",json_object_new_string("query params error"));
  490. }
  491. }
  492. void qry_iot_status(json_object *q_body, json_object *body)
  493. {
  494. int comm,adds,i;
  495. int count[5],alarm[5],out[5];
  496. json_object *iotlist,*obj[5],*obj_list[5];
  497. char tmp[256];
  498. iotlist = json_object_new_array();
  499. for(i=0;i<5;i++)
  500. {
  501. count[i]=0;
  502. alarm[i]=0;
  503. out[i]=0;
  504. obj[i] = json_object_new_object();
  505. obj_list[i] = json_object_new_array();
  506. }
  507. for(comm=0;comm<4;comm++)
  508. {
  509. if(ytShm->commList.comm[comm].ENABLED==0x01){
  510. for(adds=0;adds<256;adds++)
  511. {
  512. if(ytShm->sPointList.sPoint[comm][adds].ENABLED==0x01)
  513. {
  514. json_object *sp = json_object_new_object();
  515. for(i=0;i<5;i++)
  516. {
  517. if((ytShm->sPointList.sPoint[comm][adds].SUB_TYPE&(0x01<<i))==(0x01<<i))
  518. count[i]++;
  519. }
  520. if(ytShm->commStatusList.commStatus[comm].STATUS!=0x01)
  521. {
  522. if(ytShm->sPointList.sPoint[comm][adds].SUB_TYPE>0)
  523. {
  524. for(i=0;i<5;i++)
  525. {
  526. if((ytShm->sPointList.sPoint[comm][adds].SUB_TYPE&(0x01<<i))==(0x01<<i))
  527. out[i]++;
  528. }
  529. json_object_object_add(sp,"STATUS_ID",json_object_new_int(1));
  530. json_object_object_add(sp,"STATUS",json_object_new_string("离线"));
  531. }
  532. }else{
  533. if(ytShm->spStatusList.spStatus[comm][adds].ALARM_STATUS!=0x00)
  534. {
  535. for(i=0;i<5;i++)
  536. {
  537. if((ytShm->sPointList.sPoint[comm][adds].SUB_TYPE&(0x01<<i))==(0x01<<i))
  538. alarm[i]++;
  539. }
  540. json_object_object_add(sp,"STATUS_ID",json_object_new_int(2));
  541. json_object_object_add(sp,"STATUS",json_object_new_string("告警"));
  542. }else{
  543. json_object_object_add(sp,"STATUS_ID",json_object_new_int(0));
  544. json_object_object_add(sp,"STATUS",json_object_new_string("正常"));
  545. }
  546. }
  547. json_object_object_add(sp,"COMM",json_object_new_int(comm));
  548. json_object_object_add(sp,"ADDR",json_object_new_int(adds));
  549. json_object_object_add(sp,"PNO_NAME",
  550. json_object_new_string(ytShm->sPointList.sPoint[comm][adds].NAME));
  551. json_object_object_add(sp,"PNO_TYPE",json_object_new_int(ytShm->sPointList.sPoint[comm][adds].PNO_TYPE));
  552. if(ytShm->sPointList.sPoint[comm][adds].PNO_TYPE==0x01){
  553. if(ytShm->spStatusList.spStatus[comm][adds].REALTIME_VALUE==0.0)
  554. json_object_object_add(sp,"VALUE",json_object_new_string("0"));
  555. else
  556. json_object_object_add(sp,"VALUE",json_object_new_string("1"));
  557. }else{
  558. sprintf(tmp,"%.3f",ytShm->spStatusList.spStatus[comm][adds].REALTIME_VALUE);
  559. json_object_object_add(sp,"VALUE",json_object_new_string(tmp));
  560. }
  561. time_t t = ytShm->spStatusList.spStatus[comm][adds].REALTIME_TIMESTAMP;
  562. struct tm *loc_time = localtime(&t);
  563. sprintf(tmp,"%04d-%02d-%02d %02d:%02d:%02d",
  564. loc_time->tm_year+1900,
  565. loc_time->tm_mon+1,
  566. loc_time->tm_mday,
  567. loc_time->tm_hour,
  568. loc_time->tm_min,
  569. loc_time->tm_sec);
  570. json_object_object_add(sp,"TIME",json_object_new_string(tmp));
  571. for(i=0;i<5;i++)
  572. {
  573. if((ytShm->sPointList.sPoint[comm][adds].SUB_TYPE&(0x01<<i))==(0x01<<i))
  574. json_object_array_add(obj_list[i],sp);
  575. }
  576. }
  577. }
  578. }
  579. }
  580. for(i=0;i<5;i++)
  581. {
  582. json_object_object_add(obj[i],"ID",json_object_new_int(i+1));
  583. switch(i){
  584. case 0:
  585. json_object_object_add(obj[i],"NAME",json_object_new_string("火灾监控实时信息"));
  586. break;
  587. case 1:
  588. json_object_object_add(obj[i],"NAME",json_object_new_string("电气火灾监控实时信息"));
  589. break;
  590. case 2:
  591. json_object_object_add(obj[i],"NAME",json_object_new_string("消防水系统监控实时信息"));
  592. break;
  593. case 3:
  594. json_object_object_add(obj[i],"NAME",json_object_new_string("通道监控实时信息"));
  595. break;
  596. case 4:
  597. json_object_object_add(obj[i],"NAME",json_object_new_string("消防电源监控实时信息"));
  598. break;
  599. }
  600. json_object_object_add(obj[i],"COUNT",json_object_new_int(count[i]));
  601. json_object_object_add(obj[i],"OUT",json_object_new_int(out[i]));
  602. json_object_object_add(obj[i],"ALARM",json_object_new_int(alarm[i]));
  603. json_object_object_add(obj[i],"LIST",obj_list[i]);
  604. json_object_array_add(iotlist,obj[i]);
  605. }
  606. json_object_object_add(body,"IOT_LIST",iotlist);
  607. }
  608. void json_cmd(int cmd, json_object *q_body, json_object *body)
  609. {
  610. switch (cmd) {
  611. case CMD_GET_REALTIME_STATUS:
  612. get_realtime_status(q_body,body);
  613. break;
  614. case CMD_GET_HISTORY_VALUE:
  615. get_history_value(q_body,body);
  616. break;
  617. case CMD_GET_COMM_PARAMS:
  618. get_comm_params(q_body,body);
  619. break;
  620. case CMD_SET_COMM_PARAMS:
  621. set_comm_params(q_body,body);
  622. break;
  623. case CMD_GET_SPNO_PARAMS:
  624. get_spno_params(q_body, body);
  625. break;
  626. case CMD_SET_SPNO_PARAMS:
  627. set_spno_params(q_body, body);
  628. break;
  629. case CMD_QRY_IOT_STATUS:
  630. qry_iot_status(q_body,body);
  631. break;
  632. case CMD_CTL:
  633. break;
  634. default:
  635. break;
  636. }
  637. }
  638. bool json_request_action(char *request, json_object *body)
  639. {
  640. json_object *q_json = NULL, *q_ver = NULL, *q_body =NULL;
  641. json_object *q_cmd = NULL, *q_reply=NULL;
  642. int cmd;
  643. q_json = json_tokener_parse(request);
  644. if(q_json!=NULL){
  645. json_object_object_foreach(q_json,key,val){
  646. if(strcmp(key,"REPLY")==0){
  647. q_reply = val;
  648. }else if(strcmp(key,"VER")==0){
  649. q_ver = val;
  650. }else if(strcmp(key,"BODY")==0){
  651. q_body = val;
  652. }
  653. }
  654. if(q_reply==NULL){
  655. json_object_put(q_json);
  656. json_object_object_add(body,"ErrMsg",json_object_new_string("reply null"));
  657. return false;
  658. }else if(strcmp(json_object_get_string(q_reply),"0")!=0){
  659. json_object_put(q_json);
  660. json_object_object_add(body,"ErrMsg",json_object_new_string("reply error"));
  661. return false;
  662. }
  663. if(q_ver==NULL){
  664. json_object_put(q_json);
  665. json_object_object_add(body,"ErrMsg",json_object_new_string("version null"));
  666. return false;
  667. }else if(strcmp(json_object_get_string(q_ver),"1.00")!=0){
  668. json_object_put(q_json);
  669. json_object_object_add(body,"ErrMsg",json_object_new_string("version error"));
  670. return false;
  671. }
  672. if(q_body==NULL){
  673. json_object_put(q_json);
  674. json_object_object_add(body,"ErrMsg",json_object_new_string("body null"));
  675. return false;
  676. }
  677. json_object_object_foreach(q_body,key2,val2){
  678. if(strcmp(key2,"CMD")==0){
  679. q_cmd = val2;
  680. }
  681. }
  682. if(q_cmd==NULL){
  683. json_object_put(q_json);
  684. json_object_object_add(body,"ErrMsg",json_object_new_string("cmd null"));
  685. return false;
  686. }else if(json_object_get_type(q_cmd)!=json_type_int){
  687. json_object_put(q_json);
  688. json_object_object_add(body,"ErrMsg",json_object_new_string("cmd datatype error"));
  689. return false;
  690. }
  691. cmd = json_object_get_int(q_cmd);
  692. json_object_object_add(body,"CMD",json_object_new_int(cmd));
  693. json_cmd(cmd,q_body,body);
  694. json_object_put(q_json);
  695. }else{
  696. json_object_object_add(body,"ErrMsg",json_object_new_string("query null"));
  697. return false;
  698. }
  699. return true;
  700. }
  701. int main(void)
  702. {
  703. char *input,*lenstr,*data;
  704. char tmpstr[1024];
  705. json_object *json,*json_body;
  706. int len;
  707. printf("%s%c%c\n","Content-Type:text/html;charset=UTF-8",13,10);
  708. json = json_object_new_object();
  709. json_object_object_add(json,"VER",json_object_new_string("1.00"));
  710. json_object_object_add(json,"NAME",json_object_new_string("Usky_IoT-03"));
  711. lenstr = getenv("CONTENT_LENGTH");
  712. if(lenstr){
  713. len = atoi(lenstr);
  714. if((len>0)&&(len<1024)){
  715. input = (char *)malloc((unsigned)(len+2));
  716. data = (char *)malloc((unsigned)(len+2));
  717. fgets(input,len+1,stdin);
  718. if(load_shm()){
  719. ytShm->dogTimeList.dogTime[63].LASTFEED=0x01;
  720. sprintf(tmpstr,"%04d-%02d-%02d %02d:%02d:%02d",
  721. ytShm->currentTime.YEAR,
  722. ytShm->currentTime.MONTH,
  723. ytShm->currentTime.DAY,
  724. ytShm->currentTime.HOUR,
  725. ytShm->currentTime.MINUTE,
  726. ytShm->currentTime.SECOND);
  727. json_object_object_add(json,"TIMESTAMP",json_object_new_string(tmpstr));
  728. json_body = json_object_new_object();
  729. if(json_request_action(input, json_body)){
  730. json_object_object_add(json,"RESULT",json_object_new_string("1"));
  731. json_object_object_add(json,"BODY",json_body);
  732. }else{
  733. json_object_object_add(json,"RESULT",json_object_new_string("0"));
  734. json_object_object_add(json,"MESSAGE",json_object_new_string("Query Json error."));
  735. json_object_object_add(json,"BODY",json_body);
  736. }
  737. shmdt(ytShm);
  738. }else{
  739. json_object_object_add(json,"RESULT",json_object_new_string("0"));
  740. json_object_object_add(json,"MESSAGE",json_object_new_string("Load Share Memory failed."));
  741. }
  742. json_object_object_add(json,"VER",json_object_new_string("1.00"));
  743. json_object_object_add(json,"REPLY",json_object_new_string("1"));
  744. free(data);
  745. free(input);
  746. }else{
  747. json_object_object_add(json,"RESULT",json_object_new_string("0"));
  748. json_object_object_add(json,"MESSAGE",json_object_new_string("Length error."));
  749. }
  750. }else{
  751. json_object_object_add(json,"RESULT",json_object_new_string("0"));
  752. json_object_object_add(json,"MESSAGE",json_object_new_string("No Content error."));
  753. }
  754. printf("%s",json_object_to_json_string(json));
  755. json_object_put(json);
  756. return 0;
  757. }