46 lines
973 B
TypeScript
46 lines
973 B
TypeScript
import express from "npm:express"
|
|
import mqtt from "npm:mqtt"
|
|
|
|
const app = express()
|
|
const client = mqtt.connect("mqtt://localhost")
|
|
|
|
var vals = {
|
|
"min": {
|
|
"temp": 100,
|
|
"place": ""
|
|
},
|
|
"max": {
|
|
"temp": 0,
|
|
"place": ''
|
|
}
|
|
}
|
|
|
|
app.get("/temp", (req, res) => {
|
|
res.status(200).send(vals)
|
|
})
|
|
|
|
client.on("connect", () => {
|
|
client.subscribe("Building/temp", (err) => {
|
|
console.log("Subscribed to Building/temp topic.")
|
|
});
|
|
});
|
|
|
|
client.on("message", (topic, message) => {
|
|
if (topic == "Building/temp") {
|
|
const [ temp_str, place ] = message.toString().split(":")
|
|
const temp = parseFloat(temp_str)
|
|
if (temp < vals.min.temp) {
|
|
vals.min.temp = temp
|
|
vals.min.place = place
|
|
console.log(`Updated min temp to : [${place}] : ${temp}`)
|
|
}
|
|
if (temp > vals.max.temp) {
|
|
vals.max.temp = temp
|
|
vals.max.place = place
|
|
console.log(`Updated max temp to : [${place}] : ${temp}`)
|
|
}
|
|
}
|
|
})
|
|
|
|
app.listen(3000)
|
|
|