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..d4933a2 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, @@ -1172,10 +1173,17 @@ 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. - this.registerConnectionStateChange((currentState, prevState) => { - if (currentState === "online" && prevState !== "online") { - this._publishCurrentState(); + // 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") { + // only called on reconnect + this.debug("Connection online again."); + this.publishConnectionState(ConnectionState.Online); + this._publishCurrentState(); + } } });