Skip to content

Commit

Permalink
fix invalid date error
Browse files Browse the repository at this point in the history
hopefully
  • Loading branch information
Garfonso committed Aug 8, 2023
1 parent e5dac6e commit dc7b1b3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
34 changes: 34 additions & 0 deletions lib/entities/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,39 @@ function createEntityNameFromCustom(obj, namespace) {
}
}

function updateTimestamps(entity, state) {
let lc;
let lu;
if (!state) {
//if we don't know anything, all is now.
lc = Date.now();
lu = Date.now();
} else {
lc = state.lc || Date.now();
lu = state.ts || Date.now();
}

try {
new Date(lc).getTime();
} catch (e) {
entityData.adapter.log.debug('Invalid lc time for ' + state._id + ' in ' + entity.entity_id + ': ' + e);
lc = Date.now();
}
try {
new Date(lu).getTime();
} catch (e) {
entityData.adapter.log.debug('Invalid lu time for ' + state._id + ' in ' + entity.entity_id + ': ' + e);
lu = Date.now();
}

if (lc / 1000 > entity.last_changed) {
entity.last_changed = lc / 1000;
}
if (lu / 1000 > entity.last_updated) {
entity.last_updated = lu / 1000;
}
}

//required to read user name
exports.getEnumName = getEnumName;

Expand All @@ -449,6 +482,7 @@ exports.setJsonAttribute = setJsonAttribute;
exports.getSmartName = getSmartName;
exports.fillEntityIntoCaches = fillEntityIntoCaches;
exports.extractValidEntityIds = extractValidEntityIds; //used to manage subscriptions.
exports.updateTimestamps = updateTimestamps;

//used for intelligent update:
exports.removeEntity = removeEntity;
Expand Down
14 changes: 2 additions & 12 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -777,12 +777,7 @@ class WebServer {
// {id: 2, type: "event", "event": {"event_type": "state_changed", "data": {"entity_id": "sun.sun", "old_state": {"entity_id": "sun.sun", "state": "above_horizon", "attributes": {"next_dawn": "2019-05-17T02:57:08+00:00", "next_dusk": "2019-05-16T19:44:32+00:00", "next_midnight": "2019-05-16T23:21:40+00:00", "next_noon": "2019-05-17T11:21:38+00:00", "next_rising": "2019-05-17T03:36:58+00:00", "next_setting": "2019-05-16T19:04:54+00:00", "elevation": 54.81, "azimuth": 216.35, "friendly_name": "Sun"}, "last_changed": "2019-05-16T09:09:53.424242+00:00", "last_updated": "2019-05-16T12:46:30.001390+00:00", "context": {id: "05356b1a7df54b2f939d3c7f8a3e05b4", "parent_id": null, "user_id": null}}, "new_state": {"entity_id": "sun.sun", "state": "above_horizon", "attributes": {"next_dawn": "2019-05-17T02:57:08+00:00", "next_dusk": "2019-05-16T19:44:32+00:00", "next_midnight": "2019-05-16T23:21:40+00:00", "next_noon": "2019-05-17T11:21:38+00:00", "next_rising": "2019-05-17T03:36:58+00:00", "next_setting": "2019-05-16T19:04:54+00:00", "elevation": 54.71, "azimuth": 216.72, "friendly_name": "Sun"}, "last_changed": "2019-05-16T09:09:53.424242+00:00", "last_updated": "2019-05-16T12:47:30.000414+00:00", "context": {id: "e738dc26af1d48b4964c6d9805179595", "parent_id": null, "user_id": null}}}, "origin": "LOCAL", "time_fired": "2019-05-16T12:47:30.000414+00:00", "context": {id: "e738dc26af1d48b4964c6d9805179595", "parent_id": null, "user_id": null}}}
if (entity.context.STATE.getId === id) {
updated = true;
if (state.lc / 1000 > entity.last_changed) {
entity.last_changed = state.lc / 1000;
}
if (state.ts / 1000 > entity.last_updated) {
entity.last_updated = state.ts / 1000;
}
utils.updateTimestamps(entity, state);

if (entity.context.STATE.getParser) {
entity.context.STATE.getParser(entity, 'state', state);
Expand All @@ -797,12 +792,7 @@ class WebServer {
for (const attr of attributes) {
updated = true;
//only update if newer than already present time.
if (state.lc / 1000 > entity.last_changed) {
entity.last_changed = state.lc / 1000;
}
if (state.ts / 1000 > entity.last_updated) {
entity.last_updated = state.ts / 1000;
}
utils.updateTimestamps(entity, state);

if (attr.getParser) {
attr.getParser(entity, attr, state);
Expand Down

0 comments on commit dc7b1b3

Please sign in to comment.