From ad82e778ed40df1eb000042970b000266201f532 Mon Sep 17 00:00:00 2001 From: cheetah Date: Tue, 11 May 2021 07:56:50 +0000 Subject: [PATCH] added DeviceEvents over websocket --- index.js | 6 +++++- types/DeviceRegistry.js | 6 ++++++ types/MessageManager.js | 19 +++++++++++++++---- types/devices/BirdySlim.js | 10 ++++++++++ 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 97485f2..ac8afaa 100644 --- a/index.js +++ b/index.js @@ -113,11 +113,15 @@ io.on("connection", socket => { console.log('new socket.io connection') }) -types.MessageManager.events.on('msgmgr:event', (eventType, eventData) => { +types.MessageManager.events.on('event', (eventType, eventData) => { console.log(eventType, eventData) io.sockets.emit('msgmgr:event', eventType, eventData) }) +types.DeviceRegistry.events.on('event', (deviceType, deviceId, eventData) => { + console.log(deviceType, deviceId, eventData) + io.sockets.emit('device:event', deviceType, deviceId, eventData) +}) /** CONFIG Routes */ appConfig.get('/config', async (req, res) => { diff --git a/types/DeviceRegistry.js b/types/DeviceRegistry.js index 3f8de72..cbb16b8 100644 --- a/types/DeviceRegistry.js +++ b/types/DeviceRegistry.js @@ -1,6 +1,9 @@ +const events = require('events') + class DeviceRegistry { constructor() { this.Devices = {} + this.events = new events.EventEmitter() this.DeviceStates = {} // for keeping device states } register(device) { @@ -15,6 +18,9 @@ class DeviceRegistry { this.DeviceStates[ key ] = {} Object.assign(this.DeviceStates[ key], stateData) } + deviceEvent(deviceType, deviceId, eventData) { + this.events.emit('event', deviceType, deviceId, eventData) + } /*tryReceive(deviceName, data) { return this.Devices[ deviceName ].tryReceive(data) }*/ diff --git a/types/MessageManager.js b/types/MessageManager.js index c661bc6..4e0a672 100644 --- a/types/MessageManager.js +++ b/types/MessageManager.js @@ -2,7 +2,11 @@ const events = require('events') const ConnectorRegistry = require("./ConnectorRegistry") const config = require('../config.json') const md5 = require('md5') +const axios = require('axios') +function clamp(v,min,max) { + return Math.max(Math.min(v,min),max) +} class MessageManager { constructor() { this.messages = {} @@ -47,7 +51,7 @@ class MessageManager { if (status === 'delivered') this.messages[ msgId ]._routerData.recvAck = true //this.Deliver(msgId) console.log(msgId, uuid, 'status is', status) - this.events.emit('msgmgr:event', 'status', [msgId, uuid, status]) + this.events.emit('event', 'status', [msgId, uuid, status]) } async Deliver(msgId) { if (this.messages[ msgId ].type === 'duplex') @@ -67,13 +71,20 @@ class MessageManager { markMessageRead(msgId) { this.messages[ msgId ]._routerData.readAck = true - this.events.emit('msgmgr:event', 'read', msgId) + this.events.emit('event', 'read', msgId) } respondToMessage(msgId, response) { this.messages[ msgId ]._routerData.response = response - this.events.emit('msgmgr:event', 'response', [msgId, response]) + this.events.emit('event', 'response', [msgId, response]) if (!!this.messages[ msgId ]._routerData.menu) { - //TODO: nice handling + const menuOptions = this.messages[ msgId ]._routerData.menu + const selectedMenu = menuOptions[ Object.keys(menuOptions)[ + clamp(+response-1, 0, Object.keys(menuOptions).length) + ] ] + this.events.emit('event', 'menu', [msgId, response, selectedMenu]) + if (!!menuOptions.url) { + axios.get(menuOptions.url).then( () => false ) + } } } _clearEventHandlers4MsgID(msgId) { diff --git a/types/devices/BirdySlim.js b/types/devices/BirdySlim.js index 6e0dd16..841099e 100644 --- a/types/devices/BirdySlim.js +++ b/types/devices/BirdySlim.js @@ -71,6 +71,16 @@ class BirdySlim extends PagerDevice { date: data.date, } break; + + case 'status': + case 'cannedMessage': + case 'low_battery': + case 'power': + case 'battery': + case 'sos': + case 'gps': + require('../DeviceRegistry').deviceEvent(this.name, data.device_id, data) + break; } require('../DeviceRegistry').stateSet(this.name, data.device_id, stateSet) return true