From 611464436285eee9299afb709e4133c845af7f0e Mon Sep 17 00:00:00 2001 From: cheetah Date: Wed, 3 Mar 2021 18:54:22 +0000 Subject: [PATCH] first version to work, for simplex --- index.js | 27 ++++++++++++----- package.json | 1 + types/ConnectorRegistry.js | 4 +-- types/DeviceRegistry.js | 4 --- types/Message.js | 12 ++++---- types/MessageManager.js | 45 +++++++++++++++++++++++++++++ types/connectors/Connector.js | 2 +- types/connectors/POCSAGConnector.js | 12 ++++---- types/devices/BirdySlim.js | 14 ++++++++- types/devices/Device.js | 5 ++-- types/devices/GenericPager.js | 27 +++++++++++++---- types/index.js | 2 ++ 12 files changed, 120 insertions(+), 35 deletions(-) create mode 100644 types/MessageManager.js diff --git a/index.js b/index.js index 7b81eb7..a30ced9 100644 --- a/index.js +++ b/index.js @@ -1,14 +1,27 @@ const amqp = require('amqp-connection-manager') // Create a connetion manager -const connection = amqp.connect(['amqp://daemon:daemon@10.13.37.37:5672/']) -connection.on('connect', () => console.log('Connected!')) -connection.on('disconnect', err => console.log('Disconnected.', err.stack)) +const connection = amqp.connect([ + 'amqp://daemon:daemon@10.13.37.37:5672/' +]) +connection.on('connect', () => console.log('Connected to AMQP.')) +connection.on('disconnect', err => console.log('Disconnected from AMQP.', err.stack)) -const types = require('./types') // also initalized the CentralEventEmitter +const types = require('./types') // also initializes the registries, if they havent been loaded types.ConnectorRegistry.register(new types.Connectors.POCSAGConnector(connection)) // activate POCSAG -types.ConnectorRegistry.transmit("pocsag", { +types.DeviceRegistry.register(new types.devices.GenericPager()) +types.DeviceRegistry.register(new types.devices.BirdySlim()) + +async function main() { + let id = await types.MessageManager.New('simple', { device: null, connectors: [ + ['pocsag', '133701A'] + ] }, "Hallo") + console.log('msgid', id) + await types.MessageManager.Deliver(id) +} +main() +/*types.ConnectorRegistry.transmit("pocsag", { ric: '133701', - body: "AAAA" -}) \ No newline at end of file + body: "AAAAhh, ich\n\n\n\nHabe einen Sprung" +})*/ \ No newline at end of file diff --git a/package.json b/package.json index cc6d21e..9e463e6 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ }, "homepage": "https://github.com/smartpager-network/pocsag-daemon#readme", "dependencies": { + "@supercharge/strings": "^1.18.0", "amqp-connection-manager": "^3.2.2", "amqplib": "^0.7.0" } diff --git a/types/ConnectorRegistry.js b/types/ConnectorRegistry.js index 6adc20e..29c46c8 100644 --- a/types/ConnectorRegistry.js +++ b/types/ConnectorRegistry.js @@ -7,9 +7,9 @@ class ConnectorRegistry { register(connector) { this.Connectors[ connector.name ] = connector } - transmit(name, message) { + transmit(name, msg, params) { if (!this.Connectors[ name ]) throw "not registred" - this.Connectors[ name ].transmitMessage(message) + this.Connectors[ name ].transmitMessage(msg, params) } } const registry = new ConnectorRegistry() diff --git a/types/DeviceRegistry.js b/types/DeviceRegistry.js index b07d97d..8aa6a19 100644 --- a/types/DeviceRegistry.js +++ b/types/DeviceRegistry.js @@ -1,5 +1,3 @@ -const { GenericPager, BirdySlim } = require("./devices") - class DeviceRegistry { constructor() { this.Devices = {} @@ -9,6 +7,4 @@ class DeviceRegistry { } } const registry = new DeviceRegistry() -registry.register(new GenericPager()) -registry.register(new BirdySlim()) module.exports = registry \ No newline at end of file diff --git a/types/Message.js b/types/Message.js index 63c822c..9bd198e 100644 --- a/types/Message.js +++ b/types/Message.js @@ -1,12 +1,12 @@ class Message { - ID - Type - RoutingMask - Content - constructor() { - + this.ID // get from MessageManager.New + this.Type // "simple" or "duplex" + this.RoutingMask // device=birdy&lora=devID:fPort&pocsag=133701B&dapnet=testID + this.Content // Blabla + this.Headers } + } module.exports = Message \ No newline at end of file diff --git a/types/MessageManager.js b/types/MessageManager.js new file mode 100644 index 0000000..dc71327 --- /dev/null +++ b/types/MessageManager.js @@ -0,0 +1,45 @@ +const ConnectorRegistry = require("./ConnectorRegistry") + +class MessageManager { + constructor() { + this.messages = {} + } + async New(type, routingParams, payload) { + if (!routingParams.device) { + routingParams.device = 'generic' + } + const msgObj = { + type, + routingParams, + payload, + } + await require("./DeviceRegistry").Devices[ routingParams.device ].formatTX(msgObj) + // console.log('finished msg obj is ', msgObj) + this.messages[ msgObj.id ]._routerData = this.messages[ msgObj.id ]._routerData || { + recvAck: false, + readAck: false, + response: false, + } + console.log('finished msg obj is ', msgObj) + return msgObj.id + } + async Deliver(msgId) { + const msg = this.messages[ msgId ] + // Throw error, because wtf + if (!msg._routerData) throw `No Routerdata attached to msg with id ${ msgId }` + // attach Start Delivery Unix Timestamp if not already there + if (!msg._routerData.startDelivery) msg._routerData.startDelivery = Math.floor(new Date().valueOf()/1e3) + if (!msg._routerData.deliveryLog) msg._routerData.deliveryLog = [] + let logLength = msg._routerData.deliveryLog.length + if (logLength < msg.routingParams.connectors.length) { + let deliveryConnector = msg.routingParams.connectors[ logLength ] + console.log('delivering with ', deliveryConnector) + await ConnectorRegistry.transmit(deliveryConnector[0], msg, deliveryConnector.slice(1)) + msg._routerData.deliveryLog.push(deliveryConnector) + } + } + async BindMsg(msg) { + this.messages[ msg.id ] = msg + } +} +module.exports = new MessageManager() \ No newline at end of file diff --git a/types/connectors/Connector.js b/types/connectors/Connector.js index e1551e9..cffa7bc 100644 --- a/types/connectors/Connector.js +++ b/types/connectors/Connector.js @@ -3,7 +3,7 @@ class Connector { this.amqpConnMngr = amqpConnMngr this.name = "_base" } - transmitMessage(msg) { + async transmitMessage(msg, params) { throw "not implemented" } } diff --git a/types/connectors/POCSAGConnector.js b/types/connectors/POCSAGConnector.js index 4ee3e83..4cab382 100644 --- a/types/connectors/POCSAGConnector.js +++ b/types/connectors/POCSAGConnector.js @@ -12,13 +12,13 @@ class POCSAGConnector extends Connector { } }) } - transmitMessage(msg) { - if (!msg.ric) return false - const RIC = msg.ric + async transmitMessage(msg, params) { + if (params.length < 1) return false + const RIC = params[0] const lastChar = RIC[RIC.length - 1].charCodeAt(0) - 65 const addressPart = lastChar >= 0 && lastChar <= 3 ? RIC.substring(0, RIC.length - 1) : RIC const functionBits = lastChar >= 0 && lastChar <= 3 ? lastChar : 0 - this.channelWrapper.sendToQueue('tx_pocsag', Buffer.from(msg.body), { + this.channelWrapper.sendToQueue('tx_pocsag', Buffer.from(msg.payload), { headers: { ric: { '!': 'int64', @@ -31,9 +31,9 @@ class POCSAGConnector extends Connector { } }) .then(function() { - return console.log("Message was sent! Hooray!") + return true }).catch(function(err) { - return console.log(err,"Message was rejected... Boo!") + return false }) } diff --git a/types/devices/BirdySlim.js b/types/devices/BirdySlim.js index a2d3c95..ff2013b 100644 --- a/types/devices/BirdySlim.js +++ b/types/devices/BirdySlim.js @@ -4,9 +4,21 @@ const PagerDevice = require("./Device") class BirdySlim extends PagerDevice { constructor () { super() + this.duplex = true this.name = "birdyslim" } - formatTX(msg) { + RandID() { + return `B${ Str.random(4) }` + } + async formatTX(msg) { + msg.id = this.RandID() + await MessageManager.BindMsg(msg) + msg.payload = `${ msg.id }${ msg.payload }` + msg._routerData = { + recvAck: true, + readAck: true, + response: true, + } } } module.exports = BirdySlim \ No newline at end of file diff --git a/types/devices/Device.js b/types/devices/Device.js index 679f921..b031d01 100644 --- a/types/devices/Device.js +++ b/types/devices/Device.js @@ -1,8 +1,9 @@ class PagerDevice { constructor () { + this.duplex = false this.name = "_base" } - formatTX(msg) { - } + async formatTX(msg) { } + RandID() { } } module.exports = PagerDevice \ No newline at end of file diff --git a/types/devices/GenericPager.js b/types/devices/GenericPager.js index b75d58e..78e808b 100644 --- a/types/devices/GenericPager.js +++ b/types/devices/GenericPager.js @@ -1,11 +1,26 @@ +const MessageManager = require("../MessageManager") const PagerDevice = require("./Device") +const Str = require('@supercharge/strings') class GenericPager extends PagerDevice { - constructor () { - super() - this.name = "generic" - } - transmit(msg) { + constructor () { + super() + this.duplex = false + this.name = "generic" + } + RandID() { + return `G${ Str.random(8) }` + } + async formatTX(msg) { + msg.id = this.RandID() + await MessageManager.BindMsg(msg) + msg.payload = `${ msg.id }${ msg.payload }` + msg._routerData = { + recvAck: false, + readAck: false, + response: false, } + // return msg } - module.exports = GenericPager \ No newline at end of file +} +module.exports = GenericPager \ No newline at end of file diff --git a/types/index.js b/types/index.js index 366ae3e..fb8b761 100644 --- a/types/index.js +++ b/types/index.js @@ -6,6 +6,8 @@ module.exports = { devices: require("./devices"), Message: require("./Message"), RouteablePage: require("./RouteablePage"), + DeviceRegistry: require("./DeviceRegistry"), + MessageManager: require("./MessageManager"), ConnectorRegistry: require("./ConnectorRegistry"), } \ No newline at end of file