Moved to rules system

This commit is contained in:
Myk
2025-08-03 02:19:12 +03:00
parent ff6296fd4c
commit 2ae2a675af
3 changed files with 171 additions and 111 deletions

140
index.js
View File

@@ -1,6 +1,7 @@
const mqtt = require('mqtt');
const { MatrixClient, SimpleFsStorageProvider, AutojoinRoomsMixin } = require('matrix-bot-sdk');
const config = require('./config.json');
const rules = require('./rules.js');
// Matrix client
const storage = new SimpleFsStorageProvider("matrix-storage.json");
@@ -8,93 +9,15 @@ const matrix = new MatrixClient(config.matrix.homeserverUrl, config.matrix.acces
AutojoinRoomsMixin.setupOnClient(matrix);
matrix.start().then(() => console.log("Matrix bot started"));
function formatMessageCQ(data) {
const {
callsign,
msg,
ccode,
country,
mode,
freq,
db,
dt,
locator
} = data;
// Country flag emoji
const emojiFlag = countryCodeToFlagEmoji(ccode);
// Convert frequency Hz to MHz with 3 decimal digits
const freqMHz = (freq / 1_000_000).toFixed(3);
// RSSI visual bars
const rssiBars = snrToBars(db);
// Google Maps locator search
const mapLink = `https://k7fry.com/grid/?qth=${locator}`;
return `📡 ${callsign} ${emojiFlag} 📍<a href="${mapLink}">${locator}</a>
📶 ${db} dB | ${dt} s | ${rssiBars}
🔊 ${freqMHz}MHz ${mode} `;
async function postMessage(msg) {
await matrix.sendMessage(config.matrix.roomId, {
msgtype: "m.text",
format: "org.matrix.custom.html",
formatted_body: msg,
body: msg,
});
}
function formatMessageWX(data) {
const {
temperature_F,
humidity,
rain_mm,
wind_dir_deg,
wind_avg_m_s,
wind_max_m_s,
light_lux,
uv,
mic,
mode,
timestamp
} = data;
const emoji = {
temp: "🌡️",
humidity: "💧",
rain: "🌧️",
wind: "🍃",
light: "💡",
uv: "☀️",
mic: "🎙️",
time: "🕒",
mode: "⚙️"
};
const dirEmoji = getWindDirectionEmoji(wind_dir_deg);
const readableTime = new Date(timestamp).toLocaleString();
return `
${emoji.temp} Temperature: ${temperature_F.toFixed(1)} °C
${emoji.humidity} Humidity: ${humidity}% (may be out of range!)
${emoji.rain} Rainfall: ${rain_mm} mm
${emoji.wind} Wind: ${wind_avg_m_s} m/s avg, ${wind_max_m_s} m/s max ${dirEmoji} (${wind_dir_deg}°)
${emoji.light} Light: ${light_lux} lux
${emoji.uv} UV Index: ${uv}
${emoji.time} Time: ${readableTime}
`.trim();
}
// Helper: SNR (dB) to emoji "bar chart"
function snrToBars(db) {
const snr = Math.max(Math.min(db, 0), -30); // clamp -30 to 0
const levels = 5;
const filled = Math.round(((snr + 30) / 30) * levels);
return '▓'.repeat(filled) + '░'.repeat(levels - filled);
}
// Helper: ISO country code (e.g. "CA") → 🇨🇦 emoji
function countryCodeToFlagEmoji(cc) {
if (!cc) return '';
return cc.toUpperCase().replace(/./g, c =>
String.fromCodePoint(0x1f1e6 - 65 + c.charCodeAt(0))
);
}
// MQTT client
const mqttClient = mqtt.connect(config.mqtt.url, {
username: config.mqtt.username,
@@ -114,30 +37,25 @@ mqttClient.on('connect', () => {
mqttClient.on('message', async (topic, payload) => {
const data = JSON.parse(payload.toString());
console.log(`MQTT message on ${topic}: ${data?.msg}`);
// Simple filter—customize as needed
if (data?.model?.includes('CQ')) {
await matrix.sendMessage(config.matrix.roomId, {
msgtype: "m.text",
format: "org.matrix.custom.html",
formatted_body: formatMessageCQ(data),
body: formatMessageCQ(data),
});
} else if (data?.msg?.includes('Sharp-SPC775')) {
const msg = `🌡️${data.temperature_C} | 💧${data.humidity}`;
await matrix.sendMessage(config.matrix.roomId, {
msgtype: "m.text",
format: "org.matrix.custom.html",
formatted_body: msg,
body: msg,
});
}
else if (data?.msg?.includes('Cotech-367959')) {
await matrix.sendMessage(config.matrix.roomId, {
msgtype: "m.text",
format: "org.matrix.custom.html",
formatted_body: formatMessageWX(data),
body: formatMessageWX(data),
});
}
// console.log(`MQTT message on ${topic}: ${data?.msg}`);
rules.forEach((r) => {
if (r.rule(data)) {
console.log(`${topic} aplying ${r.name}`);
postMessage(r.msg(data));
}
});
});
// Simple filter—customize as needed
/* if (data?.model?.includes('CQ')) {
* await postMessage(formatMessageCQ(data));
* } else if (data?.msg?.includes('Sharp-SPC775')) {
* const msg = `🌡️${data.temperature_C} | 💧${data.humidity}`;
* await postMessage(msg);
* }
* else if (data?.msg?.includes('Cotech-367959')) {
* await postMessage(formatMessageCQWX(data));
* } */