- websocket client on ESP32
- telemetry protocol builder - server-side message handling - database migrations - telemetry persistence
This commit is contained in:
@@ -19,8 +19,59 @@ static inline int append(char* buf, size_t size, int pos, const char* fmt, ...)
|
||||
return pos + written;
|
||||
}
|
||||
|
||||
int build_telemetry(
|
||||
char* buf,
|
||||
size_t buf_size,
|
||||
uint32_t msg_id,
|
||||
int64_t ts,
|
||||
uint32_t device_id,
|
||||
const char* metric,
|
||||
const char* source,
|
||||
const char* unit,
|
||||
const int* values,
|
||||
size_t count
|
||||
) {
|
||||
int pos = 0;
|
||||
|
||||
// --- header ---
|
||||
pos = append(buf, buf_size, pos,
|
||||
"{\"v\":1,\"t\":\"t\",\"id\":%lu,\"ts\":%lld,\"d\":%lu,\"p\":{",
|
||||
(unsigned long)msg_id,
|
||||
(long long)ts,
|
||||
(unsigned long)device_id
|
||||
);
|
||||
if (pos < 0) return -1;
|
||||
|
||||
// --- payload meta ---
|
||||
pos = append(buf, buf_size, pos,
|
||||
"\"m\":\"%s\",\"s\":\"%s\",\"u\":\"%s\",\"v\":[",
|
||||
metric,
|
||||
source,
|
||||
unit
|
||||
);
|
||||
if (pos < 0) return -1;
|
||||
|
||||
// --- values ---
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
pos = append(buf, buf_size, pos,
|
||||
"[%u,%d]%s",
|
||||
(unsigned)i, // delta time (пока просто индекс)
|
||||
values[i],
|
||||
(i < count - 1) ? "," : ""
|
||||
);
|
||||
if (pos < 0) return -1;
|
||||
}
|
||||
|
||||
// --- close ---
|
||||
pos = append(buf, buf_size, pos, "]}}");
|
||||
if (pos < 0) return -1;
|
||||
|
||||
return pos;
|
||||
}
|
||||
|
||||
__attribute__((deprecated))
|
||||
// --- TELEMETRY (single measurement) ---
|
||||
int build_telemetry_single(
|
||||
int build_telemetry_single(
|
||||
char* buf,
|
||||
size_t buf_size,
|
||||
const char* id,
|
||||
@@ -76,6 +127,7 @@ int build_telemetry_single(
|
||||
return pos;
|
||||
}
|
||||
|
||||
__attribute__((deprecated))
|
||||
// --- EVENT ---
|
||||
int build_event(
|
||||
char* buf,
|
||||
|
||||
Reference in New Issue
Block a user