2019-05-20 11:18:25 +00:00
"use strict" ;
2019-07-04 19:53:00 +00:00
import MqttClient from 'simple-mqtt-client' ;
2019-07-04 16:18:38 +00:00
2019-05-20 11:18:25 +00:00
class TTNAppServer {
2019-05-23 21:39:10 +00:00
// Destructure the awilix container
2019-07-04 16:18:38 +00:00
constructor ( { settings , ansi , message _handler } ) {
2019-05-23 21:39:10 +00:00
this . settings = settings ;
2019-05-29 10:59:40 +00:00
/** @type {Ansi} */
this . a = ansi ;
2019-07-04 16:18:38 +00:00
/** @type {MessageHandler} */
this . message _handler = message _handler ;
2019-05-20 11:18:25 +00:00
}
2019-07-04 16:18:38 +00:00
async start ( ) {
if ( this . settings . ttn . app _id == "CHANGE_THIS" || this . settings . ttn . access _key == "CHANGE_THIS" ) {
console . error ( ` ${ this . a . fred } ${ this . a . hicol } Error: No TTN app id specified. Try filling in the required values in settings.toml. If they don't exist yet, try using server/settings.default.toml as a reference. ${ this . a . reset } ` ) ;
return false ;
}
2019-07-04 19:53:00 +00:00
this . ttn _client = MqttClient . new ( ) . init ( ) (
2019-07-04 16:18:38 +00:00
this . settings . ttn . app _id ,
this . settings . ttn . access _key
) ;
2019-06-03 13:14:23 +00:00
2019-07-04 16:18:38 +00:00
this . ttn _client . on ( "uplink" , this . handle _message . bind ( this ) ) ;
}
async handle _message ( device _id , payload ) {
await this . message _handler . handle ( device _id , payload ) ;
2019-05-20 11:18:25 +00:00
}
}
export default TTNAppServer ;