diff --git a/example/src/modules/httpInterfaceServer.ts b/example/src/modules/httpInterfaceServer.ts index 56814b2..b1afaef 100644 --- a/example/src/modules/httpInterfaceServer.ts +++ b/example/src/modules/httpInterfaceServer.ts @@ -215,8 +215,8 @@ export default class HTTPInterfaceServer extends AbstractInterfaceServer 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 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); diff --git a/example/src/modules/mqttConnector.ts b/example/src/modules/mqttConnector.ts index ba835ef..305a26a 100644 --- a/example/src/modules/mqttConnector.ts +++ b/example/src/modules/mqttConnector.ts @@ -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 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): 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): 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 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 }); diff --git a/testasset/testasset.js b/testasset/testasset.js index 1e5e495..2e8eef6 100644 --- a/testasset/testasset.js +++ b/testasset/testasset.js @@ -5,11 +5,12 @@ import { v4 } from "uuid"; const isDocker = process.env.AM_I_IN_A_DOCKER_CONTAINER ?? false; const broker = isDocker ? "mqtt://mqttbroker:1883" : "mqtt://localhost:1883"; -// Start const main = () => { + console.log("Start") + // Create an mqtt client for broker localhost:1883 with a random client id - let client = connect(broker, { - clientId: "testasset-" + v4(), + const client = connect(broker, { + clientId: v4(), }); // Asset stuff @@ -18,28 +19,24 @@ const main = () => { const testoperation1 = () => { console.log("This is a result of testoperation 1"); testparam1++; - client.publish("testasset/testparam1/value", testparam1.toString()); + client.publish("testasset/testparam1", testparam1.toString()); } const testoperation2 = (input) => { console.log(`This is a result of testoperation 2 with input ${input}`); testparam1--; - client.publish("testasset/testparam1/value", testparam1.toString()); - + client.publish("testasset/testparam1", testparam1.toString()); } 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, valueFromInput: input.TestInput, error: false })); + client.publish("testasset/testoperation3/result", JSON.stringify({message: "Wuh", input, error: false})); } // Asset listeners client.on("message", (topic, message) => { - // console.log(`${topic}: ${message.toString()}`); switch (topic) { - case "testasset/testparam2/set": - const value = /^(true|1)$/i.test(message.toString()); - console.log(`Testparam2 changed to ${value}`); - testparam2 = value; + case "testasset/testparam2/write": + testparam2 = /(true|1)/i.test(message.toString()); + client.publish("testasset/testparam2", testparam2.toString()); break; case "testasset/testoperation1": testoperation1(); @@ -62,19 +59,13 @@ const main = () => { console.log("Connection to broker closed"); }); - client.subscribe("testasset/testoperation1"); - client.subscribe("testasset/testoperation2"); - client.subscribe("testasset/testoperation3"); - client.subscribe("testasset/+/set"); - - setInterval(() => { - client.publish("testasset/testparam1/value", testparam1.toString()); - client.publish("testasset/testparam2/value", testparam2.toString()); - }, 1000); + client.subscribe("testasset/#"); setInterval(() => { - client.publish("testasset/testevent", "Timer ran down") - }, 10000); + client.publish("testasset/testparam1", testparam1.toString()); + client.publish("testasset/testparam2", testparam2.toString()); + }, 1000); + setInterval(() => { client.publish("testasset/testevent", "Timer ran down") }, 10000); client.publish("testasset/hello", "Hello World"); } if (isDocker) setTimeout(main.bind(this), 3000);