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

fix: publish connection when reconnected #41

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down Expand Up @@ -102,4 +102,4 @@
"engines": {
"node": ">=11.0.0"
}
}
}
15 changes: 7 additions & 8 deletions src/client/agv-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,23 +168,22 @@ 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);
}

/**
* Before client disconnects actively, publish a retained connection topic
* 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 },
);
}

}
16 changes: 12 additions & 4 deletions src/controller/agv-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
BatteryState,
BlockingType,
ClientOptions,
ConnectionState,
Edge,
EdgeState,
Error,
Expand Down Expand Up @@ -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();
}
}
});

Expand Down