MQTT Status based on Last Packet Received

This commit is contained in:
Skordy
2024-08-28 01:10:14 -07:00
parent 0262dd3ee5
commit 7b8c9f82e5
2 changed files with 64 additions and 41 deletions

View File

@ -604,37 +604,6 @@ client.on("connect", () => {
// handle message received
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 = convertHexIdToNumericId(nodeIdHex);
// 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){
@ -661,6 +630,16 @@ client.on("message", async (topic, message) => {
}
}
// Update Node MQTT status based on Last Packet Received time
const ret = await prisma.node.updateMany({
where: {
node_id: convertHexIdToNumericId(envelope.gatewayId),
},
data: {
mqtt_connection_state_updated_at: new Date(),
},
});
// attempt to decrypt encrypted packets
const isEncrypted = envelope.packet.encrypted?.length > 0;
if(isEncrypted){