Skip to content

Commit

Permalink
Merge pull request #28 from QuickSander/beta
Browse files Browse the repository at this point in the history
Silently reconnect on lost Miele event server connection
  • Loading branch information
QuickSander authored Mar 23, 2021
2 parents e5fdb5d + 3e62592 commit 7ada37f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[![verified-by-homebridge](https://badgen.net/badge/homebridge/verified/purple)](https://github.com/homebridge/homebridge/wiki/Verified-Plugins)
![Build and Lint](https://github.com/QuickSander/homebridge-mieleathome/workflows/Build%20and%20Lint/badge.svg)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
[![npm version](https://badge.fury.io/js/homebridge-mieleathome.svg)](https://badge.fury.io/js/homebridge-mieleathome)
[![npm (beta))](https://img.shields.io/npm/v/homebridge-mieleathome/beta.svg)](https://www.npmjs.com/package/homebridge-mieleathome)
[![npm (latest)](https://img.shields.io/npm/v/homebridge-mieleathome/latest)](https://www.npmjs.com/package/homebridge-mieleathome)

![Miele + Homebridge](./homebridge-ui/public/miele-homebridge.png "Miele + Homebridge")

Expand All @@ -19,7 +20,7 @@ It (currently) requires a
- Miele Washing machine (e.g. WCG370, WMV960).
- Miele Dryer (e.g. TMV840WP)
- Miele Dishwasher.
- Experimental Miele Fridge and Fridge Freezer combination support.
- Miele Fridge and Fridge Freezer combination.

## Features
- Easy setup: guided process to retrieve token via OAuth2 from Miele.
Expand All @@ -28,8 +29,11 @@ It (currently) requires a
- Start / stop (dish) washing machine program (with an option to disable to prevent unintentional program stop requests).
- Remaining run time.
- Washing machine / dish washer program target temperature.
- Fridge / Freezer target and current temperature
- Set Fridge / Freezer target temperature.
- HomeKit identify support via Homebridge log.


## Breaking changes
### Versions > 2.8.0
- The introduction of event based updating removed the need for the _Poll interval_ setting. This option can be removed from
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
"release": {
"branches": [
"master",
"next"
"next",
"beta"
]
}
}
41 changes: 31 additions & 10 deletions src/mieleBasePlatformAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,13 @@ export abstract class MieleBasePlatformAccessory {

let reconnectTimeout = this.platform.reconnectEventServerInterval;
if(this.platform.reconnectEventServerInterval <=0) {
this.platform.log.warn('Incorrect \'reconnectEventServerInterval\' specified in yur configuration. '+
`Value: ${reconnectTimeout}. Should be >0. Using default value ${DEFAULT_RECONNECT_EVENT_SERVER_INTERVAL_MIN}[min] instead.`);
this.platform.log.warn('Incorrect \'reconnectEventServerInterval\' specified in your configuration. '+
`Value: ${reconnectTimeout} should be >0. Using default value ${DEFAULT_RECONNECT_EVENT_SERVER_INTERVAL_MIN}[min] instead.`);
reconnectTimeout = DEFAULT_RECONNECT_EVENT_SERVER_INTERVAL_MIN;
}

const reconnectTimeoutMs=reconnectTimeout*60*1000;
const reconnectTimeoutMs= (reconnectTimeout*60*1000);
setInterval(this.connectToEventServer.bind(this), reconnectTimeoutMs);

}

//-----------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -96,14 +95,35 @@ export abstract class MieleBasePlatformAccessory {
'Connection with Miele event server succesfully (re-)established.');
};

this.eventSource.addEventListener('ping', (_event) => {
//this.platform.log.debug(`${this.accessory.context.device.displayName}: Event '${event.type}' received.`);
});

this.eventSource.onerror = (error) => {
this.eventSource?.close();
this.platform.log.error(`${this.accessory.context.device.displayName}: Error received from Miele event server: `+
`'${JSON.stringify(error)}'`);
this.mainService.setCharacteristic(this.platform.Characteristic.StatusFault, this.platform.Characteristic.StatusFault.GENERAL_FAULT);

this.platform.log.info(`${this.accessory.context.device.displayName}: Will attempt to reconnect to the Miele event server after`+
` ${EVENT_SERVER_RECONNECT_DELAY_S}[s].`);
interface IError{
message: string;
status: number;
type: string;
}

const errorObj = (<IError><unknown>error);

// If Miele closed the connection on their end, EventSource raises an empty error object.
if(!errorObj.status) {
this.platform.log.debug(`${this.accessory.context.device.displayName}: Miele event server `+
`connection lost. Auto-reconnect after ${EVENT_SERVER_RECONNECT_DELAY_S}[s]`);
} else {
this.platform.log.error(`${this.accessory.context.device.displayName}: Error received from Miele event server. `+
`Status: ${errorObj.status}. Message: '${errorObj.message}'`);
this.mainService.setCharacteristic(this.platform.Characteristic.StatusFault,
this.platform.Characteristic.StatusFault.GENERAL_FAULT);

this.platform.log.info(`${this.accessory.context.device.displayName}: Will attempt to reconnect to the Miele event server after`+
` ${EVENT_SERVER_RECONNECT_DELAY_S}[s].`);
}

setTimeout(()=> {
this.connectToEventServer();
}, EVENT_SERVER_RECONNECT_DELAY_S*1000);
Expand All @@ -113,7 +133,8 @@ export abstract class MieleBasePlatformAccessory {

//-----------------------------------------------------------------------------------------------
protected identify(_value: CharacteristicValue, _callback: CharacteristicSetCallback) {
this.platform.log.info(`Identify requested for: ${this.accessory.context.device.modelNumber} `+
this.platform.log.info(`Identify requested for: ${this.accessory.context.device.displayName} `+
`(${this.accessory.context.device.modelNumber}) `+
`with serial number: ${this.accessory.context.device.uniqueId}`);
}

Expand Down

0 comments on commit 7ada37f

Please sign in to comment.