38 lines
1.4 KiB
JavaScript
38 lines
1.4 KiB
JavaScript
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"
|
|
};
|
|
}
|