使用go-zero微服务框架实现云监控后台(三.终端状态更新json文件实现)

Posted 特立独行的猫a

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用go-zero微服务框架实现云监控后台(三.终端状态更新json文件实现)相关的知识,希望对你有一定的参考价值。

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

终端应用程序定时更新状态文件,应用中跑的另一个后台服务则定时读取该状态文件并上送至后台服务。以此无耦合的实现对终端的状态监控。参见:https://blog.csdn.net/yyz_1987/article/details/118358038

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

//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");

以上是关于使用go-zero微服务框架实现云监控后台(三.终端状态更新json文件实现)的主要内容,如果未能解决你的问题,请参考以下文章

go-zero微服务框架rpc的使用

一文读懂云原生 go-zero 微服务框架

Go-Zero - 集成了各种工程实践的 Web 和 RPC 微服务框架

做好微服务的难题,被rpc框架go-zero解决,免你五年功

自适应微服务治理背后的算法

微服务从代码到k8s部署应有尽有系列全集