-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathmodbus-client-factory.ts
More file actions
54 lines (48 loc) · 2.04 KB
/
Copy pathmodbus-client-factory.ts
File metadata and controls
54 lines (48 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/********************************************************************************
* Copyright (c) 2020 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the W3C Software Notice and
* Document License (2015-05-13) which is available at
* https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document.
*
* SPDX-License-Identifier: EPL-2.0 OR W3C-20150513
********************************************************************************/
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 getSupportedProtocols(): Array<[string, string?]> {
return [["modbus+tcp"]];
}
public getClient(): ProtocolClient {
debug(`Get client for '${this.scheme}'`);
this.init();
// singleton is initialized in init()
return this.singleton!;
}
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;
}
}