931d740ad380b91089f47b5bd04a9126ddca5593.svn-base 32 KB

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