Skip to content

Instantly share code, notes, and snippets.

@tcha-tcho
Last active June 5, 2024 20:05
Show Gist options
  • Save tcha-tcho/ee166789bd892970df9c5de440f4c832 to your computer and use it in GitHub Desktop.
Save tcha-tcho/ee166789bd892970df9c5de440f4c832 to your computer and use it in GitHub Desktop.
Real time fixando valor
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)
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment