added DeviceEvents over websocket

master
cheetah 4 years ago
parent 2e614ccc6a
commit ad82e778ed

@ -113,11 +113,15 @@ io.on("connection", socket => {
console.log('new socket.io connection') 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) console.log(eventType, eventData)
io.sockets.emit('msgmgr:event', 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 */ /** CONFIG Routes */
appConfig.get('/config', async (req, res) => { appConfig.get('/config', async (req, res) => {

@ -1,6 +1,9 @@
const events = require('events')
class DeviceRegistry { class DeviceRegistry {
constructor() { constructor() {
this.Devices = {} this.Devices = {}
this.events = new events.EventEmitter()
this.DeviceStates = {} // for keeping device states this.DeviceStates = {} // for keeping device states
} }
register(device) { register(device) {
@ -15,6 +18,9 @@ class DeviceRegistry {
this.DeviceStates[ key ] = {} this.DeviceStates[ key ] = {}
Object.assign(this.DeviceStates[ key], stateData) Object.assign(this.DeviceStates[ key], stateData)
} }
deviceEvent(deviceType, deviceId, eventData) {
this.events.emit('event', deviceType, deviceId, eventData)
}
/*tryReceive(deviceName, data) { /*tryReceive(deviceName, data) {
return this.Devices[ deviceName ].tryReceive(data) return this.Devices[ deviceName ].tryReceive(data)
}*/ }*/

@ -2,7 +2,11 @@ const events = require('events')
const ConnectorRegistry = require("./ConnectorRegistry") const ConnectorRegistry = require("./ConnectorRegistry")
const config = require('../config.json') const config = require('../config.json')
const md5 = require('md5') const md5 = require('md5')
const axios = require('axios')
function clamp(v,min,max) {
return Math.max(Math.min(v,min),max)
}
class MessageManager { class MessageManager {
constructor() { constructor() {
this.messages = {} this.messages = {}
@ -47,7 +51,7 @@ class MessageManager {
if (status === 'delivered') this.messages[ msgId ]._routerData.recvAck = true if (status === 'delivered') this.messages[ msgId ]._routerData.recvAck = true
//this.Deliver(msgId) //this.Deliver(msgId)
console.log(msgId, uuid, 'status is', status) 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) { async Deliver(msgId) {
if (this.messages[ msgId ].type === 'duplex') if (this.messages[ msgId ].type === 'duplex')
@ -67,13 +71,20 @@ class MessageManager {
markMessageRead(msgId) { markMessageRead(msgId) {
this.messages[ msgId ]._routerData.readAck = true this.messages[ msgId ]._routerData.readAck = true
this.events.emit('msgmgr:event', 'read', msgId) this.events.emit('event', 'read', msgId)
} }
respondToMessage(msgId, response) { respondToMessage(msgId, response) {
this.messages[ msgId ]._routerData.response = 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) { 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) { _clearEventHandlers4MsgID(msgId) {

@ -71,6 +71,16 @@ class BirdySlim extends PagerDevice {
date: data.date, date: data.date,
} }
break; 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) require('../DeviceRegistry').stateSet(this.name, data.device_id, stateSet)
return true return true

Loading…
Cancel
Save