Fixing all the stuff

This commit is contained in:
Daniel Kluge
2023-12-09 17:45:36 +01:00
parent e6c5e739bd
commit d7aa44228f
5 changed files with 41 additions and 26 deletions
+9 -7
View File
@@ -55,6 +55,7 @@ export default class HTTPInterfaceServer extends AbstractInterfaceServer<Config>
* @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<Config>
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<Config>
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();
+19 -9
View File
@@ -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<mqtt.IClient
if (!this.client || !this.client.connected) throw new Error("Not connected");
}
private static urlOrPathToTopic(uri: string): string {
try {
const u = new URL(uri);
return u.pathname.substring(1);
} catch {
return uri.substring(1);
}
}
/**
* Read a property value from the asset.
*
@@ -110,7 +120,7 @@ export default class MQTTConnector extends AbstractConnectionObject<mqtt.IClient
const errorReturn = mapping.default ?? null;
if (!this.client || !this.client.connected) return errorReturn;
const value = this.messageStore[mapping.forms[0].href.substring(1)];
const value = this.messageStore[MQTTConnector.urlOrPathToTopic(mapping.forms[0].href)];
if (value === undefined) return errorReturn;
switch (mapping.type) {
case "integer":
@@ -137,7 +147,7 @@ export default class MQTTConnector extends AbstractConnectionObject<mqtt.IClient
public writeProperty(_target: AASCoreTypes.Property, mapping: Types.RequestTypes.ConnectionConfiguration, value: any): boolean {
if (!this.client || !this.client.connected) return false;
const topic = mapping.forms[0].href.substring(1);
const topic = MQTTConnector.urlOrPathToTopic(mapping.forms[0].href);
if (topic === undefined) return false;
this.client.publish(topic, value.toString());
@@ -156,7 +166,7 @@ export default class MQTTConnector extends AbstractConnectionObject<mqtt.IClient
public observeProperty(target: AASCoreTypes.Property, mapping: Types.RequestTypes.ConnectionConfiguration, cb: (value: Types.RequestTypes.MMBEvent) => 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<mqtt.IClient
}
public unobserveProperty(target: AASCoreTypes.Property, mapping: Types.RequestTypes.ConnectionConfiguration): void {
const topic = mapping.forms[0].href.substring(1);
const topic = MQTTConnector.urlOrPathToTopic(mapping.forms[0].href);
this.observerStore[topic];
//! TODO
}
@@ -182,8 +192,8 @@ export default class MQTTConnector extends AbstractConnectionObject<mqtt.IClient
public callActionSync(_target: AASCoreTypes.Operation, mapping: Types.RequestTypes.ConnectionConfiguration, args: Record<string, any>): 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<mqtt.IClient
public callActionAsync(_target: AASCoreTypes.Operation, mapping: Types.RequestTypes.ConnectionConfiguration, args: Record<string, any>): 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<mqtt.IClient
public subscribeEvent(target: AASCoreTypes.Class, mapping: Types.RequestTypes.ConnectionConfiguration, cb: (event: Types.RequestTypes.MMBEvent) => 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 });
+4 -2
View File
@@ -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}`);
+3 -2
View File
@@ -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";
+6 -6
View File
@@ -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"
},
{