main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * main.c
  3. *
  4. * Created on: 2022年9月22日
  5. * Author: zyj
  6. */
  7. #include <time.h>
  8. #include <sys/time.h>
  9. #include <stdlib.h>
  10. #include <stdbool.h>
  11. #include <unistd.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <json-c/json.h>
  15. #include "../ytCore/yt_unit_shm.h"
  16. YT_UNIT_SHM *ytShm;
  17. typedef union {
  18. float f;
  19. unsigned short s[2];
  20. }UNFI;
  21. bool load_shm()
  22. {
  23. int shmid;
  24. key_t key;
  25. if((key = ftok(SHM_PATH,(int)SHM_PORT))==-1)
  26. return false;
  27. if((shmid = shmget(key,sizeof(YT_UNIT_SHM),IPC_CREAT|0666))==-1)
  28. return false;
  29. ytShm = (YT_UNIT_SHM *)shmat(shmid,NULL,0);
  30. return true;
  31. }
  32. void qry_iot_param(json_object *body,int cid)
  33. {
  34. json_object *iotlist;
  35. iotlist = json_object_new_array();
  36. json_object *sp = json_object_new_object();
  37. json_object_object_add(sp,"field1",json_object_new_double(ytShm->spStatusList.spStatus[cid][1].REALTIME_VALUE));
  38. json_object_object_add(sp,"field2",json_object_new_double(ytShm->spStatusList.spStatus[cid][2].REALTIME_VALUE));
  39. json_object_object_add(sp,"field3",json_object_new_double(ytShm->spStatusList.spStatus[cid][3].REALTIME_VALUE));
  40. json_object_object_add(sp,"field4",json_object_new_double(ytShm->spStatusList.spStatus[cid][4].REALTIME_VALUE));
  41. json_object_object_add(sp,"field5",json_object_new_double(ytShm->spStatusList.spStatus[cid][5].REALTIME_VALUE));
  42. json_object_object_add(sp,"field6",json_object_new_double(ytShm->spStatusList.spStatus[cid][6].REALTIME_VALUE));
  43. json_object_object_add(sp,"field7",json_object_new_double(ytShm->spStatusList.spStatus[cid][7].REALTIME_VALUE));
  44. json_object_object_add(sp,"field8",json_object_new_double(ytShm->spStatusList.spStatus[cid][8].REALTIME_VALUE));
  45. json_object_object_add(sp,"field9",json_object_new_double(ytShm->spStatusList.spStatus[cid][9].REALTIME_VALUE));
  46. json_object_object_add(sp,"field10",json_object_new_double(ytShm->spStatusList.spStatus[cid][10].REALTIME_VALUE));
  47. json_object_object_add(sp,"field11",json_object_new_double(ytShm->spStatusList.spStatus[cid][11].REALTIME_VALUE));
  48. json_object_object_add(sp,"field12",json_object_new_double(ytShm->spStatusList.spStatus[cid][12].REALTIME_VALUE));
  49. json_object_object_add(sp,"field13",json_object_new_double(ytShm->spStatusList.spStatus[cid][13].REALTIME_VALUE));
  50. json_object_object_add(sp,"field14",json_object_new_double(ytShm->spStatusList.spStatus[cid][14].REALTIME_VALUE));
  51. json_object_object_add(sp,"field15",json_object_new_double(ytShm->spStatusList.spStatus[cid][15].REALTIME_VALUE));
  52. json_object_object_add(sp,"field16",json_object_new_double(ytShm->spStatusList.spStatus[cid][16].REALTIME_VALUE));
  53. json_object_object_add(sp,"field17",json_object_new_double(ytShm->spStatusList.spStatus[cid][17].REALTIME_VALUE));
  54. json_object_object_add(sp,"field18",json_object_new_double(ytShm->spStatusList.spStatus[cid][18].REALTIME_VALUE));
  55. json_object_object_add(sp,"field19",json_object_new_double(ytShm->spStatusList.spStatus[cid][19].REALTIME_VALUE));
  56. json_object_object_add(sp,"field20",json_object_new_double(ytShm->spStatusList.spStatus[cid][20].REALTIME_VALUE));
  57. json_object_object_add(sp,"field21",json_object_new_double(ytShm->spStatusList.spStatus[cid][21].REALTIME_VALUE));
  58. json_object_object_add(sp,"field22",json_object_new_double(ytShm->spStatusList.spStatus[cid][22].REALTIME_VALUE));
  59. json_object_object_add(sp,"field23",json_object_new_double(ytShm->spStatusList.spStatus[cid][23].REALTIME_VALUE));
  60. json_object_object_add(sp,"field24",json_object_new_double(ytShm->spStatusList.spStatus[cid][24].REALTIME_VALUE));
  61. json_object_object_add(sp,"field25",json_object_new_double(ytShm->spStatusList.spStatus[cid][25].REALTIME_VALUE));
  62. json_object_object_add(sp,"field26",json_object_new_double(ytShm->spStatusList.spStatus[cid][26].REALTIME_VALUE));
  63. json_object_object_add(sp,"field27",json_object_new_double(ytShm->spStatusList.spStatus[cid][27].REALTIME_VALUE));
  64. json_object_object_add(sp,"field28",json_object_new_double(ytShm->spStatusList.spStatus[cid][28].REALTIME_VALUE));
  65. json_object_object_add(sp,"field29",json_object_new_double(ytShm->spStatusList.spStatus[cid][29].REALTIME_VALUE));
  66. json_object_object_add(sp,"field30",json_object_new_double(ytShm->spStatusList.spStatus[cid][30].REALTIME_VALUE));
  67. json_object_object_add(sp,"field31",json_object_new_double(ytShm->spStatusList.spStatus[cid][31].REALTIME_VALUE));
  68. json_object_array_add(iotlist,sp);
  69. json_object_object_add(body,"IOT_LIST",iotlist);
  70. }
  71. void set_real_value(json_object *param,int cid)
  72. {
  73. uint t = 0;
  74. uint timestamp = 0;
  75. json_object_object_foreach(param,key,val){
  76. if(strcmp(key,"timestamp")==0){
  77. t=json_object_get_int(val);
  78. timestamp=json_object_get_int(val);
  79. }else if(strcmp(key,"field16")==0){
  80. ytShm->spStatusList.spStatus[cid][16].REALTIME_VALUE=json_object_get_double(val);
  81. ytShm->spStatusList.spStatus[cid][16].REALTIME_TIMESTAMP=t;
  82. }else if(strcmp(key,"field17")==0){
  83. ytShm->spStatusList.spStatus[cid][17].REALTIME_VALUE=json_object_get_double(val);
  84. ytShm->spStatusList.spStatus[cid][17].REALTIME_TIMESTAMP=t;
  85. }else if(strcmp(key,"field18")==0){
  86. ytShm->spStatusList.spStatus[cid][18].REALTIME_VALUE=json_object_get_double(val);
  87. ytShm->spStatusList.spStatus[cid][18].REALTIME_TIMESTAMP=t;
  88. }else if(strcmp(key,"field19")==0){
  89. ytShm->spStatusList.spStatus[cid][19].REALTIME_VALUE=json_object_get_double(val);
  90. ytShm->spStatusList.spStatus[cid][19].REALTIME_TIMESTAMP=t;
  91. }else if(strcmp(key,"field20")==0){
  92. ytShm->spStatusList.spStatus[cid][20].REALTIME_VALUE=json_object_get_double(val);
  93. ytShm->spStatusList.spStatus[cid][20].REALTIME_TIMESTAMP=t;
  94. }else if(strcmp(key,"field21")==0){
  95. ytShm->spStatusList.spStatus[cid][21].REALTIME_VALUE=json_object_get_double(val);
  96. ytShm->spStatusList.spStatus[cid][21].REALTIME_TIMESTAMP=t;
  97. }else if(strcmp(key,"field22")==0){
  98. ytShm->spStatusList.spStatus[cid][22].REALTIME_VALUE=json_object_get_double(val);
  99. ytShm->spStatusList.spStatus[cid][22].REALTIME_TIMESTAMP=t;
  100. }else if(strcmp(key,"field23")==0){
  101. ytShm->spStatusList.spStatus[cid][23].REALTIME_VALUE=json_object_get_double(val);
  102. ytShm->spStatusList.spStatus[cid][23].REALTIME_TIMESTAMP=t;
  103. }else if(strcmp(key,"field24")==0){
  104. ytShm->spStatusList.spStatus[cid][24].REALTIME_VALUE=json_object_get_double(val);
  105. ytShm->spStatusList.spStatus[cid][24].REALTIME_TIMESTAMP=t;
  106. }else if(strcmp(key,"field25")==0){
  107. ytShm->spStatusList.spStatus[cid][25].REALTIME_VALUE=json_object_get_double(val);
  108. ytShm->spStatusList.spStatus[cid][25].REALTIME_TIMESTAMP=t;
  109. }else if(strcmp(key,"field26")==0){
  110. ytShm->spStatusList.spStatus[cid][26].REALTIME_VALUE=json_object_get_double(val);
  111. ytShm->spStatusList.spStatus[cid][26].REALTIME_TIMESTAMP=t;
  112. }else if(strcmp(key,"field27")==0){
  113. ytShm->spStatusList.spStatus[cid][27].REALTIME_VALUE=json_object_get_double(val);
  114. ytShm->spStatusList.spStatus[cid][27].REALTIME_TIMESTAMP=t;
  115. }else if(strcmp(key,"field28")==0){
  116. ytShm->spStatusList.spStatus[cid][28].REALTIME_VALUE=json_object_get_double(val);
  117. ytShm->spStatusList.spStatus[cid][28].REALTIME_TIMESTAMP=t;
  118. }else if(strcmp(key,"field29")==0){
  119. ytShm->spStatusList.spStatus[cid][29].REALTIME_VALUE=json_object_get_double(val);
  120. ytShm->spStatusList.spStatus[cid][29].REALTIME_TIMESTAMP=t;
  121. }else if(strcmp(key,"field30")==0){
  122. ytShm->spStatusList.spStatus[cid][30].REALTIME_VALUE=json_object_get_double(val);
  123. ytShm->spStatusList.spStatus[cid][30].REALTIME_TIMESTAMP=t;
  124. }else if(strcmp(key,"field31")==0){
  125. ytShm->spStatusList.spStatus[cid][31].REALTIME_VALUE=json_object_get_double(val);
  126. ytShm->spStatusList.spStatus[cid][31].REALTIME_TIMESTAMP=t;
  127. }
  128. }
  129. }
  130. int main(void)
  131. {
  132. char *input,*lenstr,*data;
  133. int i;
  134. int cmd=-1,cid=-1,pno=-1,act=-1,type=-1;
  135. int wait=50;
  136. json_object *json,*q_json,*json_body,*json_param;
  137. int len;
  138. bool hadparam=false;
  139. printf("%s%c%c\n","Content-Type:text/html;charset=UTF-8",13,10);
  140. json = json_object_new_object();
  141. json_object_object_add(json,"VER",json_object_new_string("1.00"));
  142. json_object_object_add(json,"NAME",json_object_new_string("Dmp_IoT-01"));
  143. lenstr = getenv("CONTENT_LENGTH");
  144. if(lenstr){
  145. len = atoi(lenstr);
  146. if((len>0)&&(len<1024)){
  147. input = (char *)malloc((unsigned)(len+2));
  148. data = (char *)malloc((unsigned)(len+2));
  149. fgets(input,len+1,stdin);
  150. for(i=0;i<len-10;i++)
  151. data[i] = input[10+i];
  152. // printf("%s\n",data);
  153. if(load_shm()){
  154. q_json = json_tokener_parse(data);
  155. json_object_object_foreach(q_json,key,val){
  156. if(strcmp(key,"CMD")==0){
  157. cmd = json_object_get_int(val);
  158. }else if(strcmp(key,"COMMID")==0){
  159. cid = json_object_get_int(val);
  160. }else if(strcmp(key,"PARAM")==0){
  161. json_param=val;
  162. hadparam=true;
  163. }
  164. }
  165. if((cid>=0)&&(cid<4)&&(cmd>0)){
  166. switch(cmd){
  167. case 1:
  168. if(hadparam){
  169. json_object_object_add(json,"RESULT",json_object_new_string("1"));
  170. set_real_value(json_param,cid);
  171. }else{
  172. json_object_object_add(json,"RESULT",json_object_new_string("0"));
  173. }
  174. break;
  175. case 2:
  176. json_body = json_object_new_object();
  177. qry_iot_param(json_body,cid);
  178. json_object_object_add(json,"RESULT",json_body);
  179. break;
  180. default:break;
  181. }
  182. }else{
  183. json_object_object_add(json,"RESULT",json_object_new_string("0"));
  184. json_object_object_add(json,"MESSAGE",json_object_new_string("Params Error."));
  185. }
  186. shmdt(ytShm);
  187. }else{
  188. json_object_object_add(json,"RESULT",json_object_new_string("0"));
  189. json_object_object_add(json,"MESSAGE",json_object_new_string("Load Share Memory failed."));
  190. }
  191. json_object_object_add(json,"VER",json_object_new_string("1.00"));
  192. json_object_object_add(json,"REPLY",json_object_new_string("1"));
  193. free(data);
  194. free(input);
  195. }else{
  196. json_object_object_add(json,"RESULT",json_object_new_string("0"));
  197. json_object_object_add(json,"MESSAGE",json_object_new_string("Length error."));
  198. }
  199. }else{
  200. json_object_object_add(json,"RESULT",json_object_new_string("0"));
  201. json_object_object_add(json,"MESSAGE",json_object_new_string("No Content error."));
  202. }
  203. printf("%s",json_object_to_json_string(json));
  204. json_object_put(json);
  205. return 0;
  206. }