Skip to content
Snippets Groups Projects
Commit 579ad72c authored by echicken's avatar echicken :chicken:
Browse files

Pass list of (configured for this module) devices into the module constructor,...

Pass list of (configured for this module) devices into the module constructor, so a module can send unsolicted messages (eg. broadcast an motd at intervals).
parent 67feeab4
No related branches found
No related tags found
No related merge requests found
...@@ -2234,7 +2234,7 @@ ...@@ -2234,7 +2234,7 @@
}, },
"node_modules/@sota/meshtastic": { "node_modules/@sota/meshtastic": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "git+https://gitlab.synchro.net/sota/meshtastic.git#f0be29711a62c8b8bf6ce0899ed72ddc3e84767d", "resolved": "git+https://gitlab.synchro.net/sota/meshtastic.git#2ef5d96104218121e896153d9375653ce3f8be16",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@buf/meshtastic_protobufs.bufbuild_es": "^1.8.0-20240411120154-019267fa4cc6.2", "@buf/meshtastic_protobufs.bufbuild_es": "^1.8.0-20240411120154-019267fa4cc6.2",
...@@ -5237,7 +5237,7 @@ ...@@ -5237,7 +5237,7 @@
"optional": true "optional": true
}, },
"@sota/meshtastic": { "@sota/meshtastic": {
"version": "git+https://gitlab.synchro.net/sota/meshtastic.git#f0be29711a62c8b8bf6ce0899ed72ddc3e84767d", "version": "git+https://gitlab.synchro.net/sota/meshtastic.git#2ef5d96104218121e896153d9375653ce3f8be16",
"from": "@sota/meshtastic@git+https://gitlab.synchro.net/sota/meshtastic.git", "from": "@sota/meshtastic@git+https://gitlab.synchro.net/sota/meshtastic.git",
"requires": { "requires": {
"@buf/meshtastic_protobufs.bufbuild_es": "^1.8.0-20240411120154-019267fa4cc6.2", "@buf/meshtastic_protobufs.bufbuild_es": "^1.8.0-20240411120154-019267fa4cc6.2",
......
...@@ -4,9 +4,11 @@ import { IModuleConfig } from './config'; ...@@ -4,9 +4,11 @@ import { IModuleConfig } from './config';
export default abstract class Module { export default abstract class Module {
config: IModuleConfig; config: IModuleConfig;
devices: Device[];
constructor(config: IModuleConfig) { constructor(config: IModuleConfig, devices: Device[]) {
this.config = config; this.config = config;
this.devices = devices;
} }
abstract handlePacket(packet: protobuf.Mesh.MeshPacket, device: Device, replyTo: number): boolean; // 'true' if handled abstract handlePacket(packet: protobuf.Mesh.MeshPacket, device: Device, replyTo: number): boolean; // 'true' if handled
......
...@@ -4,8 +4,8 @@ import { IModuleConfig } from '../lib/config'; ...@@ -4,8 +4,8 @@ import { IModuleConfig } from '../lib/config';
export default class Echo extends Module { export default class Echo extends Module {
constructor(config: IModuleConfig) { constructor(config: IModuleConfig, devices: Device[]) {
super(config); super(config, devices);
} }
handlePacket(packet: protobuf.Mesh.MeshPacket, device: Device, replyTo: number): boolean { handlePacket(packet: protobuf.Mesh.MeshPacket, device: Device, replyTo: number): boolean {
...@@ -15,7 +15,7 @@ export default class Echo extends Module { ...@@ -15,7 +15,7 @@ export default class Echo extends Module {
const msg = dec.decode(packet.payloadVariant.value.payload); const msg = dec.decode(packet.payloadVariant.value.payload);
if (!msg.startsWith('.echo ')) return false; if (!msg.startsWith('.echo ')) return false;
const echo = `You sent: ${msg.replace(/^.echo\s/, '')}`; const echo = `You sent: ${msg.replace(/^.echo\s/, '')}`;
device.sendText({ text: echo, to: replyTo, channel: packet.channel }); device.sendText({ text: echo, to: replyTo, channel: packet.channel, replyId: packet.id });
return true; return true;
} }
......
...@@ -5,8 +5,8 @@ import { IModuleConfig } from '../lib/config'; ...@@ -5,8 +5,8 @@ import { IModuleConfig } from '../lib/config';
export default class Weather extends Module { export default class Weather extends Module {
constructor(config: IModuleConfig) { constructor(config: IModuleConfig, devices: Device[]) {
super(config); super(config, devices);
} }
handlePacket(packet: protobuf.Mesh.MeshPacket, device: Device, replyTo: number): boolean { handlePacket(packet: protobuf.Mesh.MeshPacket, device: Device, replyTo: number): boolean {
...@@ -26,7 +26,7 @@ export default class Weather extends Module { ...@@ -26,7 +26,7 @@ export default class Weather extends Module {
ws += `\r\nHumidity: ${weather.current.humidity}%`; ws += `\r\nHumidity: ${weather.current.humidity}%`;
ws += `\r\nWind: ${weather.current.wind_speed} KPH ${wd}, Gust: ${weather.current.wind_gust} KPH`; ws += `\r\nWind: ${weather.current.wind_speed} KPH ${wd}, Gust: ${weather.current.wind_gust} KPH`;
ws += `\r\n${weather.current.clouds}% cloudy`; ws += `\r\n${weather.current.clouds}% cloudy`;
device.sendText({ text: ws, to: replyTo, channel: packet.channel }); device.sendText({ text: ws, to: replyTo, channel: packet.channel, replyId: packet.id });
return true; return true;
} catch (err: any) { } catch (err: any) {
this.error(err as string); this.error(err as string);
......
...@@ -8,22 +8,8 @@ import Weather from './modules/weather'; ...@@ -8,22 +8,8 @@ import Weather from './modules/weather';
function init(): void { function init(): void {
const config: IServerConfig = getConfig(); const config: IServerConfig = getConfig();
const devices: Record<number, Device> = {};
const modules: Module[] = []; const modules: Module[] = [];
for (const module in config.modules) {
switch (config.modules[module].path) {
case 'echo':
modules.push(new Echo(config.modules[module]));
break;
case 'weather':
modules.push(new Weather(config.modules[module]));
break;
default:
const m = js.global.load({}, config.modules[module].path);
modules.push(new m(config.modules[module]));
break;
}
}
for (const device in config.devices) { for (const device in config.devices) {
...@@ -32,11 +18,11 @@ function init(): void { ...@@ -32,11 +18,11 @@ function init(): void {
if (config.devices[device].type === 'serial') { if (config.devices[device].type === 'serial') {
const cfg = config.devices[device] as ISerialDeviceConfig; const cfg = config.devices[device] as ISerialDeviceConfig;
dev = new SerialDevice(cfg.port, cfg.baud, undefined, undefined, cfg.debug, cfg.id); dev = new SerialDevice(cfg.port, cfg.baud, undefined, undefined, cfg.debug, cfg.id);
dev.on('ready', () => js.global.log(sbbsdefs.LOG_DEBUG, `${cfg.type} device ${cfg.port} is ready`)); dev.on('ready', () => js.global.log(sbbsdefs.LOG_INFO, `${cfg.type} device ${cfg.port} is ready`));
} else if (config.devices[device].type === 'socket') { } else if (config.devices[device].type === 'socket') {
const cfg = config.devices[device] as ISocketDeviceConfig; const cfg = config.devices[device] as ISocketDeviceConfig;
dev = new SocketDevice(cfg.host, cfg.port, cfg.debug, cfg.id); dev = new SocketDevice(cfg.host, cfg.port, cfg.debug, cfg.id);
dev.on('ready', () => js.global.log(sbbsdefs.LOG_DEBUG, `${cfg.type} device ${cfg.host}:${cfg.port} is ready`)); dev.on('ready', () => js.global.log(sbbsdefs.LOG_INFO, `${cfg.type} device ${cfg.host}:${cfg.port} is ready`));
} }
if (dev === undefined) continue; if (dev === undefined) continue;
...@@ -61,6 +47,30 @@ function init(): void { ...@@ -61,6 +47,30 @@ function init(): void {
dev.connect(); dev.connect();
devices[config.devices[device].id] = dev;
}
for (const module in config.modules) {
const devs: Device[] = [];
for (const devId in config.modules[module].devices) {
if (devices[devId] !== undefined) devs.push(devices[devId]);
}
switch (config.modules[module].path) {
case 'echo':
modules.push(new Echo(config.modules[module], devs));
break;
case 'weather':
modules.push(new Weather(config.modules[module], devs));
break;
default:
const m = js.global.load({}, config.modules[module].path);
modules.push(new m(config.modules[module]));
break;
}
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment