关键词:
这是我计划的终端状态监控服务的终端部分的模块组件。
终端应用程序定时更新状态文件,应用中跑的另一个后台服务则定时读取该状态文件并上送至后台服务。以此无耦合的实现对终端的状态监控。参见: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微服务框架rpc的使用(代码片段)
...e/details/118358038以终端状态上送监控服务为例,记录下go-zero微服务的简单使用,实现一个简易的后台监控云服务,监控所有出厂状态上报的终端状态。新建一个Golang服务后台项目代码的目录, 查看详情
自适应微服务治理背后的算法(代码片段)
前言go-zero群里经常有同学问:服务监控是通过什么算法实现的?滑动窗口是怎么工作的?能否讲讲这块的原理?熔断算法是怎么设计的?为啥没有半开半闭状态呢?本篇文章,来分析一下go-zero中指标统计背后的实现算法和逻辑... 查看详情
go语言十一大主流微服务框架
...ff08;15.8K)官方文档地址: https://go-kratos.dev/docs/4.Go-zero项目简介:go-zero是一个集成 查看详情
微服务从代码到k8s部署应有尽有系列全集(代码片段)
...志到监控等各个方面的微服务完整实践。整个项目使用了go-zero开发的微服务,基本包含了go-zero以及相关go-zero作者开发的一些中间件,所用到的技术栈基本是go-zero项目组的自研组件,基本是go-zero全家桶了。实战项目地址:https:/... 查看详情
使用springbootactuator监控应用
微服务的特点决定了功能模块的部署是分布式的,大部分功能模块都是运行在不同的机器上,彼此通过服务调用进行交互,前后台的业务流会经过很多个微服务的处理和传递,出现了异常如何快速定位是哪个环节出现了问题?在... 查看详情
微服务架构:基于微服务和docker容器技术的paas云平台架构设计
...的,别担心,业界已经有非常优秀的开源框架供我们参考使用。目前业界比较成熟的微服务框架有Netflix、SpringCloud和阿里的Dubbo等。SpringCloud是基于SpringBoot的一整套实现微服务的框架,它提供了开发微服务所需的组件,跟SpringBoot... 查看详情
go-zero微服务开发环境搭建(代码片段)
原创不易,未经允许,请勿转载。更新于2022-03-04,增加插件安装文章目录一、Docker以及mysql、redis等软件的安装二、安装protoc-gen-go三、安装protoc四、安装goctl工具五、GoLand插件安装一、Docker以及mysql、redis等软件的安装... 查看详情
go-zero微服务开发环境搭建(代码片段)
原创不易,未经允许,请勿转载。更新于2022-03-04,增加插件安装文章目录一、Docker以及mysql、redis等软件的安装二、安装protoc-gen-go三、安装protoc四、安装goctl工具五、GoLand插件安装一、Docker以及mysql、redis等软件的安装... 查看详情
springboot(十九):使用springbootactuator监控应用
springBoot(十九):使用SpringBootActuator监控应用微服务的特点决定了功能模块的部署是分布式的,大部分功能模块都是运行在不同的机器上,彼此通过服务调用进行交互,前后台的业务流会经过很多个微服务的处理和传递,出现了异... 查看详情
微服务从代码到k8s部署应有尽有大结局(k8s部署)(代码片段)
...志到监控等各个方面的微服务完整实践。整个项目使用了go-zero开发的微服务,基本包含了go-zero以及相关go-zero作者开发的一些中间件,所用到的技术栈基本是go-zero项目组的自研组件,基本是go-zero全家桶了。实战项目地址:https:/... 查看详情
go-zero成长之路—微服务电商实战系列(六条件查询)(代码片段)
...楚的,可通过如下传送门查看该系列其他文章:go-zero成长之路—微服务电商实战系列(五、RPC定义)go-zero成长之路—微服务电商实战系列(四、API定义)go-zero成长之路—微服务电商实战系列(三、表... 查看详情
springcloud微服务分布式云架构简介
...pringBoot的开发模式简化了分布式系统基础设施的开发,如服务发现、注册、配置中心、消息总线、负载均衡、断路器、数据监控等(这里只简单的列了一部分),都可以用SpringBoot的开发风格做到一键启动和部署。SpringCloud将目前... 查看详情
使用springcloudsleuth实现微服务跟踪(代码片段)
使用SpringCloudSleuth实现微服务跟踪之前已经了解了几种监控微服务的方式,例如使用SpringBootActuator监控微服务示例,使用Hystrix监控HystrixCommand等。为什么要实现微服务追踪PeterDeutsch的分布式计算的八大误区。网络可靠延迟... 查看详情
jeespringcloud-互联网云快速开发框架
...考技术A(一款免费开源的JAVA互联网云快速开发平台)微服务分布式代码生成的敏捷开发系统架构。项目代码简洁,注释丰富,上手容易,还同时集中分布式、微服务,同时包含许多基础模块和监控、服务模块。演示版地址:http://bknfd... 查看详情
十次方基础教程(后台)influxdbcadvisorcadvisor的安装与使用(监控微服务内存,自动扩容)
首先拉取镜像并安装(expose表示暴露端口)dockerpulltutum/influxdb dockerrun-di-p8083:8083-p8086:8086--expose8090--expose8099--nameinfluxsrvtutum/influxdb用浏览器打开http://192.168.79.131:8083/操作时候的语句不要自己写,点右下角(图上查 查看详情
springcloud微服务分布式云架构-springcloud简介
...pringBoot的开发模式简化了分布式系统基础设施的开发,如服务发现、注册、配置中心、消息总线、负载均衡、断路器、数据监控等(这里只简单的列了一部分),都可以用SpringBoot的开发风格做到一键启动和部署。SpringCloud将目前... 查看详情
springcloud微服务分布式云架构-springcloud简介
...pringBoot的开发模式简化了分布式系统基础设施的开发,如服务发现、注册、配置中心、消息总线、负载均衡、断路器、数据监控等(这里只简单的列了一部分),都可以用SpringBoot的开发风格做到一键启动和部署。SpringCloud将目前... 查看详情
springcloud微服务分布式云架构-springcloud简介
...pringBoot的开发模式简化了分布式系统基础设施的开发,如服务发现、注册、配置中心、消息总线、负载均衡、断路器、数据监控等(这里只简单的列了一部分),都可以用SpringBoot的开发风格做到一键启动和部署。SpringCloud将目前... 查看详情