2019-05-20 11:18:25 +00:00
"use strict" ;
2019-07-08 15:06:06 +00:00
import mqtt from 'async-mqtt' ;
2019-07-04 16:18:38 +00:00
2019-05-20 11:18:25 +00:00
class TTNAppServer {
2019-07-08 14:42:05 +00:00
get connection _string ( ) {
return ` ${ this . settings . ttn . tls ? "mqtts" : "mqtt" } :// ${ this . settings . ttn . host } : ${ this . settings . ttn . port } / `
}
2019-05-23 21:39:10 +00:00
// Destructure the awilix container
2019-07-08 14:42:05 +00:00
constructor ( { settings , ansi , MessageHandler , log } ) {
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-08 14:42:05 +00:00
/** @type {Log} */
this . log = log ;
2019-07-04 16:18:38 +00:00
/** @type {MessageHandler} */
2019-07-08 14:42:05 +00:00
this . message _handler = MessageHandler ;
2019-05-20 11:18:25 +00:00
}
2019-07-08 14:42:05 +00:00
async run ( ) {
2019-07-08 15:06:06 +00:00
// 1: Sanity checks
2019-07-04 16:18:38 +00:00
if ( this . settings . ttn . app _id == "CHANGE_THIS" || this . settings . ttn . access _key == "CHANGE_THIS" ) {
2019-07-08 14:42:05 +00:00
this . log . error ( ` 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. ` ) ;
2019-07-04 16:18:38 +00:00
return false ;
}
2019-07-04 19:53:00 +00:00
2019-07-08 15:06:06 +00:00
if ( this . settings . ttn . devices . includes ( "CHANGE_THIS" ) ) {
this . log . error ( ` Error: No device names 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. ` ) ;
return false ;
2019-07-08 14:42:05 +00:00
}
2019-06-03 13:14:23 +00:00
2019-07-08 15:06:06 +00:00
// 2: Connect
this . ttn _client = await this . connect ( ) ;
this . ttn _client . on ( "message" , this . handle _message . bind ( this ) ) ;
2019-07-10 13:44:52 +00:00
this . log . log ( ` [mqtt] Connected to ${ this . connection _string } ` ) ;
2019-07-08 15:06:06 +00:00
// 3: Subscribe
let result = await this . ttn _client . subscribe ( this . settings . ttn . devices . map (
( device _name ) => ` ${ this . settings . ttn . app _id } /devices/ ${ device _name } /up `
) ) ;
2019-07-10 13:44:52 +00:00
this . log . log ( ` [mqtt] Subscribed to ${ result . map ( ( el ) => el . topic ) . join ( ", " ) } ` ) ;
2019-07-08 14:42:05 +00:00
}
connect ( ) {
return new Promise ( ( resolve , _reject ) => {
2019-07-08 15:06:06 +00:00
let client = mqtt . connect ( this . connection _string , {
username : this . settings . ttn . app _id ,
password : this . settings . ttn . access _key
} ) ;
client . once ( "connect" , ( ) => resolve ( client ) ) ;
} ) ;
2019-07-04 16:18:38 +00:00
}
2019-07-08 15:06:06 +00:00
async handle _message ( topic , message _buffer ) {
2019-07-09 10:46:35 +00:00
let message = message _buffer . toString ( "utf8" ) ,
message _object = null ;
try {
message _object = JSON . parse ( message ) ;
} catch ( error ) {
this . log . log ( error ) ;
return false ;
}
2019-07-10 13:44:52 +00:00
this . log . log ( ` [mqtt/handler] Got message from ${ topic } ` ) ;
2019-07-09 12:14:25 +00:00
await this . message _handler . handle ( message _object ) ;
2019-05-20 11:18:25 +00:00
}
}
export default TTNAppServer ;