From 3ae8ba5900d2adf0d773d20bfafe5c9d08838d63 Mon Sep 17 00:00:00 2001 From: zw <3466561528@qq.com> Date: Wed, 9 Jul 2025 10:03:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E5=AE=9E=E6=97=B6?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=9A=84=E6=8A=A5=E6=96=87=E7=BB=84=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LFtid1056/PQSMsg.cpp | 32 ++++++++++++++++++++++++ LFtid1056/PQSMsg.h | 12 ++++++--- LFtid1056/client2.cpp | 58 +++++++++++++++++++++++++++++++++++-------- LFtid1056/client2.h | 7 ++++++ LFtid1056/dealMsg.cpp | 1 + 5 files changed, 97 insertions(+), 13 deletions(-) diff --git a/LFtid1056/PQSMsg.cpp b/LFtid1056/PQSMsg.cpp index 14e5e5c..225d669 100644 --- a/LFtid1056/PQSMsg.cpp +++ b/LFtid1056/PQSMsg.cpp @@ -203,5 +203,37 @@ std::vector generate_statequerystat_message(tagTime time, uint16_ return GetMsg(DataBuf, static_cast(MsgRequestType::Request_Stat)); } +//询问实时数据报文 测点1-6 数据类型1-11 谐波次数0-2 +std::vector generate_realstat_message(unsigned char nCpuNo, + unsigned char StaTtype, + unsigned char flag) +{ + // 计算总大小:3(备用) + 1(nCpuNo) + 1(StaTtype) + 1(固定值) + 1(flag) + const size_t totalSize = 7; + std::vector DataBuf(totalSize, 0x00); // 初始化为全0 + size_t offset = 0; + + // 1. 跳过3字节备用区(已初始化为0) + offset += 3; + + // 2. 写入nCpuNo(1字节) 监测点序号 1-6 + memcpy(DataBuf.data() + offset, &nCpuNo, sizeof(unsigned char)); + offset += sizeof(unsigned char); + + // 3. 写入StaTtype(1字节) 数据类型 1-11 一般仅读 1-6 + memcpy(DataBuf.data() + offset, &StaTtype, sizeof(unsigned char)); + offset += sizeof(unsigned char); + + // 4. 写入固定值1(1字节) 3秒实时数据仅有1 表示平均值 + const unsigned char fixedValue = 1; + memcpy(DataBuf.data() + offset, &fixedValue, sizeof(unsigned char)); + offset += sizeof(unsigned char); + + // 5. 写入flag(1字节) 标志位 0-2 谐波次数 25 50 100次 + memcpy(DataBuf.data() + offset, &flag, sizeof(unsigned char)); + + // 生成完整报文 + return GetMsg(DataBuf, static_cast(MsgRequestType::Request_New_3S)); +} diff --git a/LFtid1056/PQSMsg.h b/LFtid1056/PQSMsg.h index 689c81e..7bb96bb 100644 --- a/LFtid1056/PQSMsg.h +++ b/LFtid1056/PQSMsg.h @@ -21,14 +21,18 @@ enum class MsgRequestType : unsigned char { //询问统计数据时间 Request_StatTime = 0x8b, //询问统计数据 - Request_Stat = 0x8a + Request_Stat = 0x8a, + //询问实时数据 + Request_New_3S = 0x04 }; // 接收报文功能码枚举 enum class MsgResponseType : unsigned char { //询问统计数据时间 Response_StatTime = 0x27, //询问统计数据 - Response_Stat = 0x26 + Response_Stat = 0x26, + //询问实时数据 + Response_New_3S = 0x84 }; //基础消息结构 class MessageParser { @@ -802,4 +806,6 @@ std::vector generate_frontlogin_message(const std::string& strMac //生成询问统计数据时间报文 std::vector generate_statequerytime_message(); //生成询问统计数据报文 -std::vector generate_statequerystat_message(tagTime time, uint16_t nDeviceNo, uint16_t nDataType); \ No newline at end of file +std::vector generate_statequerystat_message(tagTime time, uint16_t nDeviceNo, uint16_t nDataType); +//生成询问实时数据报文 +std::vector generate_realstat_message(unsigned char nCpuNo, unsigned char StaTtype, unsigned char flag); \ No newline at end of file diff --git a/LFtid1056/client2.cpp b/LFtid1056/client2.cpp index 6ea7720..b91ddc2 100644 --- a/LFtid1056/client2.cpp +++ b/LFtid1056/client2.cpp @@ -53,7 +53,7 @@ void ClientContext::init_tcp() { void ClientContext::start_timer() { if (!uv_is_active((uv_handle_t*)&timer)) { - uv_timer_start(&timer, on_timer, 5000,5000); + uv_timer_start(&timer, on_timer, 1000,1000); } } @@ -355,29 +355,36 @@ void on_write(uv_write_t* req, int status) { } /* 瀹氭椂鍙戦佸洖璋 */ -//5绉掓墽琛屼竴娆″畾鏃跺櫒 +//1绉掓墽琛屼竴娆″畾鏃跺櫒 void on_timer(uv_timer_t* handle) { ClientContext* ctx = static_cast(handle->data); if (ctx->state != ConnectionState::CONNECTED) { return; } - - static int statequerytime = 0;//璇㈤棶缁熻鏁版嵁鏃堕棿鏍囧織 20绉掓墽琛屼竴娆 - // 妫鏌ョ姸鎬佽秴鏃 30绉掔姸鎬佹湭鏇存柊鍒欓噸缃负绌洪棽鐘舵 ctx->check_state_timeout(); // 瑁呯疆鐧诲綍鎴愬姛鍚庯紝鍙湪绌洪棽鐘舵佸鐞嗗悗缁姩浣 if (ctx->cloudstatus == 1) { - + uint64_t now = uv_now(ctx->loop);//褰撳墠鏃堕棿鎴 //20绉掍竴娆 鎵ц缁熻鏁版嵁鏃堕棿璇㈤棶 - if (++statequerytime >= 4 && ctx->current_state_ == DeviceState::IDLE) { - statequerytime = 0;//閲嶇疆璁℃椂 + if (ctx->current_state_ == DeviceState::IDLE && now - ctx->last_state_query_time_ >= 20000) { + // 鏇存柊缁熻鏁版嵁鏈鍚庢煡璇㈡椂闂 + ctx->last_state_query_time_ = now; + auto sendbuff = generate_statequerytime_message();//缁勮璇㈤棶缁熻鏁版嵁鏃堕棿鎶ユ枃 ctx->add_action(DeviceState::READING_STATS_TIME, sendbuff);//灏嗚鐘舵佷互鍙婂緟鍙戦佹姤鏂囧瓨鍏ラ槦鍒 } - + //涓绉掍竴娆 鎵ц瀹炴椂鏁版嵁璇㈤棶 浠呮墽琛屾寚瀹氭鏁 + if (ctx->current_state_ == DeviceState::IDLE && now - ctx->real_state_query_time_ >= 1000 && ctx->real_state_count > 0) { + // 鏇存柊瀹炴椂鏁版嵁鎵ц鏃堕棿鍜屽疄鏃舵敹鍙戣鏁 + ctx->real_state_query_time_ = now; + ctx->real_state_count--; + + //auto sendbuff = generate_realstat_message(static_cast(ctx->real_point_id_), static_cast(0x01), static_cast(0x01));//缁勮璇㈤棶瀹炴椂鏁版嵁鎶ユ枃 + //ctx->add_action(DeviceState::READING_REALSTAT, sendbuff);//灏嗚鐘舵佷互鍙婂緟鍙戦佹姤鏂囧瓨鍏ラ槦鍒 + } //澶勭悊鍚庣画宸ヤ綔闃熷垪鐨勫伐浣 鍙栧嚭涓涓苟鎵ц if (ctx->current_state_ == DeviceState::IDLE) { ctx->process_next_action(); @@ -489,7 +496,10 @@ void on_connect(uv_connect_t* req, int status) { ctx->state = ConnectionState::CONNECTED; ctx->reconnect_attempts = 0; - + // 鏂板锛氬垵濮嬪寲鍚勪釜璁℃椂鏃堕棿鎴 + ctx->last_state_query_time_ = uv_now(ctx->loop);//鍒濆鍖栫粺璁℃暟鎹椂闂存埑 + ctx->real_state_query_time_ = uv_now(ctx->loop);//鍒濆鍖栧疄鏃舵暟鎹椂闂存埑 + ctx->real_state_count = 0;//瀹炴椂鏁版嵁鏀跺彂璁℃暟 //瀹㈡埛绔繛鎺ュ畬姣曞悗锛屽彂閫佽缃櫥闄嗘秷鎭 std::cout << "connected: " << ctx->device_info.mac << " send login msg!" << std::endl; auto binary_data = generate_frontlogin_message(ctx->device_info.mac); @@ -973,4 +983,32 @@ bool ClientManager::clear_float_cache(const std::string& identifier) { } } return false; +} + +bool ClientManager::set_real_state_count(const std::string& identifier, int count, ushort point_id = 1) { + 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->real_state_count = count; + + // 璁剧疆娴嬬偣搴忓彿锛堝鏋滄彁渚涗簡鏈夋晥鍊硷級 + if (point_id != 0) { + ctx->real_point_id_ = point_id; + } + + std::cout << "[Device " << identifier + << "] Real state params set - count: " << count + << ", point_id: " << ctx->real_point_id_.load() + << std::endl; + return true; + } + } + + std::cerr << "[set_real_state_count] Device not found: " << identifier << std::endl; + return false; } \ No newline at end of file diff --git a/LFtid1056/client2.h b/LFtid1056/client2.h index 37cf800..415fa54 100644 --- a/LFtid1056/client2.h +++ b/LFtid1056/client2.h @@ -43,6 +43,7 @@ enum class DeviceState { IDLE, // 空闲状态 READING_STATS, // 读取统计数据 READING_STATS_TIME, // 读取统计时间 + READING_REALSTAT, // 读取实时数据 // 可根据需要添加更多状态 CUSTOM_ACTION // 自定义动作 }; @@ -62,6 +63,10 @@ public: ConnectionState state; int reconnect_attempts; volatile bool shutdown; + uint64_t last_state_query_time_ = 0; // 统计数据计时时间戳 + uint64_t real_state_query_time_ = 0; // 实时数据计时时间戳 + std::atomic real_state_count{ 0 };//实时数据收发计数 原子操作保证线程安全 + std::atomic real_point_id_{ 1 }; // 新增:实时数据读取的测点序号(原子操作) DeviceInfo device_info; // 装置信息 int cloudstatus; // 云前置登录状态(0:未登录 1:已登录) @@ -207,6 +212,8 @@ public: // 清除设备的所有浮点缓存 bool clear_float_cache(const std::string& identifier); + // 新增:设置实时数据收发计数 + bool set_real_state_count(const std::string& identifier, int count, ushort point_id = 1); private: ClientManager() : loop_(nullptr) {} std::unordered_map> clients_; diff --git a/LFtid1056/dealMsg.cpp b/LFtid1056/dealMsg.cpp index 56c145d..2232e29 100644 --- a/LFtid1056/dealMsg.cpp +++ b/LFtid1056/dealMsg.cpp @@ -37,6 +37,7 @@ void process_received_message(string mac, string id,const char* data, size_t len std::cout << "cloud login: " << mac << " state: success!" << std::endl; //装置登录成功 ClientManager::instance().set_cloud_status(id, 1); //设置了云前置登录状态为已登录 + //ClientManager::instance().set_real_state_count("D002", 10);//登录后测试实时 } if (udata[19] == 0x00) { std::cout << "cloud login: " << mac << " state: fail!" << std::endl;