f8107a75aa434ba8e8551c819146557fb25f1c19.svn-base 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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 *obj[5],*obj_list[5];
  497. char tmp[256];
  498. for(i=0;i<5;i++)
  499. {
  500. count[i]=0;
  501. alarm[i]=0;
  502. out[i]=0;
  503. obj[i] = json_object_new_object();
  504. obj_list[i] = json_object_new_array();
  505. }
  506. for(comm=0;comm<4;comm++)
  507. {
  508. if(ytShm->commList.comm[comm].ENABLED==0x01){
  509. for(adds=0;adds<256;adds++)
  510. {
  511. if(ytShm->sPointList.sPoint[comm][adds].ENABLED==0x01)
  512. {
  513. json_object *sp = json_object_new_object();
  514. for(i=0;i<5;i++)
  515. {
  516. if((ytShm->sPointList.sPoint[comm][adds].SUB_TYPE&(0x01<<i))==(0x01<<i))
  517. count[i]++;
  518. }
  519. if(ytShm->commStatusList.commStatus[comm].STATUS!=0x00)
  520. {
  521. if(ytShm->sPointList.sPoint[comm][adds].SUB_TYPE>0)
  522. {
  523. for(i=0;i<5;i++)
  524. {
  525. if((ytShm->sPointList.sPoint[comm][adds].SUB_TYPE&(0x01<<i))==(0x01<<i))
  526. out[i]++;
  527. }
  528. json_object_object_add(sp,"STATUS_ID",json_object_new_int(1));
  529. json_object_object_add(sp,"STATUS",json_object_new_string("离线"));
  530. }
  531. }else{
  532. if(ytShm->spStatusList.spStatus[comm][adds].ALARM_STATUS!=0x00)
  533. {
  534. for(i=0;i<5;i++)
  535. {
  536. if((ytShm->sPointList.sPoint[comm][adds].SUB_TYPE&(0x01<<i))==(0x01<<i))
  537. alarm[i]++;
  538. }
  539. json_object_object_add(sp,"STATUS_ID",json_object_new_int(2));
  540. json_object_object_add(sp,"STATUS",json_object_new_string("告警"));
  541. }else{
  542. json_object_object_add(sp,"STATUS_ID",json_object_new_int(0));
  543. json_object_object_add(sp,"STATUS",json_object_new_string("正常"));
  544. }
  545. }
  546. json_object_object_add(sp,"COMM",json_object_new_int())
  547. json_object_object_add(sp,"PNO_NAME",
  548. json_object_new_string(ytShm->spStatusList.spStatus[comm][adds].NAME));
  549. }
  550. }
  551. }
  552. }
  553. }
  554. void json_cmd(int cmd, json_object *q_body, json_object *body)
  555. {
  556. switch (cmd) {
  557. case CMD_GET_REALTIME_STATUS:
  558. get_realtime_status(q_body,body);
  559. break;
  560. case CMD_GET_HISTORY_VALUE:
  561. get_history_value(q_body,body);
  562. break;
  563. case CMD_GET_COMM_PARAMS:
  564. get_comm_params(q_body,body);
  565. break;
  566. case CMD_SET_COMM_PARAMS:
  567. set_comm_params(q_body,body);
  568. break;
  569. case CMD_GET_SPNO_PARAMS:
  570. get_spno_params(q_body, body);
  571. break;
  572. case CMD_SET_SPNO_PARAMS:
  573. set_spno_params(q_body, body);
  574. break;
  575. case CMD_QRY_IOT_STATUS:
  576. qry_iot_status(q_body,body);
  577. break;
  578. case CMD_CTL:
  579. break;
  580. default:
  581. break;
  582. }
  583. }
  584. bool json_request_action(char *request, json_object *body)
  585. {
  586. json_object *q_json = NULL, *q_ver = NULL, *q_body =NULL;
  587. json_object *q_cmd = NULL, *q_reply=NULL;
  588. int cmd;
  589. q_json = json_tokener_parse(request);
  590. if(q_json!=NULL){
  591. json_object_object_foreach(q_json,key,val){
  592. if(strcmp(key,"REPLY")==0){
  593. q_reply = val;
  594. }else if(strcmp(key,"VER")==0){
  595. q_ver = val;
  596. }else if(strcmp(key,"BODY")==0){
  597. q_body = val;
  598. }
  599. }
  600. if(q_reply==NULL){
  601. json_object_put(q_json);
  602. json_object_object_add(body,"ErrMsg",json_object_new_string("reply null"));
  603. return false;
  604. }else if(strcmp(json_object_get_string(q_reply),"0")!=0){
  605. json_object_put(q_json);
  606. json_object_object_add(body,"ErrMsg",json_object_new_string("reply error"));
  607. return false;
  608. }
  609. if(q_ver==NULL){
  610. json_object_put(q_json);
  611. json_object_object_add(body,"ErrMsg",json_object_new_string("version null"));
  612. return false;
  613. }else if(strcmp(json_object_get_string(q_ver),"1.00")!=0){
  614. json_object_put(q_json);
  615. json_object_object_add(body,"ErrMsg",json_object_new_string("version error"));
  616. return false;
  617. }
  618. if(q_body==NULL){
  619. json_object_put(q_json);
  620. json_object_object_add(body,"ErrMsg",json_object_new_string("body null"));
  621. return false;
  622. }
  623. json_object_object_foreach(q_body,key2,val2){
  624. if(strcmp(key2,"CMD")==0){
  625. q_cmd = val2;
  626. }
  627. }
  628. if(q_cmd==NULL){
  629. json_object_put(q_json);
  630. json_object_object_add(body,"ErrMsg",json_object_new_string("cmd null"));
  631. return false;
  632. }else if(json_object_get_type(q_cmd)!=json_type_int){
  633. json_object_put(q_json);
  634. json_object_object_add(body,"ErrMsg",json_object_new_string("cmd datatype error"));
  635. return false;
  636. }
  637. cmd = json_object_get_int(q_cmd);
  638. json_object_object_add(body,"CMD",json_object_new_int(cmd));
  639. json_cmd(cmd,q_body,body);
  640. json_object_put(q_json);
  641. }else{
  642. json_object_object_add(body,"ErrMsg",json_object_new_string("query null"));
  643. return false;
  644. }
  645. return true;
  646. }
  647. int main(void)
  648. {
  649. char *input,*lenstr,*data;
  650. char tmpstr[1024];
  651. json_object *json,*json_body;
  652. int len;
  653. printf("%s%c%c\n","Content-Type:text/html;charset=UTF-8",13,10);
  654. json = json_object_new_object();
  655. json_object_object_add(json,"VER",json_object_new_string("1.00"));
  656. json_object_object_add(json,"NAME",json_object_new_string("Usky_IoT-03"));
  657. lenstr = getenv("CONTENT_LENGTH");
  658. if(lenstr){
  659. len = atoi(lenstr);
  660. if((len>0)&&(len<1024)){
  661. input = (char *)malloc((unsigned)(len+2));
  662. data = (char *)malloc((unsigned)(len+2));
  663. fgets(input,len+1,stdin);
  664. if(load_shm()){
  665. // ytShm->dogTimeList.dogTime[63].LASTFEED=0x01;
  666. sprintf(tmpstr,"%04d-%02d-%02d %02d:%02d:%02d",
  667. ytShm->currentTime.YEAR,
  668. ytShm->currentTime.MONTH,
  669. ytShm->currentTime.DAY,
  670. ytShm->currentTime.HOUR,
  671. ytShm->currentTime.MINUTE,
  672. ytShm->currentTime.SECOND);
  673. json_object_object_add(json,"TIMESTAMP",json_object_new_string(tmpstr));
  674. json_body = json_object_new_object();
  675. if(json_request_action(input, json_body)){
  676. json_object_object_add(json,"RESULT",json_object_new_string("1"));
  677. json_object_object_add(json,"BODY",json_body);
  678. }else{
  679. json_object_object_add(json,"RESULT",json_object_new_string("0"));
  680. json_object_object_add(json,"MESSAGE",json_object_new_string("Query Json error."));
  681. json_object_object_add(json,"BODY",json_body);
  682. }
  683. shmdt(ytShm);
  684. }else{
  685. json_object_object_add(json,"RESULT",json_object_new_string("0"));
  686. json_object_object_add(json,"MESSAGE",json_object_new_string("Load Share Memory failed."));
  687. }
  688. json_object_object_add(json,"VER",json_object_new_string("1.00"));
  689. json_object_object_add(json,"REPLY",json_object_new_string("1"));
  690. free(data);
  691. free(input);
  692. }else{
  693. json_object_object_add(json,"RESULT",json_object_new_string("0"));
  694. json_object_object_add(json,"MESSAGE",json_object_new_string("Length error."));
  695. }
  696. }else{
  697. json_object_object_add(json,"RESULT",json_object_new_string("0"));
  698. json_object_object_add(json,"MESSAGE",json_object_new_string("No Content error."));
  699. }
  700. printf("%s",json_object_to_json_string(json));
  701. json_object_put(json);
  702. return 0;
  703. }