first version to work, for simplex

master
cheetah 4 years ago
parent 9a738d41cd
commit 6114644362

@ -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"
})
body: "AAAAhh, ich\n\n\n\nHabe einen Sprung"
})*/

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

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

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

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

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

@ -3,7 +3,7 @@ class Connector {
this.amqpConnMngr = amqpConnMngr
this.name = "_base"
}
transmitMessage(msg) {
async transmitMessage(msg, params) {
throw "not implemented"
}
}

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

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

@ -1,8 +1,9 @@
class PagerDevice {
constructor () {
this.duplex = false
this.name = "_base"
}
formatTX(msg) {
}
async formatTX(msg) { }
RandID() { }
}
module.exports = PagerDevice

@ -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
}
module.exports = GenericPager

@ -6,6 +6,8 @@ module.exports = {
devices: require("./devices"),
Message: require("./Message"),
RouteablePage: require("./RouteablePage"),
DeviceRegistry: require("./DeviceRegistry"),
MessageManager: require("./MessageManager"),
ConnectorRegistry: require("./ConnectorRegistry"),
}
Loading…
Cancel
Save