使用go-zero微服务框架实现云监控后台(三.c语言操作cJson封装,json和结构体互转)

简介: 使用go-zero微服务框架实现云监控后台(三.c语言操作cJson封装,json和结构体互转)

c语言操作cJson封装,以及json和结构体互转。如果不做封装直接操作cJSON有点儿繁琐。


所以封装下是很有必要的,使用起来简单好用多了。


这是我计划的终端状态监控服务的终端部分的模块组件。


终端应用程序定时更新状态文件,应用中跑的另一个后台服务则定时读取该状态文件并上送至后台服务。以此无耦合的实现对终端的状态监控。参见:终端出厂后自动化运维方案_独行猫A 的沉淀、积累、总结。天天学习,好好向上...linux,Android,Vue,Go)-CSDN博客


下面是c语言读写json文件的简单封装,完成c语言结构体到json文件,json到结构体的转换。


#include "jsonopt.h"
/**
 * Function:GetArrayValueString
 * Data:2021-11-11
 * Author:yangyongzhen
 * Description: 从json中获取ArrayItem内容
 */
int GetArrayValueString(cJSON* json,int id, char* name, char* param){
    cJSON* node;
    node = cJSON_GetArrayItem(json,id);
    if(!node){
        return -1;
    }
    sprintf(param, "%s", cJSON_GetObjectItem(node, name)->valuestring);
    return 0 ;
}
/**
 * Function:GetValueString
 * Data:2021-11-11
 * Author:yangyongzhen
 * Description: 从json中读取字符串内容
 */
char* GetValueString(cJSON* json,char* name){
  cJSON* pObj = NULL;
    pObj = cJSON_GetObjectItem(json,name);
    if(pObj != NULL){
      return pObj->valuestring;
    }else{
      return NULL;
    }
}
/**
 * Function:GetValueInt
 * Data:2021-11-11
 * Author:yangyongzhen
 * Description: 从json中读取int类型值
 */
int GetValueInt(cJSON* json,char* name){
  cJSON* pObj = NULL;
    pObj = cJSON_GetObjectItem(json,name);
    if(pObj != NULL){
      return pObj->valueint;
    }else{
      return 0;
    }
}
/**
 * Function:SetValueString
 * Data:2021-11-11
 * Author:yangyongzhen
 * Description: 向json中的String类型字段赋值
 */
int SetValueString(cJSON* json,char* name,char* value){
  //printf("set key:%s,val:%s\n",name,value);
  if(cJSON_GetObjectItem(json,name)->valuestring != NULL){
    cJSON_ReplaceItemInObject(json,name,cJSON_CreateString(value));
  }else{
    //cJSON_AddItemToObject(json, name, cJSON_CreateString(value)); 
    cJSON_AddStringToObject(json, name, value);  
  }
  return 0;
}
/**
 * Function:SetValueInt
 * Data:2021-11-11
 * Author:yangyongzhen
 * Description: 向json中的Int类型字段赋值
 */
int SetValueInt(cJSON* json,char* name,int value){
  cJSON* pObj = NULL;
  pObj = cJSON_GetObjectItem(json,name);
  if(pObj != NULL){
    pObj->valueint = value;
    pObj->valuedouble = value;
    return 0;
  }else{
    cJSON_AddNumberToObject(json, name, value); 
  }
  return 0;
}


/**
   * Copyright (C), 2021-2021,yangyongzhen 
   * File name:  jsonopt.h 
   * Author:   yangyongzhen
   * Version:  0.1
   * Date:                   2021-11-11 
   * Description:   c语言的cJSON操作封装
 **/
#ifndef _JSON_OPT_H_
#define _JSON_OPT_H_
#include <string.h>
#include "cJSON.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/**
 * Function:S2J_setInt
 * Description:Struct向Json的Int类型赋值
 * */
#define S2J_setInt(json,stru,element) SetValueInt(json,#element,(stru)->element)  
/**
 * Function:S2J_setString
 * Description:Struct向Json的String类型赋值
 * */
#define S2J_setString(json,stru,element) SetValueString(json,#element,(stru)->element)  
/**
 * Function:J2S_getString
 * Description:Json向Struct中的String类型赋值
 * */
#define J2S_getString(json,stru,element) {char *item=NULL; item = GetValueString(json,#element); if(item!=NULL){strcpy((stru)->element,item);}}
/**
 * Function:J2S_getInt
 * Description:Json向Struct中的Int类型赋值
 * */
