Fixing some stuff
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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 });
|
||||
|
||||
|
||||
+15
-24
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user