From 10a4de0c700a9965f065d37814973a205af7d309 Mon Sep 17 00:00:00 2001 From: Garfonso Date: Sat, 9 Dec 2023 08:52:27 +0100 Subject: [PATCH] try to fix more invalid date issues --- README.md | 4 ++++ lib/entities/utils.js | 4 ++-- lib/modules/history.js | 49 +++++++++++++++++++----------------------- lib/server.js | 4 ++-- 4 files changed, 30 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index b307e1cfc..0ae8e0048 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/entities/utils.js b/lib/entities/utils.js index fb019ab98..347fcf6ad 100644 --- a/lib/entities/utils.js +++ b/lib/entities/utils.js @@ -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; } } diff --git a/lib/modules/history.js b/lib/modules/history.js index 533fd5c21..08bb14325 100644 --- a/lib/modules/history.js +++ b/lib/modules/history.js @@ -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. @@ -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) { @@ -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; @@ -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); } } @@ -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); } } @@ -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); } diff --git a/lib/server.js b/lib/server.js index 86214d1bf..61c4b5dcc 100644 --- a/lib/server.js +++ b/lib/server.js @@ -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'];