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')
})
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) => {

@ -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)
}*/

@ -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) {

@ -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

Loading…
Cancel
Save