f01ef688bdf66fae624973fecc0b170b4e1e3f18.svn-base 33 KB

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