63 lines
1.7 KiB
C++
63 lines
1.7 KiB
C++
#include <uv.h>
|
|
#include <string>
|
|
#include <vector>
|
|
#include <memory>
|
|
|
|
// 测点信息结构
|
|
struct PointInfo {
|
|
std::string point_id; // 测点ID
|
|
std::string name; // 测点名称
|
|
std::string device_id; // 所属装置ID
|
|
double PT1; // 电压变比1
|
|
double PT2; // 电压变比2
|
|
double CT1; // 电流变比1
|
|
double CT2; // 电流变比2
|
|
};
|
|
|
|
// 装置信息结构
|
|
struct DeviceInfo {
|
|
std::string device_id; // 装置ID
|
|
std::string name; // 装置名称
|
|
std::string model; // 装置型号
|
|
std::string mac; // 装置MAC地址
|
|
int status; // 运行状态 (0: 离线, 1: 在线)
|
|
std::vector<PointInfo> points; // 下属测点
|
|
};
|
|
|
|
enum class ConnectionState {
|
|
DISCONNECTED,
|
|
CONNECTING,
|
|
CONNECTED
|
|
};
|
|
|
|
class ClientContext {
|
|
public:
|
|
uv_loop_t* loop;
|
|
uv_tcp_t client;
|
|
uv_timer_t timer;
|
|
uv_timer_t reconnect_timer;
|
|
ConnectionState state;
|
|
int reconnect_attempts;
|
|
volatile bool shutdown;
|
|
DeviceInfo device_info; // 装置信息
|
|
|
|
ClientContext(uv_loop_t* loop, const DeviceInfo& device, int index);
|
|
~ClientContext();
|
|
|
|
void init_tcp();
|
|
void start_timer();
|
|
void start_reconnect_timer(int delay);
|
|
void stop_timers();
|
|
void close_handles();
|
|
|
|
private:
|
|
int index_;
|
|
};
|
|
|
|
// 函数声明
|
|
void start_client_connect(const std::vector<DeviceInfo>& devices);
|
|
void send_binary_data(ClientContext* ctx, const unsigned char* data, size_t data_size);
|
|
void on_timer(uv_timer_t* handle);//装置定时回调
|
|
void try_reconnect(uv_timer_t* timer);//装置重连回调
|
|
void on_connect(uv_connect_t* req, int status);//装置连接回调
|
|
void on_close(uv_handle_t* handle);//装置关闭回调
|