diff --git a/Cmd4Accessory.js b/Cmd4Accessory.js index b6ac492..182ac89 100644 --- a/Cmd4Accessory.js +++ b/Cmd4Accessory.js @@ -8,9 +8,6 @@ const commandExistsSync = require( "command-exists" ).sync; let settings = require( "./cmd4Settings" ); const constants = require( "./cmd4Constants" ); -var cmd4Dbg = settings.cmd4Dbg; - - let Logger = require( "./utils/Logger" ); const { getAccessoryName, getAccessoryDisplayName } = require( "./utils/getAccessoryNameFunctions" ); @@ -93,7 +90,7 @@ class Cmd4Accessory this.log = log; // Carry the debug flag from the platform - cmd4Dbg = log.debugEnabled; + settings.cmd4Dbg = log.debugEnabled; } else { @@ -104,11 +101,10 @@ class Cmd4Accessory process.env.DEBUG == settings.PLATFORM_NAME ) { settings.cmd4Dbg = true; - cmd4Dbg = true; } } - this.log.setDebugEnabled( cmd4Dbg ); + this.log.setDebugEnabled( settings.cmd4Dbg ); this.config = config; this.api = api; @@ -125,7 +121,7 @@ class Cmd4Accessory let typeMsg = [ "", "Linked ", "Added " ][ this.LEVEL ] || ""; - if ( cmd4Dbg ) log.debug( chalk.blue ( `Creating ${ typeMsg }${ this.CMD4 } Accessory type for : ${ config.displayName } LEVEL: ${ this.LEVEL }` ) ); + if ( settings.cmd4Dbg ) log.debug( chalk.blue ( `Creating ${ typeMsg }${ this.CMD4 } Accessory type for : ${ config.displayName } LEVEL: ${ this.LEVEL }` ) ); this.services = [ ]; this.linkedAccessories = [ ]; @@ -189,11 +185,11 @@ class Cmd4Accessory if ( existingData ) { //Z this.log.info( chalk.blue ( `THIS MSG TO BE REMOVED. Found existing data for: ${ this.displayName }` ) ); - if ( cmd4Dbg ) this.log.debug(`Cmd4Accessory: found existingData for ${ this.displayName }` ); + if ( settings.cmd4Dbg ) this.log.debug(`Cmd4Accessory: found existingData for ${ this.displayName }` ); if ( existingData.storedValuesPerCharacteristic ) { //Z this.log.info( chalk.blue ( `THIS MSG TO BE REMOVED. Found old storedValuesPerCharacteristic for: ${ this.displayName }` ) ); - if ( cmd4Dbg ) this.log.debug( `Upgrading to cmd4Storage` ); + if ( settings.cmd4Dbg ) this.log.debug( `Upgrading to cmd4Storage` ); this.cmd4Storage = new Cmd4Storage( this.log, existingData.storedValuesPerCharacteristic ); this.STORED_DATA_ARRAY.push( { [ constants.UUID ]: uuid, [ constants.CMD4_STORAGE_lv ]: this.cmd4Storage @@ -204,7 +200,7 @@ class Cmd4Accessory } else if ( existingData.cmd4Storage ) { //Z this.log.info( chalk.blue ( `THIS MSG TO BE REMOVED. Using existing cmd4Storage for: ${ this.displayName }` ) ); - if ( cmd4Dbg ) this.log.debug( `Using existing cmd4Storage` ); + if ( settings.cmd4Dbg ) this.log.debug( `Using existing cmd4Storage` ); this.cmd4Storage = new Cmd4Storage( this.log, existingData.cmd4Storage ); this.STORED_DATA_ARRAY.push( { [ constants.UUID ]: uuid, [ constants.CMD4_STORAGE_lv ]: this.cmd4Storage @@ -229,7 +225,7 @@ class Cmd4Accessory } else { //Z log.info( chalk.blue ( `THIS MSG TO BE REMOVED. Creating new cmd4Storage for: ${ this.displayName }` ) ); - if ( cmd4Dbg ) this.log.debug(`Cmd4Accessory: creating new cmd4Storage for ${ this.displayName }` ); + if ( settings.cmd4Dbg ) this.log.debug(`Cmd4Accessory: creating new cmd4Storage for ${ this.displayName }` ); // Instead of local variables for every characteristic, create an array to // hold values for all characteristics based on the size of all possible // characteristics. Placing them in .config will make them be cached over @@ -305,12 +301,12 @@ class Cmd4Accessory // This can be done for only LEVEL 0 accessories and itself if ( this.LEVEL == 0 ) { - // if ( cmd4Dbg ) log.debug( "CMD4=%s LEVEL=%s for %s", accessory.CMD4, accessory.LEVEL, accessory.displayName ); + // if ( settings.cmd4Dbg ) log.debug( "CMD4=%s LEVEL=%s for %s", accessory.CMD4, accessory.LEVEL, accessory.displayName ); // The linked accessory children are at different levels of recursion, so only // allow what is posssible. if ( this.linkedAccessories && this.linkedAccessories.length > 0 ) { - if ( cmd4Dbg ) this.log.debug( `Setting up which characteristics will be polled for Linked Accessories of ${ this.displayName }` ); + if ( settings.cmd4Dbg ) this.log.debug( `Setting up which characteristics will be polled for Linked Accessories of ${ this.displayName }` ); this.linkedAccessories.forEach( ( linkedAccessory ) => { @@ -324,7 +320,7 @@ class Cmd4Accessory // The Television Speaker Platform Example if ( this.accessories && this.accessories.length > 0 ) { - if ( cmd4Dbg ) this.log.debug( `Setting up which characteristics will be polled for Added Accessories of ${ this.displayName }` ); + if ( settings.cmd4Dbg ) this.log.debug( `Setting up which characteristics will be polled for Added Accessories of ${ this.displayName }` ); this.accessories.forEach( ( addedAccessory ) => { if ( addedAccessory.polling ) @@ -334,7 +330,7 @@ class Cmd4Accessory }); } - if ( cmd4Dbg ) this.log.debug( `Setting up which characteristics will be polled for ${ this.displayName }` ); + if ( settings.cmd4Dbg ) this.log.debug( `Setting up which characteristics will be polled for ${ this.displayName }` ); this.determineCharacteristicsToPollForAccessory( this ); } @@ -343,7 +339,7 @@ class Cmd4Accessory // polling started. Otherwise the platform will have to do this. if ( this.CMD4 == constants.STANDALONE && this.LEVEL == 0 ) { - if ( cmd4Dbg ) log.debug( `Creating Standalone service for: ${ this.displayName }` ); + if ( settings.cmd4Dbg ) log.debug( `Creating Standalone service for: ${ this.displayName }` ); this.createServicesForStandaloneAccessoryAndItsChildren( this ) } @@ -358,9 +354,9 @@ class Cmd4Accessory { //if ( this.services ) //{ - // if ( cmd4Dbg ) this.log.debug( Fg.Red + "ZZZZ Returning:%s number of services for:%s" + Fg.Rm, this.services.length, this.displayName ); + // if ( settings.cmd4Dbg ) this.log.debug( Fg.Red + "ZZZZ Returning:%s number of services for:%s" + Fg.Rm, this.services.length, this.displayName ); //} else { - // if ( cmd4Dbg ) this.log.debug( Fg.Red + "ZZZZ Returning this.services:%s for:%s" + Fg.Rm, this.services, this.displayName ); + // if ( settings.cmd4Dbg ) this.log.debug( Fg.Red + "ZZZZ Returning this.services:%s for:%s" + Fg.Rm, this.services, this.displayName ); //} return this.services; } @@ -409,7 +405,7 @@ class Cmd4Accessory if ( accTypeEnumIndex == CMD4_ACC_TYPE_ENUM.ConfiguredName ) defaultValue = getAccessoryName( this.config ); - if ( cmd4Dbg ) this.log.debug( `*****Adding default value ${ defaultValue } for: ${ this.displayName }` ); + if ( settings.cmd4Dbg ) this.log.debug( `*****Adding default value ${ defaultValue } for: ${ this.displayName }` ); this.cmd4Storage.setStoredValueForIndex( accTypeEnumIndex, defaultValue ); } @@ -421,7 +417,7 @@ class Cmd4Accessory if ( trueTypeOf( pollingConfig ) != Array ) return; - if ( cmd4Dbg ) this.log.debug( `Checking ${ this.displayName } for polling of unset characteristics.` ); + if ( settings.cmd4Dbg ) this.log.debug( `Checking ${ this.displayName } for polling of unset characteristics.` ); pollingConfig.forEach( ( jsonPollingConfig ) => { @@ -475,10 +471,10 @@ class Cmd4Accessory valueToStore = null; let rcDirective = isAccDirective( value, false ); - if ( rcDirective == null ) + if ( rcDirective.accTypeEnumIndex == null ) { rcDirective = isAccDirective( value, true ); - if ( rcDirective == null ) + if ( rcDirective.accTypeEnumIndex == null ) throw new Error( `No such polling characteristic: "${ value }" for: "${ this.displayName }".` ); this.log.warn( `The config.json Polling characteristic: ${ value } is Capitalized it should be: ${ rcDirective.type }. In the near future this will be an Error so that Cmd4 can use homebridge-ui.\nTo remove this Warning, Please fix your config.json.` ); @@ -524,10 +520,10 @@ class Cmd4Accessory createServicesForStandaloneAccessoryAndItsChildren( accessory ) { - if ( cmd4Dbg ) accessory.log.debug( chalk.blue( `createServicesFor${ this.CMD4 }AccessoryAndItsChildren` ) ); + if ( settings.cmd4Dbg ) accessory.log.debug( chalk.blue( `createServicesFor${ this.CMD4 }AccessoryAndItsChildren` ) ); if ( accessory.ServiceCreated == true ) { - if ( cmd4Dbg ) accessory.log.debug( chalk.red( `SERVICES ALREADY CREATED FOR ${ this.displayName } ${ this.CMD4 } ${ this.LEVEL }` ) ); + if ( settings.cmd4Dbg ) accessory.log.debug( chalk.red( `SERVICES ALREADY CREATED FOR ${ this.displayName } ${ this.CMD4 } ${ this.LEVEL }` ) ); return; } else { accessory.ServiceCreated = true; @@ -543,7 +539,7 @@ class Cmd4Accessory // Create the accessory's service accessory.service = new properties.service( accessory.name, accessory.subType ) - if ( cmd4Dbg ) accessory.log.debug( `Creating information service for standalone accessory: ${ accessory.displayName }` ); + if ( settings.cmd4Dbg ) accessory.log.debug( `Creating information service for standalone accessory: ${ accessory.displayName }` ); // Create the Standalone accessory's information service. @@ -558,16 +554,16 @@ class Cmd4Accessory // Standalone Step 4. // const hdmi1InputService = this.tvAccessory.addService( this.Service.InputSource, `hdmi1', 'HDMI 1' ); - if ( cmd4Dbg ) accessory.log.debug( `Standalone Step 4. linkedAccessory( ${ accessory.displayName } ).service = new Service( ${ linkedAccessory.name }, ${ linkedAccessory.subType } )` ); + if ( settings.cmd4Dbg ) accessory.log.debug( `Standalone Step 4. linkedAccessory( ${ accessory.displayName } ).service = new Service( ${ linkedAccessory.name }, ${ linkedAccessory.subType } )` ); linkedAccessory.service = new properties.service( linkedAccessory.name, linkedAccessory.subType ) accessory.services.push( linkedAccessory.service ); // Hmmm Double Check this !! // Create Information Service - //if ( cmd4Dbg ) linkedAccessory.log.debug( "Creating information service for linkedAccessory:%s", linkedAccessory.displayName ); + //if ( settings.cmd4Dbg ) linkedAccessory.log.debug( "Creating information service for linkedAccessory:%s", linkedAccessory.displayName ); //createAccessorysInformationService( linkedAccessory ); - if ( cmd4Dbg ) accessory.log.debug( `Standalone Step 5. ${ accessory.displayName }.service.addLinkedService( ${ linkedAccessory.displayName }.service` ); + if ( settings.cmd4Dbg ) accessory.log.debug( `Standalone Step 5. ${ accessory.displayName }.service.addLinkedService( ${ linkedAccessory.displayName }.service` ); // Standalone Step 5. // tvService.addLinkedService( hdmi1InputService ); // link to tv service accessory.service.addLinkedService( linkedAccessory.service ); @@ -609,7 +605,7 @@ class Cmd4Accessory if ( self.hV.statusMsg == "TRUE" ) self.log.info( chalk.blue( `Setting (Cached) ${ self.displayName } ${ characteristicString }` ) + ` ${ value }` ); else - if ( cmd4Dbg ) self.log.debug( `setCachedvalue accTypeEnumIndex:( ${ accTypeEnumIndex } )-"${ characteristicString }" function for: ${ self.displayName } value: ${ value }` ); + if ( settings.cmd4Dbg ) self.log.debug( `setCachedvalue accTypeEnumIndex:( ${ accTypeEnumIndex } )-"${ characteristicString }" function for: ${ self.displayName } value: ${ value }` ); // Save the cached value. @@ -657,7 +653,7 @@ class Cmd4Accessory callback( 10, null ); } - if ( cmd4Dbg ) self.log.debug( `getCachedValue ${ characteristicString } for: ${ self.displayName } returned (CACHED) value: ${ storedValue }` ); + if ( settings.cmd4Dbg ) self.log.debug( `getCachedValue ${ characteristicString } for: ${ self.displayName } returned (CACHED) value: ${ storedValue }` ); callback( 0, storedValue ); @@ -718,7 +714,7 @@ class Cmd4Accessory // We fix this here. if ( accTypeEnumIndex == CMD4_ACC_TYPE_ENUM.CurrentHeatingCoolingState ) { - if ( cmd4Dbg ) this.log.debug( "fixing heatingCoolingState" ); + if ( settings.cmd4Dbg ) this.log.debug( "fixing heatingCoolingState" ); accessory.service.getCharacteristic( CMD4_ACC_TYPE_ENUM.properties[ accTypeEnumIndex ]. characteristic ).setProps( @@ -765,7 +761,7 @@ class Cmd4Accessory // *********************************************** addAllServiceCharacteristicsForAccessory( accessory ) { - if ( cmd4Dbg ) accessory.log.debug( `Adding All Service Characteristics for: ${ accessory.displayName }` ); + if ( settings.cmd4Dbg ) accessory.log.debug( `Adding All Service Characteristics for: ${ accessory.displayName }` ); let perms = ""; @@ -780,7 +776,7 @@ class Cmd4Accessory let storedValue = accessory.cmd4Storage.getStoredValueForIndex( accTypeEnumIndex ); if ( storedValue != undefined ) { - if ( cmd4Dbg ) accessory.log.debug( "Found characteristic:%s value:%s for:%s", + if ( settings.cmd4Dbg ) accessory.log.debug( "Found characteristic:%s value:%s for:%s", uCCharacteristicString, storedValue, this.displayName ); @@ -790,8 +786,8 @@ class Cmd4Accessory if ( ! accessory.service.testCharacteristic( CMD4_ACC_TYPE_ENUM.properties[ accTypeEnumIndex ].characteristic ) ) { - //if ( cmd4Dbg ) accessory.log.debug( "Adding optional characteristic:%s for: %s", CMD4_ACC_TYPE_ENUM.properties[ accTypeEnumIndex ].type, this.displayName ); - if ( cmd4Dbg ) accessory.log.debug( "Adding optional characteristic:%s for: %s", uCCharacteristicString, this.displayName ); + //if ( settings.cmd4Dbg ) accessory.log.debug( "Adding optional characteristic:%s for: %s", CMD4_ACC_TYPE_ENUM.properties[ accTypeEnumIndex ].type, this.displayName ); + if ( settings.cmd4Dbg ) accessory.log.debug( "Adding optional characteristic:%s for: %s", uCCharacteristicString, this.displayName ); accessory.service.addCharacteristic( CMD4_ACC_TYPE_ENUM.properties[ accTypeEnumIndex ].characteristic ); } @@ -802,8 +798,8 @@ class Cmd4Accessory let props = accessory.configHasCharacteristicProps( accTypeEnumIndex ); if ( props ) { - //if ( cmd4Dbg ) accessory.log.debug( "Overriding characteristic %s props for: %s ", CMD4_ACC_TYPE_ENUM.properties[ accTypeEnumIndex ].type, this.displayName ); - if ( cmd4Dbg ) accessory.log.debug( "Overriding characteristic %s props for: %s ", uCCharacteristicString, this.displayName ); + //if ( settings.cmd4Dbg ) accessory.log.debug( "Overriding characteristic %s props for: %s ", CMD4_ACC_TYPE_ENUM.properties[ accTypeEnumIndex ].type, this.displayName ); + if ( settings.cmd4Dbg ) accessory.log.debug( "Overriding characteristic %s props for: %s ", uCCharacteristicString, this.displayName ); accessory.service.getCharacteristic( CMD4_ACC_TYPE_ENUM.properties[ accTypeEnumIndex ]. characteristic ) .setProps( @@ -863,7 +859,7 @@ class Cmd4Accessory accessory.listOfPollingCharacteristics[ accTypeEnumIndex ] == undefined ) { - if ( cmd4Dbg ) this.log.debug( chalk.yellow( `Adding getCachedValue for ${ accessory.displayName } characteristic: ${ uCCharacteristicString } ` ) ); + if ( settings.cmd4Dbg ) this.log.debug( chalk.yellow( `Adding getCachedValue for ${ accessory.displayName } characteristic: ${ uCCharacteristicString } ` ) ); //Get cachedValue accessory.service.getCharacteristic( CMD4_ACC_TYPE_ENUM.properties[ accTypeEnumIndex ] @@ -871,7 +867,7 @@ class Cmd4Accessory .on( "get", accessory.getCachedValue.bind( accessory, accTypeEnumIndex, uCCharacteristicString ) ); } else { - if ( cmd4Dbg ) this.log.debug( chalk.yellow( `Adding priorityGetValue for ${ accessory.displayName } characteristic: ${ uCCharacteristicString }` ) ); + if ( settings.cmd4Dbg ) this.log.debug( chalk.yellow( `Adding priorityGetValue for ${ accessory.displayName } characteristic: ${ uCCharacteristicString }` ) ); let details = accessory.lookupAccessoryHVForPollingCharacteristic( accessory, accTypeEnumIndex ); @@ -897,7 +893,7 @@ class Cmd4Accessory if ( ! accessory.polling || accessory.listOfPollingCharacteristics[ accTypeEnumIndex ] == undefined) { - if ( cmd4Dbg ) this.log.debug( chalk.yellow( `Adding setCachedValue for ${ accessory.displayName } characteristic: ${ uCCharacteristicString } ` ) ); + if ( settings.cmd4Dbg ) this.log.debug( chalk.yellow( `Adding setCachedValue for ${ accessory.displayName } characteristic: ${ uCCharacteristicString } ` ) ); // setCachedValue has parameters: // accTypeEnumIndex, value, callback // The first bound value though is "this" @@ -910,7 +906,7 @@ class Cmd4Accessory }); } else { - if ( cmd4Dbg ) this.log.debug( chalk.yellow( `Adding prioritySetValue for ${ accessory.displayName } characteristic: ${ uCCharacteristicString } ` ) ); + if ( settings.cmd4Dbg ) this.log.debug( chalk.yellow( `Adding prioritySetValue for ${ accessory.displayName } characteristic: ${ uCCharacteristicString } ` ) ); let details = accessory.lookupAccessoryHVForPollingCharacteristic( accessory, accTypeEnumIndex ); @@ -954,7 +950,7 @@ class Cmd4Accessory firstParmValue = ( this.cmd4Storage.testStoredValueForIndex( firstParmDirective.accTypeEnumIndex ) == undefined ) ? firstParmValue : this.cmd4Storage.getStoredValueForIndex( firstParmDirective.accTypeEnumIndex ); - if ( cmd4Dbg ) this.log.debug( `Logging ${ constants.POWER }: ${ firstParmValue }` ); + if ( settings.cmd4Dbg ) this.log.debug( `Logging ${ constants.POWER }: ${ firstParmValue }` ); // Eve Energy ( Outlet service ) this.loggingService.addEntry( { [ constants.TIME ] : moment( ).unix( ), @@ -979,7 +975,7 @@ class Cmd4Accessory thirdParmValue : this.cmd4Storage.getStoredValueForIndex( thirdParmDirective.accTypeEnumIndex ); - if ( cmd4Dbg ) this.log.debug( `Logging ${ constants.TEMP }:${ firstParmValue } ${constants.HUMIDITY }:${ secondParmValue } ${ constants.PPM }:${ thirdParmValue }` ); + if ( settings.cmd4Dbg ) this.log.debug( `Logging ${ constants.TEMP }:${ firstParmValue } ${constants.HUMIDITY }:${ secondParmValue } ${ constants.PPM }:${ thirdParmValue }` ); // Eve Room ( TempSensor, HumiditySensor and AirQuality Services ) this.loggingService.addEntry( { [ constants.TIME ] : moment( ).unix( ), @@ -1005,7 +1001,7 @@ class Cmd4Accessory thirdParmValue = ( this.cmd4Storage.testStoredValueForIndex( thirdParmDirective.accTypeEnumIndex ) == undefined ) ? thirdParmValue : this.cmd4Storage.getStoredValueForIndex( thirdParmDirective.accTypeEnumIndex ); - if ( cmd4Dbg ) this.log.debug( `Logging ${ constants.TEMP }: ${ firstParmValue } ${ constants.PRESSURE }: ${ secondParmValue } ${ constants.HUMIDITY }: ${ thirdParmValue }` ); + if ( settings.cmd4Dbg ) this.log.debug( `Logging ${ constants.TEMP }: ${ firstParmValue } ${ constants.PRESSURE }: ${ secondParmValue } ${ constants.HUMIDITY }: ${ thirdParmValue }` ); // Eve Weather ( TempSensor Service ) this.loggingService.addEntry( @@ -1024,7 +1020,7 @@ class Cmd4Accessory firstParmValue = ( this.cmd4Storage.testStoredValueForIndex( firstParmDirective.accTypeEnumIndex ) == undefined ) ? firstParmValue : this.cmd4Storage.getStoredValueForIndex( firstParmDirective.accTypeEnumIndex ); - if ( cmd4Dbg ) this.log.debug( `Logging ${ constants.STATUS } status: ${ firstParmValue }` ); + if ( settings.cmd4Dbg ) this.log.debug( `Logging ${ constants.STATUS } status: ${ firstParmValue }` ); this.loggingService.addEntry( { [ constants.TIME ] : moment( ).unix( ), @@ -1040,7 +1036,7 @@ class Cmd4Accessory firstParmValue = ( this.cmd4Storage.testStoredValueForIndex( firstParmDirective.accTypeEnumIndex ) == undefined ) ? firstParmValue : this.cmd4Storage.getStoredValueForIndex( firstParmDirective.accTypeEnumIndex ); - if ( cmd4Dbg ) this.log.debug( `Logging ${ constants.STATUS }: ${ firstParmValue }` ); + if ( settings.cmd4Dbg ) this.log.debug( `Logging ${ constants.STATUS }: ${ firstParmValue }` ); this.loggingService.addEntry( { [ constants.TIME ] : moment( ).unix( ), @@ -1064,7 +1060,7 @@ class Cmd4Accessory thirdParmValue = ( this.cmd4Storage.testStoredValueForIndex( thirdParmDirective.accTypeEnumIndex ) == undefined ) ? thirdParmValue : this.cmd4Storage.getStoredValueForIndex( thirdParmDirective.accTypeEnumIndex ); - if ( cmd4Dbg ) this.log.debug( `Logging ${ constants.CURRENTTEMP }: ${ firstParmValue } ${ constants.SETTEMP }:${ secondParmValue } ${constants.VALVEPOSITION }:${ thirdParmValue } ` ); + if ( settings.cmd4Dbg ) this.log.debug( `Logging ${ constants.CURRENTTEMP }: ${ firstParmValue } ${ constants.SETTEMP }:${ secondParmValue } ${constants.VALVEPOSITION }:${ thirdParmValue } ` ); // Eve Thermo ( Thermostat service ) this.loggingService.addEntry( @@ -1087,7 +1083,7 @@ class Cmd4Accessory secondParmValue = ( this.cmd4Storage.testStoredValueForIndex( secondParmDirective.accTypeEnumIndex ) == undefined ) ? secondParmValue : this.cmd4Storage.getStoredValueForIndex( secondParmDirective.accTypeEnumIndex ); - if ( cmd4Dbg ) this.log.debug( `Logging ${ constants.STATUS }: ${ firstParmValue } ${ constants.WATERAMOUNT }: ${ secondParmValue }` ); + if ( settings.cmd4Dbg ) this.log.debug( `Logging ${ constants.STATUS }: ${ firstParmValue } ${ constants.WATERAMOUNT }: ${ secondParmValue }` ); // Eve Aqua ( Valve service set to Irrigation Type ) this.LoggingService.addEntry( @@ -1171,10 +1167,10 @@ class Cmd4Accessory if ( value != "0" ) { let rcDirective = isAccDirective( value, false ); - if ( rcDirective == null ) + if ( rcDirective.accTypeEnumIndex == null ) { rcDirective = isAccDirective( value, true ); - if ( rcDirective == null ) + if ( rcDirective.accTypeEnumIndex == null ) throw new Error( `Invalid characteristic "${ value }" for fakegato to log of "${ key }".` ); this.log.warn( `The config.json FakeGato characteristic: ${ value } is Capitalized it should be: ${ rcDirective.type }. In the near future this will be an Error so that Cmd4 can use homebridge-ui.\nTo remove this Warning, Please fix your config.json.` ); @@ -1310,10 +1306,10 @@ class Cmd4Accessory { // fix the their scripts, fix it here. let rcDirective = isAccDirective( key, false ); - if ( rcDirective == null ) + if ( rcDirective.accTypeEnumIndex == null ) { rcDirective = isAccDirective( key, true ); - if ( rcDirective == null ) + if ( rcDirective.accTypeEnumIndex == null ) throw new Error( `OOPS: "${ key }" not found for parsing characteristics in: "${ this.displayName }".` ); this.log.warn( `The config.json characteristic key: ${ key } is Capitalized. It should be: ${ rcDirective.type }. In the near future this will be an error for homebridge-ui integration.\nTo remove this Warning, Please fix your config.json.` ); @@ -1372,7 +1368,7 @@ class Cmd4Accessory throw new Error( `Requires definition: "${ required }" must be a string.` ); - if ( cmd4Dbg ) this.log.debug( `Requiring ${ required }` ); + if ( settings.cmd4Dbg ) this.log.debug( `Requiring ${ required }` ); require( required ); } @@ -1686,12 +1682,12 @@ class Cmd4Accessory break; case constants.LINKEDTYPES: - if ( cmd4Dbg ) this.log.debug( `parseConfig. Found linked Accessories` ); + if ( settings.cmd4Dbg ) this.log.debug( `parseConfig. Found linked Accessories` ); this.linkedAccessoriesConfig = value; break; case constants.ACCESSORIES: - if ( cmd4Dbg ) this.log.debug( `parseConfig. Found Accessories` ); + if ( settings.cmd4Dbg ) this.log.debug( `parseConfig. Found Accessories` ); this.accessoriesConfig = value; break; @@ -1780,10 +1776,10 @@ class Cmd4Accessory { case null: case undefined: - if ( cmd4Dbg ) this.log.debug( `No polling configured.` ); + if ( settings.cmd4Dbg ) this.log.debug( `No polling configured.` ); return; case Boolean: - if ( cmd4Dbg ) this.log.debug( `Polling config is Default Polling. Nothing to check for unset polling characteristics` ); + if ( settings.cmd4Dbg ) this.log.debug( `Polling config is Default Polling. Nothing to check for unset polling characteristics` ); return; case String: throw new Error( `Unknown type for Polling "${ this.polling }" given in: "${ this.displayName }".` ); @@ -1905,7 +1901,7 @@ class Cmd4Accessory // which happens in the Demo mode without polling. if ( typeof accessory.polling == "object" ) { - if ( cmd4Dbg ) this.log.debug( `Characteristic polling for: ${ accessory.displayName }` ); + if ( settings.cmd4Dbg ) this.log.debug( `Characteristic polling for: ${ accessory.displayName }` ); accessory.polling.forEach( ( jsonPollingConfig ) => { // Characteristic polling is a json type @@ -1968,10 +1964,10 @@ class Cmd4Accessory case constants.CHARACTERISTIC: { rcDirective = isAccDirective( value, false ); - if ( rcDirective == null ) + if ( rcDirective.accTypeEnumIndex == null ) { rcDirective = isAccDirective( value, true ); - if ( rcDirective == null ) + if ( rcDirective.accTypeEnumIndex == null ) throw new Error( `No such polling characteristic: "${ value }" for: "${ this.displayName }".` ); this.log.warn( `1. The config.json Polling characteristic: ${ value } is Capitalized it should be: ${ rcDirective.type }. In the near future this will be an Error so that Cmd4 can use homebridge-ui.\nTo remove this Warning, Please fix your config.json.` ); @@ -2015,7 +2011,7 @@ class Cmd4Accessory // This has to be UC as it gets passed to the getValue cmd string let characteristicString = CMD4_ACC_TYPE_ENUM.accEnumIndexToUC( accTypeEnumIndex ); - if ( cmd4Dbg ) this.log.debug( `Setting up accessory: ${ accessory.displayName } for polling of: ${ characteristicString } timeout: ${ timeout } interval: ${ interval } queueName: "${ this.queue.queueName }"` ); + if ( settings.cmd4Dbg ) this.log.debug( `Setting up accessory: ${ accessory.displayName } for polling of: ${ characteristicString } timeout: ${ timeout } interval: ${ interval } queueName: "${ this.queue.queueName }"` ); let record = { [ constants.ACCESSORY_lv ]: accessory, [ constants.ACC_TYPE_ENUM_INDEX_lv ]: accTypeEnumIndex, [ constants.CHARACTERISTIC_STRING_lv ]: characteristicString, [ constants.INTERVAL_lv ]: interval, [ constants.TIMEOUT_lv ]: timeout, [ constants.STATE_CHANGE_RESPONSE_TIME_lv ]: stateChangeResponseTime, [ constants.QUEUE_NAME_lv ]: this.queue.queueName }; @@ -2059,7 +2055,7 @@ class Cmd4Accessory if ( accessory.polling == true ) { - if ( cmd4Dbg ) this.log.debug( `Adding ${ record.accessory.displayName } ${ CMD4_ACC_TYPE_ENUM.properties[ record.accTypeEnumIndex ].type } record.timeout: ${ record.timeout } record.interval: ${ record.interval } to Polled Queue ${ record.queueName }` ); + if ( settings.cmd4Dbg ) this.log.debug( `Adding ${ record.accessory.displayName } ${ CMD4_ACC_TYPE_ENUM.properties[ record.accTypeEnumIndex ].type } record.timeout: ${ record.timeout } record.interval: ${ record.interval } to Polled Queue ${ record.queueName }` ); this.queue.addLowPriorityGetPolledQueueEntry( record.accessory, @@ -2105,7 +2101,7 @@ class Cmd4Accessory function checkAccessoryForDuplicateUUID( accessory, uuid ) { // check for UUID+subtype conflict - if ( cmd4Dbg ) accessory.log.debug( `Checking ${ accessory.name } for Duplicate UUID: ${ accessory.uuid }` ); + if ( settings.cmd4Dbg ) accessory.log.debug( `Checking ${ accessory.name } for Duplicate UUID: ${ accessory.uuid }` ); for ( let existingAccessory in accessory.createdCmd4Accessories ) { @@ -2142,7 +2138,7 @@ function checkAccessoryForDuplicateUUID( accessory, uuid ) } } - if ( cmd4Dbg ) accessory.log.debug( `No Duplicate UUID's for this Accessory - ` + chalk.green( `OK` ) + `. Using: ${ accessory.uuid }` ); + if ( settings.cmd4Dbg ) accessory.log.debug( `No Duplicate UUID's for this Accessory - ` + chalk.green( `OK` ) + `. Using: ${ accessory.uuid }` ); } exports.Cmd4Accessory = Cmd4Accessory; diff --git a/Cmd4Platform.js b/Cmd4Platform.js index 835ff5d..4e7603f 100644 --- a/Cmd4Platform.js +++ b/Cmd4Platform.js @@ -37,7 +37,6 @@ const Cmd4Accessory = require( "./Cmd4Accessory" ).Cmd4Accessory; // Settings, Globals and Constants let settings = require( "./cmd4Settings" ); const constants = require( "./cmd4Constants" ); -let cmd4Dbg = settings.cmd4Dbg; // Platform definition class Cmd4Platform @@ -51,7 +50,7 @@ class Cmd4Platform this.log = log; // Carry the debug flag from the platform - cmd4Dbg = log.debugEnabled; + settings.cmd4Dbg = log.debugEnabled; } else { @@ -63,13 +62,12 @@ class Cmd4Platform process.env.DEBUG == settings.PLATFORM_NAME ) { settings.cmd4Dbg = true; - cmd4Dbg = true; } } - this.log.setDebugEnabled( cmd4Dbg ); + this.log.setDebugEnabled( settings.cmd4Dbg ); - if ( cmd4Dbg ) this.log.debug( chalk.blue( `Class Cmd4Platform` ) ); + if ( settings.cmd4Dbg ) this.log.debug( chalk.blue( `Class Cmd4Platform` ) ); if ( config === undefined ) return; @@ -151,7 +149,7 @@ class Cmd4Platform { if ( platformAccessory ) { - if ( cmd4Dbg ) this.log.debug( `Found cached accessory: ${ platformAccessory.displayName }` ); + if ( settings.cmd4Dbg ) this.log.debug( `Found cached accessory: ${ platformAccessory.displayName }` ); this.toBeRestoredPlatforms.push( platformAccessory ); } } @@ -324,7 +322,7 @@ class Cmd4Platform this.definitions.forEach( ( definition, definitionIndex ) => { - if ( cmd4Dbg ) this.log.debug( `Processing definition index: ${ definitionIndex }` ); + if ( settings.cmd4Dbg ) this.log.debug( `Processing definition index: ${ definitionIndex }` ); if ( trueTypeOf( definition.type ) != String ) throw new Error( `definition.type at index: ${ definitionIndex } is not a String.` ); @@ -392,7 +390,7 @@ class Cmd4Platform } CMD4_ACC_TYPE_ENUM.add( this.api, definition.type, definition.description, definition.props, definition.validValues ); - if ( cmd4Dbg ) this.log.debug( `Created definition type: "${ definition.type }".` ); + if ( settings.cmd4Dbg ) this.log.debug( `Created definition type: "${ definition.type }".` ); }); } @@ -407,7 +405,7 @@ class Cmd4Platform // already been registered. this.config.accessories && this.config.accessories.forEach( ( device ) => { - if ( cmd4Dbg ) this.log.debug( `Fetching config.json Platform accessories.` ); + if ( settings.cmd4Dbg ) this.log.debug( `Fetching config.json Platform accessories.` ); this.Service=this.api.hap.Service; device.name = getAccessoryName( device ); @@ -473,7 +471,7 @@ class Cmd4Platform // If the saved context has our STORED_DATA_ARRAY, then use it. if ( existingAccessory.context.STORED_DATA_ARRAY ) { - if ( cmd4Dbg ) this.log.debug(`Cmd4Platform: Using context.STORED_DATA_ARRAY` ); + if ( settings.cmd4Dbg ) this.log.debug(`Cmd4Platform: Using context.STORED_DATA_ARRAY` ); STORED_DATA_ARRAY = existingAccessory.context.STORED_DATA_ARRAY; } @@ -493,7 +491,7 @@ class Cmd4Platform // Get the properties for this accessories device type let devProperties = CMD4_DEVICE_TYPE_ENUM.properties[ accessory.typeIndex ]; - if ( cmd4Dbg ) this.log.debug( `Step 2. ${ accessory.displayName }.service = platform.getService( Service.${ devProperties.deviceName }, ${ accessory.subType })` ); + if ( settings.cmd4Dbg ) this.log.debug( `Step 2. ${ accessory.displayName }.service = platform.getService( Service.${ devProperties.deviceName }, ${ accessory.subType })` ); accessory.service = platform.getService( devProperties.service, accessory.name, accessory.subType ); // Determine which characteristics, if any, will be polled. This @@ -521,7 +519,7 @@ class Cmd4Platform // Create the new PlatformAccessory if ( device.category == undefined ) { - if ( cmd4Dbg ) this.log.debug( `Step 1. platformAccessory = new platformAccessory( ${ displayName }, ${ uuid } )` ); + if ( settings.cmd4Dbg ) this.log.debug( `Step 1. platformAccessory = new platformAccessory( ${ displayName }, ${ uuid } )` ); platform = new this.api.platformAccessory( displayName, uuid ); } else @@ -533,7 +531,7 @@ class Cmd4Platform if ( ! category ) throw new Error( `Category specified: ${ device.category } is not a valid homebridge category.` ); - if ( cmd4Dbg ) this.log.debug( `Step 1. platformAccessory = new platformAccessory( ${ displayName }, ${ uuid }, ${ category } )` ); + if ( settings.cmd4Dbg ) this.log.debug( `Step 1. platformAccessory = new platformAccessory( ${ displayName }, ${ uuid }, ${ category } )` ); platform = new this.api.platformAccessory( displayName, uuid, category ); } @@ -548,7 +546,7 @@ class Cmd4Platform // Put the accessory into its correct collection array. this.createdCmd4Accessories.push( accessory ); - if ( cmd4Dbg ) this.log.debug( `Created platformAccessory: ${ accessory.displayName }` ); + if ( settings.cmd4Dbg ) this.log.debug( `Created platformAccessory: ${ accessory.displayName }` ); // Store a copy of the device object in the `accessory.context` // the `context` property can be used to store any data about the @@ -560,7 +558,7 @@ class Cmd4Platform // MOVE OUSTSIDE // Platform Step 2. const tvService = this.tvAccessory.addService( this.Service.Television ); - if ( cmd4Dbg ) this.log.debug( `Step 2. ${ accessory.displayName }.service = platform.addService( this.Service.${ devProperties.deviceName }, ${ accessory.name }, ${ accessory.subType })` ); + if ( settings.cmd4Dbg ) this.log.debug( `Step 2. ${ accessory.displayName }.service = platform.addService( this.Service.${ devProperties.deviceName }, ${ accessory.name }, ${ accessory.subType })` ); accessory.service = platform.addService( new devProperties.service( accessory.name, accessory.subType ) ); // Create all the services for the accessory, including fakegato @@ -570,12 +568,12 @@ class Cmd4Platform // Step 6. this.api.publishExternalAccessories( PLUGIN_NAME, [ this.tvAccessory ] ); if ( accessory.publishExternally ) { - if ( cmd4Dbg ) this.log.debug( `Step 6. publishExternalAccessories( ${ settings.PLUGIN_NAME }, [ ${accessory.displayName } ] )` ); + if ( settings.cmd4Dbg ) this.log.debug( `Step 6. publishExternalAccessories( ${ settings.PLUGIN_NAME }, [ ${accessory.displayName } ] )` ); this.api.publishExternalAccessories( settings.PLUGIN_NAME, [ platform ] ); } else { - if ( cmd4Dbg ) this.log.debug( `Step 6. registerPlatformAccessories( ${ settings.PLUGIN_NAME }, ${ settings.PLATFORM_NAME }, [ ${ accessory.displayName } ] ) `); + if ( settings.cmd4Dbg ) this.log.debug( `Step 6. registerPlatformAccessories( ${ settings.PLUGIN_NAME }, ${ settings.PLATFORM_NAME }, [ ${ accessory.displayName } ] ) `); this.api.registerPlatformAccessories( settings.PLUGIN_NAME, settings.PLATFORM_NAME, [ platform ] ); } @@ -599,28 +597,28 @@ class Cmd4Platform // for us and the getService hangs off the platform, not the accessory. if ( cmd4PlatformAccessory.model ) { - if ( cmd4Dbg ) cmd4PlatformAccessory.log.debug( `Adding model( ${ cmd4PlatformAccessory.model } ) to information service of ${ cmd4PlatformAccessory.displayName }` ); + if ( settings.cmd4Dbg ) cmd4PlatformAccessory.log.debug( `Adding model( ${ cmd4PlatformAccessory.model } ) to information service of ${ cmd4PlatformAccessory.displayName }` ); cmd4PlatformAccessory.platform.getService( cmd4PlatformAccessory.platform.Service.AccessoryInformation ) .setCharacteristic( this.api.hap.Characteristic.Model, cmd4PlatformAccessory.model ); } if ( cmd4PlatformAccessory.manufacturer ) { - if ( cmd4Dbg ) cmd4PlatformAccessory.log.debug( `Adding manufacturer( ${ cmd4PlatformAccessory.manufacturer } ) to information service of ${ cmd4PlatformAccessory.displayName }` ); + if ( settings.cmd4Dbg ) cmd4PlatformAccessory.log.debug( `Adding manufacturer( ${ cmd4PlatformAccessory.manufacturer } ) to information service of ${ cmd4PlatformAccessory.displayName }` ); cmd4PlatformAccessory.platform.getService( cmd4PlatformAccessory.platform.Service.AccessoryInformation ) .setCharacteristic( this.api.hap.Characteristic.Manufacturer, cmd4PlatformAccessory.manufacturer ); } if ( cmd4PlatformAccessory.serialNumber ) { - if ( cmd4Dbg ) cmd4PlatformAccessory.log.debug( `Adding serial Number( ${ cmd4PlatformAccessory.serialNumber } ) to information service of ${ cmd4PlatformAccessory.displayName }` ); + if ( settings.cmd4Dbg ) cmd4PlatformAccessory.log.debug( `Adding serial Number( ${ cmd4PlatformAccessory.serialNumber } ) to information service of ${ cmd4PlatformAccessory.displayName }` ); cmd4PlatformAccessory.platform.getService( cmd4PlatformAccessory.platform.Service.AccessoryInformation ) .setCharacteristic( this.api.hap.Characteristic.SerialNumber, cmd4PlatformAccessory.serialNumber ); } if ( cmd4PlatformAccessory.firmwareRevision ) { - if ( cmd4Dbg ) cmd4PlatformAccessory.log.debug( `Adding Firmware Revision( ${ cmd4PlatformAccessory.firmwareRevision } ) to information service of ${ cmd4PlatformAccessory.displayName }` ); + if ( settings.cmd4Dbg ) cmd4PlatformAccessory.log.debug( `Adding Firmware Revision( ${ cmd4PlatformAccessory.firmwareRevision } ) to information service of ${ cmd4PlatformAccessory.displayName }` ); cmd4PlatformAccessory.platform.getService( cmd4PlatformAccessory.platform.Service.AccessoryInformation ) .setCharacteristic( this.api.hap.Characteristic.FirmwareRevision, cmd4PlatformAccessory.firmwareRevision ); } @@ -640,19 +638,19 @@ class Cmd4Platform // Existing Accessories would have existing services if ( fromExisting == true ) { - if ( cmd4Dbg ) this.log.debug( `Platform (AddedAccessory-existing) Step 3, ${ addedAccessory.displayName }.service = accessory.platform.getService( Service.${ devProperties.deviceName }, ${ addedAccessory.name }, ${ addedAccessory.subType }` ); + if ( settings.cmd4Dbg ) this.log.debug( `Platform (AddedAccessory-existing) Step 3, ${ addedAccessory.displayName }.service = accessory.platform.getService( Service.${ devProperties.deviceName }, ${ addedAccessory.name }, ${ addedAccessory.subType }` ); addedAccessory.service = addedAccessory.platform.getService( devProperties.service, addedAccessory.name, addedAccessory.subType ); } else { - if ( cmd4Dbg ) this.log.debug( `Platform (AddedAccessory-new) Step 3, ${ addedAccessory.displayName }.service = PlatformAccessory: ${ cmd4PlatformAccessory.displayName } addService( Service:${ devProperties.deviceName }, ${ addedAccessory.name }, ${ addedAccessory.subType } )` ); + if ( settings.cmd4Dbg ) this.log.debug( `Platform (AddedAccessory-new) Step 3, ${ addedAccessory.displayName }.service = PlatformAccessory: ${ cmd4PlatformAccessory.displayName } addService( Service:${ devProperties.deviceName }, ${ addedAccessory.name }, ${ addedAccessory.subType } )` ); addedAccessory.service = cmd4PlatformAccessory.platform.addService( new devProperties.service( addedAccessory.name, addedAccessory.subType ) ); } addedAccessory.addAllServiceCharacteristicsForAccessory( addedAccessory ); // Create Information Service for the addedAccessory - if ( cmd4Dbg ) addedAccessory.log.debug( `Creating information service for AddedAccessory: ${ addedAccessory.displayName }` ); + if ( settings.cmd4Dbg ) addedAccessory.log.debug( `Creating information service for AddedAccessory: ${ addedAccessory.displayName }` ); createAccessorysInformationService( addedAccessory ); // Setup the fakegato service if defined in the config.json file @@ -679,12 +677,12 @@ class Cmd4Platform // Existing Accessories would have existing services if ( fromExisting == true ) { - if ( cmd4Dbg ) this.log.debug( `Platform (LinkedAccessory-existing) Step 4. ${ linkedAccessory.displayName }.service = ${ cmd4PlatformAccessory.displayName }.getService:( ${ devProperties.deviceName }.service, ${linkedAccessory.name }, ${linkedAccessory.subType } )` ); + if ( settings.cmd4Dbg ) this.log.debug( `Platform (LinkedAccessory-existing) Step 4. ${ linkedAccessory.displayName }.service = ${ cmd4PlatformAccessory.displayName }.getService:( ${ devProperties.deviceName }.service, ${linkedAccessory.name }, ${linkedAccessory.subType } )` ); linkedAccessory.service = linkedAccessory.platform.getService( devProperties.service, linkedAccessory.name, linkedAccessory.subType ); } else { - if ( cmd4Dbg ) this.log.debug( `Platform (LinkedAccessory-new) Step 4. ${ linkedAccessory.displayName }.service = ${ cmd4PlatformAccessory.displayName }.addService:( ${ devProperties.deviceName }.service, ${linkedAccessory.name }, ${linkedAccessory.subType } )` ); + if ( settings.cmd4Dbg ) this.log.debug( `Platform (LinkedAccessory-new) Step 4. ${ linkedAccessory.displayName }.service = ${ cmd4PlatformAccessory.displayName }.addService:( ${ devProperties.deviceName }.service, ${linkedAccessory.name }, ${linkedAccessory.subType } )` ); linkedAccessory.service = cmd4PlatformAccessory.platform.addService( new devProperties.service( linkedAccessory.name, linkedAccessory.subType ) ); } @@ -693,12 +691,12 @@ class Cmd4Platform if ( fromExisting == false ) { - if ( cmd4Dbg ) this.log.debug( `Platform Step 5. ${ cmd4PlatformAccessory.displayName }.service.addLinkedService( ${ linkedAccessory.displayName }.service )` ); + if ( settings.cmd4Dbg ) this.log.debug( `Platform Step 5. ${ cmd4PlatformAccessory.displayName }.service.addLinkedService( ${ linkedAccessory.displayName }.service )` ); cmd4PlatformAccessory.service.addLinkedService( linkedAccessory.service ); } // Create Information Service for the linkedAccessory - if ( cmd4Dbg ) linkedAccessory.log.debug( `Creating information service for Linked Platform Accessory: ${ linkedAccessory.displayName }` ); + if ( settings.cmd4Dbg ) linkedAccessory.log.debug( `Creating information service for Linked Platform Accessory: ${ linkedAccessory.displayName }` ); createAccessorysInformationService( linkedAccessory ); // Setup the fakegato service if defined in the config.json file @@ -725,7 +723,7 @@ class Cmd4Platform // Check for any queued characteristics if ( Object.keys( settings.listOfCreatedPriorityQueues ).length == 0 ) { - if ( cmd4Dbg ) this.log.debug( `No queued polling characteristics` ); + if ( settings.cmd4Dbg ) this.log.debug( `No queued polling characteristics` ); return; } diff --git a/Cmd4PriorityPollingQueue.js b/Cmd4PriorityPollingQueue.js index 272e093..b4da233 100644 --- a/Cmd4PriorityPollingQueue.js +++ b/Cmd4PriorityPollingQueue.js @@ -29,8 +29,6 @@ let HIGH_PRIORITY_SET = 0; let HIGH_PRIORITY_GET = 1; let LOW_PRIORITY_GET = 2; -let cmd4Dbg = settings.cmd4Dbg; - class Cmd4PriorityPollingQueue { constructor( log, queueName, queueType = constants.DEFAULT_QUEUE_TYPE ) @@ -38,7 +36,7 @@ class Cmd4PriorityPollingQueue this.log = log; // This works better for Unit testing - cmd4Dbg = log.debugEnabled; + settings.cmd4Dbg = log.debugEnabled; this.queueName = queueName; this.queueType = queueType; @@ -139,7 +137,7 @@ class Cmd4PriorityPollingQueue processHighPrioritySetQueue( entry ) { - if ( cmd4Dbg ) this.log.debug( `Processing high priority queue "Set" entry: ${ entry.accTypeEnumIndex } length: ${ this.highPriorityQueue.length }` ); + if ( settings.cmd4Dbg ) this.log.debug( `Processing high priority queue "Set" entry: ${ entry.accTypeEnumIndex } length: ${ this.highPriorityQueue.length }` ); this.inProgressSets ++; this.qSetValue( entry.accessory, entry.accTypeEnumIndex, entry.characteristicString, entry.timeout, entry.value, function ( error ) @@ -213,7 +211,7 @@ class Cmd4PriorityPollingQueue processHighPriorityGetQueue( entry ) { - if ( cmd4Dbg ) this.log.debug( `Processing high priority queue "Get" entry: ${ entry.accTypeEnumIndex } isUpdate: ${ entry.queueGetIsUpdate } length: ${ this.highPriorityQueue.length }` ); + if ( settings.cmd4Dbg ) this.log.debug( `Processing high priority queue "Get" entry: ${ entry.accTypeEnumIndex } isUpdate: ${ entry.queueGetIsUpdate } length: ${ this.highPriorityQueue.length }` ); this.inProgressGets ++; @@ -253,7 +251,7 @@ class Cmd4PriorityPollingQueue processEntryFromLowPriorityQueue( entry ) { - if ( cmd4Dbg ) this.log.debug( `Processing low priority queue entry: ${ entry.accTypeEnumIndex }` ); + if ( settings.cmd4Dbg ) this.log.debug( `Processing low priority queue entry: ${ entry.accTypeEnumIndex }` ); let queue = entry.accessory.queue; @@ -308,7 +306,7 @@ class Cmd4PriorityPollingQueue let cmd = self.state_cmd_prefix + self.state_cmd + " Get '" + self.displayName + "' '" + characteristicString + "'" + self.state_cmd_suffix; - if ( cmd4Dbg ) self.log.debug( `getValue: accTypeEnumIndex:( ${ accTypeEnumIndex } )-"${ characteristicString }" function for: ${ self.displayName } cmd: ${ cmd } timeout: ${ timeout }` ); + if ( settings.cmd4Dbg ) self.log.debug( `getValue: accTypeEnumIndex:( ${ accTypeEnumIndex } )-"${ characteristicString }" function for: ${ self.displayName } cmd: ${ cmd } timeout: ${ timeout }` ); let reply = "NxN"; @@ -415,11 +413,11 @@ class Cmd4PriorityPollingQueue self.log.warn( `getValue: Warning, Retrieving ${ characteristicString }, expected only one word value for: ${ self.displayName } of: ${ trimmedReply }` ); } - if ( cmd4Dbg ) self.log.debug( `getValue: ${ characteristicString } function for: ${ self.displayName } returned: ${ unQuotedReply }` ); + if ( settings.cmd4Dbg ) self.log.debug( `getValue: ${ characteristicString } function for: ${ self.displayName } returned: ${ unQuotedReply }` ); var transposed = transposeConstantToValidValue( CMD4_ACC_TYPE_ENUM.properties, accTypeEnumIndex, unQuotedReply ) - if ( cmd4Dbg && transposed != unQuotedReply ) self.log.debug( `getValue: ${ characteristicString } for: ${ self.displayName } transposed: ${ transposed }` ); + if ( settings.cmd4Dbg && transposed != unQuotedReply ) self.log.debug( `getValue: ${ characteristicString } for: ${ self.displayName } transposed: ${ transposed }` ); // Return the appropriate type, by seeing what it is @@ -434,7 +432,7 @@ class Cmd4PriorityPollingQueue return; } - if ( cmd4Dbg && properValue != transposed ) self.log.debug( `getValue: ${ characteristicString } for: ${ self.displayName } properValue: ${ properValue }` ); + if ( settings.cmd4Dbg && properValue != transposed ) self.log.debug( `getValue: ${ characteristicString } for: ${ self.displayName } properValue: ${ properValue }` ); // Success !!!! callback( 0, properValue ); @@ -493,7 +491,7 @@ class Cmd4PriorityPollingQueue if ( accessory.hV.statusMsg == "TRUE" ) self.log.info( chalk.blue( `Setting ${ self.displayName } ${ characteristicString }` ) + ` ${ value }` ); - if ( cmd4Dbg ) self.log.debug( `setValue: accTypeEnumIndex:( ${ accTypeEnumIndex } )-"${ characteristicString }" function for: ${ self.displayName } ${ value } cmd: ${ cmd } timeout: ${ timeout }` ); + if ( settings.cmd4Dbg ) self.log.debug( `setValue: accTypeEnumIndex:( ${ accTypeEnumIndex } )-"${ characteristicString }" function for: ${ self.displayName } ${ value } cmd: ${ cmd } timeout: ${ timeout }` ); // Execute command to Set a characteristic value for an accessory let child = exec( cmd, { timeout: timeout }, function ( error, stdout, stderr ) @@ -608,7 +606,7 @@ class Cmd4PriorityPollingQueue return false; } else { - if ( cmd4Dbg ) this.log.debug( `Unhandled lastTransactionType: ${ lastTransactionType } inProgressSets: ${ queue.inProgressSets } inProgressGets: ${ queue.inProgressGets } queueStarted: ${ queue.queueStarted } lowQueueLen: ${ queue.lowPriorityQueue.length } hiQueueLen: ${ queue.highPriorityQueue.length }` ); + if ( settings.cmd4Dbg ) this.log.debug( `Unhandled lastTransactionType: ${ lastTransactionType } inProgressSets: ${ queue.inProgressSets } inProgressGets: ${ queue.inProgressGets } queueStarted: ${ queue.queueStarted } lowQueueLen: ${ queue.lowPriorityQueue.length } hiQueueLen: ${ queue.highPriorityQueue.length }` ); } } @@ -679,7 +677,7 @@ class Cmd4PriorityPollingQueue return false; } else { - if ( cmd4Dbg ) this.log.debug( `Unhandled lastTransactionType: ${ lastTransactionType } inProgressSets: ${ queue.inProgressSets } inProgressGets: ${ queue.inProgressGets } queueStarted: ${ queue.queueStarted } lowQueueLen: ${ queue.lowPriorityQueue.length } hiQueueLen: ${ queue.highPriorityQueue.length }` ); + if ( settings.cmd4Dbg ) this.log.debug( `Unhandled lastTransactionType: ${ lastTransactionType } inProgressSets: ${ queue.inProgressSets } inProgressGets: ${ queue.inProgressGets } queueStarted: ${ queue.queueStarted } lowQueueLen: ${ queue.lowPriorityQueue.length } hiQueueLen: ${ queue.highPriorityQueue.length }` ); } } @@ -711,7 +709,7 @@ class Cmd4PriorityPollingQueue let self = entry.accessory; let queue = entry.accessory.queue; - if ( cmd4Dbg ) self.log.debug( `Scheduling Poll of index: ${ entry.accTypeEnumIndex } characteristic: ${ entry.characteristicString } for: ${ self.displayName } timeout: ${ entry.timeout } interval: ${ entry.interval }` ); + if ( settings.cmd4Dbg ) self.log.debug( `Scheduling Poll of index: ${ entry.accTypeEnumIndex } characteristic: ${ entry.characteristicString } for: ${ self.displayName } timeout: ${ entry.timeout } interval: ${ entry.interval }` ); // Clear polling if ( queue.listOfRunningPolls && @@ -723,7 +721,7 @@ class Cmd4PriorityPollingQueue // If the queue was busy/not available, schedule the entry at a later time if ( queue.processQueueFunc( LOW_PRIORITY_GET, queue, entry ) == false ) { - if ( cmd4Dbg ) self.log.debug( `processsQueue returned false` ); + if ( settings.cmd4Dbg ) self.log.debug( `processsQueue returned false` ); queue.scheduleLowPriorityEntry( entry ); } @@ -782,7 +780,7 @@ class Cmd4PriorityPollingQueue let lastAccessoryUUID = "" let allDoneCount = 0; - if ( cmd4Dbg ) this.log.debug( `enablePolling for the first time` ); + if ( settings.cmd4Dbg ) this.log.debug( `enablePolling for the first time` ); // If there is nothing in the lowPriorityQueue, we are dome. // Demo mode or Unit testing. @@ -799,19 +797,19 @@ class Cmd4PriorityPollingQueue allDoneCount ++; setTimeout( ( ) => { - if ( entryIndex == 0 && cmd4Dbg ) + if ( entryIndex == 0 && settings.cmd4Dbg ) if ( queue.queueType == constants.QUEUETYPE_WORM ) entry.accessory.log.debug( `Started staggered kick off of ${ queue.lowPriorityQueue.length } polled characteristics for queue: "${ entry.accessory.queue.queueName }"` ); else entry.accessory.log.debug( `Started staggered kick off of ${ queue.lowPriorityQueue.length } polled characteristics for "${ entry.accessory.displayName }"` ); - if ( cmd4Dbg ) entry.accessory.log.debug( `Kicking off polling for: ${ entry.accessory.displayName } ${ entry.characteristicString } interval:${ entry.interval }, staggered:${ staggeredDelays[ staggeredDelayIndex ] }` ); + if ( settings.cmd4Dbg ) entry.accessory.log.debug( `Kicking off polling for: ${ entry.accessory.displayName } ${ entry.characteristicString } interval:${ entry.interval }, staggered:${ staggeredDelays[ staggeredDelayIndex ] }` ); queue.scheduleLowPriorityEntry( entry ); if ( entryIndex == queue.lowPriorityQueue.length -1 ) { - if ( cmd4Dbg ) + if ( settings.cmd4Dbg ) if ( queue.queueType == constants.QUEUETYPE_WORM ) entry.accessory.log.debug( `All characteristics are now being polled for queue: "${ queue.queueName }"` ); else @@ -858,7 +856,7 @@ class Cmd4PriorityPollingQueue this.processQueueFunc = this.processWormQueue; // When not in debug mode, do not echo errors for the WoRm queue // as errors are handled through retries. - if ( ! cmd4Dbg ) + if ( ! settings.cmd4Dbg ) this.echoE = false; break; case constants.QUEUETYPE_STANDARD: @@ -969,7 +967,7 @@ var parseAddQueueTypes = function ( log, entrys ) if ( queueName == null ) throw new Error( `"${ constants.QUEUE }" not provided at index ${ entryIndex }` ); - if ( cmd4Dbg ) log.debug( `calling addQueue: ${ queueName } type: ${ queueType }` ); + if ( settings.cmd4Dbg ) log.debug( `calling addQueue: ${ queueName } type: ${ queueType }` ); addQueue( log, queueName, queueType ); } ); } diff --git a/test/fakeGato.js b/test/fakeGato.js index 7f479d3..1df9f07 100644 --- a/test/fakeGato.js +++ b/test/fakeGato.js @@ -76,6 +76,9 @@ describe('Testing FakeGato gets created', ( ) => // Put back the polling queues settings.listOfCreatedPriorityQueues = { }; + // MaxListenersExceededWarning: Possible EventEmitter memory leak detected + _api.removeAllListeners(); + }); it( `Test if Cmd4Platform exists`, function ( ) @@ -629,6 +632,76 @@ describe('Testing FakeGato gets created', ( ) => + done( ); + }); + + it( `Test FakeGato updateAccessoryAttribute creates a log for THERMO`, function( done ) + { + let platformConfig = + { + accessories: [ + { + type: "Thermostat", + name: "My_Thermostat", + displayName: "My_Thermostat", + currentHeatingCoolingState: "OFF", + targetHeatingCoolingState: "OFF", + currentTemperature: 22.2, + targetTemperature: 22.2, + temperatureDisplayUnits: "CELSIUS", + currentRelativeHumidity: 60, + targetRelativeHumidity: 60, + coolingThresholdTemperature: 21.4, + heatingThresholdTemperature: 20.2, + polling: [ + { characteristic: "currentTemperature" }, + { characteristic: "targetTemperature" } + ], + fakegato: { eve: "thermo", + currentTemp: "currentTemperature", + setTemp: "targetTemperature", + valvePosition: 0, + storage: "fs", + storagePath: ".homebridge/FakegatoStorage", + folder: "folderName", + keyPath: "/place/to/store/my/keys/" + }, + state_cmd: "./test/echoScripts/echo_ACTIVE" + } + ] + } + + let log = new Logger( ); + log.setBufferEnabled( true ); + log.setOutputEnabled( false ); + log.setDebugEnabled( false ); + + let cmd4Platform = new Cmd4Platform( log, platformConfig, _api ); + + cmd4Platform.discoverDevices( ); + + let cmd4Accessory = cmd4Platform.createdCmd4Accessories[0]; + + expect( cmd4Accessory ).to.be.a.instanceOf( Cmd4Accessory, `Fan is not an instance of Cmd4Accessory` ); + + + + assert.include( log.logBuf, `[39m Adding new platformAccessory: My_Thermostat` ); + assert.include( log.logBuf, `35mConfiguring platformAccessory: \u001b[39mMy_Thermostat` ); + assert.equal( 2, log.logLineCount, ` Unexpected number of stdout lines: ${ log.logBuf }` ); + //assert.equal( "", log.logBuf, ` Unexpected stdout: ${ log.logBuf }` ); + assert.equal( 0, log.errLineCount, ` setCachedValue logged lines than one: ${ log.errBuf }` ); + + log.reset( ); + log.setBufferEnabled( true ); + log.setOutputEnabled( false ); + log.setDebugEnabled( true ); + + cmd4Accessory.updateAccessoryAttribute( CMD4_ACC_TYPE_ENUM.CurrentTemperature, 18); + + + assert.include( log.logBuf, `[39m \u001b[90mLogging currentTemp: 18 setTemp:22.2 valvePosition:0`, `Incorrect stdout: ${ log.logBuf }` ); + done( ); }); }); diff --git a/test/isAccDirective.js b/test/isAccDirective.js index e876b78..7ae8d7b 100644 --- a/test/isAccDirective.js +++ b/test/isAccDirective.js @@ -35,12 +35,14 @@ describe( `Test isAccDirective`, ( ) => it( `isAccDirective should NOT identify an uppercase type `, ( ) => { let ucType = CMD4_ACC_TYPE_ENUM.accEnumIndexToUC( CMD4_ACC_TYPE_ENUM.On ); - assert.isNull( isAccDirective( ucType ), `Expected result to be null` ); + let rc = isAccDirective( ucType ); + assert.isNull( rc.accTypeEnumIndex, `Expected result to be null` ); }); it( `isAccDirective should NOT identify an unknown type `, ( ) => { let type = "Blast"; - assert.isNull( isAccDirective( type ), `Expected result to be null` ); + let rc = isAccDirective( type ); + assert.isNull( rc.accTypeEnumIndex, `Expected result to be null` ); }); it( `isAccDirective should identify an uppercase type if upperCase is checked`, ( ) => { diff --git a/utils/Logger.js b/utils/Logger.js index 39991ca..66cb214 100644 --- a/utils/Logger.js +++ b/utils/Logger.js @@ -57,6 +57,7 @@ class Logger setDebugEnabled( enabled = true ) { this.debugEnabled = enabled; + settings.cmd4Dbg = enabled; } setOutputEnabled( enabled = true ) { diff --git a/utils/isAccDirective.js b/utils/isAccDirective.js index 1baba87..6c5ebd0 100644 --- a/utils/isAccDirective.js +++ b/utils/isAccDirective.js @@ -12,10 +12,17 @@ let CMD4_ACC_TYPE_ENUM = require( "../lib/CMD4_ACC_TYPE_ENUM" ).CMD4_ACC_TYPE_EN function isAccDirective( type, allowUpper = false ) { + // For backward compatability of testStoredValueForIndex of FakeGato + // we must return a null accTypeIndex, which should be checked instead + // of just rc. + let defaultRc = { "type": type, + "accTypeEnumIndex": null + }; + if ( ! type ) { console.warn( "No parameter passed to isCmd4Directive" ); - return null; + return defaultRc; } let accTypeEnumIndex; @@ -29,7 +36,7 @@ function isAccDirective( type, allowUpper = false ) // Note: There are othes like WiFi ... but nobody uses them thankfully ! if ( allowUpper == true ) { - accTypeEnumIndex = CMD4_ACC_TYPE_ENUM.properties.indexOfEnum( i => i.type === type ) + accTypeEnumIndex = CMD4_ACC_TYPE_ENUM.properties.indexOfEnum( i => i.type === type ); // We return the correct lower case if ( accTypeEnumIndex >= 0 ) @@ -37,7 +44,7 @@ function isAccDirective( type, allowUpper = false ) "accTypeEnumIndex": accTypeEnumIndex }; } - return null; + return defaultRc; }