diff --git a/index.js b/index.js index 365947f..4620543 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,14 @@ const rules = require('./rules.js'); // Matrix client 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); matrix.start().then(() => console.log("Matrix bot started")); @@ -39,8 +46,10 @@ mqttClient.on('message', async (topic, payload) => { const data = JSON.parse(payload.toString()); // console.log(`MQTT message on ${topic}: ${data?.msg}`); - rules.forEach((r) => { - if (r.rule(data)) { + rules + .filter(({ name }) => enabledRules.includes(name)) + .forEach((r) => { + if (r.rule(data)) { console.log(`${topic} aplying ${r.name}`); postMessage(r.msg(data)); } diff --git a/rules.js b/rules.js index f6448b9..3c75419 100644 --- a/rules.js +++ b/rules.js @@ -1,3 +1,5 @@ +const config = require('./config.json'); + // Helper: SNR (dB) to emoji "bar chart" function snrToBars(db) { 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 readableTime = new Date(timestamp).toLocaleString(); // 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 = `${emoji.mic} reporting ${model}\n` + - (temperature !== undefined ? - `${emoji.temp} Temperature: ${temperature.toFixed(1)} °C\n` : '') + - (humidity !== undefined ? - `${emoji.humidity} Humidity: ${humidity}%\n` : '') + - (rain_mm !==undefined ? - `${emoji.rain} Rainfall: ${rain_mm} mm\n` : '') + - (wind_dir_deg !==undefined ? - `${emoji.wind} Wind: ${wind_avg_m_s} m/s avg, ${wind_max_m_s}m/s max + (temperature !== undefined ? + `${emoji.temp} Temperature: ${temperature.toFixed(1)} °C\n` : '') + + (humidity !== undefined ? + `${emoji.humidity} Humidity: ${humidity}%\n` : '') + + (rain_mm !==undefined ? + `${emoji.rain} Rainfall: ${rain_mm} mm\n` : '') + + (wind_dir_deg !==undefined ? + `${emoji.wind} Wind: ${wind_avg_m_s} m/s avg, ${wind_max_m_s}m/s max ${dirEmoji} (${wind_dir_deg}°)\n` : '') + - (light_lux !== undefined ? - `${emoji.light} Light: ${light_lux} lux\n` : '') + - (uv !== undefined ? - `${emoji.uv} UV Index: ${uv}\n` : '') + - `${emoji.time} Time: ${readableTime}\n`; + (light_lux !== undefined ? + `${emoji.light} Light: ${light_lux} lux\n` : '') + + (uv !== undefined ? + `${emoji.uv} UV Index: ${uv}\n` : '') + + `${emoji.time} Time: ${readableTime}\n`; console.log(`hh response`, response); return response.trim(); @@ -131,6 +140,11 @@ module.exports = [ rule: (data) => data?.msg?.includes('CQ'), msg: formatMessageCQ, }, + { + name: "C4LL", + rule: (data) => data?.msg?.includes(config.callsign), + msg: formatMessageCQ, + }, { name: "WX", // Not to miss the zero degree