@@ -6,6 +6,8 @@ import { HsdAccessory } from './hsdAccessory';
66
77import { PLATFORM_NAME , PLUGIN_NAME } from './settings' ;
88import { HsdPlatformConfig } from './config' ;
9+ // import { exec } from 'child_process';
10+ // import { error } from 'console';
911
1012export class HsdPlatform implements DynamicPlatformPlugin {
1113 private cachedAccessories : Map < string , HsdPlatformAccessory > = new Map ( ) ;
@@ -20,29 +22,41 @@ export class HsdPlatform implements DynamicPlatformPlugin {
2022 } ;
2123
2224 private async connect ( ) : Promise < HomeServerConnector > {
23- const link = HomeServerConnector . getInstance ( this . logger , this . hsdAccessories ) ;
24- link . connect ( this . config . hsIp , this . config . hsPort , this . config . hsUserName , this . config . hsUserPw ) ;
25- this . logger . info ( `hsdPlatform.ts | HsdPlatform | HSD IP gateway ${ this . config . hsIp } connection established.` ) ;
25+ if ( ! isHsdPlatformConfig ( this . config , this . logger ) ) {
26+ return Promise . reject ( new Error ( 'hsdPlatform.ts | HsdPlatform | connect | Invalid platfrom config. Plugin not working!' ) ) ;
27+ }
28+ try {
29+ const link = HomeServerConnector . getInstance ( this . logger , this . hsdAccessories ) ;
2630
27- this . api . on ( APIEvent . SHUTDOWN , async ( ) => {
28- link . disconnect ( ) ;
29- this . logger . warn ( `hsdPlatform.ts | HsdPlatform | hsd IP gateway ${ this . config . hsdIp } connection closed.` ) ;
30- } ) ;
31+ link . connect ( this . config . hsIp , this . config . hsPort , this . config . hsUserName , this . config . hsUserPw ) ;
32+ this . logger . info ( `hsdPlatform.ts | HsdPlatform | HSD IP gateway ${ this . config . hsIp } connection established.` ) ;
33+
34+ this . api . on ( APIEvent . SHUTDOWN , async ( ) => {
35+ link . disconnect ( ) ;
36+ this . logger . warn ( `hsdPlatform.ts | HsdPlatform | hsd IP gateway ${ this . config . hsdIp } connection closed.` ) ;
37+ } ) ;
3138
32- return link ;
39+ return link ;
40+ } catch ( error ) {
41+ this . logger . info ( `hsdPlatform.ts | HsdPlatform | Error: ${ error } ` ) ;
42+ return Promise . reject ( new Error ( `hsdPlatform.ts | HsdPlatform | connect | Error: ${ error } . Plugin not working!` ) ) ;
43+ }
3344 }
3445
3546 public constructor ( private logger : Logging , config : PlatformConfig , private api : API ) {
3647 this . logger . debug ( 'hsdPlatform.ts | HsdPlatform | Constructor' ) ;
37- if ( isHsdPlatformConfig ( config ) ) {
38-
48+ if ( isHsdPlatformConfig ( config , logger ) ) {
3949 this . config = config ;
4050 } else {
41- this . logger . error ( 'hsdPlatform.ts | HsdPlatform | Invalid configuration' ) ;
51+ this . logger . error ( 'hsdPlatform.ts | HsdPlatform | Invalid configuration. Plugin not working ' ) ;
4252 }
4353
4454 api . on ( APIEvent . DID_FINISH_LAUNCHING , async ( ) => {
45- this . configureAccessories ( await this . connect ( ) ) ;
55+ try {
56+ this . configureAccessories ( await this . connect ( ) ) ;
57+ } catch ( error ) {
58+ this . logger . error ( `hsdPlatform.ts | api.on | Error: ${ error } ` ) ;
59+ }
4660 } ) ;
4761 }
4862
@@ -51,6 +65,10 @@ export class HsdPlatform implements DynamicPlatformPlugin {
5165 }
5266
5367 private configureAccessories ( hsd : HomeServerConnector ) : void {
68+ if ( ! isHsdPlatformConfig ( this . config , this . logger ) ) {
69+ this . logger . error ( 'hsdPlatform.ts | HsdPlatform | Invalid configuration. Plugin not working' ) ;
70+ return ;
71+ }
5472 for ( const config of this . config . accessories ) {
5573 try {
5674 const hsdAccessory = new HsdAccessory ( config , this . logger , hsd , this . api ) ;
0 commit comments