Skip to content

Instantly share code, notes, and snippets.

@tcha-tcho
Last active July 12, 2024 18:18
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)
};
});
@eoguvo
Copy link

eoguvo commented Jul 8, 2024

New approach prioritizing ft over dt, while filtering valid dates

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: []
};

const checkValidDate = function (date) {
    const parsed = new Date(date);
    if (!parsed) { return null };
    const oneDayInMilliseconds = 24 * 60 * 60 * 1000;
    const threeMonthsAgo = new Date(Date.now() - 3 * 30 * oneDayInMilliseconds);
    const oneDayInFuture = new Date(Date.now() + oneDayInMilliseconds);
    const isFromPast = parsed <= threeMonthsAgo;
    const isFromFuture = parsed >= oneDayInFuture;
    if (isFromPast || isFromFuture) return null;
    return date;
}

const getValidTime = function (position) {
    if (!position) return 0;
    return checkValidDate(position.ft) || checkValidDate(Math.round(position.dt)) || 0;
}
 
cache.sendCommand([ "HGET", "last_updates", imei ]).then((prev_data_raw) => {
    try {
        const previous_position = (prev_data_raw) ? YAML.parse(prev_data_raw) : {};
        const curr_dt = getValidTime(yamldata);
        const found_dt = getValidTime(previous_position);
        const one_day = 24 * 60 * 60 * 1000;
        const maxdate = (new Date() + one_day);
        const is_valid_latlng = newdata.lat && newdata.lng;
        const is_update = curr_dt > found_dt && newdata.ok && is_valid_latlng;
        const is_moving = newdata.spd >= 6;
        const is_valid_to_update_cache = is_update && is_moving;

        const conditions = {
            fut: curr_dt > maxdate,
            off: curr_dt < found_dt,
            "!ok": !newdata.ok,
            "!pos": !is_valid_latlng
        };
        
        for (const [key, value] of Object.entries(conditions)) {
            if (value) {
                yamldata.ctrl.push(key);
            }
        }
        
        if (!is_valid_to_update_cache) {
            yamldata = { 
                ...previous_position, 
                ctrl: yamldata.ctrl, 
                t: yamldata.t,
            };
        }
        if (!newdata.ok || !is_valid_latlng) {
            yamldata.lat = previous_position.lat;
            yamldata.lng = previous_position.lng;
        };
        const result = YAML.stringify(yamldata);
        cache.sendCommand([ "HSET", "last_updates", imei, result ]);
        cache.sendCommand([ "SET", `exp_${imei}`, result, "EX", "10"]);
    } catch(err) {
        console.error("Error while updating cache last_updates", err);
    }
});

@tcha-tcho
Copy link
Author

    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: []
    };

    const checkValidDate = function (date) {
      const parsed = new Date(date);
      if (!parsed) { return null };
      const oneDayInMilliseconds = 24 * 60 * 60 * 1000;
      const threeMonthsAgo = new Date(Date.now() - 3 * 30 * oneDayInMilliseconds);
      const oneDayInFuture = new Date(Date.now() + oneDayInMilliseconds);
      const isFromPast = parsed <= threeMonthsAgo;
      const isFromFuture = parsed >= oneDayInFuture;
      if (isFromPast || isFromFuture) return null;
      return date;
    }

    const getValidTime = function (position) {
      if (!position) return 0;
      return checkValidDate(position.ft) || checkValidDate(Math.round(position.dt)) || 0;
    }

    cache.sendCommand([ "HGET", "last_updates", imei ]).then((prev_data_raw) => {
      try {
        const previous_position = (prev_data_raw) ? YAML.parse(prev_data_raw) : {};
        const curr_dt = getValidTime(yamldata);
        const found_dt = getValidTime(previous_position);
        const one_day = 24 * 60 * 60 * 1000;
        const maxdate = (new Date().getTime() + one_day);
        const is_valid_latlng = newdata.lat && newdata.lng;
        const is_update = curr_dt > found_dt && newdata.ok && is_valid_latlng;
        const is_moving = newdata.spd >= 6;
        const is_valid_to_update_cache = is_update;// && is_moving;

        const conditions = {
          fut: curr_dt > maxdate,
          off: curr_dt <= found_dt,
          "!ok": !newdata.ok,
          "!pos": !is_valid_latlng
        };

        for (const [key, value] of Object.entries(conditions)) {
          if (value) {
            yamldata.ctrl.push(key);
          }
        }

        if (!is_valid_to_update_cache) {
          yamldata = { 
            ...yamldata,
            ...previous_position,
            ctrl: yamldata.ctrl,
            t: yamldata.t
          };
        }
        if (!newdata.ok || !is_valid_latlng) {
          yamldata.lat = previous_position.lat;
          yamldata.lng = previous_position.lng;
        };
        const result = YAML.stringify(yamldata);
        cache.sendCommand([ "HSET", "last_updates", imei, result ]);
        cache.sendCommand([ "SET", `exp_${imei}`, result, "EX", "10"]);
      } catch(err) {
        console.error("Error while updating cache last_updates", err);
      }
    });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment