chenluhua1980
2025-12-11 33f080ddc32f3545b685b2e0a7a5df3c35894270
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#pragma once
#include <cstdint>
#include <string>
#include <vector>
 
namespace Proto {
 
    // ¹Ì¶¨Ö¡
    inline constexpr uint8_t kHead[4] = { 0x11, 0x88, 0x11, 0x88 };
    inline constexpr uint8_t kTail = 0x88;
 
    // Ö¸ÁîÂë
    enum : uint16_t {
        // ÎÕÊÖ
        CMD_REQ_VERSION = 0x0001,
        CMD_RSP_VERSION = 0xF001,
 
        // µ¥Í¨µÀÔöÁ¿À­È¡
        CMD_REQ_SINCE = 0x0101,
        CMD_RSP_SINCE = 0xF101,
 
        // Í³¼Æ/ͨµÀÁбí
        CMD_REQ_STATS = 0x0103,
        CMD_RSP_STATS = 0xF103,
 
        // »ų́Áбí
        CMD_REQ_MACHINES = 0x0104,
        CMD_RSP_MACHINES = 0xF104,
 
        // Õû»ú¶àͨµÀÔöÁ¿À­È¡
        CMD_REQ_SINCE_ALL = 0x0105,
        CMD_RSP_SINCE_ALL = 0xF105,
 
        // Åú´ÎÐÅÏ¢
        CMD_REQ_BATCH_INFO = 0x0120,
        CMD_RSP_BATCH_INFO = 0xF120,
 
        // ´íÎó
        CMD_RSP_ERROR = 0xE100,
    };
 
    // === since* ÇëÇóÀ︽¼Ó batchId µÄ±ê־λ ===
    inline constexpr uint16_t SINCE_FLAG_HAS_BATCH = 0x0001;
 
    // ===== Êý¾Ý½á¹¹£¨Á½¶Ë¹²Óã© =====
 
    // 0x0001 / 0xF001
    struct ReqVersion {
        uint32_t dataId = 0;
    };
    struct RspVersion {
        uint32_t   dataId = 0;
        std::string version;   // UTF-8£¬ÀýÈç "1.0.1"
    };
 
    // 0x0104 / 0xF104
    struct MachineInfo {
        uint32_t    id = 0;
        std::string name;
    };
    struct ReqMachines {
        uint32_t dataId = 0;
    };
    struct RspMachines {
        uint32_t                dataId = 0;
        std::vector<MachineInfo> machines;
    };
 
    // 0x0103 / 0xF103
    struct ReqStats {
        uint32_t dataId = 0;
        uint32_t machineId = 0;
        uint16_t flags = 0;
    };
    struct ChannelStatInfo {
        uint32_t    channelId = 0;
        uint64_t    earliestTs = 0;
        uint64_t    latestTs = 0;
        uint32_t    size = 0;
        std::string name;      // UTF-8
    };
    struct RspStats {
        uint32_t dataId = 0;
        uint32_t machineId = 0;
        std::vector<ChannelStatInfo> channels;
    };
 
    // 0x0101 / 0xF101£¨µ¥Í¨µÀÔöÁ¿£©
    struct ReqSince {
        uint32_t dataId = 0;
        uint32_t machineId = 0;
        uint32_t channelId = 0;
        uint64_t sinceTsExclusive = 0;     // ms
        uint16_t maxCount = 1024;
        uint16_t flags = 0;     // °´Î»£¬¼û SINCE_FLAG_HAS_BATCH
        std::string batchId;               // flags & SINCE_FLAG_HAS_BATCH Ê±ÓÐЧ
    };
    struct SamplePair {
        uint64_t ts_ms = 0;
        double   value = 0.0;
    };
    struct RspSince {
        uint32_t dataId = 0;
        uint32_t machineId = 0;
        uint32_t channelId = 0;
        uint64_t lastTsSent = 0;
        uint8_t  more = 0;           // ¸ÃͨµÀÊÇ·ñ»¹ÓÐδ·¢
        std::vector<SamplePair> samples;
    };
 
    // 0x0105 / 0xF105£¨Õû»ú¶àͨµÀÔöÁ¿£©
    struct ChannelBlock {
        uint32_t channelId = 0;
        uint64_t lastTsSent = 0;          // ¸ÃͨµÀ±¾´Î×îºóÑù±¾Ê±¼ä´Á
        uint8_t  more = 0;          // ¸ÃͨµÀÊÇ·ñ»¹ÓÐδ·¢
        std::vector<SamplePair> samples;
    };
    struct ReqSinceAll {
        uint32_t dataId = 0;
        uint32_t machineId = 0;
        uint64_t sinceTsExclusive = 0;     // ¶ÔËùÓÐͨµÀͳһ since
        uint16_t maxPerChannel = 1024;  // Ã¿ÌõÇúÏßÉÏÏÞ
        uint16_t flags = 0;     // °´Î»£¬¼û SINCE_FLAG_HAS_BATCH
        std::string batchId;               // flags & SINCE_FLAG_HAS_BATCH Ê±ÓÐЧ
    };
    struct RspSinceAll {
        uint32_t dataId = 0;
        uint32_t machineId = 0;
        uint8_t  moreAny = 0;            // ÊÇ·ñ»¹ÓÐÈÎÒâͨµÀÓÐÊ£Óà
        std::vector<ChannelBlock> blocks;  // ¶à¸öͨµÀµÄÔöÁ¿
    };
 
    // === Åú´Î״̬ ===
    enum class BatchState : uint8_t {
        Idle = 0,  // Î޻Åú´Î£ºactiveBatchId="", activeStartTs=0, expectedEndTs=0
        Active = 1,
    };
 
    // === ´íÎóÂë ===
    enum class ErrCode : uint16_t {
        NoActiveBatch = 0x0001, // µ±Ç°Î޻Åú´Î
        BatchMismatch = 0x0002, // ÇëÇóЯ´øµÄ batchId Ó뵱ǰ»î¶¯Åú´Î²»Ò»ÖÂ
    };
 
    // 0x0120 / 0xF120£¨Åú´ÎÐÅÏ¢£©
    struct ReqBatchInfo {
        uint32_t dataId = 0;
        uint32_t machineId = 0;
    };
    struct RspBatchInfo {
        uint32_t   dataId = 0;
        uint32_t   machineId = 0;
        BatchState state = BatchState::Idle;
        std::string activeBatchId;         // state=Idle Ê±Ó¦Îª¿Õ
        uint64_t   activeStartTs = 0;
        uint64_t   expectedEndTs = 0;      // =0 ±íʾδ֪/Idle
    };
 
    // 0xE100 ´íÎóÖ¡
    struct RspError {
        uint32_t   dataId = 0;
        uint16_t   refCmd = 0;          // ³ö´íÇëÇóµÄÖ¸ÁÈç 0x0101/0x0105/0x0120...
        uint32_t   machineId = 0;
        ErrCode    code = ErrCode::NoActiveBatch;
        std::string message;               // ¼ò¶ÌÎı¾
    };
 
} // namespace Proto