add:增加上位机交互框架、Flash R/W 框架
This commit is contained in:
@@ -15,6 +15,34 @@ ip4_addr_t stake_ip_4 = IPADDR4_INIT_BYTES(10, 12, 19, 104); // 桩 4 IP地址
|
||||
ip4_addr_t stake_ip_5 = IPADDR4_INIT_BYTES(10, 12, 19, 105); // 桩 5 IP地址
|
||||
ip4_addr_t stake_ip_6 = IPADDR4_INIT_BYTES(10, 12, 19, 106); // 桩 6 IP地址
|
||||
|
||||
ip4_addr_t server_ip = IPADDR4_INIT_BYTES(10, 12, 19, 107); // 上位机管理地址
|
||||
|
||||
/**
|
||||
* @brief 将十六进制字符串转换为字节数组
|
||||
* @note 输入字符串长度必须为偶数
|
||||
*/
|
||||
int hex_string_to_bytes(const char *hex_str, uint8_t *bytes, int len)
|
||||
{
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
unsigned int val;
|
||||
sscanf(hex_str + i * 2, "%02x", &val);
|
||||
bytes[i] = (uint8_t)val;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 将字节数组转换为十六进制字符串
|
||||
* @note 输出字符串长度必须为32
|
||||
*/
|
||||
void bytes_to_hex_string(const uint8_t *bytes, char *hex_str)
|
||||
{
|
||||
for (int i = 0; i < 16; i++)
|
||||
{
|
||||
sprintf(hex_str + i * 2, "%02X", bytes[i]);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @brief UDP发送
|
||||
* @note 发送数据到指定桩
|
||||
@@ -26,31 +54,49 @@ ip4_addr_t stake_ip_6 = IPADDR4_INIT_BYTES(10, 12, 19, 106); // 桩 6 IP地址
|
||||
err_t udp_send_response(uint8_t stake_index, uint8_t *data, u16_t len)
|
||||
{
|
||||
struct netbuf *buf = netbuf_new();
|
||||
if (!buf) {
|
||||
if (!buf)
|
||||
{
|
||||
return ERR_MEM;
|
||||
}
|
||||
|
||||
|
||||
ip4_addr_t *dst_ip = NULL;
|
||||
switch (stake_index) {
|
||||
case 1: dst_ip = &stake_ip_1; break;
|
||||
case 2: dst_ip = &stake_ip_2; break;
|
||||
case 3: dst_ip = &stake_ip_3; break;
|
||||
case 4: dst_ip = &stake_ip_4; break;
|
||||
case 5: dst_ip = &stake_ip_5; break;
|
||||
case 6: dst_ip = &stake_ip_6; break;
|
||||
switch (stake_index)
|
||||
{
|
||||
case 1:
|
||||
dst_ip = &stake_ip_1;
|
||||
break;
|
||||
case 2:
|
||||
dst_ip = &stake_ip_2;
|
||||
break;
|
||||
case 3:
|
||||
dst_ip = &stake_ip_3;
|
||||
break;
|
||||
case 4:
|
||||
dst_ip = &stake_ip_4;
|
||||
break;
|
||||
case 5:
|
||||
dst_ip = &stake_ip_5;
|
||||
break;
|
||||
case 6:
|
||||
dst_ip = &stake_ip_6;
|
||||
break;
|
||||
case 7:
|
||||
dst_ip = &server_ip;
|
||||
break;
|
||||
default:
|
||||
printf("Invalid stake index\r\n");
|
||||
netbuf_delete(buf);
|
||||
return ERR_VAL; // ← 修复 return 无返回值
|
||||
return ERR_VAL; // ← 修复 return 无返回值
|
||||
}
|
||||
|
||||
void *buf_data = netbuf_alloc(buf, len); // ← 在 netbuf 内部分配内存
|
||||
if (!buf_data) {
|
||||
|
||||
void *buf_data = netbuf_alloc(buf, len); // ← 在 netbuf 内部分配内存
|
||||
if (!buf_data)
|
||||
{
|
||||
netbuf_delete(buf);
|
||||
return ERR_MEM;
|
||||
}
|
||||
memcpy(buf_data, data, len); // ← 拷贝数据,不再依赖外部指针
|
||||
|
||||
memcpy(buf_data, data, len); // ← 拷贝数据,不再依赖外部指针
|
||||
|
||||
err_t err = netconn_sendto(datalink_conn, buf, dst_ip, LINK_STAKE_PORT);
|
||||
netbuf_delete(buf);
|
||||
return err;
|
||||
@@ -166,7 +212,7 @@ void UDPTask_Function(void const *argument)
|
||||
*/
|
||||
void local_on_cmd_callback_power_on(uint8_t stake_index, cJSON *json_pack)
|
||||
{
|
||||
if (stake_index > 6)
|
||||
if (stake_index > MAX_CHARGER_COUNT)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -203,7 +249,7 @@ void local_on_cmd_callback_power_on(uint8_t stake_index, cJSON *json_pack)
|
||||
*/
|
||||
void local_on_cmd_callback_heartbeat_response(uint8_t stake_index, cJSON *json_pack)
|
||||
{
|
||||
if (stake_index > 6)
|
||||
if (stake_index > MAX_CHARGER_COUNT)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -258,6 +304,432 @@ void local_on_cmd_callback_heartbeat_response(uint8_t stake_index, cJSON *json_p
|
||||
|
||||
printf("南向:对电桩 %d 心跳回复成功\r\n", stake_index);
|
||||
}
|
||||
/**
|
||||
* @brief 解析充电桩实时数据指令
|
||||
* @note 回复实时数据应答
|
||||
* @param stake_index 桩索引
|
||||
* @param json_pack json数据包
|
||||
*/
|
||||
void local_on_cmd_callback_realtime_data_response(uint8_t stake_index, cJSON *json_pack)
|
||||
{
|
||||
if (stake_index > MAX_CHARGER_COUNT)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
cJSON *transaction_id = cJSON_GetObjectItem(json_pack, "transaction_id"); // 提取交易序列号
|
||||
cJSON *gun_id = cJSON_GetObjectItem(json_pack, "gun_id"); // 提取枪号
|
||||
cJSON *state = cJSON_GetObjectItem(json_pack, "state"); // 提取枪状态
|
||||
cJSON *gun_back = cJSON_GetObjectItem(json_pack, "gun_back"); // 提取枪归位状态
|
||||
cJSON *gun_insert = cJSON_GetObjectItem(json_pack, "gun_insert"); // 提取枪插入状态
|
||||
cJSON *voltage = cJSON_GetObjectItem(json_pack, "voltage"); // 提取电压
|
||||
cJSON *current = cJSON_GetObjectItem(json_pack, "current"); // 提取电流
|
||||
cJSON *cable_temp = cJSON_GetObjectItem(json_pack, "cable_temp"); // 提取电缆温度
|
||||
cJSON *cable_code = cJSON_GetObjectItem(json_pack, "cable_code"); // 提取电缆编码
|
||||
cJSON *soc = cJSON_GetObjectItem(json_pack, "soc"); // 提取SOC
|
||||
cJSON *battery_temp = cJSON_GetObjectItem(json_pack, "battery_temp"); // 提取电池温度
|
||||
cJSON *charge_time = cJSON_GetObjectItem(json_pack, "charge_time"); // 提取充电时间
|
||||
cJSON *remain_time = cJSON_GetObjectItem(json_pack, "remain_time"); // 提取剩余时间
|
||||
cJSON *charge_kwh = cJSON_GetObjectItem(json_pack, "charge_kwh"); // 提取充电量
|
||||
cJSON *loss_kwh = cJSON_GetObjectItem(json_pack, "loss_kwh"); // 提取损失量
|
||||
cJSON *charge_amount = cJSON_GetObjectItem(json_pack, "charge_amount"); // 提取充电金额
|
||||
cJSON *fault = cJSON_GetObjectItem(json_pack, "fault"); // 提取故障状态
|
||||
// 检查交易序列号、枪号、状态字段是否存在
|
||||
if (!transaction_id || !gun_id || !state)
|
||||
{
|
||||
printf(" └── [error] 缺少必要字段 transaction_id/gun_id/state\r\n");
|
||||
return;
|
||||
}
|
||||
// 检查交易序列号长度是否为32位
|
||||
if (strlen(transaction_id->valuestring) != 32)
|
||||
{
|
||||
printf(" └── [error] transaction_id长度错误: %d\r\n", (int)strlen(transaction_id->valuestring));
|
||||
return;
|
||||
}
|
||||
// 检查枪号是否有效
|
||||
uint8_t gun_idx = (uint8_t)gun_id->valueint;
|
||||
if (gun_idx == 0 || gun_idx > MAX_GUN_PER_CHARGER)
|
||||
{
|
||||
printf(" └── [error] 无效的gun_id: %d\r\n", gun_idx);
|
||||
return;
|
||||
}
|
||||
|
||||
ChargerPile *pile = &g_charger_manager.charger_piles[stake_index - 1];
|
||||
ChargerGun *gun = &pile->guns[gun_idx - 1];
|
||||
|
||||
// 更新枪状态
|
||||
gun->real_time_data.status = (uint8_t)state->valueint;
|
||||
// 更新枪归位状态
|
||||
gun->real_time_data.gun_back = (uint8_t)gun_back->valueint;
|
||||
// 更新枪插入状态
|
||||
gun->real_time_data.gun_is_insert = (uint8_t)gun_insert->valueint;
|
||||
// 更新电压
|
||||
gun->real_time_data.out_voltage = (uint16_t)(voltage->valuedouble * 10);
|
||||
// 更新电流
|
||||
gun->real_time_data.out_current = (uint16_t)(current->valuedouble * 10);
|
||||
// 更新电缆温度
|
||||
gun->real_time_data.gun_line_temp = (uint8_t)cable_temp->valueint;
|
||||
// 更新SOC
|
||||
gun->real_time_data.soc = (uint8_t)soc->valueint;
|
||||
// 更新电池温度
|
||||
gun->real_time_data.battery_temp = (uint8_t)battery_temp->valueint;
|
||||
// 更新累计充电时间
|
||||
gun->real_time_data.charge_time = (uint16_t)charge_time->valueint;
|
||||
// 更新剩余时间
|
||||
gun->real_time_data.remain_time = (uint16_t)remain_time->valueint;
|
||||
// 更新充电量
|
||||
gun->real_time_data.charge_energy = (uint32_t)(charge_kwh->valuedouble * 10000);
|
||||
// 更新损失量
|
||||
gun->real_time_data.loss_energy = (uint32_t)(loss_kwh->valuedouble * 10000);
|
||||
// 更新充电金额
|
||||
gun->real_time_data.charge_money = (uint32_t)(charge_amount->valuedouble * 10000);
|
||||
// 更新故障状态
|
||||
gun->real_time_data.hard_fault = (uint16_t)fault->valueint;
|
||||
|
||||
// 回复实时数据应答
|
||||
cJSON *root = NULL;
|
||||
char *str = NULL;
|
||||
|
||||
root = cJSON_CreateObject();
|
||||
if (root == NULL)
|
||||
{
|
||||
printf("Failed to create JSON object for stake %d\r\n", stake_index);
|
||||
return;
|
||||
}
|
||||
|
||||
/* 添加一条字符串类型的JSON数据(添加一个链表节点) */
|
||||
cJSON_AddNumberToObject(root, "id", stake_index);
|
||||
cJSON_AddStringToObject(root, "cmd", "realtime data");
|
||||
cJSON_AddStringToObject(root, "type", "response");
|
||||
|
||||
str = cJSON_Print(root);
|
||||
udp_send_response(stake_index, str, strlen(str));
|
||||
free(str);
|
||||
cJSON_Delete(root);
|
||||
|
||||
printf("南向:对电桩 %d 实时数据回复成功\r\n", stake_index);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 主动下发启动指令
|
||||
* @note 发送启动指令到充电桩,符合协议格式要求
|
||||
* @param stake_index 桩索引 (1~6)
|
||||
* @param gun_id 枪编码 (1~N)
|
||||
*/
|
||||
void local_on_cmd_send_start_charging(uint8_t stake_index, uint8_t gun_id)
|
||||
{
|
||||
if (stake_index > 6 || stake_index == 0)
|
||||
{
|
||||
printf("Invalid stake index: %d\r\n", stake_index);
|
||||
return;
|
||||
}
|
||||
|
||||
if (gun_id == 0 || gun_id > MAX_GUN_PER_CHARGER)
|
||||
{
|
||||
printf("Invalid gun id: %d\r\n", gun_id);
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查是否已获取费率模型
|
||||
if (!g_charger_manager.charger_piles[stake_index - 1].get_model)
|
||||
{
|
||||
printf("Stake %d fee model not ready\r\n", stake_index);
|
||||
return;
|
||||
}
|
||||
|
||||
cJSON *root = NULL;
|
||||
cJSON *billing_array = NULL;
|
||||
cJSON *billing_item = NULL;
|
||||
char *str = NULL;
|
||||
|
||||
// 转换交易ID:16位数组转字符串
|
||||
char transaction_id_str[33] = {0}; // 32位 + 结束符
|
||||
uint8_t *trade_serial = g_charger_manager.charger_piles[stake_index - 1].guns[gun_id - 1].real_time_data.trade_serial;
|
||||
trade_serial_to_string(trade_serial, transaction_id_str);
|
||||
|
||||
root = cJSON_CreateObject();
|
||||
if (root == NULL)
|
||||
{
|
||||
printf("Failed to create JSON object for stake %d\r\n", stake_index);
|
||||
return;
|
||||
}
|
||||
|
||||
/* 添加基本字段 */
|
||||
cJSON_AddNumberToObject(root, "id", stake_index);
|
||||
cJSON_AddStringToObject(root, "cmd", "start charging");
|
||||
cJSON_AddStringToObject(root, "transaction_id", transaction_id_str); // 使用转换后的32位字符串
|
||||
cJSON_AddNumberToObject(root, "gun_id", gun_id);
|
||||
cJSON_AddStringToObject(root, "type", "request");
|
||||
|
||||
/* 添加限制金额(根据实际业务设置)*/
|
||||
cJSON_AddNumberToObject(root, "limit_amount", 00.00);
|
||||
|
||||
/* 创建计费模型数组 */
|
||||
billing_array = cJSON_CreateArray();
|
||||
if (billing_array == NULL)
|
||||
{
|
||||
printf("Failed to create billing array\r\n");
|
||||
cJSON_Delete(root);
|
||||
return;
|
||||
}
|
||||
|
||||
/* 计费时段:生成简化格式 [start_time, end_time, electricity_fee, service_fee] */
|
||||
int i = 0;
|
||||
|
||||
while (i < 48) // 48个半小时时段
|
||||
{
|
||||
uint8_t current_type = g_charger_manager.fee_model_global.fee_num[i];
|
||||
|
||||
// 跳过无效费率
|
||||
if (current_type == 0xFF)
|
||||
{
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// 查找连续相同费率的结束位置
|
||||
int j = i;
|
||||
while (j < 48 && g_charger_manager.fee_model_global.fee_num[j] == current_type)
|
||||
{
|
||||
j++;
|
||||
}
|
||||
|
||||
// 计算起始时间(小时.半小时:6.0=6:00, 6.5=6:30)
|
||||
float start_time = i * 0.5f;
|
||||
float end_time = j * 0.5f;
|
||||
|
||||
// 跨天处理:24.0显示为0.0
|
||||
if (end_time == 24.0f)
|
||||
{
|
||||
end_time = 0.0f;
|
||||
}
|
||||
|
||||
// 根据费率类型设置电费和服务费
|
||||
float electricity_fee = 0.0f;
|
||||
float service_fee = 0.0f;
|
||||
|
||||
switch (current_type)
|
||||
{
|
||||
case 0x00: // 尖时
|
||||
electricity_fee = g_charger_manager.fee_model_global.shark_fee_ratio / 100000.0f;
|
||||
service_fee = g_charger_manager.fee_model_global.shark_service_ratio / 100000.0f;
|
||||
break;
|
||||
case 0x01: // 峰时
|
||||
electricity_fee = g_charger_manager.fee_model_global.peak_fee_ratio / 100000.0f;
|
||||
service_fee = g_charger_manager.fee_model_global.peak_service_ratio / 100000.0f;
|
||||
break;
|
||||
case 0x02: // 平时
|
||||
electricity_fee = g_charger_manager.fee_model_global.flat_fee_ratio / 100000.0f;
|
||||
service_fee = g_charger_manager.fee_model_global.flat_service_ratio / 100000.0f;
|
||||
break;
|
||||
case 0x03: // 谷时
|
||||
electricity_fee = g_charger_manager.fee_model_global.valley_fee_ratio / 100000.0f;
|
||||
service_fee = g_charger_manager.fee_model_global.valley_service_ratio / 100000.0f;
|
||||
break;
|
||||
default:
|
||||
electricity_fee = 0.0f;
|
||||
service_fee = 0.0f;
|
||||
break;
|
||||
}
|
||||
|
||||
// 创建计费时段数组 [start_time, end_time, electricity_fee, service_fee]
|
||||
if (electricity_fee > 0 || service_fee > 0)
|
||||
{
|
||||
billing_item = cJSON_CreateArray();
|
||||
if (billing_item != NULL)
|
||||
{
|
||||
cJSON_AddNumberToObject(billing_item, NULL, start_time);
|
||||
cJSON_AddNumberToObject(billing_item, NULL, end_time);
|
||||
cJSON_AddNumberToObject(billing_item, NULL, electricity_fee);
|
||||
cJSON_AddNumberToObject(billing_item, NULL, service_fee);
|
||||
cJSON_AddItemToArray(billing_array, billing_item);
|
||||
}
|
||||
}
|
||||
|
||||
i = j; // 跳到下一个不同费率段
|
||||
}
|
||||
|
||||
// 将数组添加到根对象
|
||||
cJSON_AddItemToObject(root, "billing_model", billing_array);
|
||||
|
||||
// 转换为JSON字符串并发送
|
||||
str = cJSON_Print(root);
|
||||
if (str != NULL)
|
||||
{
|
||||
// #ifdef DEBUG
|
||||
// printf("Send start command JSON: %s\r\n", str);
|
||||
// #endif
|
||||
|
||||
udp_send_response(stake_index, str, strlen(str));
|
||||
|
||||
free(str);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Failed to print JSON for stake %d\r\n", stake_index);
|
||||
}
|
||||
|
||||
cJSON_Delete(root);
|
||||
printf("Southbound: Start command sent to stake %d, gun %d, total %d billing periods\r\n",
|
||||
stake_index, gun_id, (int)cJSON_GetArraySize(billing_array));
|
||||
}
|
||||
|
||||
void local_on_cmd_callback_server_login_response(cJSON *json_pack)
|
||||
{
|
||||
|
||||
cJSON *request_id = cJSON_GetObjectItem(json_pack, "request_id");
|
||||
cJSON *username = cJSON_GetObjectItem(json_pack, "username");
|
||||
cJSON *password = cJSON_GetObjectItem(json_pack, "password");
|
||||
|
||||
cJSON *root = NULL;
|
||||
char *str = NULL;
|
||||
|
||||
root = cJSON_CreateObject();
|
||||
if (root == NULL)
|
||||
{
|
||||
printf("Failed to create JSON object for server login\r\n");
|
||||
return;
|
||||
}
|
||||
|
||||
/* 添加一条字符串类型的JSON数据(添加一个链表节点) */
|
||||
cJSON_AddStringToObject(root, "request_id", request_id->valuestring);
|
||||
|
||||
if (strcmp(username->valuestring, "admin") != 0 || strcmp(password->valuestring, "123456") != 0)
|
||||
{
|
||||
cJSON_AddStringToObject(root, "error", "用户或密码错误 ");
|
||||
cJSON_AddBoolToObject(root, "success", cJSON_False);
|
||||
printf("登录失败\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
cJSON_AddBoolToObject(root, "success", cJSON_True);
|
||||
printf("登录成功\r\n");
|
||||
}
|
||||
str = cJSON_Print(root);
|
||||
udp_send_response(7, str, strlen(str));
|
||||
free(str);
|
||||
cJSON_Delete(root);
|
||||
|
||||
}
|
||||
|
||||
// 处理获取状态查询指令
|
||||
void local_on_cmd_callback_server_get_status_response(cJSON *json_pack)
|
||||
{
|
||||
cJSON *request_id = cJSON_GetObjectItem(json_pack, "request_id");
|
||||
|
||||
cJSON *root = NULL;
|
||||
cJSON *piles = NULL;
|
||||
cJSON *pile = NULL;
|
||||
cJSON *guns = NULL;
|
||||
cJSON *gun = NULL;
|
||||
char *str = NULL;
|
||||
|
||||
root = cJSON_CreateObject();
|
||||
if (root == NULL)
|
||||
{
|
||||
printf("Failed to create JSON object for get_status\r");
|
||||
return;
|
||||
}
|
||||
|
||||
cJSON_AddStringToObject(root, "request_id", request_id->valuestring);
|
||||
cJSON_AddBoolToObject(root, "success", cJSON_True);
|
||||
|
||||
/* ── piles 数组 ── */
|
||||
piles = cJSON_CreateArray();
|
||||
|
||||
/* 桩 1 */
|
||||
pile = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(pile, "serial", "32010601111558");
|
||||
cJSON_AddBoolToObject(pile, "is_online", cJSON_True);
|
||||
guns = cJSON_CreateArray();
|
||||
gun = cJSON_CreateObject();
|
||||
cJSON_AddNumberToObject(gun, "status", 1);
|
||||
cJSON_AddItemToArray(guns, gun);
|
||||
gun = cJSON_CreateObject();
|
||||
cJSON_AddNumberToObject(gun, "status", 0);
|
||||
cJSON_AddItemToArray(guns, gun);
|
||||
cJSON_AddItemToObject(pile, "guns", guns);
|
||||
cJSON_AddItemToArray(piles, pile);
|
||||
|
||||
/* 桩 2 */
|
||||
pile = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(pile, "serial", "32010601111559");
|
||||
cJSON_AddBoolToObject(pile, "is_online", cJSON_True);
|
||||
guns = cJSON_CreateArray();
|
||||
gun = cJSON_CreateObject();
|
||||
cJSON_AddNumberToObject(gun, "status", 1);
|
||||
cJSON_AddItemToArray(guns, gun);
|
||||
gun = cJSON_CreateObject();
|
||||
cJSON_AddNumberToObject(gun, "status", 1);
|
||||
cJSON_AddItemToArray(guns, gun);
|
||||
cJSON_AddItemToObject(pile, "guns", guns);
|
||||
cJSON_AddItemToArray(piles, pile);
|
||||
|
||||
/* 桩 3 */
|
||||
pile = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(pile, "serial", "32010601111560");
|
||||
cJSON_AddBoolToObject(pile, "is_online", cJSON_True);
|
||||
guns = cJSON_CreateArray();
|
||||
gun = cJSON_CreateObject();
|
||||
cJSON_AddNumberToObject(gun, "status", 0);
|
||||
cJSON_AddItemToArray(guns, gun);
|
||||
gun = cJSON_CreateObject();
|
||||
cJSON_AddNumberToObject(gun, "status", 0);
|
||||
cJSON_AddItemToArray(guns, gun);
|
||||
cJSON_AddItemToObject(pile, "guns", guns);
|
||||
cJSON_AddItemToArray(piles, pile);
|
||||
|
||||
/* 桩 4 */
|
||||
pile = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(pile, "serial", "32010601111561");
|
||||
cJSON_AddBoolToObject(pile, "is_online", cJSON_False);
|
||||
guns = cJSON_CreateArray();
|
||||
gun = cJSON_CreateObject();
|
||||
cJSON_AddNumberToObject(gun, "status", 0);
|
||||
cJSON_AddItemToArray(guns, gun);
|
||||
gun = cJSON_CreateObject();
|
||||
cJSON_AddNumberToObject(gun, "status", 0);
|
||||
cJSON_AddItemToArray(guns, gun);
|
||||
cJSON_AddItemToObject(pile, "guns", guns);
|
||||
cJSON_AddItemToArray(piles, pile);
|
||||
|
||||
/* 桩 5 */
|
||||
pile = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(pile, "serial", "32010601111562");
|
||||
cJSON_AddBoolToObject(pile, "is_online", cJSON_True);
|
||||
guns = cJSON_CreateArray();
|
||||
gun = cJSON_CreateObject();
|
||||
cJSON_AddNumberToObject(gun, "status", 1);
|
||||
cJSON_AddItemToArray(guns, gun);
|
||||
gun = cJSON_CreateObject();
|
||||
cJSON_AddNumberToObject(gun, "status", 0);
|
||||
cJSON_AddItemToArray(guns, gun);
|
||||
cJSON_AddItemToObject(pile, "guns", guns);
|
||||
cJSON_AddItemToArray(piles, pile);
|
||||
|
||||
/* 桩 6 */
|
||||
pile = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(pile, "serial", "32010601111563");
|
||||
cJSON_AddBoolToObject(pile, "is_online", cJSON_False);
|
||||
guns = cJSON_CreateArray();
|
||||
gun = cJSON_CreateObject();
|
||||
cJSON_AddNumberToObject(gun, "status", 0);
|
||||
cJSON_AddItemToArray(guns, gun);
|
||||
gun = cJSON_CreateObject();
|
||||
cJSON_AddNumberToObject(gun, "status", 0);
|
||||
cJSON_AddItemToArray(guns, gun);
|
||||
cJSON_AddItemToObject(pile, "guns", guns);
|
||||
cJSON_AddItemToArray(piles, pile);
|
||||
|
||||
cJSON_AddItemToObject(root, "piles", piles);
|
||||
|
||||
/* ── 发送 ── */
|
||||
str = cJSON_Print(root);
|
||||
udp_send_response(7, str, strlen(str));
|
||||
free(str);
|
||||
cJSON_Delete(root);
|
||||
printf("get_status 回复已发送 \r\n");
|
||||
}
|
||||
|
||||
void handle_udp_downlink(uint8_t id, const char *cmd, cJSON *json_pack)
|
||||
{
|
||||
@@ -275,6 +747,26 @@ void handle_udp_downlink(uint8_t id, const char *cmd, cJSON *json_pack)
|
||||
printf("南向:收到电桩 %d 心跳指令\r\n", id);
|
||||
local_on_cmd_callback_heartbeat_response(id, json_pack);
|
||||
}
|
||||
// 处理实时数据指令
|
||||
else if (strcmp(cmd, "realtime data") == 0)
|
||||
{
|
||||
printf("南向:收到电桩 %d 实时数据指令 \r\n", id);
|
||||
local_on_cmd_callback_realtime_data_response(id, json_pack);
|
||||
}
|
||||
|
||||
/*处理上位机指令 */
|
||||
// 处理登录指令
|
||||
else if (strcmp(cmd, "server_login") == 0)
|
||||
{
|
||||
printf("上位机:收到登录指令 \r\n");
|
||||
local_on_cmd_callback_server_login_response(json_pack);
|
||||
}
|
||||
// 处理获取状态查询指令
|
||||
else if (strcmp(cmd, "server_get_status") == 0)
|
||||
{
|
||||
printf("上位机:收到获取状态查询指令 \r\n");
|
||||
local_on_cmd_callback_server_get_status_response(json_pack);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Unknown CMD: '%s' from ID %d\r\n", cmd, id);
|
||||
@@ -292,209 +784,10 @@ void trade_serial_to_string(uint8_t *trade_serial, char *output_str)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
for (int i = 0; i < 16; i++)
|
||||
{
|
||||
sprintf(&output_str[i * 2], "%02X", trade_serial[i]);
|
||||
}
|
||||
output_str[32] = '\0';
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 主动下发启动指令
|
||||
* @note 发送启动指令到充电桩,符合协议格式要求
|
||||
* @param stake_index 桩索引 (1~6)
|
||||
* @param gun_id 枪编码 (1~N)
|
||||
*/
|
||||
void local_on_cmd_send_start_charging(uint8_t stake_index, uint8_t gun_id)
|
||||
{
|
||||
if (stake_index > 6 || stake_index == 0)
|
||||
{
|
||||
printf("Invalid stake index: %d\r\n", stake_index);
|
||||
return;
|
||||
}
|
||||
|
||||
if (gun_id == 0 || gun_id > MAX_GUN_PER_CHARGER)
|
||||
{
|
||||
printf("Invalid gun id: %d\r\n", gun_id);
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查是否已获取费率模型
|
||||
if (!g_charger_manager.charger_piles[stake_index - 1].get_model)
|
||||
{
|
||||
printf("Stake %d fee model not ready\r\n", stake_index);
|
||||
return;
|
||||
}
|
||||
|
||||
cJSON *root = NULL;
|
||||
cJSON *billing_array = NULL;
|
||||
cJSON *billing_item = NULL;
|
||||
char *str = NULL;
|
||||
|
||||
// 转换交易ID:16位数组转字符串
|
||||
char transaction_id_str[33] = {0}; // 32位 + 结束符
|
||||
uint8_t *trade_serial = g_charger_manager.charger_piles[stake_index - 1].guns[gun_id - 1].real_time_data.trade_serial;
|
||||
trade_serial_to_string(trade_serial, transaction_id_str);
|
||||
|
||||
root = cJSON_CreateObject();
|
||||
if (root == NULL)
|
||||
{
|
||||
printf("Failed to create JSON object for stake %d\r\n", stake_index);
|
||||
return;
|
||||
}
|
||||
|
||||
/* 添加基本字段 */
|
||||
cJSON_AddNumberToObject(root, "id", stake_index);
|
||||
cJSON_AddStringToObject(root, "cmd", "start charging");
|
||||
cJSON_AddStringToObject(root, "transaction_id", transaction_id_str); // 使用转换后的32位字符串
|
||||
cJSON_AddNumberToObject(root, "gun_id", gun_id);
|
||||
cJSON_AddStringToObject(root, "type", "request");
|
||||
|
||||
/* 添加限制金额(根据实际业务设置)*/
|
||||
cJSON_AddNumberToObject(root, "limit_amount", 30.00);
|
||||
|
||||
/* 创建计费模型数组 */
|
||||
billing_array = cJSON_CreateArray();
|
||||
if (billing_array == NULL)
|
||||
{
|
||||
printf("Failed to create billing array\r\n");
|
||||
cJSON_Delete(root);
|
||||
return;
|
||||
}
|
||||
|
||||
/* 计费时段:根据费率表生成 period_id 1:尖 2:峰 3:平 4:谷 */
|
||||
int period_id = 1;
|
||||
int i = 0;
|
||||
|
||||
while (i < 48) // 48个半小时时段
|
||||
{
|
||||
uint8_t current_type = g_charger_manager.fee_model_global.fee_num[i];
|
||||
|
||||
// 跳过无效费率
|
||||
if (current_type == 0xFF)
|
||||
{
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// 查找连续相同费率的结束位置
|
||||
int j = i;
|
||||
while (j < 48 && g_charger_manager.fee_model_global.fee_num[j] == current_type)
|
||||
{
|
||||
j++;
|
||||
}
|
||||
|
||||
// 计算起始时间(第i个半小时段)
|
||||
int start_hour = i / 2;
|
||||
int start_min = (i % 2) * 30;
|
||||
|
||||
// 计算结束时间(第j个半小时段的起始时间)
|
||||
int end_hour = j / 2;
|
||||
int end_min = (j % 2) * 30;
|
||||
|
||||
// 格式化时间字符串 "HH:MM"
|
||||
char start_time[6];
|
||||
char end_time[6];
|
||||
sprintf(start_time, "%02d:%02d", start_hour, start_min);
|
||||
|
||||
if (end_hour == 24 && end_min == 0)
|
||||
{
|
||||
sprintf(end_time, "00:00"); // 跨天显示为00:00
|
||||
}
|
||||
else
|
||||
{
|
||||
sprintf(end_time, "%02d:%02d", end_hour, end_min);
|
||||
}
|
||||
|
||||
// 根据费率类型设置电费和服务费
|
||||
float electricity_fee = 0.0f;
|
||||
float service_fee = 0.0f;
|
||||
int mapped_period_id = 0; // 映射到协议要求的period_id
|
||||
|
||||
switch (current_type)
|
||||
{
|
||||
case 0x00: // 尖时
|
||||
electricity_fee = g_charger_manager.fee_model_global.shark_fee_ratio / 100000.0f;
|
||||
service_fee = g_charger_manager.fee_model_global.shark_service_ratio / 100000.0f;
|
||||
mapped_period_id = 1; // 1:尖
|
||||
break;
|
||||
case 0x01: // 峰时
|
||||
electricity_fee = g_charger_manager.fee_model_global.peak_fee_ratio / 100000.0f;
|
||||
service_fee = g_charger_manager.fee_model_global.peak_service_ratio / 100000.0f;
|
||||
mapped_period_id = 2; // 2:峰
|
||||
break;
|
||||
case 0x02: // 平时
|
||||
electricity_fee = g_charger_manager.fee_model_global.flat_fee_ratio / 100000.0f;
|
||||
service_fee = g_charger_manager.fee_model_global.flat_service_ratio / 100000.0f;
|
||||
mapped_period_id = 3; // 3:平
|
||||
break;
|
||||
case 0x03: // 谷时
|
||||
electricity_fee = g_charger_manager.fee_model_global.valley_fee_ratio / 100000.0f;
|
||||
service_fee = g_charger_manager.fee_model_global.valley_service_ratio / 100000.0f;
|
||||
mapped_period_id = 4; // 4:谷
|
||||
break;
|
||||
default:
|
||||
electricity_fee = 0.0f;
|
||||
service_fee = 0.0f;
|
||||
mapped_period_id = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
// 创建计费时段对象(只有有效费率类型才添加)
|
||||
if (mapped_period_id > 0)
|
||||
{
|
||||
billing_item = cJSON_CreateObject();
|
||||
if (billing_item != NULL)
|
||||
{
|
||||
cJSON_AddNumberToObject(billing_item, "period_id", mapped_period_id);
|
||||
cJSON_AddStringToObject(billing_item, "start_time", start_time);
|
||||
cJSON_AddStringToObject(billing_item, "end_time", end_time);
|
||||
cJSON_AddNumberToObject(billing_item, "electricity_fee", electricity_fee);
|
||||
cJSON_AddNumberToObject(billing_item, "service_fee", service_fee);
|
||||
|
||||
cJSON_AddItemToArray(billing_array, billing_item);
|
||||
|
||||
// 调试打印
|
||||
#ifdef DEBUG
|
||||
const char *period_name = "";
|
||||
switch(mapped_period_id)
|
||||
{
|
||||
case 1: period_name = "Sharp"; break;
|
||||
case 2: period_name = "Peak"; break;
|
||||
case 3: period_name = "Flat"; break;
|
||||
case 4: period_name = "Valley"; break;
|
||||
}
|
||||
printf("Period %d: %s-%s [%s] electricity:%.3f service:%.3f\r\n",
|
||||
mapped_period_id, start_time, end_time, period_name,
|
||||
electricity_fee, service_fee);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
i = j; // 跳到下一个不同费率段
|
||||
}
|
||||
|
||||
// 将数组添加到根对象
|
||||
cJSON_AddItemToObject(root, "billing_model", billing_array);
|
||||
|
||||
// 转换为JSON字符串并发送
|
||||
str = cJSON_Print(root);
|
||||
if (str != NULL)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
printf("Send start command JSON: %s\r\n", str);
|
||||
#endif
|
||||
|
||||
udp_send_response(stake_index, str, strlen(str));
|
||||
free(str);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Failed to print JSON for stake %d\r\n", stake_index);
|
||||
}
|
||||
|
||||
cJSON_Delete(root);
|
||||
printf("Southbound: Start command sent to stake %d, gun %d, total %d billing periods\r\n",
|
||||
stake_index, gun_id, (int)cJSON_GetArraySize(billing_array));
|
||||
}
|
||||
@@ -24,13 +24,12 @@
|
||||
*/
|
||||
|
||||
uint8_t SETP = 0;
|
||||
|
||||
void YkcTask_Function(void const *argument)
|
||||
{
|
||||
init_chargers(); /* 初始化桩结构体*/
|
||||
ulTaskNotifyTake(pdTRUE, portMAX_DELAY); /* 等待桩通讯协议层完成*/
|
||||
osDelay(5000);
|
||||
// send_server_address_to_air724();
|
||||
// send_server_address_to_air724();
|
||||
while (1)
|
||||
{
|
||||
TaskRunTimeStat.YkcTask.threads_runtime = GetTask_RunTime(YkcTaskID);
|
||||
@@ -39,52 +38,56 @@ void YkcTask_Function(void const *argument)
|
||||
|
||||
switch (SETP)
|
||||
{
|
||||
case 0:
|
||||
{
|
||||
|
||||
if (g_charger_manager.charger_piles[1 - 1].is_udp_online)
|
||||
{
|
||||
uint8_t open_cmd[] = { 0x55, 0xAA, 0x3, 0x01, 0x01, 0xAA, 0x55};
|
||||
Air724_Message_Send(open_cmd, sizeof(open_cmd));
|
||||
osDelay(3000);
|
||||
SETP = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("网关:等待桩上电指令\r\n");
|
||||
osDelay(1000);
|
||||
SETP = 0;
|
||||
}
|
||||
|
||||
case 0:
|
||||
{
|
||||
// 等待桩上电
|
||||
if (!g_charger_manager.charger_piles[1 - 1].is_udp_online)
|
||||
{
|
||||
uint8_t open_cmd[] = {0x55, 0xAA, 0x3, 0x01, 0x01, 0xAA, 0x55};
|
||||
Air724_Message_Send(open_cmd, sizeof(open_cmd));
|
||||
osDelay(3000);
|
||||
SETP = 1;
|
||||
}
|
||||
break;
|
||||
else
|
||||
{
|
||||
printf("网关:等待桩上电指令\r\n");
|
||||
osDelay(1000);
|
||||
SETP = 0;
|
||||
}
|
||||
}
|
||||
break;
|
||||
// 云快充登录认证
|
||||
case 1:
|
||||
{
|
||||
if (!g_charger_manager.charger_piles[1 - 1].is_online)
|
||||
{
|
||||
charger_to_server_0X01(1);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
SETP = 2;
|
||||
charger_to_server_0X05(1,1);
|
||||
}
|
||||
osDelay(5000);
|
||||
osDelay(5000);
|
||||
}
|
||||
break;
|
||||
// 云快充计费模型请求
|
||||
case 2:
|
||||
{
|
||||
if (!g_charger_manager.charger_piles[1 - 1].get_model)
|
||||
|
||||
|
||||
charger_to_server_0X09(1); // 桩1计费模型请求
|
||||
else
|
||||
SETP = 3;
|
||||
osDelay(3000);
|
||||
}
|
||||
break;
|
||||
|
||||
case 3:
|
||||
{
|
||||
charger_to_server_0X13(1, 1); // 上传状态
|
||||
local_on_cmd_send_start_charging(1,1);
|
||||
// local_on_cmd_send_start_charging(1, 1);
|
||||
osDelay(3000);
|
||||
}
|
||||
break;
|
||||
@@ -92,7 +95,7 @@ void YkcTask_Function(void const *argument)
|
||||
break;
|
||||
}
|
||||
|
||||
//osDelay(2000);
|
||||
// osDelay(2000);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user