Skip to content

Commit

Permalink
Use singleton pattern in the Modbus client factory (#909)
Browse files Browse the repository at this point in the history
feat(binding-modbus): use a singleton client for modbus ops
  • Loading branch information
relu91 committed Jan 19, 2023
1 parent 3acb1aa commit f370612
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions packages/binding-modbus/src/modbus-client-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,38 @@
*
* SPDX-License-Identifier: EPL-2.0 OR W3C-20150513
********************************************************************************/
import { ProtocolClientFactory, ProtocolClient, createDebugLogger } from "@node-wot/core";
import { ProtocolClientFactory, ProtocolClient, createDebugLogger, createWarnLogger } from "@node-wot/core";
import ModbusClient from "./modbus-client";

const debug = createDebugLogger("binding-modbus", "modbus-client-factory");
const warn = createWarnLogger("binding-modbus", "modbus-client-factory");

export default class ModbusClientFactory implements ProtocolClientFactory {
public readonly scheme: string = "modbus+tcp";
private singleton: ModbusClient;

public getClient(): ProtocolClient {
debug(`Creating client for '${this.scheme}'`);
return new ModbusClient();
debug(`Get client for '${this.scheme}'`);
this.init();
return this.singleton;
}

public init = (): boolean => true;
public destroy = (): boolean => true;
public init(): boolean {
if (!this.singleton) {
debug(`Initializing client for '${this.scheme}'`);
this.singleton = new ModbusClient();
}
return true;
}

public destroy(): boolean {
debug(`Destroying client for '${this.scheme}'`);
if (!this.singleton) {
warn(`Destroying a not initialized client factory for '${this.scheme}'`);
return true; // do not cause an error
}
this.singleton.stop();
this.singleton = undefined;
return true;
}
}

0 comments on commit f370612

Please sign in to comment.