Work on connector

This commit is contained in:
Daniel Kluge
2023-10-26 16:41:31 +02:00
parent 778202e61a
commit 18625d447b
2 changed files with 16 additions and 6 deletions
+9 -2
View File
@@ -1,5 +1,5 @@
import * as mqtt from "mqtt"; import * as mqtt from "mqtt";
import { Property, Operation, OperationVariable, BasicEventElement } from "@aas-core-works/aas-core3.0-typescript/dist/types/types"; import type { Property, Operation, OperationVariable, BasicEventElement } from "@aas-core-works/aas-core3.0-typescript/dist/types/types";
import InterfaceConnectionObject from "interfaceConnectionObject"; import InterfaceConnectionObject from "interfaceConnectionObject";
class MQTTConnector extends InterfaceConnectionObject<mqtt.IClientOptions> { class MQTTConnector extends InterfaceConnectionObject<mqtt.IClientOptions> {
@@ -10,8 +10,15 @@ class MQTTConnector extends InterfaceConnectionObject<mqtt.IClientOptions> {
private client: mqtt.MqttClient|null = null; private client: mqtt.MqttClient|null = null;
private store: Record<string, any> = {};
public connect(): boolean { public connect(): boolean {
if (!this.client) this.client = mqtt.connect(this.config); if (!this.client) this.client = mqtt.connect(this.connectionParameter);
this.client.on("message", (topic, message) => {
// If json is expected you could parse it here
this.store[topic] = message.toString("utf-8");
});
this.client.subscribe("#");
return this.client.connected; return this.client.connected;
} }
+7 -4
View File
@@ -1,4 +1,5 @@
import type { types } from "@aas-core-works/aas-core3.0-typescript"; import type { types } from "@aas-core-works/aas-core3.0-typescript";
import type { EndpointMetadata } from "types/aidConf";
type ConnectionType = "ON_DEMAND" | "PERMANENT"; type ConnectionType = "ON_DEMAND" | "PERMANENT";
@@ -10,11 +11,13 @@ export default abstract class InterfaceConnectionObject<ConfigInterface> {
protected abstract readonly connectionType: ConnectionType; protected abstract readonly connectionType: ConnectionType;
protected abstract readonly supportsSubscriptions: boolean; protected abstract readonly supportsSubscriptions: boolean;
protected config: ConfigInterface; protected readonly connectionParameter: ConfigInterface;
private onResponseCallback: OnResponseCallback; protected readonly endpointMetadata: EndpointMetadata;
private readonly onResponseCallback: OnResponseCallback;
public constructor(config: ConfigInterface, onResponseCallback: OnResponseCallback) { public constructor(connectionParameter: ConfigInterface, endpointMetadata: EndpointMetadata, onResponseCallback: OnResponseCallback) {
this.config = config; this.connectionParameter = connectionParameter;
this.endpointMetadata = endpointMetadata;
this.onResponseCallback = onResponseCallback; this.onResponseCallback = onResponseCallback;
} }