chenluhua1980
2025-12-11 1958d1d4d370f80a0a664c08e60238919fc07a1c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "stdafx.h"
#include "ClientListDlg.h"
#include "afxdialogex.h"
#include "../DAQBridge/core/Collector.h"
#include "CMaster.h"
#include "Model.h"
#include "Servo.h"
#include <chrono>
#include <iomanip>
#include <sstream>
 
// CClientListDlg 对话框
 
IMPLEMENT_DYNAMIC(CClientListDlg, CDialogEx)
 
CClientListDlg::CClientListDlg(CWnd* pParent /*=nullptr*/)
    : CDialogEx(IDD_DIALOG_CLIENT_LIST, pParent)
{
}
 
CClientListDlg::~CClientListDlg()
{
}
 
void CClientListDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_LIST_CLIENTS, m_listClients);
}
 
BEGIN_MESSAGE_MAP(CClientListDlg, CDialogEx)
    ON_BN_CLICKED(IDC_BUTTON_REFRESH, &CClientListDlg::OnBnClickedRefresh)
    ON_WM_SYSCOMMAND()
END_MESSAGE_MAP()
 
// CClientListDlg 消息处理程序
 
BOOL CClientListDlg::OnInitDialog()
{
    CDialogEx::OnInitDialog();
 
    // 初始化列表控件
    m_listClients.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_HEADERDRAGDROP);
    
    // 添加列
    m_listClients.InsertColumn(0, _T("IP地址"), LVCFMT_LEFT, 120);
    m_listClients.InsertColumn(1, _T("端口"), LVCFMT_CENTER, 80);
    m_listClients.InsertColumn(2, _T("版本状态"), LVCFMT_CENTER, 100);
    m_listClients.InsertColumn(3, _T("连接状态"), LVCFMT_CENTER, 100);
    m_listClients.InsertColumn(4, _T("连接时间"), LVCFMT_LEFT, 150);
 
    // 刷新客户端列表
    RefreshClientList();
 
    return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}
 
void CClientListDlg::OnBnClickedRefresh()
{
    RefreshClientList();
}
 
void CClientListDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
    if (nID == SC_CLOSE)
    {
        CDialogEx::OnCancel();
        return;
    }
    CDialogEx::OnSysCommand(nID, lParam);
}
 
void CClientListDlg::RefreshClientList()
{
    // 清空当前列表
    m_listClients.DeleteAllItems();
    m_clients.clear();
 
    // 获取Collector实例
    extern CServoApp theApp;
    SERVO::CMaster& master = theApp.m_model.getMaster();
    Collector* pCollector = master.getCollector();
    
    if (pCollector)
    {
        // 获取真实的客户端列表
        auto clientSummaries = pCollector->getClientList();
        
        // 转换数据格式
        for (const auto& summary : clientSummaries)
        {
            ClientInfo client;
            client.ip = summary.ip;
            client.port = summary.port;
            client.versionOk = summary.versionOk;
            client.status = summary.versionOk ? "已连接" : "版本不匹配";
            
            // 获取当前时间作为连接时间(实际实现中应该从Collector获取真实连接时间)
            auto now = std::chrono::system_clock::now();
            auto time_t = std::chrono::system_clock::to_time_t(now);
            std::tm tm;
            localtime_s(&tm, &time_t);
            
            std::ostringstream oss;
            oss << std::put_time(&tm, "%Y-%m-%d %H:%M:%S");
            client.connectTime = oss.str();
            
            m_clients.push_back(client);
        }
    }
    else
    {
        // 如果无法获取Collector实例,显示提示信息
        ClientInfo noData;
        noData.ip = "无法获取数据";
        noData.port = 0;
        noData.versionOk = false;
        noData.status = "Collector未初始化";
        noData.connectTime = "";
        m_clients.push_back(noData);
    }
 
    // 更新列表显示
    UpdateClientList(m_clients);
}
 
void CClientListDlg::UpdateClientList(const std::vector<ClientInfo>& clients)
{
    m_listClients.DeleteAllItems();
 
    for (size_t i = 0; i < clients.size(); ++i)
    {
        const ClientInfo& client = clients[i];
        
        int nItem = m_listClients.InsertItem(i, CString(client.ip.c_str()));
        m_listClients.SetItemText(nItem, 1, CString(std::to_string(client.port).c_str()));
        m_listClients.SetItemText(nItem, 2, client.versionOk ? _T("正常") : _T("异常"));
        m_listClients.SetItemText(nItem, 3, CString(client.status.c_str()));
        m_listClients.SetItemText(nItem, 4, CString(client.connectTime.c_str()));
    }
}