Created
November 22, 2024 12:50
-
-
Save tcha-tcho/5c6a3b71391574508ffc977e38e3c98c to your computer and use it in GitHub Desktop.
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
function oldFix (newdata, previous_position, imei) { | |
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; | |
} | |
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, | |
blk: newdata.out1, | |
pwr: newdata.pwr, | |
imei: imei, | |
ok: newdata.ok, | |
fxd: 0, | |
ctrl: [] | |
}; | |
try { | |
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 && yamldata.ok && is_valid_latlng; | |
const is_valid_to_update_cache = is_update;// && is_moving; | |
const conditions = { | |
fut: curr_dt > maxdate, | |
off: curr_dt <= found_dt, | |
"!ok": !yamldata.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) { | |
for (let prop in yamldata) { | |
if (prop !== "t" && yamldata[prop] === undefined) { | |
yamldata[prop] = previous_position[prop]; | |
} | |
} | |
} else { | |
yamldata.fxd = 3; | |
} | |
if (!yamldata.ok || !is_valid_latlng) { | |
yamldata.lat = previous_position.lat; | |
yamldata.lng = previous_position.lng; | |
yamldata.fxd = 1; | |
}; | |
// new devices are bringing valid false. | |
if (newdata.lat && !yamldata.lat) yamldata.lat = newdata.lat; | |
if (newdata.lng && !yamldata.lng) yamldata.lng = newdata.lng; | |
} catch(err) { | |
console.error("Error while updating cache last_updates", err); | |
} | |
return yamldata; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment