Fixed testasset

This commit is contained in:
Daniel Kluge
2023-12-09 14:38:44 +01:00
parent 0794e2e66f
commit e6c5e739bd
3 changed files with 76 additions and 61 deletions
+4 -5
View File
@@ -1,14 +1,13 @@
version: "3.7" version: "3.7"
services: services:
mqttbroker: mqttbroker:
image: emqx/emqx:5.3.2 image: eclipse-mosquitto:latest
container_name: mqttbroker container_name: mqttbroker
ports: ports:
- 1883:1883 - 1883:1883
- 8083:8083 - 9001:9001
- 8084:8084 volumes:
- 8883:8883 - ./mosquitto.conf:/mosquitto/config/mosquitto.conf
- 18083:18083
testasset: testasset:
build: build:
+1
View File
@@ -1,2 +1,3 @@
allow_anonymous true allow_anonymous true
listener 1883 listener 1883
persistence false
+28 -13
View File
@@ -5,10 +5,11 @@ 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 = () => {
// 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
const client = connect(broker, { let client = connect(broker, {
clientId: v4(), clientId: "testasset-" + v4(),
}); });
// Asset stuff // Asset stuff
@@ -17,21 +18,28 @@ let testparam2 = true;
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());
} }
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());
} }
const testoperation3 = (input) => { const testoperation3 = (input) => {
client.publish("testasset/testoperation3/result", JSON.stringify({message: "Wuh", input, error: false})); 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 }));
} }
// 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": case "testasset/testparam2/set":
testparam2 = new Boolean(message.toString()); const value = /^(true|1)$/i.test(message.toString());
console.log(`Testparam2 changed to ${value}`);
testparam2 = value;
break; break;
case "testasset/testoperation1": case "testasset/testoperation1":
testoperation1(); testoperation1();
@@ -54,13 +62,20 @@ client.on("close", () => {
console.log("Connection to broker closed"); console.log("Connection to broker closed");
}); });
// Start client.subscribe("testasset/testoperation1");
const start = () => { client.subscribe("testasset/testoperation2");
client.connect(); client.subscribe("testasset/testoperation3");
client.subscribe("testasset/#"); client.subscribe("testasset/+/set");
setInterval(() => { client.publish("testasset/testevent", "Timer ran down") }, 10000); setInterval(() => {
client.publish("testasset/testparam1/value", testparam1.toString());
client.publish("testasset/testparam2/value", testparam2.toString());
}, 1000);
setInterval(() => {
client.publish("testasset/testevent", "Timer ran down")
}, 10000);
client.publish("testasset/hello", "Hello World"); client.publish("testasset/hello", "Hello World");
} }
if (isDocker) setTimeout(start.bind(this), 3000); if (isDocker) setTimeout(main.bind(this), 3000);
else start(); else main();