updated rulefiltering, added selfmonitor (C4ll)
This commit is contained in:
15
index.js
15
index.js
@@ -5,7 +5,14 @@ const rules = require('./rules.js');
|
|||||||
|
|
||||||
// Matrix client
|
// Matrix client
|
||||||
const storage = new SimpleFsStorageProvider("matrix-storage.json");
|
const storage = new SimpleFsStorageProvider("matrix-storage.json");
|
||||||
const matrix = new MatrixClient(config.matrix.homeserverUrl, config.matrix.accessToken, storage);
|
const matrix = new MatrixClient(
|
||||||
|
config.matrix.homeserverUrl,
|
||||||
|
config.matrix.accessToken,
|
||||||
|
storage);
|
||||||
|
|
||||||
|
const enabledRules = config.enabledRules;
|
||||||
|
console.log(`Enabled Rules`, enabledRules.join(", "));
|
||||||
|
|
||||||
AutojoinRoomsMixin.setupOnClient(matrix);
|
AutojoinRoomsMixin.setupOnClient(matrix);
|
||||||
matrix.start().then(() => console.log("Matrix bot started"));
|
matrix.start().then(() => console.log("Matrix bot started"));
|
||||||
|
|
||||||
@@ -39,8 +46,10 @@ mqttClient.on('message', async (topic, payload) => {
|
|||||||
const data = JSON.parse(payload.toString());
|
const data = JSON.parse(payload.toString());
|
||||||
// console.log(`MQTT message on ${topic}: ${data?.msg}`);
|
// console.log(`MQTT message on ${topic}: ${data?.msg}`);
|
||||||
|
|
||||||
rules.forEach((r) => {
|
rules
|
||||||
if (r.rule(data)) {
|
.filter(({ name }) => enabledRules.includes(name))
|
||||||
|
.forEach((r) => {
|
||||||
|
if (r.rule(data)) {
|
||||||
console.log(`${topic} aplying ${r.name}`);
|
console.log(`${topic} aplying ${r.name}`);
|
||||||
postMessage(r.msg(data));
|
postMessage(r.msg(data));
|
||||||
}
|
}
|
||||||
|
42
rules.js
42
rules.js
@@ -1,3 +1,5 @@
|
|||||||
|
const config = require('./config.json');
|
||||||
|
|
||||||
// Helper: SNR (dB) to emoji "bar chart"
|
// Helper: SNR (dB) to emoji "bar chart"
|
||||||
function snrToBars(db) {
|
function snrToBars(db) {
|
||||||
const snr = Math.max(Math.min(db, 0), -30); // clamp -30 to 0
|
const snr = Math.max(Math.min(db, 0), -30); // clamp -30 to 0
|
||||||
@@ -72,24 +74,31 @@ function formatMessageWX(data) {
|
|||||||
const dirEmoji = getWindDirectionEmoji(wind_dir_deg);
|
const dirEmoji = getWindDirectionEmoji(wind_dir_deg);
|
||||||
const readableTime = new Date(timestamp).toLocaleString();
|
const readableTime = new Date(timestamp).toLocaleString();
|
||||||
// Acurite-986 is giving celsius under _F
|
// Acurite-986 is giving celsius under _F
|
||||||
const temperature = temperature_C || temperature_F;
|
let temperature;
|
||||||
|
if (temperature_C != null) {
|
||||||
|
temperature = temperature_C;
|
||||||
|
} else if (temperature_F != null) {
|
||||||
|
temperature = (temperature_F - 32) * 5 / 9;
|
||||||
|
} else {
|
||||||
|
temperature = null; // or handle the missing value case as needed
|
||||||
|
}
|
||||||
|
|
||||||
const response =
|
const response =
|
||||||
`${emoji.mic} reporting ${model}\n` +
|
`${emoji.mic} reporting ${model}\n` +
|
||||||
(temperature !== undefined ?
|
(temperature !== undefined ?
|
||||||
`${emoji.temp} Temperature: ${temperature.toFixed(1)} °C\n` : '') +
|
`${emoji.temp} Temperature: ${temperature.toFixed(1)} °C\n` : '') +
|
||||||
(humidity !== undefined ?
|
(humidity !== undefined ?
|
||||||
`${emoji.humidity} Humidity: ${humidity}%\n` : '') +
|
`${emoji.humidity} Humidity: ${humidity}%\n` : '') +
|
||||||
(rain_mm !==undefined ?
|
(rain_mm !==undefined ?
|
||||||
`${emoji.rain} Rainfall: ${rain_mm} mm\n` : '') +
|
`${emoji.rain} Rainfall: ${rain_mm} mm\n` : '') +
|
||||||
(wind_dir_deg !==undefined ?
|
(wind_dir_deg !==undefined ?
|
||||||
`${emoji.wind} Wind: ${wind_avg_m_s} m/s avg, ${wind_max_m_s}m/s max
|
`${emoji.wind} Wind: ${wind_avg_m_s} m/s avg, ${wind_max_m_s}m/s max
|
||||||
${dirEmoji} (${wind_dir_deg}°)\n` : '') +
|
${dirEmoji} (${wind_dir_deg}°)\n` : '') +
|
||||||
(light_lux !== undefined ?
|
(light_lux !== undefined ?
|
||||||
`${emoji.light} Light: ${light_lux} lux\n` : '') +
|
`${emoji.light} Light: ${light_lux} lux\n` : '') +
|
||||||
(uv !== undefined ?
|
(uv !== undefined ?
|
||||||
`${emoji.uv} UV Index: ${uv}\n` : '') +
|
`${emoji.uv} UV Index: ${uv}\n` : '') +
|
||||||
`${emoji.time} Time: ${readableTime}\n`;
|
`${emoji.time} Time: ${readableTime}\n`;
|
||||||
|
|
||||||
console.log(`hh response`, response);
|
console.log(`hh response`, response);
|
||||||
return response.trim();
|
return response.trim();
|
||||||
@@ -131,6 +140,11 @@ module.exports = [
|
|||||||
rule: (data) => data?.msg?.includes('CQ'),
|
rule: (data) => data?.msg?.includes('CQ'),
|
||||||
msg: formatMessageCQ,
|
msg: formatMessageCQ,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "C4LL",
|
||||||
|
rule: (data) => data?.msg?.includes(config.callsign),
|
||||||
|
msg: formatMessageCQ,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "WX",
|
name: "WX",
|
||||||
// Not to miss the zero degree
|
// Not to miss the zero degree
|
||||||
|
Reference in New Issue
Block a user