Fixing some stuff

This commit is contained in:
Daniel Kluge
2023-12-21 10:21:49 +01:00
parent d7aa44228f
commit 7e7d1ab9e8
3 changed files with 33 additions and 47 deletions
+2 -4
View File
@@ -215,8 +215,8 @@ export default class HTTPInterfaceServer extends AbstractInterfaceServer<Config>
if (op === null || !AASCoreTypes.isOperation(op)) return res.status(404).end();
try {
const result = this.onRequestCallback({ type: "invokeAction", target: op, extraData: { args: req.body, async: false } });
return res.status(result ? 200 : 204).end();
this.onRequestCallback({ type: "invokeAction", target: op, extraData: { args: req.body, async: false } });
return res.send(204).end();
} catch {
return res.status(500).end();
}
@@ -226,8 +226,6 @@ export default class HTTPInterfaceServer extends AbstractInterfaceServer<Config>
const op = Traverser.getElementByIdPath(this.aas, sm, idShortPath);
if (op === null || !AASCoreTypes.isOperation(op)) return res.status(404).end();
// TODO
// Invoke operation and get handle
const request: Types.RequestTypes.Request = { type: "invokeAction", target: op, extraData: { args: req.body, async: true } };
const handle = this.onRequestCallback(request);
+16 -19
View File
@@ -1,8 +1,6 @@
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";
import { AASCoreTypes, type Types } from "aas-multimessagebroker";
/**
* This is an example module for the asset connection object.
@@ -96,12 +94,11 @@ 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);
private static getTopicFromForm(form: Types.AIDTypes.InterfaceForm): string {
if (form.href.startsWith("/")) return form.href.substring(1);
else {
const url = new URL(form.href);
return url.pathname.substring(1);
}
}
@@ -120,7 +117,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[MQTTConnector.urlOrPathToTopic(mapping.forms[0].href)];
const value = this.messageStore[MQTTConnector.getTopicFromForm(mapping.forms[0])];
if (value === undefined) return errorReturn;
switch (mapping.type) {
case "integer":
@@ -128,7 +125,7 @@ export default class MQTTConnector extends AbstractConnectionObject<mqtt.IClient
case "float":
return Number.parseFloat(value);
case "boolean":
return !!value;
return !!JSON.parse(value);
case "string":
default:
return value;
@@ -147,7 +144,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 = MQTTConnector.urlOrPathToTopic(mapping.forms[0].href);
const topic = MQTTConnector.getTopicFromForm(mapping.forms[0]);
if (topic === undefined) return false;
this.client.publish(topic, value.toString());
@@ -166,7 +163,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 = MQTTConnector.urlOrPathToTopic(mapping.forms[0].href);
const topic = MQTTConnector.getTopicFromForm(mapping.forms[0]);
if (this.observerStore[topic] === undefined) this.observerStore[topic] = [{ target, cb }];
else this.observerStore[topic].push({ target, cb });
@@ -174,7 +171,7 @@ export default class MQTTConnector extends AbstractConnectionObject<mqtt.IClient
}
public unobserveProperty(target: AASCoreTypes.Property, mapping: Types.RequestTypes.ConnectionConfiguration): void {
const topic = MQTTConnector.urlOrPathToTopic(mapping.forms[0].href);
const topic = MQTTConnector.getTopicFromForm(mapping.forms[0]);
this.observerStore[topic];
//! TODO
}
@@ -192,8 +189,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 = mapping.forms[0] as Types.AIDTypes.InterfaceFormMQTTAction;
const topic = MQTTConnector.urlOrPathToTopic(form.href);
const form: Types.AIDTypes.InterfaceFormMQTTAction = mapping.forms[0];
const topic = MQTTConnector.getTopicFromForm(form);
if (topic === undefined || !mapping.forms) throw new Error("Mapping invalid.");
const message = JSON.stringify(args);
@@ -218,8 +215,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 = mapping.forms[0] as Types.AIDTypes.InterfaceFormMQTTAction;
const topic = MQTTConnector.urlOrPathToTopic(form.href);
const form: Types.AIDTypes.InterfaceFormMQTTAction = mapping.forms[0];
const topic = MQTTConnector.getTopicFromForm(form);
if (topic === undefined || !mapping.forms) throw new Error("Mapping invalid.");
const message = JSON.stringify(args);
@@ -240,7 +237,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 = MQTTConnector.urlOrPathToTopic(mapping.forms[0].href);
const topic = MQTTConnector.getTopicFromForm(mapping.forms[0]);
if (this.eventSubStore[topic] === undefined) this.eventSubStore[topic] = [{ target, cb }];
else this.eventSubStore[topic].push({ target, cb });