#define J2S_getInt(json,stru,element) (stru)->element = GetValueInt(json,#element)
/**
 * Function:GetArrayValueString
 * Data:2021-11-11
 * Author:yangyongzhen
 * Description: 从json中获取ArrayItem内容
 */
extern int GetArrayValueString(cJSON* json,int id, char* name, char* param);
/**
 * Function:GetValueString
 * Data:2021-11-11
 * Author:yangyongzhen
 * Description: 从json中读取字符串内容
 */
extern char* GetValueString(cJSON* json,char* name);
/**
 * Function:GetValueInt
 * Data:2021-11-11
 * Author:yangyongzhen
 * Description: 从json中读取int类型值
 */
extern int GetValueInt(cJSON* json,char* name);
/**
 * Function:SetValueString
 * Data:2021-11-11
 * Author:yangyongzhen
 * Description: 向json中的String类型字段赋值
 */
extern int SetValueString(cJSON* json,char* name,char* value);
/**
 * Function:SetValueInt
 * Data:2021-11-11
 * Author:yangyongzhen
 * Description: 向json中的Int类型字段赋值
 */
extern int SetValueInt(cJSON* json,char* name,int value);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif


使用方法举例:


#include "jsonopt.h"
static cJSON* root;
PosStatus posStatus;
/**
从json文件中加载信息到结构体
*/
int LoadStatus(PosStatus *status){
  printf("LoadStatus ENTER\n");
  root = GetJsonObject(STATUS_FILE_NAME);
  char * out = cJSON_Print(root);
  printf("LoadStatus:json=%s\n",out);
    if(out == NULL){
        printf("LoadStatus error,no json data\n");
        return -1;
    }
  J2S_getString(root,status,sn);
  printf("[item] = %s\n",status->sn);
    J2S_getString(root,status,posno);
  printf("[item] = %s\n",status->posno);
  J2S_getString(root,status,city);
  printf("[item] = %s\n",status->city);
  J2S_getString(root,status,tyid);
  printf("[item] = %s\n",status->tyid);
  J2S_getString(root,status,sver);
  printf("[item] = %s\n",status->sver);
  J2S_getString(root,status,ctime);
  printf("[item] = %s\n",status->ctime);
    //item = GetValueString(root,"posno");
    //if(item != NULL){
    //    strcpy(status->posno,item);
    //    printf("[item] = %s\n",status->posno);
    //}
  return 0;
}
/**
结构体信息保存为json文件
*/
int SaveStatus(PosStatus status){
  char * out = NULL;
    printf("SaveStatus ENTER\n");
  if(root == NULL){
    printf("SaveStatus error\n");
    return -1;
  }
  S2J_setString(root,&status,sn);
  S2J_setString(root,&status,posno);
  S2J_setString(root,&status,tyid);
  S2J_setString(root,&status,city);
  S2J_setString(root,&status,sver);
  S2J_setInt(root,&status,unum1);
  S2J_setInt(root,&status,unum2);
  S2J_setInt(root,&status,amount);
  S2J_setInt(root,&status,count);
  S2J_setInt(root,&status,line);
  S2J_setString(root,&status,carno);
  S2J_setString(root,&status,jd);
  S2J_setString(root,&status,wd);
  S2J_setInt(root,&status,alarm);
  S2J_setInt(root,&status,pver);
  S2J_setInt(root,&status,bver);
  S2J_setString(root,&status,empid);
  S2J_setString(root,&status,rvs1);
  S2J_setString(root,&status,rvs2);
  S2J_setString(root,&status,stime);
  S2J_setString(root,&status,ctime);
  S2J_setInt(root,&status,tenant);
  //SetValueString(root,"carno",status.carno);
  //SetValueString(root,"jd",status.jd);
  //SetValueString(root,"wd",status.wd);
  printf("SaveStatus end\n");
  out = cJSON_Print(root);
  writeFile(STATUS_FILE_NAME,out);
  printf("SetStatus:json=%s\n",out);
  return 0;
}


