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

AgvController cannot be started after being stopped #36

Open
sloebrich opened this issue Feb 22, 2024 · 1 comment
Open

AgvController cannot be started after being stopped #36

sloebrich opened this issue Feb 22, 2024 · 1 comment

Comments

@sloebrich
Copy link

sloebrich commented Feb 22, 2024

Describe the bug
When an AgvController is stopped and then started again, the application throws an error. This also happens if we wait a while between stop and start.

Is this a regression?
Afaik not

To Reproduce
With an mqtt broker running on localhost:1883, run the following script:

import { VirtualAgvAdapterOptions } from './../dist/adapter/virtual-agv-adapter.d';
import { AgvId } from './../dist/common/client-types.d';
import { ClientOptions } from './../dist/common/client.d';
import { VirtualAgvAdapter } from './adapter/virtual-agv-adapter';
import { AgvController } from './controller/agv-controller';

(async () => {
    // Use minimal client options: communication namespace and broker endpoint address.
    const agvClientOptions: ClientOptions = { interfaceName: "logctx42", transport: { brokerUrl: "mqtt://localhost:1883" }, vdaVersion: "2.0.0" };

    // The target AGV.
    const agvId001: AgvId = { manufacturer: "RobotCompany", serialNumber: "001" };

    // Specify associated adapter type; use defaults for all other AGV controller options. 
    const agvControllerOptions = {
        agvAdapterType: VirtualAgvAdapter,
    };

    // Use defaults for all adapter options of Virtual AGV adapter.
    const agvAdapterOptions: VirtualAgvAdapterOptions = {};

    // Create instance of AGV Controller with client, controller, and adapter options.
    const agvController = new AgvController(agvId001, agvClientOptions, agvControllerOptions, agvAdapterOptions);

    const sleep = async t => {await new Promise(r => setTimeout(r, 1000 * t))}
    await agvController.start();
    await sleep(5)
    await agvController.stop();
    await sleep(5)
    await agvController.start();
})()

Expected behavior
The AgvController restarts without throwing an error.

Screenshots

C:\Repositories\vda-5050-lib.js\src\common\client.ts:712
            throw new Error("Client is not started");
                  ^
Error: Client is not started
    at AgvController.publishTopic (C:\Repositories\vda-5050-lib.js\src\common\client.ts:712:19)
    at AgvController.publish (C:\Repositories\vda-5050-lib.js\src\client\agv-client.ts:63:21)
    at AgvController._publishCurrentState (C:\Repositories\vda-5050-lib.js\src\controller\agv-controller.ts:1216:43)
    at AgvController._connectionStateChangeCallback (C:\Repositories\vda-5050-lib.js\src\controller\agv-controller.ts:1178:22)
    at AgvController._emitConnectionStateChange (C:\Repositories\vda-5050-lib.js\src\common\client.ts:1186:18)
    at MqttClient.<anonymous> (C:\Repositories\vda-5050-lib.js\src\common\client.ts:1130:26)
    at MqttClient.emit (node:events:526:35)
    at MqttClient.emit (node:domain:488:12)
    at Readable.<anonymous> (C:\Repositories\vda-5050-lib.js\node_modules\mqtt\lib\client.js:1857:14)
    at Readable.emit (node:events:514:28)

Your Environment (please complete the following information):

  • Package Version: 1.4.0
  • Node.js Version: 20.10.0
  • OS: Windows 10

Additional context
When the mqtt client connects on the second start, a publish state is triggered:

this._emitConnectionStateChange("online");

in \src\common\client.ts l.1131.

However this._isStarted is only set to true after connecting the mqtt broker in \src\common\client.ts l.472, leading to the error in AgvController.publishTopic.

A possible solution would be to set this._isStarted to true when the mqtt client connects, but before it emits the connection state change:

mqtt
...
    .on("connect", () => {
            this._isStarted = true
            this._emitConnectionStateChange("online");
    })

in \src\common\client.ts ll.1129-1132.

@keryigit
Copy link

keryigit commented Mar 7, 2024

The same issue happens when the mqtt library reconnects on its own. The suggested solution should work on that as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants