import { Connector , Config } from './connector' ; import * as Winston from 'winston' ; import _ = require ( 'underscore' ) ; console . log ( 'process started' ) ; let host = process . env . CORE_HOST || '127.0.0.1' ; let port = parseInt ( process . env . CORE_PORT + '' , 10 ) || 3000 ; let logPath = process . env . CORE_LOG || '' ; let deviceId = process . env . DEVICE_ID || '' ; let deviceToken = process . env . DEVICE_TOKEN || '' ; let disableWatchdog = ( process . env . DISABLE_WATCHDOG === '1' ) || false ; let unsafeSSL = process . env . UNSAFE_SSL === '1' || false ; let certs = ( process . env . CERTIFICATES || '' ) . split ( ';' ) || [ ] ; let debug = false ; let printHelp = false ; let prevProcessArg = '' ; process . argv . forEach ( ( val ) => { val = val + '' ; let nextPrevProcessArg = val ; if ( prevProcessArg . match ( /-host/i ) ) { host = val ; } else if ( prevProcessArg . match ( /-port/i ) ) { port = parseInt ( val , 10 ) ; } else if ( prevProcessArg . match ( /-log/i ) ) { logPath = val ; } else if ( prevProcessArg . match ( /-id/i ) ) { deviceId = val ; } else if ( prevProcessArg . match ( /-token/i ) ) { deviceToken = val ; } else if ( ( val + '' ) . match ( /-debug/i ) ) { debug = true ; } else if ( ( val + ' ' ) . match ( /-h(elp)? /i ) ) { printHelp = true ; } else if ( prevProcessArg . match ( /-certificates/i ) ) { certs . push ( val ) ; nextPrevProcessArg = prevProcessArg ; } else if ( val . match ( /-disableWatchdog/i ) ) { disableWatchdog = true ; } else if ( val . match ( /-unsafeSSL/i ) ) { unsafeSSL = true ; } prevProcessArg = nextPrevProcessArg + '' ; } ) ; if ( printHelp ) { console . log ( `
The MOS-gateway acts as a gateway between mos-devices and Core
Options:
CLI                ENV
-host              CORE_HOST         Host of Core  Default: '127.0.0.1'
-port              CORE_PORT         Port of Core  Default: '3000'
-id                CORE_LOG          Custom id of this device
-token             DEVICE_ID         Custom token of this device
-log               DEVICE_TOKEN      File path to output log to (if not set, logs are sent to console)
-disableWatchdog   DISABLE_WATCHDOG  Disable the watchdog (Killing the process if no commands are received after some time)
-certificates      CERTIFICATES      Provide paths to SSL certificates, (for self-signed certificates). '-certificates path1 path2 path3'
-unsafeSSL         UNSAFE_SSL        Will cause the Node applocation to blindly accept all certificates. Not recommenced unless in local, controlled networks.
-debug                               Debug mode
-h, -help                            Displays this help message
` ) ; process . exit ( 0 ) ; } /**
 * Used when JSON.stringifying values that might be circular
 * Usage: JSON.stringify(value, JSONStringifyCircular()))
 */ let JSONStringifyCircular = ( ) => { let cacheValues = [ ] ; let cacheKeys = [ ] ; let stringifyFixer = ( key , value ) => { if ( typeof value === 'object' && value !== null ) { let i = cacheValues . indexOf ( value ) ; if ( i !== - 1 ) { try { return JSON . parse ( JSON . stringify ( value ) ) ; } catch ( error ) { return '[circular of ' + ( cacheKeys [ i ] || '*root*' ) + ']' ; } } cacheValues . push ( value ) ; cacheKeys . push ( key ) ; } return value ; } ; return stringifyFixer ; } ; let logger = new ( Winston . Logger ) ( { } ) ; if ( logPath ) { console . log ( 'Logging to' , logPath ) ; logger . add ( Winston . transports . Console , { level : 'debug' , handleExceptions : true , json : false } ) ; logger . add ( Winston . transports . File , { level : 'debug' , handleExceptions : true , json : true , stringify : ( obj ) => { return JSON . stringify ( obj , JSONStringifyCircular ( ) ) ; } , filename : logPath } ) ; let orgConsoleLog = console . log ; console . log = function ( ... args ) { if ( args . length >= 1 ) { try { logger . debug ( ... args ) ; } catch ( e ) { orgConsoleLog ( 'CATCH' ) ; orgConsoleLog ( ... args ) ; throw e ; } orgConsoleLog ( ... args ) ; } } ; } else { console . log ( 'Logging to Console' ) ; logger . add ( Winston . transports . Console , { level : 'debug' , handleExceptions : true , json : true , stringify : ( obj ) => { return JSON . stringify ( obj , JSONStringifyCircular ( ) ) ; } } ) ; let orgConsoleLog = console . log ; console . log = function ( ... args ) { if ( args . length >= 1 ) { logger . debug ( ... args ) ; } } ; } process . on ( 'unhandledRejection' , ( e ) => { logger . error ( 'Unhandled Promise rejection:' , e , e . reason || e . message , e . stack ) ; } ) ; process . on ( 'warning' , ( e ) => { logger . warn ( 'Unhandled warning:' , e , e . reason || e . message , e . stack ) ; } ) ; logger . info ( '------------------------------------------------------------------' ) ; logger . info ( '-----------------------------------' ) ; logger . info ( 'Statup options:' ) ; logger . info ( `host: "${ host }"` ) ; logger . info ( `port: ${ port }` ) ; logger . info ( `log: "${ logPath }"` ) ; logger . info ( `id: "${ deviceId }"` ) ; logger . info ( `token: "${ deviceToken }"` ) ; logger . info ( `debug: ${ debug }` ) ; logger . info ( `certificates: [${ certs . join ( ',' ) }]` ) ; logger . info ( `disableWatchdog: ${ disableWatchdog }` ) ; logger . info ( `unsafeSSL: ${ unsafeSSL }` ) ; logger . info ( '-----------------------------------' ) ; logger . info ( `Test info logging` ) ; logger . warn ( `Test warn logging` ) ; logger . debug ( `Test debug logging` ) ; let config = { process : { unsafeSSL : unsafeSSL , certificates : _ . compact ( certs ) } , device : { deviceId : deviceId , deviceToken : deviceToken } , core : { host : host , port : port , watchdog : ! disableWatchdog } , mos : { self : { debug : debug , mosID : 'N/A' , acceptsConnections : true , profiles : { '0' : true , '1' : true , '2' : true , '3' : false , '4' : false , '5' : false , '6' : false , '7' : false } , offspecFailover : true } } } ; let c = new Connector ( logger ) ; logger . info ( 'Core:          ' + config . core . host + ':' + config . core . port ) ; logger . info ( '------------------------------------------------------------------' ) ; c . init ( config ) . catch ( logger . error ) ;