Continue phase 2 device ops and dynamic QR lifecycle
This commit is contained in:
37
dist/shared/services/deviceConfigStatus.js
vendored
Normal file
37
dist/shared/services/deviceConfigStatus.js
vendored
Normal file
@ -0,0 +1,37 @@
|
||||
import { getLatestDeviceConfigAck, toDeviceConfigAckPayload, toDeviceConfigPayload } from "../store/deviceConfigStore";
|
||||
import { listMqttMessages, toMqttMessagePayload } from "../store/mqttMessageStore";
|
||||
function deriveConfigDriftStatus(config, latestAck) {
|
||||
if (!latestAck) {
|
||||
return "pending_ack";
|
||||
}
|
||||
if (latestAck.config_version < config.config_version) {
|
||||
return "stale_ack";
|
||||
}
|
||||
if (latestAck.config_version > config.config_version) {
|
||||
return "pending_ack";
|
||||
}
|
||||
if (latestAck.status === "failed") {
|
||||
return "failed_ack";
|
||||
}
|
||||
return "applied";
|
||||
}
|
||||
export async function buildDeviceConfigStatus(config) {
|
||||
const latestAck = await getLatestDeviceConfigAck(config.device_id);
|
||||
const latestPush = (await listMqttMessages({
|
||||
device_id: config.device_id,
|
||||
direction: "downlink",
|
||||
message_type: "config_push",
|
||||
correlation_id: `config:${config.config_version}`,
|
||||
limit: 1
|
||||
}))[0];
|
||||
const driftStatus = latestPush ? deriveConfigDriftStatus(config, latestAck) : "never_pushed";
|
||||
return {
|
||||
device_id: config.device_id,
|
||||
config: toDeviceConfigPayload(config),
|
||||
drift_status: driftStatus,
|
||||
desired_config_version: config.config_version,
|
||||
latest_ack: latestAck ? toDeviceConfigAckPayload(latestAck) : null,
|
||||
latest_push: latestPush ? toMqttMessagePayload(latestPush) : null,
|
||||
retry_recommended: driftStatus !== "applied"
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user