From b0195e8e49d59ed9abfd5f34ce97c2a306e9a991 Mon Sep 17 00:00:00 2001 From: cheetah Date: Thu, 6 May 2021 18:59:49 +0000 Subject: [PATCH] added socket.io for live updates --- index.js | 34 +++++++++++++++++++++++++++++++++- package.json | 3 ++- types/MessageManager.js | 12 ++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index c7ae0aa..97485f2 100644 --- a/index.js +++ b/index.js @@ -28,7 +28,9 @@ types.DeviceRegistry.register(new types.devices.BirdySlim()) types.DeviceRegistry.register(new types.devices.Skyper()) const express = require('express') +const { MessageManager } = require('./types') const app = express(), appConfig = express() +const appServer = require('http').createServer(app) app.use(express.json()) app.use(express.static('html_main')) appConfig.use(express.json()) @@ -44,6 +46,26 @@ app.post('/api/message/advanced', async (req, res) => { return res.json(id) }) +app.post('/api/message/advanced/menu', async (req, res) => { + if (!req.body.payload) return res.status(500).json("ERROR: no msg payload") + if (!req.body.options) return res.status(500).json("ERROR: no msg options") + if (!req.body.routing) return res.status(500).json("ERROR: no msg routing") + + let id = await types.MessageManager.New('duplex', req.body.routing, + req.body.payload + '\n\n' + Object.keys(req.body.options).map((key, index) => { + return `[${ index + 1 }] ${ req.body.options[key].txt }` + }).join('\n') + ) + types.MessageManager.attachMenudata(id, { + options: req.body.options, + }) + await types.MessageManager.Deliver(id) + return res.json(id) +}) +app.get('/api/message/status/:id/menu', async (req, res) => { //TODO: make this fancy + return res.json(types.MessageManager.messages[ req.params.id ]) +}) + app.get('/api/message/status/:id', async (req, res) => { //TODO: make this fancy return res.json(types.MessageManager.messages[ req.params.id ]) }) @@ -86,6 +108,16 @@ app.get('/api/devices', async (req, res) => { return res.json(types.DeviceRegistry.DeviceStates) }) +const io = require('socket.io')(appServer) +io.on("connection", socket => { + console.log('new socket.io connection') +}) + +types.MessageManager.events.on('msgmgr:event', (eventType, eventData) => { + console.log(eventType, eventData) + io.sockets.emit('msgmgr:event', eventType, eventData) +}) + /** CONFIG Routes */ appConfig.get('/config', async (req, res) => { @@ -113,7 +145,7 @@ memstats() setInterval(memstats, 10e3)*/ -app.listen(config.general.port) +appServer.listen(config.general.port) if (config.general.configWebInterfaceEnabled === true) { appConfig.listen(config.general.configPort) } \ No newline at end of file diff --git a/package.json b/package.json index 8e4a5d4..a0cec29 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "express": "^4.17.1", "md5": "^2.3.0", "mqtt": "^4.2.6", - "querystring": "^0.2.1" + "querystring": "^0.2.1", + "socket.io": "^4.0.2" } } diff --git a/types/MessageManager.js b/types/MessageManager.js index 09ec5e8..c661bc6 100644 --- a/types/MessageManager.js +++ b/types/MessageManager.js @@ -1,3 +1,4 @@ +const events = require('events') const ConnectorRegistry = require("./ConnectorRegistry") const config = require('../config.json') const md5 = require('md5') @@ -5,6 +6,7 @@ const md5 = require('md5') class MessageManager { constructor() { this.messages = {} + this.events = new events.EventEmitter() ConnectorRegistry.events.on('msg:status', this.msgStatus.bind(this)) } async New(type, routingParams, payload) { @@ -45,6 +47,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]) } async Deliver(msgId) { if (this.messages[ msgId ].type === 'duplex') @@ -58,11 +61,20 @@ class MessageManager { } this.messages[ msgId ]._routerData.metadata.push(metadata) } + attachMenudata(msgId, menu) { + this.messages[ msgId ]._routerData.menu = menu + } + markMessageRead(msgId) { this.messages[ msgId ]._routerData.readAck = true + this.events.emit('msgmgr:event', 'read', msgId) } respondToMessage(msgId, response) { this.messages[ msgId ]._routerData.response = response + this.events.emit('msgmgr:event', 'response', [msgId, response]) + if (!!this.messages[ msgId ]._routerData.menu) { + //TODO: nice handling + } } _clearEventHandlers4MsgID(msgId) { ConnectorRegistry.events.removeAllListeners(`msg:status:${ msgId }:delivered`)