From 9a738d41cde89e779031019483edac15b59ddf10 Mon Sep 17 00:00:00 2001 From: cheetah Date: Wed, 3 Mar 2021 09:40:09 +0000 Subject: [PATCH] test code --- index.js | 14 ++++++++++ package.json | 23 ++++++++++++++++ types/ConnectorRegistry.js | 16 +++++++++++ types/DeviceRegistry.js | 14 ++++++++++ types/Message.js | 12 ++++++++ types/RouteablePage.js | 6 ++++ types/connectors/Connector.js | 10 +++++++ types/connectors/DAPNETConnector.js | 9 ++++++ types/connectors/LoRaWANConnector.js | 9 ++++++ types/connectors/POCSAGConnector.js | 41 ++++++++++++++++++++++++++++ types/connectors/index.js | 6 ++++ types/devices/BirdySlim.js | 12 ++++++++ types/devices/Device.js | 8 ++++++ types/devices/GenericPager.js | 11 ++++++++ types/devices/index.js | 5 ++++ types/index.js | 11 ++++++++ 16 files changed, 207 insertions(+) create mode 100644 index.js create mode 100644 package.json create mode 100644 types/ConnectorRegistry.js create mode 100644 types/DeviceRegistry.js create mode 100644 types/Message.js create mode 100644 types/RouteablePage.js create mode 100644 types/connectors/Connector.js create mode 100644 types/connectors/DAPNETConnector.js create mode 100644 types/connectors/LoRaWANConnector.js create mode 100644 types/connectors/POCSAGConnector.js create mode 100644 types/connectors/index.js create mode 100644 types/devices/BirdySlim.js create mode 100644 types/devices/Device.js create mode 100644 types/devices/GenericPager.js create mode 100644 types/devices/index.js create mode 100644 types/index.js diff --git a/index.js b/index.js new file mode 100644 index 0000000..7b81eb7 --- /dev/null +++ b/index.js @@ -0,0 +1,14 @@ +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 types = require('./types') // also initalized the CentralEventEmitter +types.ConnectorRegistry.register(new types.Connectors.POCSAGConnector(connection)) // activate POCSAG + +types.ConnectorRegistry.transmit("pocsag", { + ric: '133701', + body: "AAAA" +}) \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..cc6d21e --- /dev/null +++ b/package.json @@ -0,0 +1,23 @@ +{ + "name": "pocsag-daemon", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/smartpager-network/pocsag-daemon.git" + }, + "author": "catSIXe", + "license": "ISC", + "bugs": { + "url": "https://github.com/smartpager-network/pocsag-daemon/issues" + }, + "homepage": "https://github.com/smartpager-network/pocsag-daemon#readme", + "dependencies": { + "amqp-connection-manager": "^3.2.2", + "amqplib": "^0.7.0" + } +} diff --git a/types/ConnectorRegistry.js b/types/ConnectorRegistry.js new file mode 100644 index 0000000..6adc20e --- /dev/null +++ b/types/ConnectorRegistry.js @@ -0,0 +1,16 @@ +const { POCSAGConnector } = require("./connectors") + +class ConnectorRegistry { + constructor() { + this.Connectors = {} + } + register(connector) { + this.Connectors[ connector.name ] = connector + } + transmit(name, message) { + if (!this.Connectors[ name ]) throw "not registred" + this.Connectors[ name ].transmitMessage(message) + } +} +const registry = new ConnectorRegistry() +module.exports = registry \ No newline at end of file diff --git a/types/DeviceRegistry.js b/types/DeviceRegistry.js new file mode 100644 index 0000000..b07d97d --- /dev/null +++ b/types/DeviceRegistry.js @@ -0,0 +1,14 @@ +const { GenericPager, BirdySlim } = require("./devices") + +class DeviceRegistry { + constructor() { + this.Devices = {} + } + register(device) { + this.Devices[ device.name ] = device + } +} +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 new file mode 100644 index 0000000..63c822c --- /dev/null +++ b/types/Message.js @@ -0,0 +1,12 @@ +class Message { + ID + Type + RoutingMask + Content + + constructor() { + + } +} + +module.exports = Message \ No newline at end of file diff --git a/types/RouteablePage.js b/types/RouteablePage.js new file mode 100644 index 0000000..2a68c66 --- /dev/null +++ b/types/RouteablePage.js @@ -0,0 +1,6 @@ +class RouteablePage { + id + + constructor() { + } +} \ No newline at end of file diff --git a/types/connectors/Connector.js b/types/connectors/Connector.js new file mode 100644 index 0000000..e1551e9 --- /dev/null +++ b/types/connectors/Connector.js @@ -0,0 +1,10 @@ +class Connector { + constructor (amqpConnMngr) { + this.amqpConnMngr = amqpConnMngr + this.name = "_base" + } + transmitMessage(msg) { + throw "not implemented" + } +} +module.exports = Connector \ No newline at end of file diff --git a/types/connectors/DAPNETConnector.js b/types/connectors/DAPNETConnector.js new file mode 100644 index 0000000..c24ad0f --- /dev/null +++ b/types/connectors/DAPNETConnector.js @@ -0,0 +1,9 @@ +const Connector = require("./Connector") + +class DAPNETConnector extends Connector { + constructor (amqpConnMngr) { + super(amqpConnMngr) + this.name = "dapnet" + } +} +module.exports = DAPNETConnector \ No newline at end of file diff --git a/types/connectors/LoRaWANConnector.js b/types/connectors/LoRaWANConnector.js new file mode 100644 index 0000000..806a95a --- /dev/null +++ b/types/connectors/LoRaWANConnector.js @@ -0,0 +1,9 @@ +const Connector = require("./Connector") + +class LoRaWANConnector extends Connector { + constructor (amqpConnMngr) { + super(amqpConnMngr) + this.name = "lorawan" + } +} +module.exports = LoRaWANConnector \ No newline at end of file diff --git a/types/connectors/POCSAGConnector.js b/types/connectors/POCSAGConnector.js new file mode 100644 index 0000000..4ee3e83 --- /dev/null +++ b/types/connectors/POCSAGConnector.js @@ -0,0 +1,41 @@ +const Connector = require("./Connector") + +class POCSAGConnector extends Connector { + constructor (amqpConnMngr) { + super(amqpConnMngr) + this.name = "pocsag" + + this.channelWrapper = this.amqpConnMngr.createChannel({ + json: false, + setup: function(channel) { + return channel.assertQueue('tx_pocsag', { durable: true }) + } + }) + } + transmitMessage(msg) { + if (!msg.ric) return false + const RIC = msg.ric + 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), { + headers: { + ric: { + '!': 'int64', + value: addressPart + }, + function: { + '!': 'int64', + value: functionBits + }, + } + }) + .then(function() { + return console.log("Message was sent! Hooray!") + }).catch(function(err) { + return console.log(err,"Message was rejected... Boo!") + }) + + } + } + module.exports = POCSAGConnector \ No newline at end of file diff --git a/types/connectors/index.js b/types/connectors/index.js new file mode 100644 index 0000000..2f6ab3d --- /dev/null +++ b/types/connectors/index.js @@ -0,0 +1,6 @@ +module.exports = { + DAPNETConnector: require("./DAPNETConnector"), + LoRaWANConnector: require("./LoRaWANConnector"), + POCSAGConnector: require("./POCSAGConnector"), + Connector: require("./Connector"), +} \ No newline at end of file diff --git a/types/devices/BirdySlim.js b/types/devices/BirdySlim.js new file mode 100644 index 0000000..a2d3c95 --- /dev/null +++ b/types/devices/BirdySlim.js @@ -0,0 +1,12 @@ +const PagerDevice = require("./Device") + +// Birdy Slim (IoT) Device +class BirdySlim extends PagerDevice { + constructor () { + super() + this.name = "birdyslim" + } + formatTX(msg) { + } +} +module.exports = BirdySlim \ No newline at end of file diff --git a/types/devices/Device.js b/types/devices/Device.js new file mode 100644 index 0000000..679f921 --- /dev/null +++ b/types/devices/Device.js @@ -0,0 +1,8 @@ +class PagerDevice { + constructor () { + this.name = "_base" + } + formatTX(msg) { + } +} +module.exports = PagerDevice \ No newline at end of file diff --git a/types/devices/GenericPager.js b/types/devices/GenericPager.js new file mode 100644 index 0000000..b75d58e --- /dev/null +++ b/types/devices/GenericPager.js @@ -0,0 +1,11 @@ +const PagerDevice = require("./Device") + +class GenericPager extends PagerDevice { + constructor () { + super() + this.name = "generic" + } + transmit(msg) { + } + } + module.exports = GenericPager \ No newline at end of file diff --git a/types/devices/index.js b/types/devices/index.js new file mode 100644 index 0000000..0c11d41 --- /dev/null +++ b/types/devices/index.js @@ -0,0 +1,5 @@ +module.exports = { + GenericPager: require("./GenericPager"), + BirdySlim: require("./BirdySlim"), + Device: require("./Device"), +} \ No newline at end of file diff --git a/types/index.js b/types/index.js new file mode 100644 index 0000000..366ae3e --- /dev/null +++ b/types/index.js @@ -0,0 +1,11 @@ +const events = require('events') + +module.exports = { + CentralEventEmitter: new events.EventEmitter(), + Connectors: require("./connectors"), + devices: require("./devices"), + Message: require("./Message"), + RouteablePage: require("./RouteablePage"), + DeviceRegistry: require("./DeviceRegistry"), + ConnectorRegistry: require("./ConnectorRegistry"), +} \ No newline at end of file