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(); if (op === null || !AASCoreTypes.isOperation(op)) return res.status(404).end();
try { try {
const result = this.onRequestCallback({ type: "invokeAction", target: op, extraData: { args: req.body, async: false } }); this.onRequestCallback({ type: "invokeAction", target: op, extraData: { args: req.body, async: false } });
return res.status(result ? 200 : 204).end(); return res.send(204).end();
} catch { } catch {
return res.status(500).end(); return res.status(500).end();
} }
@@ -226,8 +226,6 @@ export default class HTTPInterfaceServer extends AbstractInterfaceServer<Config>
const op = Traverser.getElementByIdPath(this.aas, sm, idShortPath); const op = Traverser.getElementByIdPath(this.aas, sm, idShortPath);
if (op === null || !AASCoreTypes.isOperation(op)) return res.status(404).end(); 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 request: Types.RequestTypes.Request = { type: "invokeAction", target: op, extraData: { args: req.body, async: true } };
const handle = this.onRequestCallback(request); const handle = this.onRequestCallback(request);
+16 -19
View File
@@ -1,8 +1,6 @@
import * as mqtt from "mqtt"; import * as mqtt from "mqtt";
import { AbstractConnectionObject } from "aas-multimessagebroker"; import { AbstractConnectionObject } from "aas-multimessagebroker";
import { AASCoreTypes, Types } from "aas-multimessagebroker"; import { AASCoreTypes, type 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. * 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"); if (!this.client || !this.client.connected) throw new Error("Not connected");
} }
private static urlOrPathToTopic(uri: string): string { private static getTopicFromForm(form: Types.AIDTypes.InterfaceForm): string {
try { if (form.href.startsWith("/")) return form.href.substring(1);
const u = new URL(uri); else {
return u.pathname.substring(1); const url = new URL(form.href);
} catch { return url.pathname.substring(1);
return uri.substring(1);
} }
} }
@@ -120,7 +117,7 @@ export default class MQTTConnector extends AbstractConnectionObject<mqtt.IClient
const errorReturn = mapping.default ?? null; const errorReturn = mapping.default ?? null;
if (!this.client || !this.client.connected) return errorReturn; 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; if (value === undefined) return errorReturn;
switch (mapping.type) { switch (mapping.type) {
case "integer": case "integer":
@@ -128,7 +125,7 @@ export default class MQTTConnector extends AbstractConnectionObject<mqtt.IClient
case "float": case "float":
return Number.parseFloat(value); return Number.parseFloat(value);
case "boolean": case "boolean":
return !!value; return !!JSON.parse(value);
case "string": case "string":
default: default:
return value; 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 { public writeProperty(_target: AASCoreTypes.Property, mapping: Types.RequestTypes.ConnectionConfiguration, value: any): boolean {
if (!this.client || !this.client.connected) return false; 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; if (topic === undefined) return false;
this.client.publish(topic, value.toString()); 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 { 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; 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 }]; if (this.observerStore[topic] === undefined) this.observerStore[topic] = [{ target, cb }];
else this.observerStore[topic].push({ 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 { 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]; this.observerStore[topic];
//! TODO //! 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 { public callActionSync(_target: AASCoreTypes.Operation, mapping: Types.RequestTypes.ConnectionConfiguration, args: Record<string, any>): any {
this.assertConnected(); this.assertConnected();
const form = mapping.forms[0] as Types.AIDTypes.InterfaceFormMQTTAction; const form: Types.AIDTypes.InterfaceFormMQTTAction = mapping.forms[0];
const topic = MQTTConnector.urlOrPathToTopic(form.href); const topic = MQTTConnector.getTopicFromForm(form);
if (topic === undefined || !mapping.forms) throw new Error("Mapping invalid."); if (topic === undefined || !mapping.forms) throw new Error("Mapping invalid.");
const message = JSON.stringify(args); 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 { public callActionAsync(_target: AASCoreTypes.Operation, mapping: Types.RequestTypes.ConnectionConfiguration, args: Record<string, any>): string {
this.assertConnected(); this.assertConnected();
const form = mapping.forms[0] as Types.AIDTypes.InterfaceFormMQTTAction; const form: Types.AIDTypes.InterfaceFormMQTTAction = mapping.forms[0];
const topic = MQTTConnector.urlOrPathToTopic(form.href); const topic = MQTTConnector.getTopicFromForm(form);
if (topic === undefined || !mapping.forms) throw new Error("Mapping invalid."); if (topic === undefined || !mapping.forms) throw new Error("Mapping invalid.");
const message = JSON.stringify(args); 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 { 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; 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 }]; if (this.eventSubStore[topic] === undefined) this.eventSubStore[topic] = [{ target, cb }];
else this.eventSubStore[topic].push({ target, cb }); else this.eventSubStore[topic].push({ target, cb });
+14 -23
View File
@@ -5,11 +5,12 @@ import { v4 } from "uuid";
const isDocker = process.env.AM_I_IN_A_DOCKER_CONTAINER ?? false; const isDocker = process.env.AM_I_IN_A_DOCKER_CONTAINER ?? false;
const broker = isDocker ? "mqtt://mqttbroker:1883" : "mqtt://localhost:1883"; const broker = isDocker ? "mqtt://mqttbroker:1883" : "mqtt://localhost:1883";
// Start
const main = () => { const main = () => {
console.log("Start")
// Create an mqtt client for broker localhost:1883 with a random client id // Create an mqtt client for broker localhost:1883 with a random client id
let client = connect(broker, { const client = connect(broker, {
clientId: "testasset-" + v4(), clientId: v4(),
}); });
// Asset stuff // Asset stuff
@@ -18,28 +19,24 @@ const main = () => {
const testoperation1 = () => { const testoperation1 = () => {
console.log("This is a result of testoperation 1"); console.log("This is a result of testoperation 1");
testparam1++; testparam1++;
client.publish("testasset/testparam1/value", testparam1.toString()); client.publish("testasset/testparam1", testparam1.toString());
} }
const testoperation2 = (input) => { const testoperation2 = (input) => {
console.log(`This is a result of testoperation 2 with input ${input}`); console.log(`This is a result of testoperation 2 with input ${input}`);
testparam1--; testparam1--;
client.publish("testasset/testparam1/value", testparam1.toString()); client.publish("testasset/testparam1", testparam1.toString());
} }
const testoperation3 = (input) => { const testoperation3 = (input) => {
console.log(`This is a result of testoperation 3 with input ${input}`); client.publish("testasset/testoperation3/result", JSON.stringify({message: "Wuh", input, error: false}));
client.publish("testasset/testoperation3/result", JSON.stringify({ message: "Wuh", input, valueFromInput: input.TestInput, error: false }));
} }
// Asset listeners // Asset listeners
client.on("message", (topic, message) => { client.on("message", (topic, message) => {
// console.log(`${topic}: ${message.toString()}`);
switch (topic) { switch (topic) {
case "testasset/testparam2/set": case "testasset/testparam2/write":
const value = /^(true|1)$/i.test(message.toString()); testparam2 = /(true|1)/i.test(message.toString());
console.log(`Testparam2 changed to ${value}`); client.publish("testasset/testparam2", testparam2.toString());
testparam2 = value;
break; break;
case "testasset/testoperation1": case "testasset/testoperation1":
testoperation1(); testoperation1();
@@ -62,19 +59,13 @@ const main = () => {
console.log("Connection to broker closed"); console.log("Connection to broker closed");
}); });
client.subscribe("testasset/testoperation1"); client.subscribe("testasset/#");
client.subscribe("testasset/testoperation2");
client.subscribe("testasset/testoperation3");
client.subscribe("testasset/+/set");
setInterval(() => { setInterval(() => {
client.publish("testasset/testparam1/value", testparam1.toString()); client.publish("testasset/testparam1", testparam1.toString());
client.publish("testasset/testparam2/value", testparam2.toString()); client.publish("testasset/testparam2", testparam2.toString());
}, 1000); }, 1000);
setInterval(() => { client.publish("testasset/testevent", "Timer ran down") }, 10000);
setInterval(() => {
client.publish("testasset/testevent", "Timer ran down")
}, 10000);
client.publish("testasset/hello", "Hello World"); client.publish("testasset/hello", "Hello World");
} }
if (isDocker) setTimeout(main.bind(this), 3000); if (isDocker) setTimeout(main.bind(this), 3000);