Skip to content

Commit

Permalink
Merge pull request #15 from Grizzelbee/Development_v1.0.5
Browse files Browse the repository at this point in the history
Development v1.0.5
  • Loading branch information
Grizzelbee authored Feb 14, 2020
2 parents 7f81f3a + bc42916 commit 8a24262
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 31 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
![Logo](admin/mielecloudservice.png)
# ioBroker.MieleCloudService
![Number of Installations](http://iobroker.live/badges/mielecloudservice-installed.svg)
![Number of Installations](http://iobroker.live/badges/mielecloudservice-installed.svg)
[![Downloads](https://img.shields.io/npm/dm/iobroker.mielecloudservice.svg)](https://www.npmjs.com/package/iobroker.mielecloudservice)
![Number of Installations](http://iobroker.live/badges/mielecloudservice-stable.svg)
[![NPM version](https://img.shields.io/npm/v/iobroker.mielecloudservice.svg)](https://www.npmjs.com/package/iobroker.mielecloudservice)
[![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat)](https://github.com/grizzelbee/iobroker.mielecloudservice/blob/master/LICENSE)
[![Dependency Status](https://img.shields.io/david/Grizzelbee/iobroker.mielecloudservice.svg)](https://david-dm.org/Grizzelbee/iobroker.mielecloudservice)
[![Known Vulnerabilities](https://snyk.io/test/github/Grizzelbee/ioBroker.mielecloudservice/badge.svg)](https://snyk.io/test/github/Grizzelbee/ioBroker.mielecloudservice)

[![Travis-CI](http://img.shields.io/travis/Grizzelbee/ioBroker.mielecloudservice/master.svg)](https://travis-ci.com/Grizzelbee/ioBroker.mielecloudservice)
[![NPM](https://nodei.co/npm/iobroker.mielecloudservice.png?downloads=true)](https://nodei.co/npm/iobroker.mielecloudservice/)

**Tests:**: [![Travis-CI](http://img.shields.io/travis/Grizzelbee/ioBroker.mielecloudservice/master.svg)](https://travis-ci.com/Grizzelbee/ioBroker.mielecloudservice)


=================
## Description
This adapter is for retrieving information about all your Miele@Home devices from the official Miele 3rd-party API.
Expand All @@ -34,6 +33,11 @@ To install, excecute the following:

## Changelog

### 1.0.5 (2020-02-14)
* (grizzelbee) removed node-schedule as a dependency
* (grizzelbee) implemented scheduling via setTimeout, which raises the opportunity
to schedule with less than a minute in the future

### 1.0.4 (2020-02-12)
* (grizzelbee) removed unneeded setTimeout from main
* (grizzelbee) Clearing scheduler on unload of adapter
Expand Down Expand Up @@ -97,6 +101,7 @@ To install, excecute the following:
## Next Steps
* New: (longer) poll interval when no device is active
* New: Sleeptime for complete inactivity (e.g. at night)
* New: Support for Actions

## Documentation
There are some datapoints avaliable in 2 kinds. As a human readable text and as a number.
Expand Down
14 changes: 13 additions & 1 deletion io-package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
{
"common": {
"name": "mielecloudservice",
"version": "1.0.4",
"version": "1.0.5",
"news": {
"1.0.5": {
"en": "removed dependency 'node-schedule' and realized scheduling via setTimeout",
"de": "Abhängigkeit auf 'node-schedule' wurde entfernt und die Planung über setTimeout realisiert",
"ru": "удалено расписание узлов зависимостей и реализовано планирование через setTimeout",
"pt": "agendamento de nó de dependência removido e agendamento realizado via setTimeout",
"nl": "afhankelijkheidsknooppuntschema verwijderd en planning gerealiseerd via setTimeout",
"fr": "suppression de la planification des nœuds de dépendance et planification réalisée via setTimeout",
"it": "rimossa la pianificazione dei nodi di dipendenza e la pianificazione realizzata tramite setTimeout",
"es": "Se eliminó la programación de nodo de dependencia y se realizó la programación a través de setTimeout",
"pl": "usunięto harmonogram węzłów zależności i zrealizowano planowanie za pomocą setTimeout",
"zh-cn": "删除依赖关系节点计划并通过setTimeout实现计划"
},
"1.0.4": {
"en": "removed unneeded setTimeout, clearing scheduler on unload",
"de": "nicht benötigtes setTimeout entfernt, Scheduler wird beim Entladen bereinigt",
Expand Down
47 changes: 26 additions & 21 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
/* jshint strict:true */
/* jslint esversion: 6 */
/* jslint node: true */

/**
*
* mieleCloudService Adapter for ioBroker
Expand All @@ -15,15 +14,15 @@
const BaseURL = 'https://api.mcs3.miele.com/';
const adapterName = require('./package.json').name.split('.').pop();
const utils = require('@iobroker/adapter-core'); // Get common adapter utils
const schedule = require('node-schedule');
const request = require('request');
const salt = 'Zgfr56gFe87jJOM';

// Global Variables (all uppercase)
let ACCESS_TOKEN;
let REFRESH_TOKEN;
let ADAPTER;
let SCHEDULER;
let pollTimeout;
let expiryDate;

function startadapter(options) {
options = options || {};
Expand All @@ -33,8 +32,10 @@ function startadapter(options) {
// is called when adapter shuts down - callback has to be called under any circumstances!
unload: function (callback) {
try {
ADAPTER.log.info('Canceling all scheduled events.');
SCHEDULER.cancel();
if (pollTimeout) {
ADAPTER.log.info('Clearing Timeout: pollTimeout');
clearTimeout(pollTimeout);
}
ADAPTER.setState('info.connection', false);
ADAPTER.log.info('Unloading MieleCloudService...');
callback();
Expand Down Expand Up @@ -449,20 +450,21 @@ function addMieleDeviceActions(path, currentDevice){
// Create ACTIONS folder
createExtendObject(path + '.ACTIONS', {
type: 'channel',
common: {name: 'Currently supported Actions for this device.', read: true, write: true},
common: {name: 'Supported Actions for this device.', read: true, write: true},
native: {}
});
// APIGetActions(REFRESH_TOKEN, ACCESS_TOKEN, currentDevice, callback);
}

function refreshMieledata(){
function refreshMieledata(err){
APIGetDevices(REFRESH_TOKEN, ACCESS_TOKEN, ADAPTER.config.locale, function (err, data, atoken, rtoken) {
if (err) {
ADAPTER.log.error('*** Error during APIGetDevices. ***');
ADAPTER.log.error('Errormessage: ' + err);
}else{
splitMieleDevices(data);
}
return err;
});
}

Expand All @@ -487,15 +489,13 @@ function main() {
// put tokens to global variables
ACCESS_TOKEN = access_token;
REFRESH_TOKEN = refresh_token;
// do initial population of adapter immediatly
ADAPTER.log.debug("Querying Devices from API initially.");
refreshMieledata();
// start refresh scheduler with interval from adapters config
ADAPTER.log.info('Starting Polltimer with a ' + ADAPTER.config.pollinterval + ' Minutes interval.');
SCHEDULER = schedule.scheduleJob('*/' + ADAPTER.config.pollinterval + ' * * * *', function () {
// start refresh scheduler with interval from adapters config
pollTimeout= setTimeout(function schedule() {
ADAPTER.log.debug("Updating device states (polling API scheduled).");
refreshMieledata();
});
refreshMieledata(err);
pollTimeout= setTimeout(schedule , ADAPTER.config.pollinterval * 60000);
} , 100);
}
});
} else {
Expand Down Expand Up @@ -530,18 +530,22 @@ function APIGetAccessToken(callback) {
ADAPTER.log.debug('options OAuth2-VG: [' + options.form.vg + ']');
ADAPTER.log.debug('config API Language: [' + ADAPTER.config.locale + ']');
ADAPTER.log.debug('options Miele_account: [' + options.form.username + ']');
// ADAPTER.log.debug('options Miele_Password: ['+ ADAPTER.config.Miele_pwd + ']');
ADAPTER.log.debug('options Client_ID: [' + options.form.client_id + ']');
//ADAPTER.log.debug('options Client_Secret: [' + options.form.client_secret + ']');
//ADAPTER.log.debug('options Raw: [' + JSON.stringify(options) + ']');
// Logging of passwords has been commended out intentionally. May be enabled again by user for debugging purposes
// ADAPTER.log.debug('options Miele_Password: ['+ ADAPTER.config.Miele_pwd + ']');
// ADAPTER.log.debug('options Client_Secret: [' + options.form.client_secret + ']');

request(options, function (error, response, body) {
if (response.statusCode === 200) {
let P = JSON.parse(body);
// let expiry = now + expiry in seconds
expiryDate = new Date();
expiryDate.setSeconds(expiryDate.getSeconds() + P.expires_in );
ADAPTER.log.info('Got new Access-Token!');
ADAPTER.log.debug('New Access-Token: [' + P.access_token + ']');
ADAPTER.log.debug('Access-Token-Type: [' + P.token_type + ']');
ADAPTER.log.info('Access-Token expires in: [' + P.expires_in + '] Seconds (='+ P.expires_in/3600 +'hours = '+ P.expires_in/86400 +'days)');
ADAPTER.log.info('Access-Token expires at: [' + expiryDate.toString() + ']');
ADAPTER.log.debug('New Refresh-Token: [' + P.refresh_token + ']');
// ADAPTER.log.debug('plain body: [' + body + ']');
ADAPTER.setState('info.connection', true);
Expand Down Expand Up @@ -588,7 +592,7 @@ function APIRefreshToken(callback) {
ADAPTER.log.debug('Access-Token-Type: [' + P.token_type + ']');
ADAPTER.log.Info('Access-Token expires in: [' + P.expires_in + '] Seconds (='+ P.expires_in/3600 +'hours = '+ P.expires_in/86400 +'days)');
ADAPTER.log.debug('New Refresh-Token: [' + P.refresh_token + ']');
ADAPTER.log.silly('plain body: [' + body + ']');
// ADAPTER.log.debug('plain body: [' + body + ']');
return callback(false, P.access_token, P.refresh_token);
} else {
ADAPTER.log.error('*** Error during APIRefreshToken ***');
Expand Down Expand Up @@ -650,8 +654,8 @@ function APIGetDevices(Refresh_Token, Access_Token, locale, callback) {
if (!err) {
return callback(err, data, atoken, rtoken)
} else {
ADAPTER.log.warn("Error during function APIGetDevices.");
}
ADAPTER.log.error("Error during function APIGetDevices.");
ADAPTER.log.error('Errormessage : ' + err); }
});
}

Expand All @@ -662,7 +666,8 @@ function APIGetActions(Refresh_Token, Access_Token, device, callback) {
ADAPTER.log.debug(`Got DeviceActions: [${JSON.stringify(data)}]`);
return callback(err, data, atoken, rtoken)
} else {
ADAPTER.log.warn("Error during function APIGetActions.");
ADAPTER.log.error("Error during function APIGetActions.");
ADAPTER.log.error('Errormessage : ' + err);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "iobroker.mielecloudservice",
"version": "1.0.4",
"version": "1.0.5",
"description": "Integrates your MieleCloudService (aka Miele@Home) Devices to ioBroker",
"author": {
"name": "grizzelbee",
Expand All @@ -21,8 +21,9 @@
"keywords": [
"ioBroker",
"mieleathome",
"Smart Home",
"miele@home",
"Miele",
"Smart Home",
"household",
"home automation"
],
Expand All @@ -35,7 +36,6 @@
},
"dependencies": {
"@iobroker/adapter-core": "^2.2.1",
"node-schedule": "^1.3.2",
"request": "^2.88.0",
"semver": "^7.1.2"
},
Expand Down

0 comments on commit 8a24262

Please sign in to comment.