import { Router } from 'express' ; import { DeviceStore } from '../store/deviceStore' ; import { Device } from '../interfaces/device' ; import { DCMtoMockDevice } from '../core/templates' ; import * as Utils from '../core/utils' ; import { Config } from '../config' ; import uuid = require ( 'uuid' ) ; import { DtdlStore } from '../store/dtdlStore' ; export default function ( deviceStore ) { let api = Router ( ) ; api . get ( '/:id' , function ( req , res , next ) { var id = req . params . id ; res . json ( deviceStore . exists ( id ) ) ; } ) ; api . delete ( '/:id/module/:moduleId' , function ( req , res , next ) { var id = req . params . id ; var moduleId = req . params . moduleId ; deviceStore . stopDevice ( deviceStore . exists ( moduleId ) ) ; deviceStore . deleteDevice ( deviceStore . exists ( moduleId ) ) ; deviceStore . removeDeviceModule ( deviceStore . exists ( id ) , moduleId ) ; res . json ( { device : deviceStore . exists ( id ) , devices : deviceStore . getListOfItems ( ) } ) ; } ) ; api . delete ( '/:id' , function ( req , res , next ) { var id = req . params . id ; deviceStore . stopDevice ( deviceStore . exists ( id ) ) ; deviceStore . deleteDevice ( deviceStore . exists ( id ) ) ; res . json ( deviceStore . getListOfItems ( ) ) ; } ) ; api . get ( '/:id/start' , function ( req , res , next ) { var id = req . params . id ; deviceStore . startDevice ( deviceStore . exists ( id ) ) ; res . json ( { device : deviceStore . exists ( id ) , devices : deviceStore . getListOfItems ( ) } ) ; } ) ; api . get ( '/:id/stop' , function ( req , res , next ) { var id = req . params . id ; deviceStore . stopDevice ( deviceStore . exists ( id ) ) ; res . json ( { device : deviceStore . exists ( id ) , devices : deviceStore . getListOfItems ( ) } ) ; } ) ; api . post ( '/:id/property/new' , function ( req , res , next ) { var id = req . params . id ; var body = req . body ; deviceStore . addDeviceProperty ( id , body . type , { } , true ) ; res . json ( { device : deviceStore . exists ( id ) , devices : deviceStore . getListOfItems ( ) } ) ; } ) ; api . post ( '/:id/method/new' , function ( req , res , next ) { var id = req . params . id ; deviceStore . addDeviceMethod ( id , { } , true ) ; res . json ( { device : deviceStore . exists ( id ) , devices : deviceStore . getListOfItems ( ) } ) ; } ) ; api . delete ( '/:id/property/:propertyId' , function ( req , res , next ) { var id = req . params . id ; var propertyId = req . params . propertyId ; deviceStore . deleteDeviceProperty ( id , propertyId ) ; res . json ( { device : deviceStore . exists ( id ) , devices : deviceStore . getListOfItems ( ) } ) ; } ) ; api . delete ( '/:id/method/:methodId' , function ( req , res , next ) { var id = req . params . id ; var methodId = req . params . methodId ; deviceStore . deleteDeviceProperty ( id , methodId ) ; res . json ( { device : deviceStore . exists ( id ) , devices : deviceStore . getListOfItems ( ) } ) ; } ) ; api . get ( '/:id/method/:methodId/params' , function ( req , res , next ) { var id = req . params . id ; var methodId = req . params . methodId ; res . json ( deviceStore . getDeviceMethodParams ( id , methodId ) ) ; } ) ; api . put ( '/:id/method/:methodId' , function ( req , res , next ) { var id = req . params . id ; var methodId = req . params . methodId ; var body = req . body ; deviceStore . updateDeviceMethod ( id , methodId , body ) ; res . json ( { device : deviceStore . exists ( id ) , devices : deviceStore . getListOfItems ( ) } ) ; } ) ; api . put ( '/:id/property/:propertyId/value' , function ( req , res , next ) { var id = req . params . id ; var propertyId = req . params . propertyId ; var body = req . body ; deviceStore . updateDeviceProperty ( id , propertyId , body , true ) ; res . json ( { device : deviceStore . exists ( id ) , devices : deviceStore . getListOfItems ( ) } ) ; } ) ; api . put ( '/:id/property/:propertyId' , function ( req , res , next ) { var id = req . params . id ; var propertyId = req . params . propertyId ; var body = req . body ; deviceStore . updateDeviceProperty ( id , propertyId , body , false ) ; res . json ( { device : deviceStore . exists ( id ) , devices : deviceStore . getListOfItems ( ) } ) ; } ) ; api . get ( '/:id/plan/restart' , function ( req , res , next ) { var id = req . params . id ; deviceStore . restartDevicePlan ( id ) ; res . json ( { device : deviceStore . exists ( id ) , devices : deviceStore . getListOfItems ( ) } ) ; } ) ; api . put ( '/:id/plan' , function ( req , res , next ) { var id = req . params . id ; deviceStore . updateDevice ( id , req . body . payload , 'plan' ) ; res . json ( { device : deviceStore . exists ( id ) , devices : deviceStore . getListOfItems ( ) } ) ; } ) ; api . put ( '/:id/configuration' , function ( req , res , next ) { var id = req . params . id ; const newId = deviceStore . updateDevice ( id , req . body . payload , 'configuration' ) ; res . json ( { device : deviceStore . exists ( newId ) , devices : deviceStore . getListOfItems ( ) } ) ; } ) ; api . put ( '/:id/module' , function ( req , res , next ) { var id = req . params . id ; try { deviceStore . updateDevice ( id , req . body . payload , 'module' ) ; } catch ( msg ) { res . status ( 500 ) . send ( msg ) ; return ; } res . json ( { device : deviceStore . exists ( id ) , devices : deviceStore . getListOfItems ( ) } ) ; } ) ; api . put ( '/:id/leafDevice' , function ( req , res , next ) { var id = req . params . id ; try { deviceStore . updateDevice ( id , req . body . payload , 'leafDevice' ) ; } catch ( msg ) { res . status ( 500 ) . send ( msg ) ; return ; } res . json ( { device : deviceStore . exists ( id ) , devices : deviceStore . getListOfItems ( ) } ) ; } ) ; api . post ( '/:id/property/:propertyId/mock/new' , function ( req , res , next ) { var id = req . params . id ; var propertyId = req . params . propertyId ; deviceStore . addDevicePropertyMock ( id , propertyId ) ; res . json ( deviceStore . exists ( id ) ) ; } ) ; api . delete ( '/:id/property/:propertyId/mock' , function ( req , res , next ) { var id = req . params . id ; var propertyId = req . params . propertyId ; var body = req . body ; deviceStore . deleteDevicePropertyMock ( id , propertyId ) ; res . json ( deviceStore . exists ( id ) ) ; } ) ; api . get ( '/:id/property/:propertyId/value' , function ( req , res , next ) { var id = req . params . id ; var propertyId = req . params . propertyId ; deviceStore . refreshDeviceProperty ( id , propertyId ) ; res . json ( deviceStore . exists ( id ) ) ; } ) ; api . post ( '/reorder' , function ( req , res ) { try { const { current , next } = req . body ; const list = deviceStore . getListOfItems ( ) ; const oldItem = list [ current ] ; list . splice ( current , 1 ) ; list . splice ( next , 0 , oldItem ) ; deviceStore . stopAll ( ) ; deviceStore . init ( ) ; deviceStore . createFromArray ( list ) ; res . json ( { device : deviceStore . exists ( list [ next ] . _id ) , devices : deviceStore . getListOfItems ( ) } ) ; res . end ( ) ; } catch ( err ) { res . status ( 500 ) . send ( { "message" : "Cannot reorder this data" } ) ; res . end ( ) ; } } ) ; api . post ( '/new/quick' , function ( req , res , next ) { if ( deviceStore . maxReached ( ) ) { res . status ( 500 ) . json ( { "message" : "Max devices reached" } ) ; return ; } var updatePayload = req . body ; const ds = new DtdlStore ( ) ; try { let d = new Device ( ) ; d . _id = updatePayload . deviceId != '' ? updatePayload . deviceId : uuid ( ) ; d . configuration = updatePayload ; d . configuration . deviceId = d . _id ; d . configuration . capabilityModel = ds . getDtdl ( 'mockDevices' ) ; deviceStore . addDevice ( d ) ; DCMtoMockDevice ( deviceStore , d , true ) ; deviceStore . startDevice ( d ) ; } catch ( err ) { res . status ( 500 ) . json ( { "message" : "Problem creating this quick device" } ) ; return ; } res . json ( deviceStore . getListOfItems ( ) ) ; } ) ; api . post ( '/new' , function ( req , res , next ) { if ( deviceStore . maxReached ( ) ) { res . status ( 500 ) . json ( { "message" : "Max devices reached" } ) ; return ; } var updatePayload = req . body ; if ( updatePayload . _kind === 'template' ) { try { let d = new Device ( ) ; d . _id = uuid ( ) ; d . configuration = updatePayload ; d . configuration . deviceId = d . _id ; deviceStore . addDevice ( d ) ; DCMtoMockDevice ( deviceStore , d ) ; } catch ( err ) { res . status ( 500 ) . json ( { "message" : "The DCM has errors or has an unrecognized schema" } ) ; return ; } res . json ( deviceStore . getListOfItems ( ) ) ; return ; } let items = deviceStore . getListOfItems ( ) ; let capacity = Config . MAX_NUM_DEVICES - items . length ; let from = parseInt ( updatePayload . mockDeviceCount ) ; const to = parseInt ( updatePayload . mockDeviceCountMax ) + 1 ; const count = to - from === 0 ? 1 : to - from ; let maxCount = count > capacity ? capacity : count ; for ( let i = 0 ; i < maxCount ; i ++ ) { let d = new Device ( ) ; let createId = null ; if ( updatePayload . _kind === 'dps' ) { createId = updatePayload . deviceId ; } else if ( updatePayload . _kind === 'hub' ) { createId = Utils . getDeviceId ( updatePayload . connectionString ) ; } else { createId = updatePayload . deviceId ; } createId = count > 1 ? createId + "-" + from : createId ; if ( deviceStore . exists ( createId ) ) { res . status ( 500 ) . json ( { "message" : "Device already added" } ) ; return ; } const rename = ! updatePayload . mockDeviceName ? createId : updatePayload . mockDeviceName === '' ? createId : count > 1 ? updatePayload . mockDeviceName + '-' + ( i + 1 ) : updatePayload . mockDeviceName ; d . _id = createId ; d . configuration = JSON . parse ( JSON . stringify ( updatePayload ) ) ; d . configuration . mockDeviceName = rename ; d . configuration . deviceId = createId ; deviceStore . addDevice ( d ) ; from ++ ; } res . json ( deviceStore . getListOfItems ( ) ) ; } ) ; return api ; }