//status.h头文件
#ifndef _STATUS_H_
#define _STATUS_H_
#include  "cJSON/cJSON.h"
//保存位置 终端状态监控文件存储位置
#define STATUS_FILE_NAME  "../opt/status.json"
//#define STATUS_FILE_NAME  "./status.json"
//终端设备状态信息
typedef struct _PosStatus{
  char sn[32];     //设备唯一号
    char posno[16];  //终端编号
    char city[10];   //城市代码
    char tyid[10];   //终端类型
    char sver[32];   //终端版本号
    int  unum1;      //未传记录数量--公交
    int  unum2;      //未传记录数量--三方
    char ndate[16];  //当前日期
    char ntime[16];  //当前时间
    int  amount;     //当班总额
    int  count;      //当班人数
    int  line;       //线路号
    char carno[16];  //车辆编号
    char jd[20];     //经度
    char wd[20];     //维度
    int  alarm;      //设备报警码
    char stime[16];  //开机时间
    char ctime[16];  //关机时间
    int  tenant;     //租户ID
}PosStatus;
extern PosStatus posStatus;
extern int LoadStatus(PosStatus *status);
extern int SaveStatus(PosStatus status);
extern int SetValueInt(cJSON* root,char* name,int value);
extern int SetValueString(cJSON* root,char* name,char* value);
extern char* GetValueString(cJSON* json,char* name);
extern int GetValueInt(cJSON* json,char* name);
extern cJSON* GetJsonObject(char* fileName);
extern int writeFile(char *filename,char *data);
#endif


static.c文件内容:


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "status.h"
static cJSON* root;
PosStatus posStatus;
/**
从文件中读取json信息到结构体
*/
int LoadStatus(PosStatus *status){
  root = GetJsonObject(STATUS_FILE_NAME);
  char * out = cJSON_Print(root);
  printf("LoadStatus:json=%s\n",out);
    if(out == NULL){
        printf("LoadStatus error,no json data\n");
        return -1;
    }
    char * item = NULL;
    item = GetValueString(root,"sn");
    if(item != NULL){
        strcpy(status->sn,item);
        printf("[item] = %s\n",status->sn);
    }
    item = GetValueString(root,"posno");
    if(item != NULL){
        strcpy(status->posno,item);
        printf("[item] = %s\n",status->posno);
    }
    item = GetValueString(root,"city");
    if(item != NULL){
        strcpy(status->city,item);
        printf("[item] = %s\n",status->city);
    }
    item = GetValueString(root,"tyid");
    if(item != NULL){
        strcpy(status->tyid,item);
        printf("[item] = %s\n",status->tyid);
    }
    item = GetValueString(root,"sver");
    if(item != NULL){
        strcpy(status->sver,item);
        printf("[item] = %s\n",status->sver);
    }
  return 0;
}
/**
结构体信息写入json文件
*/
int SaveStatus(PosStatus status){
  char * out = NULL;
    printf("SaveStatus ENTER\n");
  if(root == NULL){
    printf("SaveStatus error\n");
    return -1;
  }
    SetValueString(root,"sn",status.sn);
    SetValueString(root,"posno",status.posno);
    SetValueString(root,"tyid",status.tyid);
  SetValueString(root,"city",status.city);
    SetValueString(root,"sver",status.sver);
  SetValueInt(root,"unum1",status.unum1);
    SetValueInt(root,"unum2",status.unum2);
    SetValueInt(root,"amount",status.amount);
    SetValueInt(root,"count",status.count);
    SetValueInt(root,"line",status.line);
  SetValueString(root,"carno",status.carno);
    SetValueString(root,"jd",status.jd);
    SetValueString(root,"wd",status.wd);
    SetValueInt(root,"alarm",status.alarm);
    SetValueString(root,"stime",status.stime);
    SetValueString(root,"ctime",status.ctime);
    SetValueInt(root,"tenant",status.tenant);
  out = cJSON_Print(root);
  writeFile(STATUS_FILE_NAME,out);
  printf("SetStatus:json=%s\n",out);
  return 0;
}
int writeFile(char *filename,char *data){
  FILE *fp = NULL;
  fp = fopen(filename,"w+");
  if(fp == NULL){
    fprintf(stderr,"open file failed\n");
    return -1;
  }
  fprintf(fp,"%s",data);
  if(fp != NULL){
    fclose(fp);
  }
  return 0;
}
cJSON* GetJsonObject(char* fileName){
    long len;
    char* pContent;
    int tmp;
    cJSON* json;
    FILE* fp = fopen(fileName, "rb+");
    if(!fp){
        printf("open file error\n");
        return NULL;
    }
    printf("open file success\n");
    fseek(fp,0,SEEK_END);
    len=ftell(fp);
    if(0 == len){
        printf("file len is 0\n");
        return NULL;
    }
    fseek(fp,0,SEEK_SET);
    pContent = (char*) malloc (sizeof(char)*len);
    tmp = fread(pContent,1,len,fp);
    fclose(fp);
    json = cJSON_Parse(pContent);
    if (!json)
    {
        free(pContent);
        return NULL;
    }
    free(pContent);
    printf("read file ok\n");
    return json;
}
//读取cJSON索引为index的结点某个key值对应的value,索引从0开始
int GetArrayValueString(cJSON* json,int id, char* name, char* param){
    cJSON* node;
    node = cJSON_GetArrayItem(json,id);
    if(!node){
        return -1;
    }
    sprintf(param, "%s", cJSON_GetObjectItem(node, name)->valuestring);
    return 0 ;
}
char* GetValueString(cJSON* json,char* name){
  cJSON* pObj = NULL;
    pObj = cJSON_GetObjectItem(json,name);
    if(pObj != NULL){
      return pObj->valuestring;
    }else{
      return NULL;
    }
}
int GetValueInt(cJSON* json,char* name){
  cJSON* pObj = NULL;
    pObj = cJSON_GetObjectItem(json,name);
    if(pObj != NULL){
      return pObj->valueint;
    }else{
      return 0;
    }
}
int SetValueString(cJSON* json,char* name,char* value){
  strcpy(cJSON_GetObjectItem(json,name)->valuestring,value);
  return 0;
}
int SetValueInt(cJSON* json,char* name,int value){
  cJSON_GetObjectItem(json,name)->valueint = value;
  cJSON_GetObjectItem(json,name)->valuedouble = value;
  return 0;
}
/*
int main(){
  printf("cjson test,read and save\n");
  LoadStatus(&posStatus);
    strcpy(posStatus.sver,"NC_B503_YYZ");
    posStatus.alarm = 6;
    posStatus.amount = 10;
    posStatus.count = 12;
    strcpy(posStatus.posno,"413101010E");
  SaveStatus(posStatus);
}
*/


