Skip to content

Commit

Permalink
try to fix more invalid date issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Garfonso committed Dec 9, 2023
1 parent 3d6ba97 commit 10a4de0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 31 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,10 @@ After that checkout modified version in `./build` folder. Then.
PLACEHOLDER for next version:
### **WORK IN PROGRESS**
-->
### **WORK IN PROGRESS**
* (Garfonso) revert shopping list
* (Garfonso) prevent invalid date error

### 4.0.4 (2023-12-09)
* (Garfonso) fix: crash

Expand Down
4 changes: 2 additions & 2 deletions lib/entities/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -464,10 +464,10 @@ function updateTimestamps(entity, state) {
lu = Date.now();
}

if (lc / 1000 > entity.last_changed) {
if (lc / 1000 > entity.last_changed || isNaN(entity.last_changed) || new Date(entity.last_changed * 1000).toString() === 'Invalid Date') {
entity.last_changed = lc / 1000;
}
if (lu / 1000 > entity.last_updated) {
if (lu / 1000 > entity.last_updated || isNaN(entity.last_updated) || new Date(entity.last_updated * 1000).toString() === 'Invalid Date') {
entity.last_updated = lu / 1000;
}
}
Expand Down
49 changes: 22 additions & 27 deletions lib/modules/history.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
const iobState2EntityState = require('../converters/genericConverter').iobState2EntityState;

/**
* convert .ts of state e to ISOString with try/catch and now as fallback.
* @param {ioBroker.state} e
* @returns {string}
* @private
*/
function _convertStateTStoISOString(e) {
try {
return new Date(e.ts).toISOString();
} catch (error) {
return new Date().toISOString();
}
}
const updateTimestamps = require('../entities/utils').updateTimestamps;

/**
* getHistory from history instances.
Expand Down Expand Up @@ -62,8 +49,16 @@ async function getHistory(adapter, entities, start, end, noAttributes, user) {
}
}

//match attributes to states by ts:
const getAttributeValues = (ts, attributesResult, attributesUsed = [], attributeValues = {}) => {
/**
* match attributes to states by ts:
* @param state {ioBroker.State}
* @param attributesResult
* @param attributesUsed
* @param attributeValues
* @returns {{}}
*/
const getAttributeValues = (state, attributesResult, attributesUsed = [], attributeValues = {}) => {
const ts = state.ts;
if (entity.context.ATTRIBUTES) {
for (const attribute of entity.context.ATTRIBUTES) {
if (attributesUsed.indexOf(attribute.attribute) >= 0) {
Expand All @@ -75,7 +70,7 @@ async function getHistory(adapter, entities, start, end, noAttributes, user) {
const results = attributesResult[attribute.attribute].result;
for (const result of results) {
if (result.val !== null) {
const diff = Math.abs(result.ts - new Date(ts).getTime());
const diff = Math.abs(result.ts - ts);
if (diff < bestDiff) {
best = result;
bestDiff = diff;
Expand Down Expand Up @@ -104,16 +99,16 @@ async function getHistory(adapter, entities, start, end, noAttributes, user) {
const attributesUsed = [];
for (const e of stateResult.result) {
if (e.val !== null) {
const ts = _convertStateTStoISOString(e);
const result = {
entity_id: entity.entity_id,
state: typeof entity.context.STATE.historyParser === 'function' ?
entity.context.STATE.historyParser(id, e.val).toString() :
iobState2EntityState(entity, e.val).toString(),
last_changed: ts,
last_updated: ts,
attributes: getAttributeValues(ts, attributesResult, attributesUsed)
last_changed: 0,
last_updated: 0,
attributes: getAttributeValues(e, attributesResult, attributesUsed)
};
updateTimestamps(result, e);
arr.push(result);
}
}
Expand All @@ -125,17 +120,17 @@ async function getHistory(adapter, entities, start, end, noAttributes, user) {
for (const result of results) {
if (result.val !== null && !result.used) {
const attributeValues = {};
const ts = _convertStateTStoISOString(result);
attributeValues[attribute.attribute] = typeof attribute.historyParser === 'function' ? attribute.historyParser(id, result.val) : result.val;
result.used = true;
//fill in other attributes:
const data = {
entity_id: entity.entity_id,
//state: null,
last_changed: ts,
last_updated: ts,
attributes: getAttributeValues(ts, attributesResult, attributesUsed, attributeValues)
last_changed: 0,
last_updated: 0,
attributes: getAttributeValues(result, attributesResult, attributesUsed, attributeValues)
};
updateTimestamps(data, result);
arr.push(data);
}
}
Expand Down Expand Up @@ -174,8 +169,8 @@ function convertHistoryResultToWebsocketApi(entities, result) {
const entityHistoryState = {
s: res.state,
a: res.attributes,
lc: new Date(res.last_changed).getTime() / 1000,
lu: new Date(res.last_updated).getTime() / 1000
lc: res.last_changed, //changed that already in old code.
lu: res.last_updated
};
entityHistoryStates.push(entityHistoryState);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -918,8 +918,8 @@ class WebServer {
}
entityHome.attributes.latitude = parseFloat(this.systemConfig.latitude);
entityHome.attributes.longitude = parseFloat(this.systemConfig.longitude);
entityHome.last_changed = this.systemConfig.ts || Date.now() / 1000;
entityHome.last_updated = this.systemConfig.ts || Date.now() / 1000;
entityHome.last_changed = (this.systemConfig.ts || Date.now()) / 1000;
entityHome.last_updated = (this.systemConfig.ts || Date.now()) / 1000;

//shoppindlist:
/*let entityShoppingList = entityData.entityId2Entity['todo.shoppinglist'];
Expand Down

0 comments on commit 10a4de0

Please sign in to comment.