添加了实时数据的报文组装
This commit is contained in:
@@ -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<ClientContext*>(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<unsigned char>(ctx->real_point_id_), static_cast<unsigned char>(0x01), static_cast<unsigned char>(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<std::mutex> 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;
|
||||
}
|
||||
Reference in New Issue
Block a user