Fixed testasset
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
version: "3.7"
|
||||
services:
|
||||
mqttbroker:
|
||||
image: emqx/emqx:5.3.2
|
||||
image: eclipse-mosquitto:latest
|
||||
container_name: mqttbroker
|
||||
ports:
|
||||
- 1883:1883
|
||||
- 8083:8083
|
||||
- 8084:8084
|
||||
- 8883:8883
|
||||
- 18083:18083
|
||||
- 9001:9001
|
||||
volumes:
|
||||
- ./mosquitto.conf:/mosquitto/config/mosquitto.conf
|
||||
|
||||
testasset:
|
||||
build:
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
allow_anonymous true
|
||||
listener 1883
|
||||
persistence false
|
||||
|
||||
+70
-55
@@ -5,62 +5,77 @@ import { v4 } from "uuid";
|
||||
const isDocker = process.env.AM_I_IN_A_DOCKER_CONTAINER ?? false;
|
||||
const broker = isDocker ? "mqtt://mqttbroker:1883" : "mqtt://localhost:1883";
|
||||
|
||||
|
||||
// Create an mqtt client for broker localhost:1883 with a random client id
|
||||
const client = connect(broker, {
|
||||
clientId: v4(),
|
||||
});
|
||||
|
||||
// Asset stuff
|
||||
let testparam1 = 5;
|
||||
let testparam2 = true;
|
||||
const testoperation1 = () => {
|
||||
console.log("This is a result of testoperation 1");
|
||||
testparam1++;
|
||||
}
|
||||
const testoperation2 = (input) => {
|
||||
console.log(`This is a result of testoperation 2 with input ${input}`);
|
||||
testparam1--;
|
||||
}
|
||||
const testoperation3 = (input) => {
|
||||
client.publish("testasset/testoperation3/result", JSON.stringify({message: "Wuh", input, error: false}));
|
||||
}
|
||||
|
||||
// Asset listeners
|
||||
|
||||
client.on("message", (topic, message) => {
|
||||
switch (topic) {
|
||||
case "testasset/testparam2":
|
||||
testparam2 = new Boolean(message.toString());
|
||||
break;
|
||||
case "testasset/testoperation1":
|
||||
testoperation1();
|
||||
break;
|
||||
case "testasset/testoperation2":
|
||||
testoperation2(JSON.parse(message.toString()).TestInput);
|
||||
break;
|
||||
case "testasset/testoperation3":
|
||||
testoperation3(message.toString());
|
||||
break;
|
||||
}
|
||||
});
|
||||
client.on("connect", () => {
|
||||
console.log("Connected to broker");
|
||||
});
|
||||
client.on("error", (error) => {
|
||||
console.error(error);
|
||||
});
|
||||
client.on("close", () => {
|
||||
console.log("Connection to broker closed");
|
||||
});
|
||||
|
||||
// Start
|
||||
const start = () => {
|
||||
client.connect();
|
||||
client.subscribe("testasset/#");
|
||||
const main = () => {
|
||||
// Create an mqtt client for broker localhost:1883 with a random client id
|
||||
let client = connect(broker, {
|
||||
clientId: "testasset-" + v4(),
|
||||
});
|
||||
|
||||
setInterval(() => { client.publish("testasset/testevent", "Timer ran down") }, 10000);
|
||||
// Asset stuff
|
||||
let testparam1 = 5;
|
||||
let testparam2 = true;
|
||||
const testoperation1 = () => {
|
||||
console.log("This is a result of testoperation 1");
|
||||
testparam1++;
|
||||
client.publish("testasset/testparam1/value", 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());
|
||||
|
||||
}
|
||||
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 }));
|
||||
}
|
||||
|
||||
// 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;
|
||||
break;
|
||||
case "testasset/testoperation1":
|
||||
testoperation1();
|
||||
break;
|
||||
case "testasset/testoperation2":
|
||||
testoperation2(JSON.parse(message.toString()).TestInput);
|
||||
break;
|
||||
case "testasset/testoperation3":
|
||||
testoperation3(message.toString());
|
||||
break;
|
||||
}
|
||||
});
|
||||
client.on("connect", () => {
|
||||
console.log("Connected to broker");
|
||||
});
|
||||
client.on("error", (error) => {
|
||||
console.error(error);
|
||||
});
|
||||
client.on("close", () => {
|
||||
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);
|
||||
|
||||
setInterval(() => {
|
||||
client.publish("testasset/testevent", "Timer ran down")
|
||||
}, 10000);
|
||||
client.publish("testasset/hello", "Hello World");
|
||||
}
|
||||
if (isDocker) setTimeout(start.bind(this), 3000);
|
||||
else start();
|
||||
if (isDocker) setTimeout(main.bind(this), 3000);
|
||||
else main();
|
||||
Reference in New Issue
Block a user