From d7aa44228faba4dd6820c9a2fcb0643ce5536db5 Mon Sep 17 00:00:00 2001 From: Daniel Kluge Date: Sat, 9 Dec 2023 17:45:36 +0100 Subject: [PATCH] Fixing all the stuff --- example/src/modules/httpInterfaceServer.ts | 16 +++++++------ example/src/modules/mqttConnector.ts | 28 +++++++++++++++------- lib/src/multimessageBroker.ts | 6 +++-- lib/src/types/requests.ts | 5 ++-- owntest.json | 12 +++++----- 5 files changed, 41 insertions(+), 26 deletions(-) diff --git a/example/src/modules/httpInterfaceServer.ts b/example/src/modules/httpInterfaceServer.ts index 302bd83..56814b2 100644 --- a/example/src/modules/httpInterfaceServer.ts +++ b/example/src/modules/httpInterfaceServer.ts @@ -55,6 +55,7 @@ export default class HTTPInterfaceServer extends AbstractInterfaceServer * @public */ public prepare(): void { + this.app.use(express.text()); this.app.use(express.json()); this.createRoutes(); } @@ -202,7 +203,7 @@ export default class HTTPInterfaceServer extends AbstractInterfaceServer const value = this.onRequestCallback(request); - if (req.method === "PUT") res.status(204).end(); + if (req.method === "PUT") return res.status(204).end(); if (value === null) return res.status(500).end(); return res.json(value).end(); @@ -212,12 +213,13 @@ export default class HTTPInterfaceServer extends AbstractInterfaceServer if (req.method !== "POST") return res.status(405).end(); const op = Traverser.getElementByIdPath(this.aas, sm, idShortPath); if (op === null || !AASCoreTypes.isOperation(op)) return res.status(404).end(); - - // TODO - // Parameter? - const success = this.onRequestCallback({ type: "invokeAction", target: op, extraData: { args: req.body, async: false } }); - - return res.send(success ? 204 : 500).end(); + + try { + const result = this.onRequestCallback({ type: "invokeAction", target: op, extraData: { args: req.body, async: false } }); + return res.status(result ? 200 : 204).end(); + } catch { + return res.status(500).end(); + } } case "/invoke-async": { if (req.method !== "POST") return res.status(405).end(); diff --git a/example/src/modules/mqttConnector.ts b/example/src/modules/mqttConnector.ts index a10b698..ba835ef 100644 --- a/example/src/modules/mqttConnector.ts +++ b/example/src/modules/mqttConnector.ts @@ -2,6 +2,7 @@ import * as mqtt from "mqtt"; import { AbstractConnectionObject } from "aas-multimessagebroker"; import { AASCoreTypes, Types } from "aas-multimessagebroker"; import { MMBEvent } from "aas-multimessagebroker/dist/types/requests"; +import { InterfaceForm } from "aas-multimessagebroker/dist/types/aidConf"; /** * This is an example module for the asset connection object. @@ -95,6 +96,15 @@ export default class MQTTConnector extends AbstractConnectionObject void): boolean { if (!mapping.observable || !this.client || !this.client.connected) return false; - const topic = mapping.forms[0].href.substring(1); + const topic = MQTTConnector.urlOrPathToTopic(mapping.forms[0].href); if (this.observerStore[topic] === undefined) this.observerStore[topic] = [{ target, cb }]; else this.observerStore[topic].push({ target, cb }); @@ -164,7 +174,7 @@ export default class MQTTConnector extends AbstractConnectionObject): any { this.assertConnected(); - const form: Types.AIDTypes.InterfaceFormMQTTAction = mapping.forms[0]; - const topic = form.href.substring(1); + const form = mapping.forms[0] as Types.AIDTypes.InterfaceFormMQTTAction; + const topic = MQTTConnector.urlOrPathToTopic(form.href); if (topic === undefined || !mapping.forms) throw new Error("Mapping invalid."); const message = JSON.stringify(args); @@ -208,8 +218,8 @@ export default class MQTTConnector extends AbstractConnectionObject): string { this.assertConnected(); - const form: Types.AIDTypes.InterfaceFormMQTTAction = mapping.forms[0]; - const topic = form.href.substring(1); + const form = mapping.forms[0] as Types.AIDTypes.InterfaceFormMQTTAction; + const topic = MQTTConnector.urlOrPathToTopic(form.href); if (topic === undefined || !mapping.forms) throw new Error("Mapping invalid."); const message = JSON.stringify(args); @@ -230,7 +240,7 @@ export default class MQTTConnector extends AbstractConnectionObject void): boolean { if (!mapping.observable || !this.client || !this.client.connected) return false; - const topic = mapping.forms[0].href.substring(1); + const topic = MQTTConnector.urlOrPathToTopic(mapping.forms[0].href); if (this.eventSubStore[topic] === undefined) this.eventSubStore[topic] = [{ target, cb }]; else this.eventSubStore[topic].push({ target, cb }); diff --git a/lib/src/multimessageBroker.ts b/lib/src/multimessageBroker.ts index 8d6a35c..5688d7c 100644 --- a/lib/src/multimessageBroker.ts +++ b/lib/src/multimessageBroker.ts @@ -191,8 +191,10 @@ export default class MultiMessageBroker { } } else { - const mapping = getMapping(request.target); - mapping.forms = mapping.forms.filter((form: InterfaceForm) => form.op.toLocaleLowerCase() === request.type.toLocaleLowerCase()); + // Because of references we need to clone this thing as we will modify it + const mappingOrig = getMapping(request.target); + const mapping = { ...mappingOrig }; + mapping.forms = mappingOrig.forms.filter((form) => form.op.toLocaleLowerCase() === request.type.toLocaleLowerCase()) as any; if (mapping.forms.length === 0) throw new ReferenceError(`No form found for ${(request.target as any).idShort} with operation ${request.type}`); diff --git a/lib/src/types/requests.ts b/lib/src/types/requests.ts index 1e68e3e..20e5247 100644 --- a/lib/src/types/requests.ts +++ b/lib/src/types/requests.ts @@ -1,5 +1,5 @@ import type { types } from "@aas-core-works/aas-core3.0-typescript"; -import type { EndpointMetadata, InterfaceForm } from "./aidConf"; +import type { EndpointMetadata, InterfaceAction, InterfaceEvent, InterfaceForm, InterfaceProperty } from "./aidConf"; /** * Read Property Request @@ -144,7 +144,8 @@ export type Request = GetRequest | WriteRequest | ObserveRequest | UnobserveRequ */ export type OnRequestCallback = (request: Request) => any & GetRequestCallback & WriteRequestCallback & ObserveRequestCallback & UnobserveRequestCallback & CallRequestCallback & AsyncStateRequestCallback & AsyncResultRequestCallback & SubscribeRequestCallback & UnsubscribeRequestCallback; -export type ConnectionConfiguration = EndpointMetadata & InterfaceForm; // Wenn Transformationen implementiert werden, dann hier anpassen +type InterfaceElement = InterfaceProperty | InterfaceAction | InterfaceEvent; +export type ConnectionConfiguration = EndpointMetadata & InterfaceElement; // Wenn Transformationen implementiert werden, dann hier anpassen export type MMBEvent = { type: "changed" | "event"; diff --git a/owntest.json b/owntest.json index 54961b5..545e5a0 100644 --- a/owntest.json +++ b/owntest.json @@ -198,7 +198,7 @@ { "idShort": "href", "valueType": "xs:string", - "value": "/testtopic/testparam1", + "value": "/testasset/testparam1/value", "modelType": "Property" }, { @@ -253,7 +253,7 @@ { "idShort": "href", "valueType": "xs:string", - "value": "mqtt://127.0.0.1:1883/testtopic/testparam2", + "value": "mqtt://127.0.0.1:1883/testasset/testparam2/value", "modelType": "Property" }, { @@ -277,7 +277,7 @@ { "idShort": "href", "valueType": "xs:string", - "value": "mqtt://127.0.0.1:1883/testtopic/testparam2/write", + "value": "mqtt://127.0.0.1:1883/testasset/testparam2/set", "modelType": "Property" }, { @@ -332,7 +332,7 @@ { "idShort": "href", "valueType": "xs:string", - "value": "/testtopic/testoperation1", + "value": "/testasset/testoperation1", "modelType": "Property" }, { @@ -375,7 +375,7 @@ { "idShort": "href", "valueType": "xs:string", - "value": "/testtopic/testoperation2", + "value": "/testasset/testoperation2", "modelType": "Property" }, { @@ -418,7 +418,7 @@ { "idShort": "href", "valueType": "xs:string", - "value": "/testtopic/testoperation3", + "value": "/testasset/testoperation3", "modelType": "Property" }, {