collect and show mqtt state for nodes

This commit is contained in:
liamcottle
2024-03-31 18:05:31 +13:00
parent 511b894c78
commit cfb6bf1f4d
4 changed files with 46 additions and 0 deletions

View File

@ -225,6 +225,36 @@ client.on("connect", () => {
client.on("message", async (topic, message) => {
try {
// handle node status
if(topic.includes("/stat/!")){
try {
// get node id and status
const nodeIdHex = topic.split("/").pop();
const mqttConnectionState = message.toString();
// convert node id hex to int value
const nodeId = BigInt('0x' + nodeIdHex.replaceAll("!", ""));
// update mqtt connection state for node
await prisma.node.updateMany({
where: {
node_id: nodeId,
},
data: {
mqtt_connection_state: mqttConnectionState,
mqtt_connection_state_updated_at: new Date(),
},
});
// no need to continue with this mqtt message
return;
} catch(e) {
console.error(e);
}
}
// decode service envelope
const envelope = ServiceEnvelope.decode(message);
if(!envelope.packet){