added lorawan downlink+duplexTimeout

master
cheetah 4 years ago
parent b0195e8e49
commit 2e614ccc6a

@ -22,6 +22,7 @@
},
"lorawan": {
"enabled": true,
"duplexTimeout": 1800,
"mqttserver": "mqtt://eu.thethings.network:1883",
"username": "TTN_v2_APP_ID___OR___v3_MQTT_Username",
"password": "TTN_v2_APP_APIKEY__OR__TTN_v3_MQTT_Password"

@ -48,6 +48,9 @@
<label for="lorawan_enabled">Enabled:</label>
<input type="checkbox" id="lorawan_enabled"/>
</br>
<label for="lorawan_duplex_timeout">Duplex Timeout(in seconds) : recommended value equals LPWAN-FetchTime</label>
<input type="number" min="10" max="9000" id="lorawan_duplex_timeout"/>
</br>
<label for="lorawan_mqttserver">MQTT Endpoint:</label>
<input type="text" id="lorawan_mqttserver"/>
<button onClick="setLoRaWANMQTT('mqtt://eu.thethings.network:1883')">Preset TTNv2</button>
@ -104,6 +107,7 @@
$('#dapnet_password').val(config.connectors.dapnet.password)
// connectors.dapnet
$('#lorawan_enabled').attr('checked', config.connectors.lorawan.enabled)
$('#lorawan_duplex_timeout').val(config.connectors.lorawan.duplexTimeout)
$('#lorawan_mqttserver').val(config.connectors.lorawan.mqttserver)
$('#lorawan_username').val(config.connectors.lorawan.username)
$('#lorawan_password').val(config.connectors.lorawan.password)
@ -125,6 +129,7 @@
newConfig.connectors.dapnet.password = $('#dapnet_password').val()
// connectors.lorawan
newConfig.connectors.lorawan.enabled = $('#lorawan_enabled').prop('checked')
newConfig.connectors.lorawan.duplexTimeout = parseInt($('#lorawan_duplex_timeout').val())
newConfig.connectors.lorawan.mqttserver = $('#lorawan_mqttserver').val()
newConfig.connectors.lorawan.username = $('#lorawan_username').val()
newConfig.connectors.lorawan.password = $('#lorawan_password').val()

@ -1,5 +1,6 @@
const Connector = require("./Connector")
const config = require('../../config.json')
const md5 = require('md5')
class LoRaWANConnector extends Connector {
// this is optimized for only receiving LoRa uplink Messages (from Birdy Pagers)
@ -22,6 +23,36 @@ class LoRaWANConnector extends Connector {
await this.client.subscribe(`${ config.connectors.lorawan.username }/devices/#`)
console.log('[lorawan] subscribed')
}
async transmitMessage(msg, params) {
const UUID = this.name+':'+md5(JSON.stringify([this.name,...params]))
if (params.length < 1) return false
const target = params[0]
if (target.split('#').length !== 2) throw 'No valid LoRaWAN Parameter <deviceID#fPort>'
const deviceID = target.split('#')[ 0 ], fPort = target.split('#')[ 1 ]
const mqttTopic = `v3/${ config.connectors.lorawan.username }/devices/${ deviceID }/down/push`
const mqttPayload = {
downlinks:[
{
f_port: fPort,
frm_payload: Buffer.from(msg.payload, 'utf-8').toString('base64'),
priority: "NORMAL",
}
]
}
console.log (mqttTopic, mqttPayload)
this.client.publish(mqttTopic, JSON.stringify(mqttPayload))
.then(() => {
this.connectorRegistry.reportState(msg, UUID, 'routed')
return true
})
.catch((err) => {
console.error(err)
this.connectorRegistry.reportFail(msg, UUID)
return false
})
}
async onMQTTMessage(topic, message) {
//if (topic.indexOf('/up') > -1) return
const json = JSON.parse(Buffer.from(message).toString('utf-8'))

Loading…
Cancel
Save