123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- /*
- * main.c
- *
- * Created on: 2022年9月22日
- * Author: zyj
- */
- #include <time.h>
- #include <sys/time.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <string.h>
- #include <json-c/json.h>
- #include "../ytCore/yt_unit_shm.h"
- YT_UNIT_SHM *ytShm;
- typedef union {
- float f;
- unsigned short s[2];
- }UNFI;
- bool load_shm()
- {
- int shmid;
- key_t key;
- if((key = ftok(SHM_PATH,(int)SHM_PORT))==-1)
- return false;
- if((shmid = shmget(key,sizeof(YT_UNIT_SHM),IPC_CREAT|0666))==-1)
- return false;
- ytShm = (YT_UNIT_SHM *)shmat(shmid,NULL,0);
- return true;
- }
- void qry_iot_param(json_object *body,int cid)
- {
- json_object *iotlist;
- iotlist = json_object_new_array();
- json_object *sp = json_object_new_object();
- json_object_object_add(sp,"field1",json_object_new_double(ytShm->spStatusList.spStatus[cid][1].REALTIME_VALUE));
- json_object_object_add(sp,"field2",json_object_new_double(ytShm->spStatusList.spStatus[cid][2].REALTIME_VALUE));
- json_object_object_add(sp,"field3",json_object_new_double(ytShm->spStatusList.spStatus[cid][3].REALTIME_VALUE));
- json_object_object_add(sp,"field4",json_object_new_double(ytShm->spStatusList.spStatus[cid][4].REALTIME_VALUE));
- json_object_object_add(sp,"field5",json_object_new_double(ytShm->spStatusList.spStatus[cid][5].REALTIME_VALUE));
- json_object_object_add(sp,"field6",json_object_new_double(ytShm->spStatusList.spStatus[cid][6].REALTIME_VALUE));
- json_object_object_add(sp,"field7",json_object_new_double(ytShm->spStatusList.spStatus[cid][7].REALTIME_VALUE));
- json_object_object_add(sp,"field8",json_object_new_double(ytShm->spStatusList.spStatus[cid][8].REALTIME_VALUE));
- json_object_object_add(sp,"field9",json_object_new_double(ytShm->spStatusList.spStatus[cid][9].REALTIME_VALUE));
- json_object_object_add(sp,"field10",json_object_new_double(ytShm->spStatusList.spStatus[cid][10].REALTIME_VALUE));
- json_object_object_add(sp,"field11",json_object_new_double(ytShm->spStatusList.spStatus[cid][11].REALTIME_VALUE));
- json_object_object_add(sp,"field12",json_object_new_double(ytShm->spStatusList.spStatus[cid][12].REALTIME_VALUE));
- json_object_object_add(sp,"field13",json_object_new_double(ytShm->spStatusList.spStatus[cid][13].REALTIME_VALUE));
- json_object_object_add(sp,"field14",json_object_new_double(ytShm->spStatusList.spStatus[cid][14].REALTIME_VALUE));
- json_object_object_add(sp,"field15",json_object_new_double(ytShm->spStatusList.spStatus[cid][15].REALTIME_VALUE));
- json_object_object_add(sp,"field16",json_object_new_double(ytShm->spStatusList.spStatus[cid][16].REALTIME_VALUE));
- json_object_object_add(sp,"field17",json_object_new_double(ytShm->spStatusList.spStatus[cid][17].REALTIME_VALUE));
- json_object_object_add(sp,"field18",json_object_new_double(ytShm->spStatusList.spStatus[cid][18].REALTIME_VALUE));
- json_object_object_add(sp,"field19",json_object_new_double(ytShm->spStatusList.spStatus[cid][19].REALTIME_VALUE));
- json_object_object_add(sp,"field20",json_object_new_double(ytShm->spStatusList.spStatus[cid][20].REALTIME_VALUE));
- json_object_object_add(sp,"field21",json_object_new_double(ytShm->spStatusList.spStatus[cid][21].REALTIME_VALUE));
- json_object_object_add(sp,"field22",json_object_new_double(ytShm->spStatusList.spStatus[cid][22].REALTIME_VALUE));
- json_object_object_add(sp,"field23",json_object_new_double(ytShm->spStatusList.spStatus[cid][23].REALTIME_VALUE));
- json_object_object_add(sp,"field24",json_object_new_double(ytShm->spStatusList.spStatus[cid][24].REALTIME_VALUE));
- json_object_object_add(sp,"field25",json_object_new_double(ytShm->spStatusList.spStatus[cid][25].REALTIME_VALUE));
- json_object_object_add(sp,"field26",json_object_new_double(ytShm->spStatusList.spStatus[cid][26].REALTIME_VALUE));
- json_object_object_add(sp,"field27",json_object_new_double(ytShm->spStatusList.spStatus[cid][27].REALTIME_VALUE));
- json_object_object_add(sp,"field28",json_object_new_double(ytShm->spStatusList.spStatus[cid][28].REALTIME_VALUE));
- json_object_object_add(sp,"field29",json_object_new_double(ytShm->spStatusList.spStatus[cid][29].REALTIME_VALUE));
- json_object_object_add(sp,"field30",json_object_new_double(ytShm->spStatusList.spStatus[cid][30].REALTIME_VALUE));
- json_object_object_add(sp,"field31",json_object_new_double(ytShm->spStatusList.spStatus[cid][31].REALTIME_VALUE));
- json_object_array_add(iotlist,sp);
- json_object_object_add(body,"IOT_LIST",iotlist);
- }
- void set_real_value(json_object *param,int cid)
- {
- uint t = 0;
- uint timestamp = 0;
- json_object_object_foreach(param,key,val){
- if(strcmp(key,"timestamp")==0){
- t=json_object_get_int(val);
- timestamp=json_object_get_int(val);
- }else if(strcmp(key,"field16")==0){
- ytShm->spStatusList.spStatus[cid][16].REALTIME_VALUE=json_object_get_double(val);
- ytShm->spStatusList.spStatus[cid][16].REALTIME_TIMESTAMP=t;
- }else if(strcmp(key,"field17")==0){
- ytShm->spStatusList.spStatus[cid][17].REALTIME_VALUE=json_object_get_double(val);
- ytShm->spStatusList.spStatus[cid][17].REALTIME_TIMESTAMP=t;
- }else if(strcmp(key,"field18")==0){
- ytShm->spStatusList.spStatus[cid][18].REALTIME_VALUE=json_object_get_double(val);
- ytShm->spStatusList.spStatus[cid][18].REALTIME_TIMESTAMP=t;
- }else if(strcmp(key,"field19")==0){
- ytShm->spStatusList.spStatus[cid][19].REALTIME_VALUE=json_object_get_double(val);
- ytShm->spStatusList.spStatus[cid][19].REALTIME_TIMESTAMP=t;
- }else if(strcmp(key,"field20")==0){
- ytShm->spStatusList.spStatus[cid][20].REALTIME_VALUE=json_object_get_double(val);
- ytShm->spStatusList.spStatus[cid][20].REALTIME_TIMESTAMP=t;
- }else if(strcmp(key,"field21")==0){
- ytShm->spStatusList.spStatus[cid][21].REALTIME_VALUE=json_object_get_double(val);
- ytShm->spStatusList.spStatus[cid][21].REALTIME_TIMESTAMP=t;
- }else if(strcmp(key,"field22")==0){
- ytShm->spStatusList.spStatus[cid][22].REALTIME_VALUE=json_object_get_double(val);
- ytShm->spStatusList.spStatus[cid][22].REALTIME_TIMESTAMP=t;
- }else if(strcmp(key,"field23")==0){
- ytShm->spStatusList.spStatus[cid][23].REALTIME_VALUE=json_object_get_double(val);
- ytShm->spStatusList.spStatus[cid][23].REALTIME_TIMESTAMP=t;
- }else if(strcmp(key,"field24")==0){
- ytShm->spStatusList.spStatus[cid][24].REALTIME_VALUE=json_object_get_double(val);
- ytShm->spStatusList.spStatus[cid][24].REALTIME_TIMESTAMP=t;
- }else if(strcmp(key,"field25")==0){
- ytShm->spStatusList.spStatus[cid][25].REALTIME_VALUE=json_object_get_double(val);
- ytShm->spStatusList.spStatus[cid][25].REALTIME_TIMESTAMP=t;
- }else if(strcmp(key,"field26")==0){
- ytShm->spStatusList.spStatus[cid][26].REALTIME_VALUE=json_object_get_double(val);
- ytShm->spStatusList.spStatus[cid][26].REALTIME_TIMESTAMP=t;
- }else if(strcmp(key,"field27")==0){
- ytShm->spStatusList.spStatus[cid][27].REALTIME_VALUE=json_object_get_double(val);
- ytShm->spStatusList.spStatus[cid][27].REALTIME_TIMESTAMP=t;
- }else if(strcmp(key,"field28")==0){
- ytShm->spStatusList.spStatus[cid][28].REALTIME_VALUE=json_object_get_double(val);
- ytShm->spStatusList.spStatus[cid][28].REALTIME_TIMESTAMP=t;
- }else if(strcmp(key,"field29")==0){
- ytShm->spStatusList.spStatus[cid][29].REALTIME_VALUE=json_object_get_double(val);
- ytShm->spStatusList.spStatus[cid][29].REALTIME_TIMESTAMP=t;
- }else if(strcmp(key,"field30")==0){
- ytShm->spStatusList.spStatus[cid][30].REALTIME_VALUE=json_object_get_double(val);
- ytShm->spStatusList.spStatus[cid][30].REALTIME_TIMESTAMP=t;
- }else if(strcmp(key,"field31")==0){
- ytShm->spStatusList.spStatus[cid][31].REALTIME_VALUE=json_object_get_double(val);
- ytShm->spStatusList.spStatus[cid][31].REALTIME_TIMESTAMP=t;
- }
- }
- }
- int main(void)
- {
- char *input,*lenstr,*data;
- int i;
- int cmd=-1,cid=-1,pno=-1,act=-1,type=-1;
- int wait=50;
- json_object *json,*q_json,*json_body,*json_param;
- int len;
- bool hadparam=false;
- printf("%s%c%c\n","Content-Type:text/html;charset=UTF-8",13,10);
- json = json_object_new_object();
- json_object_object_add(json,"VER",json_object_new_string("1.00"));
- json_object_object_add(json,"NAME",json_object_new_string("Dmp_IoT-01"));
- lenstr = getenv("CONTENT_LENGTH");
- if(lenstr){
- len = atoi(lenstr);
- if((len>0)&&(len<1024)){
- input = (char *)malloc((unsigned)(len+2));
- data = (char *)malloc((unsigned)(len+2));
- fgets(input,len+1,stdin);
- for(i=0;i<len-10;i++)
- data[i] = input[10+i];
- // printf("%s\n",data);
- if(load_shm()){
- q_json = json_tokener_parse(data);
- json_object_object_foreach(q_json,key,val){
- if(strcmp(key,"CMD")==0){
- cmd = json_object_get_int(val);
- }else if(strcmp(key,"COMMID")==0){
- cid = json_object_get_int(val);
- }else if(strcmp(key,"PARAM")==0){
- json_param=val;
- hadparam=true;
- }
- }
- if((cid>=0)&&(cid<4)&&(cmd>0)){
- switch(cmd){
- case 1:
- if(hadparam){
- json_object_object_add(json,"RESULT",json_object_new_string("1"));
- set_real_value(json_param,cid);
- }else{
- json_object_object_add(json,"RESULT",json_object_new_string("0"));
- }
- break;
- case 2:
- json_body = json_object_new_object();
- qry_iot_param(json_body,cid);
- json_object_object_add(json,"RESULT",json_body);
- break;
- default:break;
- }
- }else{
- json_object_object_add(json,"RESULT",json_object_new_string("0"));
- json_object_object_add(json,"MESSAGE",json_object_new_string("Params Error."));
- }
- shmdt(ytShm);
- }else{
- json_object_object_add(json,"RESULT",json_object_new_string("0"));
- json_object_object_add(json,"MESSAGE",json_object_new_string("Load Share Memory failed."));
- }
- json_object_object_add(json,"VER",json_object_new_string("1.00"));
- json_object_object_add(json,"REPLY",json_object_new_string("1"));
- free(data);
- free(input);
- }else{
- json_object_object_add(json,"RESULT",json_object_new_string("0"));
- json_object_object_add(json,"MESSAGE",json_object_new_string("Length error."));
- }
- }else{
- json_object_object_add(json,"RESULT",json_object_new_string("0"));
- json_object_object_add(json,"MESSAGE",json_object_new_string("No Content error."));
- }
- printf("%s",json_object_to_json_string(json));
- json_object_put(json);
- return 0;
- }
|