Fixed testasset
This commit is contained in:
@@ -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,2 +1,3 @@
|
|||||||
allow_anonymous true
|
allow_anonymous true
|
||||||
listener 1883
|
listener 1883
|
||||||
|
persistence false
|
||||||
|
|||||||
+48
-33
@@ -5,33 +5,41 @@ 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
|
||||||
|
let client = connect(broker, {
|
||||||
|
clientId: "testasset-" + v4(),
|
||||||
|
});
|
||||||
|
|
||||||
// Create an mqtt client for broker localhost:1883 with a random client id
|
// Asset stuff
|
||||||
const client = connect(broker, {
|
let testparam1 = 5;
|
||||||
clientId: v4(),
|
let testparam2 = true;
|
||||||
});
|
const testoperation1 = () => {
|
||||||
|
|
||||||
// Asset stuff
|
|
||||||
let testparam1 = 5;
|
|
||||||
let testparam2 = true;
|
|
||||||
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) => {
|
|
||||||
client.publish("testasset/testoperation3/result", JSON.stringify({message: "Wuh", input, error: false}));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Asset listeners
|
}
|
||||||
|
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.on("message", (topic, message) => {
|
// Asset listeners
|
||||||
|
|
||||||
|
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();
|
||||||
@@ -43,24 +51,31 @@ client.on("message", (topic, message) => {
|
|||||||
testoperation3(message.toString());
|
testoperation3(message.toString());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
client.on("connect", () => {
|
client.on("connect", () => {
|
||||||
console.log("Connected to broker");
|
console.log("Connected to broker");
|
||||||
});
|
});
|
||||||
client.on("error", (error) => {
|
client.on("error", (error) => {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
});
|
});
|
||||||
client.on("close", () => {
|
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();
|
||||||
Reference in New Issue
Block a user