diff --git a/package-lock.json b/package-lock.json index 2736d3a3c53643f26c52be2b916d8f1c1a5dc42b..6b77ade22fa42d66938f532552ea17e5613ef2a4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2234,7 +2234,7 @@ }, "node_modules/@sota/meshtastic": { "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", "dependencies": { "@buf/meshtastic_protobufs.bufbuild_es": "^1.8.0-20240411120154-019267fa4cc6.2", @@ -5237,7 +5237,7 @@ "optional": true }, "@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", "requires": { "@buf/meshtastic_protobufs.bufbuild_es": "^1.8.0-20240411120154-019267fa4cc6.2", diff --git a/src/lib/module.ts b/src/lib/module.ts index e1e6165f44942618f22afe71dcb5f52a1a972b88..6e338c3bfea163c2343af67e957d83721b2cd193 100644 --- a/src/lib/module.ts +++ b/src/lib/module.ts @@ -4,9 +4,11 @@ import { IModuleConfig } from './config'; export default abstract class Module { config: IModuleConfig; + devices: Device[]; - constructor(config: IModuleConfig) { + constructor(config: IModuleConfig, devices: Device[]) { this.config = config; + this.devices = devices; } abstract handlePacket(packet: protobuf.Mesh.MeshPacket, device: Device, replyTo: number): boolean; // 'true' if handled diff --git a/src/modules/echo.ts b/src/modules/echo.ts index d9b6b2a08e5df1f461e1e9bd34c57cc0f6cf88e5..a045ea87b6fdbe5e04aa154bc4a6f9c7787b171e 100644 --- a/src/modules/echo.ts +++ b/src/modules/echo.ts @@ -4,8 +4,8 @@ import { IModuleConfig } from '../lib/config'; export default class Echo extends Module { - constructor(config: IModuleConfig) { - super(config); + constructor(config: IModuleConfig, devices: Device[]) { + super(config, devices); } handlePacket(packet: protobuf.Mesh.MeshPacket, device: Device, replyTo: number): boolean { @@ -15,7 +15,7 @@ export default class Echo extends Module { const msg = dec.decode(packet.payloadVariant.value.payload); if (!msg.startsWith('.echo ')) return false; 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; } diff --git a/src/modules/weather.ts b/src/modules/weather.ts index 671f70f9f858e740d0c1b07d8c9fbc8ff8ba0c05..76f93d6d612c65a273d03da6c250336996354faf 100644 --- a/src/modules/weather.ts +++ b/src/modules/weather.ts @@ -5,8 +5,8 @@ import { IModuleConfig } from '../lib/config'; export default class Weather extends Module { - constructor(config: IModuleConfig) { - super(config); + constructor(config: IModuleConfig, devices: Device[]) { + super(config, devices); } handlePacket(packet: protobuf.Mesh.MeshPacket, device: Device, replyTo: number): boolean { @@ -26,7 +26,7 @@ export default class Weather extends Module { ws += `\r\nHumidity: ${weather.current.humidity}%`; ws += `\r\nWind: ${weather.current.wind_speed} KPH ${wd}, Gust: ${weather.current.wind_gust} KPH`; 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; } catch (err: any) { this.error(err as string); diff --git a/src/synctastic.ts b/src/synctastic.ts index cd9761425c11082885c9d50dadaf6d40d12a816c..509032eaa51de0a7ab21b15858f228e9c757962f 100644 --- a/src/synctastic.ts +++ b/src/synctastic.ts @@ -8,22 +8,8 @@ import Weather from './modules/weather'; function init(): void { const config: IServerConfig = getConfig(); - + const devices: Record<number, Device> = {}; 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) { @@ -32,11 +18,11 @@ function init(): void { if (config.devices[device].type === 'serial') { const cfg = config.devices[device] as ISerialDeviceConfig; 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') { const cfg = config.devices[device] as ISocketDeviceConfig; 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; @@ -61,6 +47,30 @@ function init(): void { 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; + } + } }