Skip to content

Commit

Permalink
Rename property to make it less confusing (#654)
Browse files Browse the repository at this point in the history
  • Loading branch information
raman325 authored Jun 19, 2022
1 parent 010c3d9 commit eb8ea5c
Showing 1 changed file with 57 additions and 46 deletions.
103 changes: 57 additions & 46 deletions src/lib/forward.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,37 @@ export class EventForwarder {
/**
* Only load this once the driver is ready.
*
* @param clients
* @param clientsController
*/
constructor(private clients: ClientsController) {}
constructor(private clientsController: ClientsController) {}

start() {
this.clients.driver.controller.nodes.forEach((node) =>
this.clientsController.driver.controller.nodes.forEach((node) =>
this.setupNode(node)
);

// Bind to all controller events
// https://github.com/zwave-js/node-zwave-js/blob/master/packages/zwave-js/src/lib/controller/Controller.ts#L112

this.clients.driver.controller.on("node added", (node, result) => {
// forward event to all connected clients, respecting schemaVersion it supports
this.clients.clients.forEach((client) =>
this.sendEvent(client, {
source: "controller",
event: "node added",
node: dumpNode(node, client.schemaVersion),
result,
})
);
this.setupNode(node);
});
this.clientsController.driver.controller.on(
"node added",
(node, result) => {
// forward event to all connected clients, respecting schemaVersion it supports
this.clientsController.clients.forEach((client) =>
this.sendEvent(client, {
source: "controller",
event: "node added",
node: dumpNode(node, client.schemaVersion),
result,
})
);
this.setupNode(node);
}
);

this.clients.driver.controller.on("node found", (node) => {
this.clientsController.driver.controller.on("node found", (node) => {
// forward event to all connected clients, respecting schemaVersion it supports
this.clients.clients.forEach((client) =>
this.clientsController.clients.forEach((client) =>
this.sendEvent(client, {
source: "controller",
event: "node found",
Expand All @@ -62,7 +65,7 @@ export class EventForwarder {
"exclusion stopped",
];
for (const event of events) {
this.clients.driver.controller.on(event, () =>
this.clientsController.driver.controller.on(event, () =>
this.forwardEvent({
source: "controller",
event,
Expand All @@ -71,54 +74,62 @@ export class EventForwarder {
}
}

this.clients.driver.controller.on("inclusion started", (secure) =>
this.clientsController.driver.controller.on("inclusion started", (secure) =>
this.forwardEvent({
source: "controller",
event: "inclusion started",
secure,
})
);

this.clients.driver.controller.on("node removed", (node, replaced) =>
// forward event to all connected clients, respecting schemaVersion it supports
this.clients.clients.forEach((client) =>
this.sendEvent(client, {
source: "controller",
event: "node removed",
node: dumpNode(node, client.schemaVersion),
replaced,
})
)
this.clientsController.driver.controller.on(
"node removed",
(node, replaced) =>
// forward event to all connected clients, respecting schemaVersion it supports
this.clientsController.clients.forEach((client) =>
this.sendEvent(client, {
source: "controller",
event: "node removed",
node: dumpNode(node, client.schemaVersion),
replaced,
})
)
);

this.clients.driver.controller.on("heal network progress", (progress) =>
this.forwardEvent({
source: "controller",
event: "heal network progress",
progress: Object.fromEntries(progress),
})
this.clientsController.driver.controller.on(
"heal network progress",
(progress) =>
this.forwardEvent({
source: "controller",
event: "heal network progress",
progress: Object.fromEntries(progress),
})
);

this.clients.driver.controller.on("heal network done", (result) =>
this.clientsController.driver.controller.on("heal network done", (result) =>
this.forwardEvent({
source: "controller",
event: "heal network done",
result: Object.fromEntries(result),
})
);

this.clients.driver.controller.on("statistics updated", (statistics) =>
this.forwardEvent({
source: "controller",
event: "statistics updated",
statistics,
})
this.clientsController.driver.controller.on(
"statistics updated",
(statistics) =>
this.forwardEvent({
source: "controller",
event: "statistics updated",
statistics,
})
);
}

forwardEvent(data: OutgoingEvent) {
// Forward event to all connected clients
this.clients.clients.forEach((client) => this.sendEvent(client, data));
this.clientsController.clients.forEach((client) =>
this.sendEvent(client, data)
);
}

sendEvent(client: Client, data: OutgoingEvent) {
Expand All @@ -141,7 +152,7 @@ export class EventForwarder {

node.on("ready", (changedNode: ZWaveNode) => {
// Dump full node state on ready event
this.clients.clients.forEach((client) =>
this.clientsController.clients.forEach((client) =>
this.sendEvent(client, {
source: "node",
event: "ready",
Expand Down Expand Up @@ -201,7 +212,7 @@ export class EventForwarder {
(changedNode: ZWaveNode, oldArgs: ZWaveNodeMetadataUpdatedArgs) => {
// only forward value events for ready nodes
if (!changedNode.ready) return;
this.clients.clients.forEach((client) => {
this.clientsController.clients.forEach((client) => {
// Copy arguments for each client so transforms don't impact all clients
const args = { ...oldArgs };
if (args.metadata != undefined) {
Expand Down Expand Up @@ -229,7 +240,7 @@ export class EventForwarder {
(changedNode: ZWaveNode, ccId: CommandClasses, args: any) => {
// only forward value events for ready nodes
if (!changedNode.ready) return;
this.clients.clients.forEach((client) => {
this.clientsController.clients.forEach((client) => {
// Only send notification events from the Notification CC for schema version < 3
if (client.schemaVersion < 3 && ccId == CommandClasses.Notification) {
let eventData: OutgoingEvent = {
Expand Down

0 comments on commit eb8ea5c

Please sign in to comment.