Skip to content

Commit a8da012

Browse files
committed
fixed error on erroneous config & improved logging on config errors
1 parent b8c92ef commit a8da012

6 files changed

Lines changed: 105 additions & 25 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "homebridge-hsd",
33
"displayName": "Homebridge Gira Homeserver URL-Endpoint",
4-
"version": "1.1.7",
4+
"version": "1.1.9",
55
"description": "Plugin to access KNX bus via Gira Homeserver",
66
"license": "MIT",
77
"keywords": [

src/hs.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,24 @@ export class HomeServerConnector {
8585
}
8686
this.logger.info('hs.ts | HomeServerConnector | connect > Current connection state is %d', this._connState);
8787

88+
if (hsIp === '' || hsPort <= 0 || user === '' || pw === '') {
89+
this.logger.error('hs.ts | HomeserverConnector | IP, Port, user, or password are invalid. Check config!');
90+
return;
91+
}
92+
8893
this._hsIp = hsIp;
8994
this._hsPort = hsPort;
9095
this._user = user;
9196
this._pw = pw;
9297
const prot = 'wss';
9398
const url = prot + '://' + this._hsIp + ':' + this._hsPort + '/endpoints/ws?authorization=' + encodeURIComponent(btoa(user + ':' + pw));
94-
this._ws = new WebSocket.WebSocket(url, { rejectUnauthorized: false });
95-
this._connState = CONNECTION_STATE.CONNECTING;
99+
try {
100+
this._ws = new WebSocket.WebSocket(url, { rejectUnauthorized: false });
101+
this._connState = CONNECTION_STATE.CONNECTING;
102+
} catch (error) {
103+
this.logger.error('hs.ts | HomeserverConnector | Error: ' + error);
104+
return;
105+
}
96106

97107
this._ws.on('open', () => {
98108
this._connState = CONNECTION_STATE.OPEN;

src/hsdPlatform.ts

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { HsdAccessory } from './hsdAccessory';
66

77
import { PLATFORM_NAME, PLUGIN_NAME } from './settings';
88
import { HsdPlatformConfig } from './config';
9+
// import { exec } from 'child_process';
10+
// import { error } from 'console';
911

1012
export 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);

src/hsdPlatformAccessory.ts

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,60 @@
1-
import { PlatformAccessory, PlatformConfig } from 'homebridge';
1+
import { Logger, PlatformAccessory, PlatformConfig } from 'homebridge';
22
import { HsdAccessoryConfig, HsdPlatformConfig } from './config';
33

44
export type HsdPlatformAccessory = PlatformAccessory<HsdAccessoryConfig>;
55

6-
export const isHsdPlatformConfig = (config: PlatformConfig): config is HsdPlatformConfig => {
7-
let isConfig = true;
8-
isConfig = isConfig && 'hsIp' in config;
9-
isConfig = isConfig && 'hsUserName' in config;
10-
isConfig = isConfig && 'hsUserPw' in config;
11-
isConfig = isConfig && 'hsPort' in config;
6+
export const isHsdPlatformConfig = (config: PlatformConfig, logger: Logger): config is HsdPlatformConfig => {
7+
if ('hsIp' in config) {
8+
const ipv4Regex = /^(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}$/;
9+
if (!ipv4Regex.test(config['hsIp'])) {
10+
logger.error(`isHsdPlatformConfig | ${config['hsIp']} is not a valid IP`);
11+
return false;
12+
}
13+
} else {
14+
logger.error('isHsdPlatformConfig | IP definition missing.');
15+
return false;
16+
}
1217

13-
return isConfig;
18+
if ('hsPort' in config) {
19+
const isPort = (config['hsPort'] >= 1 && config['hsPort'] <= 65535 && Number.isInteger(config['hsPort']));
20+
if (!isPort) {
21+
logger.error(`isHsdPlatformConfig | Port ${config['hsPort']} of tpye ${typeof config['hsPort']} is not a valid port.`);
22+
return false;
23+
}
24+
} else {
25+
logger.error('isHsdPlatformConfig | Port is missing.');
26+
return false;
27+
}
28+
29+
if ('accessories' in config) {
30+
if (config['accessories'].lenght === 0) {
31+
logger.error('isHsdPlatformConfig | Acessories missing.');
32+
return false;
33+
}
34+
} else {
35+
logger.error('isHsdPlatformConfig | Acessories missing.');
36+
return false;
37+
}
38+
39+
if ('hsUserName' in config) {
40+
if (config['hsUserName'] === '') {
41+
logger.error('isHsdPlatformConfig | User Name is empty.');
42+
return false;
43+
}
44+
} else {
45+
logger.error('isHsdPlatformConfig | User Name definiton missing.');
46+
return false;
47+
}
48+
49+
if ('hsUserPw' in config) {
50+
if (config['hsUserPw'] === '') {
51+
logger.error('isHsdPlatformConfig | Password is empty.');
52+
return false;
53+
}
54+
} else {
55+
logger.error('isHsdPlatformConfig | Password definiton missing.');
56+
return false;
57+
}
58+
59+
return true;
1460
};

src/service/characteristic/CurrentDoorState.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ export const addCurrentDoorStateCharacteristic = (api: API,
1414
}, getEndpoint);
1515

1616
currentDoorStat.onGet(async () => {
17-
return Number(hsd.getCo(getEndpoint));
17+
const ret = hsd.getCo(getEndpoint);
18+
if (typeof(ret) === 'object') {
19+
return Promise.reject(0);
20+
//return Promise.reject(new Error('CurrentDoorState.ts | CurrentDoorStat.onGet | Invalid return object!'));
21+
}
22+
return Number(ret);
1823
});
1924
};
2025

src/service/characteristic/TargetDoorState.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ export const addTargetDoorStateCharacteristic = (api: API,
1717
targetDoorState.onGet(async () => {
1818
const ret = hsd.getCo(getEndpoint);
1919
if (typeof(ret) === 'object') {
20-
return 99;
20+
return Promise.reject(0);
21+
// return Promise.reject(new Error('TargetDoorState.ts | targetDoorState.onGet | Invalid return object!'));
2122
}
2223
return Number(ret);
2324
});

0 commit comments

Comments
 (0)