From 9559f288ca2e6ee5b5ee5104690d3c6f950308f9 Mon Sep 17 00:00:00 2001 From: zhangwen <3466561528@qq.com> Date: Sat, 28 Feb 2026 09:22:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=89=8B=E5=8A=A8=E5=AF=B9?= =?UTF-8?q?=E6=97=B6=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LFtid1056/client2.cpp | 31 ++++++++++++++++++++++++++++++- LFtid1056/client2.h | 6 +++++- LFtid1056/dealMsg.cpp | 25 ++++++++++++++++++++++++- 3 files changed, 59 insertions(+), 3 deletions(-) diff --git a/LFtid1056/client2.cpp b/LFtid1056/client2.cpp index 9b1e636..26b17d1 100644 --- a/LFtid1056/client2.cpp +++ b/LFtid1056/client2.cpp @@ -1630,7 +1630,7 @@ bool ClientManager::set_interfixedvalue_action_to_device(const std::string& iden return false; // 设备未找到 } -//读取装置运行信息 +//读取装置运行信息(外部触发调用) bool ClientManager::read_runninginformation_action_to_device(const std::string& identifier) { std::lock_guard lock(mutex_); @@ -1657,6 +1657,35 @@ bool ClientManager::read_runninginformation_action_to_device(const std::string& return false; // 设备未找到 } +//装置主动对时动作(外部触发调用) +bool ClientManager::set_righttime_action_to_device(const std::string& identifier) { + std::lock_guard lock(mutex_); + + // 查找匹配的设备 + for (auto& pair : clients_) { + auto& ctx = pair.second; + if ((ctx->device_info.device_id == identifier || + ctx->device_info.mac == identifier) && ctx->device_info.righttime && isVersionGreaterOrEqual(ctx->dev_CloudProtocolVer, "V1.5")) + { + // 获取当前时间 + auto now = std::chrono::system_clock::now(); + std::time_t time = std::chrono::system_clock::to_time_t(now); + std::tm tm_time = *std::localtime(&time); + auto sendbuff = generate_righttime_message(tm_time);//组装装置对时报文 + // 添加动作到队列 (状态: 手动对时) + ctx->add_action(DeviceState::SET_RIGHTTIME_2, sendbuff);//将该状态以及待发送报文存入队列 + + // 如果当前空闲则立即执行 + if (ctx->current_state_ == DeviceState::IDLE) { + ctx->process_next_action(); + } + + return true; // 成功添加 + } + } + return false; // 设备未找到 +} + //读取装置版本配置信息 bool ClientManager::read_devversion_action_to_device(const std::string& identifier) { std::lock_guard lock(mutex_); diff --git a/LFtid1056/client2.h b/LFtid1056/client2.h index d0cae85..c68ec93 100644 --- a/LFtid1056/client2.h +++ b/LFtid1056/client2.h @@ -60,7 +60,8 @@ enum class DeviceState { READING_RUNNINGINFORMATION_1,// ȡװϢ() READING_RUNNINGINFORMATION_2,// ȡװϢ(ʱִ) READING_DEVVERSION, // ȡװð汾Ϣ - SET_RIGHTTIME, // װöʱ + SET_RIGHTTIME, // װöʱʱִУ + SET_RIGHTTIME_2, // װöʱ READING_EVENTLOG, // ¼־ READING_STATSFILE, // ̬ļ // ɸҪӸ״̬ @@ -442,6 +443,9 @@ public: //ȡװð汾Ϣ bool read_devversion_action_to_device(const std::string& identifier); + //װʱ + bool set_righttime_action_to_device(const std::string& identifier); + //豸ж bool get_dev_status(const std::string& identifier); /** diff --git a/LFtid1056/dealMsg.cpp b/LFtid1056/dealMsg.cpp index db47593..b611cec 100644 --- a/LFtid1056/dealMsg.cpp +++ b/LFtid1056/dealMsg.cpp @@ -1933,7 +1933,30 @@ void process_received_message(string mac, string id,const char* data, size_t len break; case DeviceState::SET_RIGHTTIME: - //设置装置对时 + //设置装置对时(定时触发) + if (udata[8] == static_cast(MsgResponseType::Response_NewACK)) { + std::cout << "set success" << mac << std::endl; + //对时设置成功,调整为空闲,处理后续工作。 + ClientManager::instance().change_device_state(id, DeviceState::IDLE); + } + else if (udata[8] == static_cast(MsgResponseType::Response_NewNACK)) { + std::cout << "set error" << mac << std::endl; + + std::cout << "reason code: " << static_cast(udata[8]) << "-" << static_cast(udata[9]) << "-" << static_cast(udata[10]) << "-" << static_cast(udata[11]) << std::endl; + + // 装置否定应答,对时设置失败 + // 设置对时失败,调整为空闲状态,处理下一项工作。 + ClientManager::instance().change_device_state(id, DeviceState::IDLE); + } + else { + // 装置答非所问异常 + // 设置对时失败,调整为空闲状态,处理下一项工作。 + ClientManager::instance().change_device_state(id, DeviceState::IDLE); + } + break; + + case DeviceState::SET_RIGHTTIME_2: + //设置装置对时(主动对时) if (udata[8] == static_cast(MsgResponseType::Response_NewACK)) { std::cout << "set success" << mac << std::endl; //对时设置成功,调整为空闲,处理后续工作。