EdgeGateway_FSU/DevicePortGet/Public_Src/CjsonCreate_Init.c

685 lines
31 KiB
C
Raw Normal View History

2024-03-15 17:25:04 +08:00
/***************************************************************
Copyright © huijue Network Co., Ltd. 1998-2129. All rights reserved.
Copyright © 1998-2129. All rights reserved.
: CjsonCreate_Init.c
: kooloo
: V1.0
: /FSU Cjson kooloo add 202310
: iMX6ULL
: linux-imx-4.1.15-2.1.0-g3dc0a4b-v2.7
gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf
: V1.0 2023/7/15 kooloo
***************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include "public.h" //公共函数头文件
#include "mslog.h"
#include "ProcessisRunning.h"
#include <fcntl.h>
#include <sys/mman.h> //posix 内存共享 消息队列
#include <sys/stat.h>
#include<mqueue.h>
#include<CjsonCreate_Init.h> //添加头文件 kooloo add 202311
#include "cJSON.h" //cjson 表
#include <FuncConfigTypeID_Public.h>
/* cJSON 填坑指南
* Cjson 使 cJSON_Parse cJSON_Createxxx 使 cJSON_Delete
* 使使
* cJSON_Print 使 cJSON_free kooloo add 202312
* cJSON_DeleteItemFromObject cJSON_Createxxx 使 cJSON_Delete
*
*
*
*
*
* /
//#include "sqlite3.h"
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
** : CjsonCreate_FSUSelf_Init
** : FSU
** :
**   : 2023902
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
cJSON* CjsonCreate_FSUSelf_Init(void)
{
//创建根节点JSON(最外面大括号的JSON对象)
cJSON* jsonnode = cJSON_CreateObject();
//FSU编码 kooloo add 202312
cJSON_AddItemToObject(jsonnode, C_FSUCODENAME, cJSON_CreateString(g_DeviceCfg.devConf.DeviceNumber)); //"11010110001"
//读写属性 后续根据需要 更新 kooloo add 202312
cJSON_AddItemToObject(jsonnode, C_GATEWAY_DATATYPE, cJSON_CreateString(C_GATEWAY_DATA));//消息属性
//抓取有效数据组成cjson表 抓取设备本身数据表
cJSON* jsonContent;
if (!strcmp(C_PROTOCOLSELECTION_OID, g_DeviceCfg.ProtocolSelection))
{//如果OID
jsonContent = GetCjson_fromdb(C_SELECT_SQLSTR_OID);
}
else if (!strcmp(C_PROTOCOLSELECTION_SIGNALID, g_DeviceCfg.ProtocolSelection))
{
jsonContent = GetCjson_fromdb(C_SELECT_SQLSTR_SIGNALID);
}
else //默认暂时为OID kooloo add 202312
{
jsonContent = GetCjson_fromdb(C_SELECT_SQLSTR_OID);
}
cJSON_AddItemToObject(jsonnode, C_IDCODECONTENT, jsonContent);
//获取时间戳 kooloo add 202312
char TempTimeBuf[20];
GetLocalTimeStr(TempTimeBuf);
cJSON_AddItemToObject(jsonnode, C_TIMESTAMP, cJSON_CreateString(TempTimeBuf)); //时间戳此时间戳无具体意义仅加载在cjson文件中
cJSON* json_FSUSelf = cJSON_CreateObject();
cJSON_AddItemToObject(json_FSUSelf, g_DeviceCfg.FSUDeviceSelfTableName, jsonnode);
return json_FSUSelf;
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
** : CjsonAppend_Table_Init
** : json
** :cJSON * json
**   : 20231220
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
cJSON* CjsonAppend_Table_Init(cJSON* json)
{
//创建根节点JSON(最外面大括号的JSON对象)
cJSON* jsonsubnode = cJSON_CreateObject();
//FSU编码 kooloo add 202312
cJSON_AddItemToObject(jsonsubnode, C_FSUCODENAME, cJSON_CreateString(g_DeviceCfg.devConf.DeviceNumber)); //"11010110001"
//读写属性 后续根据需要 更新 kooloo add 202312
cJSON_AddItemToObject(jsonsubnode, C_GATEWAY_DATATYPE, cJSON_CreateString(C_GATEWAY_DATA));//消息属性
//抓取有效数据组成cjson表 抓取设备本身数据表
cJSON* jsonContent = cJSON_CreateArray();
cJSON_AddItemToObject(jsonsubnode, C_IDCODECONTENT, jsonContent);
//获取时间戳 kooloo add 202312
char TempTimeBuf[20];
GetLocalTimeStr(TempTimeBuf);
cJSON_AddItemToObject(jsonsubnode, C_TIMESTAMP, cJSON_CreateString(TempTimeBuf)); //时间戳此时间戳无具体意义仅加载在cjson文件中
cJSON_AddItemToObject(json, C_GATEWAY_DATAFORMATPUBLIC, jsonsubnode);
return json;
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
** : printfJson
** : CJSON
** :
**   : 2023902
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
void printfJson(cJSON* json) {
if (NULL == json) {
return;
}
char* cjson = cJSON_Print(json);//深拷贝
ms_info1("json:%s le %d\n", cjson, strlen(cjson));
free(cjson);
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
** : writeJsonFile
** : JSON tmp
** :
**   : 2023902
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
void writeJsonFile(char* fileName, cJSON* json)
{
if (NULL == json || NULL == fileName) {
return;
}
char* cjson = cJSON_Print(json);
FILE* fp = NULL;
//新建一个文本文件,已存在的文件将内容清空,允许读写
fp = fopen(fileName, "w+");
if (NULL != fp) {
fwrite(cjson, strlen(cjson), 1, fp);
fclose(fp);
}
if (NULL != cjson) {
free(cjson);
}
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
** : GetJson_FSUDevice_UpdateData
** : FSUDevicejson kooloo add 202312
** :
**   : 2023902
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
cJSON* GetJson_FSUDevice_UpdateData(cJSON* json)
{
int ret;
cJSON* json_rt = cJSON_GetObjectItem(json, g_DeviceCfg.FSUDeviceSelfTableName); //获取表中数组; //指向根节点
//数据更新
/* 获取信息 */
ret = sysinfo(&pSysPara->sys_info);
if (-1 == ret) //如果获取错误则 错误 暂时无法确定触发此条件要求 kooloo add 202312 暂时不跳出 kooloo add
{
ms_error1("sysinfo error!\n");
}
/* 获取信息 */
ret = uname(&pSysPara->os_info);
if (-1 == ret)
{
ms_error1("uname error!\n");
}
//获取数组 并更新数组
cJSON* json_Arr = cJSON_GetObjectItem(json_rt, C_IDCODECONTENT); //获取表中数组
//数据替换 kooloo add 202312
char strbuf[20] = { 0 };
cJSON* json_arraydata;
//系统信息
//eInfoSysname
json_arraydata = cJSON_GetArrayItem(json_Arr, eInfoSysname);
cJSON_ReplaceItemInObject(json_arraydata, "Value", cJSON_CreateString(pSysPara->os_info.sysname));
//eInfoNodename
json_arraydata = cJSON_GetArrayItem(json_Arr, eInfoNodename);
cJSON_ReplaceItemInObject(json_arraydata, "Value", cJSON_CreateString(pSysPara->os_info.nodename));
//eInfoRelease
json_arraydata = cJSON_GetArrayItem(json_Arr, eInfoRelease);
cJSON_ReplaceItemInObject(json_arraydata, "Value", cJSON_CreateString(pSysPara->os_info.release));
//eInfoVersion
json_arraydata = cJSON_GetArrayItem(json_Arr, eInfoVersion);
cJSON_ReplaceItemInObject(json_arraydata, "Value", cJSON_CreateString(pSysPara->os_info.version));
//eInfoMachine
json_arraydata = cJSON_GetArrayItem(json_Arr, eInfoMachine);
cJSON_ReplaceItemInObject(json_arraydata, "Value", cJSON_CreateString(pSysPara->os_info.machine));
//eInfoUptime
json_arraydata = cJSON_GetArrayItem(json_Arr, eInfoUptime);
sprintf(strbuf, "%d", pSysPara->sys_info.uptime);
cJSON_ReplaceItemInObject(json_arraydata, "Value", cJSON_CreateString(strbuf));
//eInfoTotalram
json_arraydata = cJSON_GetArrayItem(json_Arr, eInfoTotalram);
sprintf(strbuf, "%d", pSysPara->sys_info.totalram);
cJSON_ReplaceItemInObject(json_arraydata, "Value", cJSON_CreateString(strbuf));
//eInfoFreeram
json_arraydata = cJSON_GetArrayItem(json_Arr, eInfoFreeram);
sprintf(strbuf, "%d", pSysPara->sys_info.freeram);
cJSON_ReplaceItemInObject(json_arraydata, "Value", cJSON_CreateString(strbuf));
//eInfoProcs
json_arraydata = cJSON_GetArrayItem(json_Arr, eInfoProcs);
sprintf(strbuf, "%d", pSysPara->sys_info.procs);
cJSON_ReplaceItemInObject(json_arraydata, "Value", cJSON_CreateString(strbuf));
//获取时间戳 kooloo add 202312 更新时间戳
// char TempTimeBuf[20];
GetLocalTimeStr(strbuf);
cJSON_ReplaceItemInObject(json_rt, C_TIMESTAMP, cJSON_CreateString(strbuf));
//消息属性 更换 kooloo add 202312
cJSON_ReplaceItemInObject(json_rt, C_GATEWAY_DATATYPE, cJSON_CreateString(C_GATEWAY_DATA));
return json_rt;
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
** : mq_sendJson
** : json
** :
**   : 2023902
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
void mq_sendJson(cJSON* json)
{
if (NULL == json) {
return;
}
#if 0
if (sendcounter < 100)
{
sendcounter++;
ms_info1("sendcounter %d \n", sendcounter);
}
else
{
ms_info1("sendcounter %d \n", sendcounter);
while (1)
{
sleep(1);
}
return;
}
#endif
char* cjson = cJSON_Print(json);//深拷贝
int len = mq_send(MqdSrcToDst, cjson, strlen(cjson), 0);
ms_info1("jsonlen %d json:%s,sendlen:%d\n", strlen(cjson), cjson, len);
free(cjson);
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
** : mq_sendJson_subdevfromID
** : json ID
** :
**   : 2023902
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
void mq_sendJson_subdevfromID(int tyid, int polli)
{
//获取 表 根据检索表 kooloo add 202312
char Temp_Str[200] = { 0 }; //临时变量 kooloo 直接往strSql尾部添加字符串会造成Segmentation fault
cJSON* json_arraydata; //获取json 数组 kooloo add 202312
char strbuf[20] = { 0 }; //临时用变量 转换用
sprintf(strbuf, "%d", polli + 1);
memset(Temp_Str, 0, 200); //将数组清空
strcpy(Temp_Str, g_CfgTab_Rs485_Gather.g_CfgTab_SmartDeviceType[tyid].Name); //将表名添加 kooloo add 20230928
strcat(Temp_Str, ":"); //将间隔符添加 kooloo add 20230928
strcat(Temp_Str, strbuf); //将序号添加 kooloo add 20230928
cJSON* json_rt = cJSON_GetObjectItem(json_roottable, Temp_Str); //获取表中数组; //指向根节点
cJSON* json_sub = cJSON_GetObjectItem(json_rt, "遥测"); //获取表中数组; //指向根节点
//消息属性 更换 kooloo add 202312
cJSON_ReplaceItemInObject(json_sub, C_GATEWAY_DATATYPE, cJSON_CreateString(C_GATEWAY_DATA));
mq_sendJson(json_sub); //将遥测数据发送 kooloo add 202312
json_sub = cJSON_GetObjectItem(json_rt, "遥信"); //获取表中数组; //指向根节点
mq_sendJson(json_sub); //将遥测数据发送 kooloo add 202312
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
** : mq_sendRemJson_subdevfromID
** : json ID
** :
**   : 2023902
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
void mq_sendRemJson_subdevfromID(int tyid, int polli)
{
//获取 表 根据检索表 kooloo add 202312
char Temp_Str[200] = { 0 }; //临时变量 kooloo 直接往strSql尾部添加字符串会造成Segmentation fault
cJSON* json_arraydata; //获取json 数组 kooloo add 202312
char strbuf[20] = { 0 }; //临时用变量 转换用
sprintf(strbuf, "%d", polli + 1);
memset(Temp_Str, 0, 200); //将数组清空
strcpy(Temp_Str, g_CfgTab_Rs485_Gather.g_CfgTab_SmartDeviceType[tyid].Name); //将表名添加 kooloo add 20230928
strcat(Temp_Str, ":"); //将间隔符添加 kooloo add 20230928
strcat(Temp_Str, strbuf); //将序号添加 kooloo add 20230928
cJSON* json_rt = cJSON_GetObjectItem(json_roottable, Temp_Str); //获取表中数组; //指向根节点
cJSON* json_sub = cJSON_GetObjectItem(json_rt, "遥测"); //获取表中数组; //指向根节点
//消息属性 更换 kooloo add 202312
cJSON_ReplaceItemInObject(json_sub, C_GATEWAY_DATATYPE, cJSON_CreateString(C_GATEWAY_READDATA));
mq_sendJson(json_sub); //将遥测数据发送 kooloo add 202312
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
** : mq_sendResJson_subdevfromID
** : json ID
** :
**   : 2023902
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
void mq_sendResJson_subdevfromID(int tyid, int polli)
{
//获取 表 根据检索表 kooloo add 202312
char Temp_Str[200] = { 0 }; //临时变量 kooloo 直接往strSql尾部添加字符串会造成Segmentation fault
cJSON* json_arraydata; //获取json 数组 kooloo add 202312
char strbuf[20] = { 0 }; //临时用变量 转换用
sprintf(strbuf, "%d", polli + 1);
memset(Temp_Str, 0, 200); //将数组清空
strcpy(Temp_Str, g_CfgTab_Rs485_Gather.g_CfgTab_SmartDeviceType[tyid].Name); //将表名添加 kooloo add 20230928
strcat(Temp_Str, ":"); //将间隔符添加 kooloo add 20230928
strcat(Temp_Str, strbuf); //将序号添加 kooloo add 20230928
cJSON* json_rt = cJSON_GetObjectItem(json_roottable, Temp_Str); //获取表中数组; //指向根节点
cJSON* json_sub = cJSON_GetObjectItem(json_rt, "遥信"); //获取表中数组; //指向根节点
mq_sendJson(json_sub); //将遥测数据发送 kooloo add 202312
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
** : mq_sendRecJson_subdevfromID
** : json ID
** :
**   : 2023902
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
void mq_sendRecJson_subdevfromID(int tyid, int polli)
{
//获取 表 根据检索表 kooloo add 202312
char Temp_Str[200] = { 0 }; //临时变量 kooloo 直接往strSql尾部添加字符串会造成Segmentation fault
cJSON* json_arraydata; //获取json 数组 kooloo add 202312
char strbuf[20] = { 0 }; //临时用变量 转换用
sprintf(strbuf, "%d", polli + 1);
memset(Temp_Str, 0, 200); //将数组清空
strcpy(Temp_Str, g_CfgTab_Rs485_Gather.g_CfgTab_SmartDeviceType[tyid].Name); //将表名添加 kooloo add 20230928
strcat(Temp_Str, ":"); //将间隔符添加 kooloo add 20230928
strcat(Temp_Str, strbuf); //将序号添加 kooloo add 20230928
cJSON* json_rt = cJSON_GetObjectItem(json_roottable, Temp_Str); //获取表中数组; //指向根节点
cJSON* json_sub = cJSON_GetObjectItem(json_rt, "遥控"); //获取表中数组; //指向根节点
mq_sendJson(json_sub); //将遥测数据发送 kooloo add 202312
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
** : mq_sendRerJson_subdevfromID
** : json ID
** :
**   : 2023902
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
void mq_sendRerJson_subdevfromID(int tyid, int polli)
{
//获取 表 根据检索表 kooloo add 202312
char Temp_Str[200] = { 0 }; //临时变量 kooloo 直接往strSql尾部添加字符串会造成Segmentation fault
cJSON* json_arraydata; //获取json 数组 kooloo add 202312
char strbuf[20] = { 0 }; //临时用变量 转换用
sprintf(strbuf, "%d", polli + 1);
memset(Temp_Str, 0, 200); //将数组清空
strcpy(Temp_Str, g_CfgTab_Rs485_Gather.g_CfgTab_SmartDeviceType[tyid].Name); //将表名添加 kooloo add 20230928
strcat(Temp_Str, ":"); //将间隔符添加 kooloo add 20230928
strcat(Temp_Str, strbuf); //将序号添加 kooloo add 20230928
cJSON* json_rt = cJSON_GetObjectItem(json_roottable, Temp_Str); //获取表中数组; //指向根节点
cJSON* json_sub = cJSON_GetObjectItem(json_rt, "遥调"); //获取表中数组; //指向根节点
mq_sendJson(json_sub); //将遥测数据发送 kooloo add 202312
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
** : Mq_ReceiveJson_Handle
** : json kooloo add 202412
** :
**   : 2023902
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
void Mq_ReceiveJson_Handle(char* msgbuf, int msgsize)
{
char* tmsgbuf = (char*)malloc(msgsize);
memset(tmsgbuf, 0, msgsize);
strncpy(tmsgbuf, msgbuf, msgsize); //将数据拷贝 kooloo add 202401
cJSON* json_rt = cJSON_Parse(tmsgbuf);
int len;
if (NULL == json_rt) // 接收到其它数据 不做处理
{
ms_info1("非json:%s\n", tmsgbuf);
}
else //json 解析正常 判断
{
//获取数组 并更新数组
cJSON* json_Arr = cJSON_GetObjectItem(json_rt, C_IDCODECONTENT); //获取表中数组
if (json_Arr == NULL)
{
return;
}
cJSON* json_arraydata; //获取json 数组 kooloo add 202312
json_arraydata = cJSON_GetArrayItem(json_Arr, 0);
if (json_arraydata == NULL)
{
return;
}
cJSON* json_arraydata_Oid = cJSON_GetObjectItem(json_arraydata, C_PROTOCOLSELECTION_OID);
if (json_arraydata_Oid == NULL)
{
return;
}
OID_Content_t oid;
OidAnalysis_fromoidstr(json_arraydata_Oid->valuestring, &oid);
ms_info1("DevType %d SignalSN %d SignalType %d SimilarDevSN %d StationType %d \n", oid.DevType, oid.SignalSN, oid.SignalType, oid.SimilarDevSN, oid.StationType);
int tyid = GetTyid_fromDevCod(oid.DevType); //获取tyid kooloo add 202401 将设备编码里的设备类型对应成程序的序号
cJSON* json_FsuID = cJSON_GetObjectItem(json_rt, C_FSUCODENAME); //获取表中ID 并做判断
if (json_FsuID == NULL)
{
return;
}
if (!strcmp(g_DeviceCfg.devConf.DeviceNumber, json_FsuID->valuestring)) //如果接收到的消息是FSUCODE 正确 则将消息转发 否则忽略
{
cJSON* json_Type = cJSON_GetObjectItem(json_rt, C_GATEWAY_DATATYPE); //获取表中ID 并做判断
if (!strcmp(C_SERVER_READDATA, json_Type->valuestring)) //如果接收到的消息是FSUCODE 正确 则将消息转发 否则忽略 遥测
{
if(oid.SignalType==C_SIGNALTYPE_YX_TELESIGNAL) //遥信
{
mq_sendResJson_subdevfromID(tyid, oid.SimilarDevSN - 1); //遥信直接获取
}
else if(oid.SignalType==C_SIGNALTYPE_YC_TELEMETERING) //遥测
{
mq_sendRemJson_subdevfromID(tyid, oid.SimilarDevSN - 1); //遥测直接获取
}
else if(oid.SignalType==C_SIGNALTYPE_YK_TELECONTROL) //获取遥控
{
mq_sendRecJson_subdevfromID(tyid, oid.SimilarDevSN - 1);
}
else if(oid.SignalType==C_SIGNALTYPE_YT_TELEADJUSTING) //获取遥调 设置
{
mq_sendRerJson_subdevfromID(tyid, oid.SimilarDevSN - 1);
}
ms_info1("type :%d cjson:%s,Rxlen:%d\n",oid.SignalType,tmsgbuf, strlen(tmsgbuf));
}
else if (!strcmp(C_SERVER_WRITEDATA, json_Type->valuestring)) //如果接收到的消息是FSUCODE 正确 则将消息转发 否则忽略 遥调 更新数据
{
//cJSON_ReplaceItemInObject(json_rt, C_GATEWAY_DATATYPE, cJSON_CreateString(C_GATEWAY_WRITEDATA));
//mq_sendJson(json_rt); //将遥*数据发送 kooloo add 202312
char* tmsgbuf_spvp = (char*)malloc(C_SERIALPORT_MQ_MSGSIZE);
memset(tmsgbuf_spvp, 0, C_SERIALPORT_MQ_MSGSIZE);
strncpy(tmsgbuf_spvp, msgbuf, C_SERIALPORT_MQ_MSGSIZE); //将数据拷贝 kooloo add 202401
if(tFunTyID_DevCod_buf[tyid].PortID<eSDPort_Number_Max) //如果为普通串口 则将消息传递到 普通串口队列中,否则传到虚拟串口中 kooloo add 20240312
{
len = mq_send(MqdSerialPort, tmsgbuf_spvp, strlen(tmsgbuf_spvp), 0);
printf("DP MqdSerialPort:type :%d jsonlen %d json:%s,sendlen:%d\n",oid.SignalType, strlen(tmsgbuf_spvp), tmsgbuf_spvp, len);
}
else if(tFunTyID_DevCod_buf[tyid].PortID<(eSDPort_Number_Max+eVirtuallyPort_Number_Max)) //虚拟串口 kooloo add 20240312
{
len = mq_send(MqdVtSerialPort, tmsgbuf_spvp, strlen(tmsgbuf_spvp), 0);
printf("DP MqdVtSerialPort:type :%d jsonlen %d json:%s,sendlen:%d\n",oid.SignalType, strlen(tmsgbuf_spvp), tmsgbuf_spvp, len);
}
free(tmsgbuf_spvp); //用完释放 kooloo add 202401
//mq_sendRerJson_subdevfromID(tyid, oid.SimilarDevSN - 1);
//ms_info1("type :%d cjson:%s,Rxlen:%d\n",oid.SignalType,tmsgbuf, strlen(tmsgbuf));
}
else if (!strcmp(C_SERVER_ALARMDATA, json_Type->valuestring)) //如果接收到的消息是FSUCODE 正确 则将消息转发 否则忽略 遥信 不需要调整
{
mq_sendResJson_subdevfromID(tyid, oid.SimilarDevSN - 1);
ms_info1("type :%d cjson:%s,Rxlen:%d\n",oid.SignalType,tmsgbuf, strlen(tmsgbuf));
}
else if (!strcmp(C_SERVER_CRTLDATA, json_Type->valuestring)) //如果接收到的消息是FSUCODE 正确 则将消息转发 否则忽略 遥控 更新数据
{
//cJSON_ReplaceItemInObject(json_rt, C_GATEWAY_DATATYPE, cJSON_CreateString(C_GATEWAY_CTRLDATA));
//mq_sendJson(json_rt); //将遥*数据发送 kooloo add 202312
char* tmsgbuf_spvp = (char*)malloc(C_SERIALPORT_MQ_MSGSIZE);
memset(tmsgbuf_spvp, 0, C_SERIALPORT_MQ_MSGSIZE);
strncpy(tmsgbuf_spvp, msgbuf, C_SERIALPORT_MQ_MSGSIZE); //将数据拷贝 kooloo add 202401
if(tFunTyID_DevCod_buf[tyid].PortID<eSDPort_Number_Max) //如果为普通串口 则将消息传递到 普通串口队列中,否则传到虚拟串口中 kooloo add 20240312
{
len = mq_send(MqdSerialPort, tmsgbuf_spvp, strlen(tmsgbuf_spvp), 0);
printf("DP MqdSerialPort:type :%d jsonlen %d json:%s,sendlen:%d\n",oid.SignalType, strlen(tmsgbuf_spvp), tmsgbuf_spvp, len);
}
else if(tFunTyID_DevCod_buf[tyid].PortID<(eSDPort_Number_Max+eVirtuallyPort_Number_Max)) //虚拟串口 kooloo add 20240312
{
len = mq_send(MqdVtSerialPort, tmsgbuf_spvp, strlen(tmsgbuf_spvp), 0);
printf("DP MqdVtSerialPort:type :%d jsonlen %d json:%s,sendlen:%d\n",oid.SignalType, strlen(tmsgbuf_spvp), tmsgbuf_spvp, len);
}
free(tmsgbuf_spvp); //用完释放 kooloo add 202401
//mq_sendRecJson_subdevfromID(tyid, oid.SimilarDevSN - 1);
//ms_info1("type :%d cjson:%s,Rxlen:%d\n",oid.SignalType,tmsgbuf, strlen(tmsgbuf));
}
else
{
ms_info1("type is wrong! json:%s jsonlen %d \n",tmsgbuf, strlen(tmsgbuf));
}
}
}
free(tmsgbuf); //用完释放 kooloo add 202401
if (json_rt != NULL) {//释放
cJSON_Delete(json_rt);
}
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
** : Mq_cJson_ReturnTyid
** : json CjsonID
** :
**   : 2023902
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
int Mq_cJson_ReturnTyid(char* msgbuf, int msgsize)
{
char* tmsgbuf = (char*)malloc(msgsize);
int tyid =0;
memset(tmsgbuf, 0, msgsize);
strncpy(tmsgbuf, msgbuf, msgsize); //将数据拷贝 kooloo add 202401
cJSON* json_rt = cJSON_Parse(tmsgbuf);
int len;
if (NULL == json_rt) // 接收到其它数据 不做处理
{
ms_info1("非json:%s\n", tmsgbuf);
}
else //json 解析正常 判断
{
//获取数组 并更新数组
cJSON* json_Arr = cJSON_GetObjectItem(json_rt, C_IDCODECONTENT); //获取表中数组
if (json_Arr == NULL)
{
return tyid;
}
cJSON* json_arraydata; //获取json 数组 kooloo add 202312
json_arraydata = cJSON_GetArrayItem(json_Arr, 0);
if (json_arraydata == NULL)
{
return tyid;
}
cJSON* json_arraydata_Oid = cJSON_GetObjectItem(json_arraydata, C_PROTOCOLSELECTION_OID);
if (json_arraydata_Oid == NULL)
{
return tyid;
}
OID_Content_t oid;
OidAnalysis_fromoidstr(json_arraydata_Oid->valuestring, &oid);
tyid = GetTyid_fromDevCod(oid.DevType); //获取tyid kooloo add 202401 将设备编码里的设备类型对应成程序的序号
}
free(tmsgbuf); //用完释放 kooloo add 202401
if (json_rt != NULL) {//释放
cJSON_Delete(json_rt);
}
return tyid;
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
** : FsuDeviceSelfThread
** : FSU线
** :
**  : kooloo
**   : 20220321
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
void* FsuDeviceSelfThread(void* arg)
{
ms_info1("FsuDeviceSelfThread\n"); //创建轮询主线程 kooloo add 202309
int portnum = 1;
int i_dex;
int i_id;
//uint8_t Mqmsgbuf[C_SRCToDST_MQ_MSGSIZE];
char* Mqmsgbuf = (char*)malloc(C_SRCToDST_MQ_MSGSIZE);
memset(Mqmsgbuf, 0, C_SRCToDST_MQ_MSGSIZE);
int Mqmsglen;
while (1)
{
Mqmsglen = mq_receive(MqdDstToSrc, Mqmsgbuf, C_SRCToDST_MQ_MSGSIZE, NULL);
if (Mqmsglen > 0)
{
Mq_ReceiveJson_Handle(Mqmsgbuf, Mqmsglen);
ms_info1("Get Reqdata %s len:%d\n", Mqmsgbuf, Mqmsglen);
}
#if 0 //方便测试
//判断子进程是否运行 间隔判断
if (Timer_getDiffValueTicks(pSysPara->g_GetApp_TimeCounter) > C_FSUDEVICE_UPDATE_PERIOD)
{
cJSON* json_FsuDevice = GetJson_FSUDevice_UpdateData(json_roottable);
mq_sendJson(json_FsuDevice); //将数据发送到队列中等带mqtt 进程或者b接口进程处理
pSysPara->g_GetApp_TimeCounter = Timer_getNowTicks();
#if (C_CJSONFILE_SAVE_FALG==1)
//printfJson(json_roottable);
writeJsonFile(C_CJSONROOTTABLE_FILENAME, json_roottable); //保存 方便查看 kooloo add 202312
#endif
}
else //否则判断是否满足发送子设备数据 kooloo add 202312
{
if (Timer_getDiffValueTicks(pSysPara->g_SendSubDevData_TimeCnt) > C_SUBDEV_UPDATE_PERIOD)
{
//获取ID
i_id = g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].g_DeviceFuncDef[g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].TypeIndex_Send].FunctionConfigTypeID;
////printf("SendData portnum %d Tnum %d\n", portnum, g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].TypeNumber);
if (g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].g_DeviceFuncDef[g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].TypeIndex_Send].PollArrIndexNow_Send
< g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].g_DeviceFuncDef[g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].TypeIndex_Send].PollArrNumber) //如果小于
{
if (i_id && (i_id != C_HJWL_SERIALPORT_VPORT_TYID))
{
ms_info1("SendData PortID %d TypeID %d, Index %d\n", portnum, i_id, g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].TypeIndex_Send);
mq_sendJson_subdevfromID(i_id, i_dex); //根据ID和轮询号发送数据 kooloo add 202312
pSysPara->g_SendSubDevData_TimeCnt = Timer_getNowTicks();
g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].g_DeviceFuncDef[g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].TypeIndex_Send].PollArrIndexNow_Send++;
}
else
{
g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].g_DeviceFuncDef[g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].TypeIndex_Send].PollArrIndexNow_Send = 0;
if ((g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].TypeIndex_Send + 1) < g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].TypeNumber)
{
g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].TypeIndex_Send++;
}
else
{
g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].TypeIndex_Send = 0;
if (portnum < (eSDPort_Number_Max + eVirtuallyPort_Number_Max - 1)) //根据端口数量
{
portnum++;
}
else
{
portnum = 1;
}
}
g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].g_DeviceFuncDef[g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].TypeIndex_Send].PollArrIndexNow_Send = 0;
}
}
else
{
g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].g_DeviceFuncDef[g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].TypeIndex_Send].PollArrIndexNow_Send = 0;
if ((g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].TypeIndex_Send + 1) < g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].TypeNumber)
{
g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].TypeIndex_Send++;
}
else
{
g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].TypeIndex_Send = 0;
if (portnum < (eSDPort_Number_Max + eVirtuallyPort_Number_Max - 1)) //根据端口数量
{
portnum++;
}
else
{
portnum = 1;
}
}
g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].g_DeviceFuncDef[g_CfgTab_Rs485_Gather.g_SDPortFuncDef[portnum].TypeIndex_Send].PollArrIndexNow_Send = 0;
}
}
}
#endif
sleep(1);
}
return 0;
}