forked from gary/BCU
2
0
Fork 0
BCU/app/stm32fxxx_app/protocol/protocol_mqtt_bcu.c.orig

94 lines
2.3 KiB
C
Raw Normal View History

/******************************************************************************
* @file protocol_mqtt_bcu.c
* @brief protocol_mqtt_bcu
* @version V1.0
* @author Gary
* @copyright
******************************************************************************/
#include "protocol_mqtt_bcu.h"
const devPointMap bms_points[] = {
{"BCU_2", NULL}, // <20>ܸ澯
{"BCU_3", NULL}, // <20>ܹ<EFBFBD><DCB9><EFBFBD>
{"BCU_4", NULL}, // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѹ<EFBFBD>һ<E6BEAF><D2BB>
{"BCU_5", NULL}, // <20><><EFBFBD><EFBFBD>Ƿѹ<C7B7>һ<E6BEAF><D2BB>
{"BCU_6", NULL}, // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¸һ<E6BEAF><D2BB>
{"BCU_7", NULL}, // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¸һ<E6BEAF><D2BB>
{"BCU_8", NULL}, // <20><><EFBFBD><EFBFBD>ѹ<EFBFBD><D1B9><EFBFBD>һ<E6BEAF><D2BB>
{"BCU_9", NULL}, // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<E6BEAF><D2BB>
};
const int bms_point_count = sizeof(bms_points) / sizeof(bms_points[0]);
char* protocol_build_json(void)
{
cJSON* root = cJSON_CreateObject();
if (!root) return NULL;
// ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD>ֶΣ<D6B6><CEA3>˴<EFBFBD><CBB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E6BBBB>ʵ<EFBFBD><CAB5>ʱ<EFBFBD><EFBFBD><E4BAAF><EFBFBD><EFBFBD>
cJSON_AddNumberToObject(root, "timeStamp", drv_rtc_get_tick());
// devData <20><><EFBFBD><EFBFBD>
cJSON* devDataArr = cJSON_CreateArray();
cJSON_AddItemToObject(root, "devData", devDataArr);
// <20><><EFBFBD><EFBFBD>ÿһ<C3BF><D2BB><EFBFBD><EFBFBD><E8B1B8>
cJSON* deviceObj = cJSON_CreateObject();
cJSON_AddItemToArray(devDataArr, deviceObj);
cJSON_AddStringToObject(deviceObj, "devType", "BMS");
cJSON_AddStringToObject(deviceObj, "devName", "BCU");
cJSON_AddStringToObject(deviceObj, "devId", "001");
cJSON_AddStringToObject(deviceObj, "sn", "SN123456");
// <20><><EFBFBD><EFBFBD> data <20><><EFBFBD><EFBFBD>
cJSON* dataObj = cJSON_CreateObject();
cJSON_AddItemToObject(deviceObj, "data", dataObj);
for (int i = 0; i < bms_point_count; ++i)
{
const char* key = bms_points[i].key;
int val = 0;
if (bms_points[i].get_val != NULL)
{
val = bms_points[i].get_val();
}
else
{
val = 0; // Ĭ<><C4AC>ֵ
}
cJSON_AddNumberToObject(dataObj, key, val);
}
// תΪ JSON <20>ַ<EFBFBD><D6B7><EFBFBD>
char* json_str = cJSON_PrintUnformatted(root); // ʹ<><CAB9> Unformatted <20><><EFBFBD><EFBFBD><E2BBBB>
cJSON_Delete(root);
return json_str;
}
void mqtt_publish_bms_data(uint16_t basetime)
{
static uint32_t mqtt_cycle_tick = 0;
mqtt_cycle_tick += basetime;
if(mqtt_cycle_tick > 60000)
{
mqtt_cycle_tick = 0;
char* json_str = protocol_build_json();
if (json_str == NULL)
{
return;
}
drv_mqtt_publish(json_str, strlen(json_str));
cJSON_free(json_str);
}
}