Skip to content

Commit

Permalink
* (Apollon77) Fix several crash cases reported by Sentry
Browse files Browse the repository at this point in the history
  • Loading branch information
Apollon77 committed May 26, 2022
1 parent 3d11eb6 commit 8fa3698
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ This adapter saves state history in a two-staged process.
-->

## Changelog

### __WORK IN PROGRESS__
* (Apollon77) Fix several crash cases reported by Sentry

### 2.0.1 (2022-05-11)
* (Apollon77) BREAKING: Configuration is only working in the new Admin 5 UI!
* (Apollon77) Did bigger adjustments to the recording logic and added a lot of new Features. Please refer to Changelog and Forum post for details.
Expand Down
2 changes: 1 addition & 1 deletion docs/en/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Possible options:
- **ignoreNull** - if null values should be included (false), replaced by last not null value (true) or replaced with 0 (0)
- **removeBorderValues** - By default additional border values are returned to optimize charting. Set this option to true if this is not wanted (e.g. for script data processing)
- **returnNewestEntries** - The returned data are always sorted by timestamp ascending. When using aggregate "none" and also providing "count" or "limit" this means that normally the oldest entries are returned (unless no start data is provided). Set this option to true to get the newest entries instead.
- **aggregate** - aggregate method:
- **aggregate** - aggregate method (Default: 'average'):
- *minmax* - used special algorithm. Splice the whole time range in small intervals and find for every interval max, min, start and end values.
- *max* - Splice the whole time range in small intervals and find for every interval max value and use it for this interval (nulls will be ignored).
- *min* - Same as max, but take minimal value.
Expand Down
5 changes: 5 additions & 0 deletions lib/aggregate.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ function aggregationLogic(data, index, options) {
log = options.log || console.log;
}

if (!options.result[index]) {
log(`${options.logId} Data index ${index} not initialized, ignore!`);
return;
}

if (options.aggregate !== 'minmax' && !options.result[index].val.ts) {
options.result[index].val.ts = Math.round(options.start + (((index - 1) + 0.5) * options.step));
}
Expand Down
24 changes: 22 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,7 @@ function getOneCachedData(id, options, cache, addId) {
continue;
}
const resEntry = Object.assign({}, res[i]);
if (resEntry.val !== null && isFinite(resEntry.val) && options.round) {
if (typeof resEntry.val === 'number' && isFinite(resEntry.val) && options.round) {
resEntry.val = Math.round(resEntry.val * options.round) / options.round;
}
if (options.ack) {
Expand Down Expand Up @@ -1053,7 +1053,7 @@ function getOneFileData(dayList, dayStart, dayEnd, id, options, data, addId) {
_data[ii].ts *= 1000;
}

if (_data[ii].val !== null && isFinite(_data[ii].val) && options.round) {
if (typeof _data[ii].val === 'number' && isFinite(_data[ii].val) && options.round) {
_data[ii].val = Math.round(_data[ii].val * options.round) / options.round;
}
if (options.ack) {
Expand Down Expand Up @@ -1180,6 +1180,26 @@ function getHistory(msg) {
options.round = adapter.config.round;
}

try {
if (options.start && typeof options.start !== 'number') {
options.start = new Date(options.start).getTime();
}
} catch (err) {
return adapter.sendTo(msg.from, msg.command, {
error: 'Invalid call. Start date ' + JSON.stringify(options.start) + ' is not a valid date'
}, msg.callback);
}

try {
if (options.end && typeof options.end !== 'number') {
options.end = new Date(options.end).getTime();
}
} catch (err) {
return adapter.sendTo(msg.from, msg.command, {
error: 'Invalid call. End date ' + JSON.stringify(options.end) + ' is not a valid date'
}, msg.callback);
}

if (!options.start && options.count) {
options.returnNewestEntries = true;
}
Expand Down

0 comments on commit 8fa3698

Please sign in to comment.