生成的json文件如:


{
  "sn": "00a0c6000023",
  "posno":"413101010E",
  "city": "",
  "tyid": "",
  "sver": "NC_B503_YYZ",
  "unum1":  0,
  "unum2":  0,
  "ndate":  "2021-08-11",
  "ntime":  "17:20:30",
  "amount": 10,
  "count":  12,
  "line": 0,
  "carno":  "",
  "jd": "",
  "wd": "",
  "alarm":  6,
  "stime":  "",
  "ctime":  "",
  "tenant": 0
}


附带一个终端开机自启动脚本startmonitor.sh:


#!/bin/bash
fileName="/app/city_app/opt/monitor"
buzzGPIO="/sys/class/gpio/gpio15"
#enable buzzse for notifying success
function beep_notify()
{
   if [ ! -d "$buzzGPIO" ]; then
        echo 15 > /sys/class/gpio/export
   fi
   if [ -d "$buzzGPIO" ]; then
        echo "out" > "/sys/class/gpio/gpio15/direction"
        echo "1" > "/sys/class/gpio/gpio15/value"
        sleep 1
        echo "0" > "/sys/class/gpio/gpio15/value"
   fi
}
function CheckProcess()
{
  PROCESS_NUM=`ps | grep "$1" | grep -v "grep" | wc -l`
  return $PROCESS_NUM
}
if [ ! -f $fileName ]; then  
      echo "error!monitor file not exit!"
        exit 1
    else 
  echo "find monitor file,begin start..."
    CheckProcess qrlinux
    if [ $? -eq 0 ];then
    echo "no monitor progress find!"
    else
    echo "find monitor,..."
      killall -9 monitor
        sleep 1
        fi 
        cd /app/city_app/opt/
      ./monitor -monitorcfg=./monitorcfg.ini &  
      echo "start ok"
      beep_notify
      exit 0
fi


终端开机自启动服务应用的方法:


system("killall -9 monitor");
 system("../opt/startmonitor.sh");
