From 355e31c100d8f1a7eb9fb1409237b72b2c94e68f Mon Sep 17 00:00:00 2001 From: Kadir Eryigit Date: Fri, 27 Sep 2024 16:08:20 +0200 Subject: [PATCH 1/2] publish connection when reconnected --- package.json | 4 ++-- src/client/agv-client.ts | 15 +++++++-------- src/controller/agv-controller.ts | 13 ++++++++++--- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index e53b791..4bc6c2b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vda-5050-lib", - "version": "1.4.0", + "version": "1.4.1", "description": "Universal VDA 5050 library for Node.js and browsers", "homepage": "https://github.com/coatyio/vda-5050-lib.js", "repository": { @@ -102,4 +102,4 @@ "engines": { "node": ">=11.0.0" } -} +} \ No newline at end of file diff --git a/src/client/agv-client.ts b/src/client/agv-client.ts index e9488fe..93a5fd2 100644 --- a/src/client/agv-client.ts +++ b/src/client/agv-client.ts @@ -168,11 +168,7 @@ export class AgvClient extends Client { * connection state. */ protected async onStarted() { - await this.publish( - Topic.Connection, - { connectionState: ConnectionState.Online }, - { retainMessage: true }, - ); + await this.publishConnectionState(ConnectionState.Online); } /** @@ -180,11 +176,14 @@ export class AgvClient extends Client { * for offline connection state. */ protected async onStopping() { - await this.publish( + await this.publishConnectionState(ConnectionState.Offline); + } + + protected publishConnectionState(connectionState: ConnectionState) { + return this.publish( Topic.Connection, - { connectionState: ConnectionState.Offline }, + { connectionState: connectionState }, { retainMessage: true }, ); } - } diff --git a/src/controller/agv-controller.ts b/src/controller/agv-controller.ts index 808b614..97fd976 100644 --- a/src/controller/agv-controller.ts +++ b/src/controller/agv-controller.ts @@ -10,6 +10,7 @@ import { BatteryState, BlockingType, ClientOptions, + ConnectionState, Edge, EdgeState, Error, @@ -1173,9 +1174,15 @@ export class AgvController extends AgvClient { await this.subscribe(Topic.InstantActions, actions => this._processInstantActions(actions)); // Ensure State is reported immediately once after client is online again. - this.registerConnectionStateChange((currentState, prevState) => { - if (currentState === "online" && prevState !== "online") { - this._publishCurrentState(); + this.registerConnectionStateChange((connectionState, previousConnectionState) => { + if (connectionState !== previousConnectionState) { + this.debug(`connection state changed: ${previousConnectionState} -> ${connectionState}`); + if (connectionState === "online") { + // only called on reconnect + this.debug("Connection online again."); + this.publishConnectionState(ConnectionState.Online); + this._publishCurrentState(); + } } }); From d13030c349b6a732695d769c2a09b1d20242ac5b Mon Sep 17 00:00:00 2001 From: Kadir Eryigit Date: Fri, 27 Sep 2024 16:16:21 +0200 Subject: [PATCH 2/2] added comments --- src/controller/agv-controller.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/controller/agv-controller.ts b/src/controller/agv-controller.ts index 97fd976..d4933a2 100644 --- a/src/controller/agv-controller.ts +++ b/src/controller/agv-controller.ts @@ -1173,8 +1173,9 @@ export class AgvController extends AgvClient { await this.subscribe(Topic.Order, order => this._processOrder(order)); await this.subscribe(Topic.InstantActions, actions => this._processInstantActions(actions)); - // Ensure State is reported immediately once after client is online again. + // Ensure State and Connection are reported once immediately after client is online again. this.registerConnectionStateChange((connectionState, previousConnectionState) => { + // this is not called on the initial connection because it is registered after we connect. if (connectionState !== previousConnectionState) { this.debug(`connection state changed: ${previousConnectionState} -> ${connectionState}`); if (connectionState === "online") {