Last active
July 12, 2024 18:18
-
-
Save tcha-tcho/ee166789bd892970df9c5de440f4c832 to your computer and use it in GitHub Desktop.
Real time fixando valor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let yamldata = { | |
t: newdata.t | |
,dt: newdata.dt | |
,ft: newdata.ft | |
,spd: newdata.spd | |
,lat: newdata.lat || 0 | |
,lng: newdata.lng || 0 | |
,ang: newdata.ang | |
,stp: newdata.stp | |
,ign: newdata.ign | |
,imei: imei | |
,ctrl: [] | |
}; | |
cache.sendCommand([ "HGET", "last_updates", imei ]).then(found_update => { | |
try { | |
found_update = found_update ? YAML.parse(found_update) : {}; | |
const curr_dt = (yamldata.dt || yamldata.ft || 0); | |
const found_dt = (found_update.dt || found_update.ft || 0); | |
const offline_pos = curr_dt < found_dt; | |
const maxdate = (new Date().getTime() + 86400000 /*24h ahead*/); | |
const in_the_future = newdata.ft ? | |
Number(newdata.ft) > maxdate : | |
newdata.dt && Number(newdata.dt) > maxdate; | |
if (in_the_future) yamldata.ctrl.push("fut"); | |
if (offline_pos) yamldata.ctrl.push("off"); | |
if (!newdata.ok) yamldata.ctrl.push("!ok"); | |
if (newdata.lat == 0) yamldata.ctrl.push("!lat"); | |
if (in_the_future || offline_pos) { | |
yamldata = { ...found_update, ctrl: yamldata.ctrl, t: yamldata.t }; | |
} else if (!newdata.ok || newdata.lat == 0) { | |
yamldata.lat = found_update.lat; | |
yamldata.lng = found_update.lng; | |
}; | |
yamldata = YAML.stringify(yamldata); | |
cache.sendCommand( [ "HSET", "last_updates", imei, yamldata ] ); | |
cache.sendCommand( [ 'SET', "exp_"+imei, yamldata, "EX", "10"] ) | |
} catch(e) { | |
console.error(e) | |
}; | |
}); |
Author
tcha-tcho
commented
Jul 12, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment