const isDocker = require ( 'is-docker' ) ; import { Router } from 'express' ; import * as uuidV4 from 'uuid/v4' ; import * as fs from 'fs' ; import { REPORTING_MODES } from '../config' ; var path = require ( 'path' ) ; var QrCode = require ( 'qrcode-reader' ) ; var Jimp = require ( "jimp" ) ; export default function ( dialog , app , globalContext , ms ) { let api = Router ( ) ; api . get ( '/ping' , function ( req , res ) { res . status ( 200 ) ; } ) ; api . get ( '/ui' , function ( req , res ) { res . json ( { container : isDocker ( ) , edge : { deviceId : globalContext . IOTEDGE_DEVICEID , moduleId : globalContext . IOTEDGE_MODULEID } , latest : globalContext . LATEST_VERSION } ) ; } ) ; api . get ( '/id' , function ( req , res ) { res . status ( 200 ) . send ( uuidV4 ( ) ) . end ( ) ; } ) ; api . get ( '/help' , function ( req , res ) { res . status ( 200 ) . send ( fs . readFileSync ( path . resolve ( __dirname + '/../../../static/HELP.md' ) , { encoding : 'utf-8' } ) ) . end ( ) ; } ) ; api . post ( '/setmode/:mode' , function ( req , res ) { const mode = req . params . mode ; switch ( mode ) { case "ux" : globalContext . OPERATION_MODE = REPORTING_MODES . UX ; break ; case "server" : globalContext . OPERATION_MODE = REPORTING_MODES . SERVER ; break ; case "mixed" : globalContext . OPERATION_MODE = REPORTING_MODES . MIXED ; break ; default : globalContext . OPERATION_MODE = REPORTING_MODES . UX ; } ms . sendConsoleUpdate ( `DATA BACKEND CHANGED TO [${ globalContext . OPERATION_MODE }] ${ req . body . serverEndpoint }` ) ; res . status ( 200 ) . send ( globalContext . OPERATION_MODE . toString ( ) ) . end ( ) ; } ) ; api . get ( '/openDialog' , function ( req , res , next ) { dialog . showOpenDialog ( { properties : [ 'openFile' ] , filters : [ { name : 'JSON' , extensions : [ 'json' ] } , { name : 'All Files' , extensions : [ '*' ] } ] } ) . then ( ( result ) => { if ( result . canceled ) { res . status ( 444 ) . end ( ) ; return ; } if ( result . filePaths && result . filePaths . length > 0 ) { const fileNamePath = result . filePaths [ 0 ] ; fs . readFile ( fileNamePath , 'utf-8' , ( error , data ) => { res . json ( JSON . parse ( data ) ) . status ( 200 ) . end ( ) ; } ) ; } } ) . catch ( ( error ) => { console . error ( error ) ; res . status ( 500 ) . end ( ) ; } ) ; } ) ; api . post ( '/saveDialog' , function ( req , res , next ) { dialog . showSaveDialog ( ) . then ( ( path ) => { if ( path . canceled ) { res . status ( 444 ) . end ( ) ; return ; } if ( path . filePath === "" ) { res . status ( 204 ) . end ( ) ; return ; } if ( ! path . filePath . toLocaleLowerCase ( ) . endsWith ( '.json' ) ) { path . filePath += '.json' ; } fs . writeFile ( path . filePath , JSON . stringify ( req . body , null , 2 ) , 'utf8' , ( error ) => { if ( error ) { res . status ( 500 ) . end ( ) ; } else { res . status ( 200 ) . end ( ) ; } } ) ; } ) . catch ( ( error ) => { console . error ( error ) ; res . status ( 500 ) . end ( ) ; } ) ; } ) ; api . get ( '/qrcode' , async function ( req , res , next ) { dialog . showOpenDialog ( { properties : [ 'openFile' ] , filters : [ { name : 'PNG' , extensions : [ 'png' ] } , { name : 'All Files' , extensions : [ '*' ] } ] } ) . then ( ( result ) => { if ( result . canceled ) { res . status ( 444 ) . end ( ) ; return ; } if ( result . filePaths && result . filePaths . length > 0 ) { const fileNamePath = result . filePaths [ 0 ] ; var buffer = fs . readFileSync ( fileNamePath ) ; Jimp . read ( buffer , function ( err , image ) { if ( err ) { console . error ( err ) ; res . status ( 500 ) . end ( ) ; return ; } var qr = new QrCode ( ) ; qr . callback = function ( err , value ) { if ( err ) { console . error ( err ) ; res . status ( 500 ) . end ( ) ; return ; } res . json ( { code : value . result } ) . status ( 200 ) . end ( ) ; } ; qr . decode ( image . bitmap ) ; } ) ; res . end ; } } ) . catch ( ( error ) => { console . error ( error ) ; res . status ( 500 ) . end ( ) ; } ) ; } ) ; return api ; }