/** ****************************************************************************** * @file user\task\HeartBeatTask.c * @author luhuaishuai * @version v0.1 * @date 2026-1-12 * @brief Briefly describe the function of your function ****************************************************************************** */ /* Includes ------------------------------------------------------------------*/ #include "task_ykc.h" #include "lwip/opt.h" #include "lwip/api.h" #include "lwip/sys.h" #include "usbd_cdc_if.h" /** * @funNm : ykc_task_function * @brief : 云快充平台交互主任务 * @param : argument * @retval: void */ void ykc_task_function(void const *argument) { uint32_t now; init_chargers(); ulTaskNotifyTake(pdTRUE, portMAX_DELAY); osDelay(5000); while (1) { TaskRunTimeStat.YkcTask.threads_runtime = GetTask_RunTime(YkcTaskID); TaskRunTimeStat.YkcTask.threads_counter = GetTask_Beatcnt(YkcTaskID); TaskRunTimeStat.YkcTask.threads_freestack = Get_Free_Stack(YkcTaskID); now = HAL_GetTick(); for (int i = 0; i < g_charger_manager.charger_count; i++) { ChargerPile *pile = &g_charger_manager.charger_piles[i]; /* ========== 桩层初始化(桩0~2通用,只做一次) ========== */ if (pile->step < 3) { if (now < pile->step_tick) continue; switch (pile->step) { case 0: // 等待桩上电 + 连接本桩4G通道 { // if (pile->is_udp_online) if(1) { if (!g_air724_info.socket_connected[i]) { drv_air724_connect_channel(i + 1); pile->step_tick = now + 3000; } else { pile->step = 1; pile->step_tick = now; } } else { USB_Printf("网关:等待桩%d上电指令\r\n", i + 1); drv_air724_query_iccid(); pile->step_tick = now + 1000; } } break; case 1: // 云快充登录认证 { if (!pile->is_online) { charger_to_server_0X01(i + 1); pile->step_tick = now + 5000; } else { pile->step = 2; charger_to_server_0X05(1, i + 1); pile->step_tick = now; } } break; case 2: // 云快充计费模型请求 { if (!pile->get_model) { charger_to_server_0X09(i + 1); pile->step_tick = now + 5000; } else { pile->step = 3; pile->step_tick = now; } } break; } continue; } /* ========== 枪层独立运行(每把枪独立状态机) ========== */ for (int g = 0; g < MAX_GUN_PER_CHARGER; g++) { ChargerGun *gun = &pile->guns[g]; if (now < gun->gun_step_tick) continue; switch (gun->gun_step) { case 0: // 运行时:周期上传实时数据 + 账单处理 { charger_to_server_0X13(i + 1, g + 1); host_computer_report_data(i + 1, g + 1); if (gun->is_get_bill) { charger_to_server_0X3B(i + 1, g + 1); charger_to_server_0X3B(i + 1, g + 1); gun->is_get_bill = 0; memset(gun->real_time_data.trade_serial, 0, sizeof(gun->real_time_data.trade_serial)); } gun->gun_step_tick = now + 5000; } break; default: break; } } } osDelay(10); } } /** * @brief 云快充平台状态检查回调 * @note 每 500ms 触发一次,遍历 1-6 号桩检查状态 * @param xTimer 定时器句柄 * @retval none */ void ykc_timer_callback_function(TimerHandle_t xTimer) { for (int i = 0; i < MAX_CHARGER_COUNT; i++) { for (int g = 0; g < MAX_GUN_PER_CHARGER; g++) { ChargerGun *gun = &g_charger_manager.charger_piles[i].guns[g]; if (gun->charger_state == REDAY_CHARGER_START_STATE) { USB_Printf("状态机:[info] 桩%d 枪%d 查询中,等待启动回复.... \r\n", i + 1, g + 1); } else if (gun->charger_state == SUCCESS_CHARGER_STATE) { USB_Printf("状态机:[success] 桩%d 枪%d 启动成功.... \r\n", i + 1, g + 1); gun->charger_state = CHARGER_STATE_CHARGING; } else if (gun->charger_state == FAIL_CHARGER_STATE) { USB_Printf("状态机:[error] 桩%d 枪%d 启动失败.... \r\n", i + 1, g + 1); } else if (gun->charger_state == CHARGER_STATE_CHARGING) { USB_Printf("状态机:[info] 桩%d 枪%d 充电中.... \r\n", i + 1, g + 1); } else if (gun->charger_state == REDAY_CHARGER_STOP_STATE) { USB_Printf("状态机:[info] 桩%d 枪%d 查询中,等待停止回复.... \r\n", i + 1, g + 1); } else if (gun->charger_state == SUCCESS_CHARGER_STOP_STATE) { USB_Printf("状态机:[success] 桩%d 枪%d 停止成功.... \r\n", i + 1, g + 1); } else if (gun->charger_state == FAIL_CHARGER_STOP_STATE) { USB_Printf("状态机:[error] 桩%d 枪%d 停止失败.... \r\n", i + 1, g + 1); } else if (gun->charger_state == CHARGER_STATE_CHARGE_DONE) { printf("状态机:[success] 桩%d 枪%d 计费数据已发送,充电完成.... \r\n", i + 1, g + 1); } } } }