相关文章
|
6月前
|
监控 算法 NoSQL
Go 微服务限流与熔断最佳实践:滑动窗口、令牌桶与自适应阈值
🌟蒋星熠Jaxonic:Go微服务限流熔断实践者。分享基于滑动窗口、令牌桶与自适应阈值的智能防护体系,助力高并发系统稳定运行。
Go 微服务限流与熔断最佳实践:滑动窗口、令牌桶与自适应阈值
|
编译器 Go
揭秘 Go 语言中空结构体的强大用法
Go 语言中的空结构体 `struct{}` 不包含任何字段,不占用内存空间。它在实际编程中有多种典型用法:1) 结合 map 实现集合(set)类型;2) 与 channel 搭配用于信号通知;3) 申请超大容量的 Slice 和 Array 以节省内存;4) 作为接口实现时明确表示不关注值。此外,需要注意的是,空结构体作为字段时可能会因内存对齐原因占用额外空间。建议将空结构体放在外层结构体的第一个字段以优化内存使用。
|
JSON Java 数据格式
微服务——SpringBoot使用归纳——Spring Boot返回Json数据及数据封装——封装统一返回的数据结构
本文介绍了在Spring Boot中封装统一返回的数据结构的方法。通过定义一个泛型类`JsonResult&lt;T&gt;`,包含数据、状态码和提示信息三个属性,满足不同场景下的JSON返回需求。例如,无数据返回时可设置默认状态码&quot;0&quot;和消息&quot;操作成功!&quot;,有数据返回时也可自定义状态码和消息。同时,文章展示了如何在Controller中使用该结构,通过具体示例(如用户信息、列表和Map)说明其灵活性与便捷性。最后总结了Spring Boot中JSON数据返回的配置与实际项目中的应用技巧。
916 0
|
Shell Go 开发工具
【环境】Rocky8使用gvm配置Go多版本管理的微服务开发环境(go-zero)
通过本文的介绍,我们详细讲解了如何在Rocky8上使用gvm来管理多个Go版本,并配置go-zero框架的开发环境。通过gvm的灵活管理,开发者可以轻松切换不同的Go版本,以适应不同项目的需求。同时,go-zero框架的使用进一步提升了微服务开发的效率和质量。希望本文能帮助开发者构建高效的Go语言开发环境,提高项目开发的灵活性和稳定性。
434 63
|
11月前
|
JSON Go C语言
Go语言之定义结构体(Struct)-《Go语言实战指南》
Go 语言中的结构体(`struct`)是一种复合数据类型,可将多个不同类型的字段组合成一个类型。本文介绍了结构体的基本定义、实例创建方式、字段访问与修改、零值特性、比较规则、嵌套使用及标签功能。通过示例代码详细讲解了如何定义和操作结构体,以及其在 JSON 编码等场景的应用。
|
11月前
|
人工智能 数据可视化 JavaScript
颠覆开发效率!国内首个微服务编排框架Juggle开源啦!
Juggle是国内首个开源的微服务编排框架,专注于解决企业微服务进程中接口重复开发、系统对接复杂等问题。它提供零代码、低代码和AI增强功能,通过可视化拖拽快速组装简单API为复杂接口,支持多协议、多语言脚本和流程多版本管理。相比国外框架如Conductor,Juggle更贴合国内需求,具备高效开发、企业级可靠性及信创适配等优势,助力企业实现敏捷创新与数字化转型。
颠覆开发效率!国内首个微服务编排框架Juggle开源啦!
|
程序员 Go
go语言中结构体(Struct)
go语言中结构体(Struct)
408 71
|
监控 Go API
Go语言在微服务架构中的应用实践
在微服务架构的浪潮中,Go语言以其简洁、高效和并发处理能力脱颖而出,成为构建微服务的理想选择。本文将探讨Go语言在微服务架构中的应用实践,包括Go语言的特性如何适应微服务架构的需求,以及在实际开发中如何利用Go语言的特性来提高服务的性能和可维护性。我们将通过一个具体的案例分析,展示Go语言在微服务开发中的优势,并讨论在实际应用中可能遇到的挑战和解决方案。
|
编译器 Go
探索 Go 语言中的内存对齐:为什么结构体大小会有所不同?
在 Go 语言中,内存对齐是优化内存访问速度的重要概念。通过调整数据在内存中的位置,编译器确保不同类型的数据能够高效访问。本文通过示例代码展示了两个结构体 `A` 和 `B`,尽管字段相同但排列不同,导致内存占用分别为 40 字节和 48 字节。通过分析内存布局,解释了内存对齐的原因,并提供了优化结构体字段顺序的方法,以减少内存填充,提高性能。
192 3
|
分布式计算 Java 持续交付
如何选择合适的微服务框架
如何选择合适的微服务框架
395 0