Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added liveInspector to all mqtt events #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/Events/onConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,15 @@ async function onConnect(
connection.addConnection(client.channel, client);

console.debug(`Device connected [${client.device.id}]`);
// TODO: ADD Live Inspector support.
// deviceInspector(client.device.id, client.connID, '[MQTT] Device connected', `Token Ending: ${String(tokenResult.tokenObj.token).slice(-5)} Client-ID: ${packet.clientId} Will-Message: ${!!client.will}`);

let liveInpesctorContent = `Client-ID: ${packet.clientId} Will-Message: ${!!client.will}`;
if (configParams.connection_method === "device_token") {
liveInpesctorContent = `Token Ending: ${String(client.token).slice(-5)} ${liveInpesctorContent}`;
}
core.emitToLiveInspector(client.device.id, {
title: "Device connected",
content: liveInpesctorContent,
});

client.connack({ returnCode: 0, reasonCode: 0 });

Expand Down
14 changes: 10 additions & 4 deletions src/Events/onDisconnect.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { core } from "@tago-io/tcore-sdk";
import Connection from "../Services/connections";
import { ITagoIOClient } from "./onConnect";
import onPublish from "./onPublish";
Expand All @@ -6,8 +7,11 @@ async function onDisconnect(connection: Connection, client: ITagoIOClient, reaso
connection.delConnection(client.channel as string, client.connID as string);

if (!client.disconnected) {
// TODO: Log to Device Inspector
// deviceInspector(client.device.id, client.connID, '[MQTT] Device disconnected', `socket event: ${reason}`);
core.emitToLiveInspector(client.device.id, {
title: "Device disconnected",
content: `socket event: ${reason}`,
});

// TODO: Trigger Action for MQTT Disconnect
// const actionObj = {
// id: client.device.id,
Expand All @@ -23,8 +27,10 @@ async function onDisconnect(connection: Connection, client: ITagoIOClient, reaso
onPublish(connection, client, client.will as any).catch(() => null);
console.log(`Device executed will message [${client.device.id}]`, JSON.stringify(client.will));

// TODO: Log to Device Inspector
// deviceInspector(client.device.id, client.connID, '[MQTT] Device executed will message', JSON.stringify(client.will));
core.emitToLiveInspector(client.device.id, {
title: "Device executed will message",
content: JSON.stringify(client.will),
});
}

if (reason === "disconnect") {
Expand Down
6 changes: 4 additions & 2 deletions src/Events/onPublish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ async function onPublish(connection: Connection, client: ITagoIOClient, packet:
messageId,
});

// TODO: Log to device inspector
// deviceInspector(client.device.id, client.connID, '[MQTT] Device publish', JSON.stringify(scope));
core.emitToLiveInspector(client.device.id, {
title: "Device publish received",
content: JSON.stringify(scope),
});

// TODO: Update Device Last Input
// deviceProvider.updateLastTime(client.device.id, 'input');
Expand Down
7 changes: 5 additions & 2 deletions src/Events/onSubscribe.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { core } from "@tago-io/tcore-sdk";
import { ISubscribePacket } from "mqtt-packet";
import { getQOSMessage } from "../Services/mqtt.qos";
import { getRetainedMQTT } from "../Services/mqtt.retained";
Expand Down Expand Up @@ -39,8 +40,10 @@ async function onSubscribe(client: ITagoIOClient, packet: ISubscribePacket) {
}
}

// TODO: Log to device inspector
// deviceInspector(client.device.id, client.connID, '[MQTT] Device subscribe', `topic: ${subscription.topic}`);
core.emitToLiveInspector(client.device.id, {
title: "Device subscribed",
content: `topic: ${subscription.topic}`,
});
}
}

Expand Down
11 changes: 7 additions & 4 deletions src/Events/onUnsubscribe.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { core } from "@tago-io/tcore-sdk";
import { IUnsubscribePacket } from "mqtt-packet";
import { ITagoIOClient } from "./onConnect";

Expand All @@ -6,10 +7,12 @@ async function onUnsubscribe(client: ITagoIOClient, packet: IUnsubscribePacket &

client.suback({ granted: [packet.qos], messageId: packet.messageId });

// TODO: Log to device inspector
// for (const unsubscription of packet.unsubscriptions) {
// deviceInspector(client.device.id, client.connID, '[MQTT] Device unsubscribe', `topic: ${unsubscription}`);
// }
for (const unsubscription of packet.unsubscriptions) {
core.emitToLiveInspector(client.device.id, {
title: "Device unsubscribe",
content: `topic: ${unsubscription}`,
});
}
}

export default onUnsubscribe;