Skip to content

Commit 9e69767

Browse files
committed
Adapted writing attributes to restrictions of OpenAPI
1 parent 66ba5e7 commit 9e69767

2 files changed

Lines changed: 37 additions & 16 deletions

File tree

README.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,19 @@
1414
This adapter logs into the web API of [Alpha ESS](https://www.alphaess.com/) and retrieves information for your Alpha ESS equipment.\
1515
Depending on your Alpha ESS product, it is possible to get realtime data and configuration data for your equipment. Which data points are returned by the API depends on your Alpha ESS equipment.
1616

17-
This adapter is based on the great work of [Charles Gillanders](https://github.com/CharlesGillanders/alphaess), who reverse engineered the Alpha ESS Web API. This is an internal API which may be changed at any time by Alpha ESS.
18-
19-
Currently this adapter creates a state with a hopefully self explaining name for each data point, which I was able to identify.\
20-
All other data points are ignored. During adapter start these data points are logged as info message.
21-
22-
Basically, it is possible to change selected configuration settings using the Alpha ESS Web API. This is not implemented yet.
17+
This adapter supports two APIs, the internal Alpha ESS Web API, which may be changed at any time by Alpha ESS, and the Alpha ESS Open API, which provides less functionality but is on official and documented API to Alpha ESS devices.
18+
19+
Currently this adapter creates a state with a hopefully self explaining name for each suppoerted data point.\
20+
All other data points are ignored. During adapter start these data points are logged as debug message.
21+
22+
Staring with version 1.0.0-alpha.5, the quality attribute of each state is set accordingly to its status:
23+
| Quality | meaning |
24+
|:--------|:--------------------------------------------------|
25+
|0x00 |OK and up to date |
26+
|0x01 |value not updated due to unknown reason, see log |
27+
|0x02 |problem with online connection for this data point |
28+
|0x12 |adapter disconnected or stopped |
29+
|0x44 |API returned error or internal error, see log |
2330

2431
## Settings:
2532

main.js

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -640,17 +640,24 @@ class OpenAPI {
640640
* @param {string} group
641641
*/
642642
async writeConfigInfo(group) {
643+
let nextReadTimeout = ReadAfterWriteTimeoutIntervalInS;
643644
try {
644645
this.adapter.stopGroupWriteTimeout(group);
645646
this.adapter.stopGroupTimeout(group);
646647

647648
this.adapter.log.debug('Writing ' + group + ' data...');
648649

649650
const body = {};
650-
const gidx = this.adapter.getStateInfoList().findIndex((/** @type {{ Group: string; }} */ i) => i.Group == group);
651+
const gidx = this.stateInfoList.findIndex((/** @type {{ Group: string; }} */ i) => i.Group == group);
651652
if (gidx >= 0) {
652-
const groupStates = this.adapter.getStateInfoList()[gidx].states;
653+
654+
const groupInfo = this.stateInfoList[gidx];
655+
nextReadTimeout = this.adapter.jsonConfig.items[groupInfo.intervalName].min * groupInfo.intervalFactor;
656+
657+
const groupStates = this.stateInfoList[gidx].states;
653658
for (let i = 0; i < groupStates.length; i++) {
659+
// Ensure that watchdog does not fire, because timeout may be delayed
660+
groupStates[i].lastUpdateTs = Date.now();
654661
this.adapter.log.debug('State ' + group + '.' + groupStates[i].alphaAttrName + ' - ' + groupStates[i].id);
655662
const state = await this.adapter.getStateAsync(group + '.' + groupStates[i].id);
656663
let value = null;
@@ -676,7 +683,7 @@ class OpenAPI {
676683
this.adapter.log.error('Writing data for group ' + group + ': Exception occurred: ' + e);
677684
await this.handleError(this.emptyBody, group);
678685
}
679-
this.adapter.startGroupTimeout(ReadAfterWriteTimeoutIntervalInS, group);
686+
this.adapter.startGroupTimeout(nextReadTimeout, group);
680687
}
681688

682689
/**
@@ -1886,6 +1893,8 @@ class AlphaEss extends utils.Adapter {
18861893
this.setObjectNormalAsync = this.setObjectNotExistsAsync.bind(this);
18871894
this.setObjectMigrationAsync = this.setObjectAsync.bind(this);
18881895

1896+
this.watchDogFunction = this.watchDog.bind(this);
1897+
18891898
this.createdStates = [];
18901899

18911900
this.errorCount = 0;
@@ -2004,7 +2013,7 @@ class AlphaEss extends utils.Adapter {
20042013
catch (e) {
20052014
this.log.error('onReady Exception occurred: ' + e);
20062015
}
2007-
this.watchDogIntervalHandle = this.setInterval(this.watchDog.bind(this), WATCHDOG_TIMER);
2016+
this.watchDogIntervalHandle = this.setInterval(this.watchDogFunction, WATCHDOG_TIMER);
20082017
this.log.debug('Watchdog interval started!');
20092018
}
20102019

@@ -2418,9 +2427,14 @@ class AlphaEss extends utils.Adapter {
24182427
const states = await this.getStatesAsync(group + '.*');
24192428
for (const sid in states) {
24202429
const newState = states[sid];
2421-
newState.q = q;
2422-
this.log.debug(`Set state ${sid} to val: ${newState.val}; q: ${newState.q}`);
2423-
await this.setStateAsync(sid, newState);
2430+
if (newState.ack) {
2431+
newState.q = q;
2432+
this.log.debug(`Set state ${sid} to val: ${newState.val}; q: ${newState.q}; ack: ${newState.ack}}`);
2433+
await this.setStateAsync(sid, newState, true);
2434+
}
2435+
else {
2436+
this.log.debug(`Set state ${sid} NOT to val: ${newState.val}; q: ${newState.q} because ack of this state is ${newState.ack}`);
2437+
}
24242438
}
24252439
}
24262440

@@ -2439,14 +2453,14 @@ class AlphaEss extends utils.Adapter {
24392453
this.log.warn(`Watchdog: State ${groupInfo.Group}.${groupStates[i].id} not updated for ${Date.now() - groupStates[i].lastUpdateTs} ms`);
24402454
const newState = await this.getStateAsync(`${groupInfo.Group}.${groupStates[i].id}`);
24412455
if (newState) {
2442-
if (newState.q != 0) {
2456+
if (newState.q != 0 && newState.ack) {
24432457
// Change quality only if it was OK before
24442458
newState.q = 0x01;
24452459
this.log.debug(`Watchdog: Set state ${groupInfo.Group}.${groupStates[i].id} to val: ${newState.val}; q: ${newState.q}`);
2446-
await this.setStateAsync(`${groupInfo.Group}.${groupStates[i].id}`, newState);
2460+
await this.setStateAsync(`${groupInfo.Group}.${groupStates[i].id}`, newState, true);
24472461
}
24482462
else {
2449-
this.log.silly(`Watchdog: Quality of state ${groupInfo.Group}.${groupStates[i].id} not changed, was already set to ${newState.q}!`);
2463+
this.log.silly(`Watchdog: Quality of state ${groupInfo.Group}.${groupStates[i].id} not changed, was already set to ${newState.q} and ack is ${newState.ack}!`);
24502464
}
24512465
}
24522466
else {

0 commit comments

Comments
 (0)