Skip to content

Instantly share code, notes, and snippets.

@tcha-tcho
Last active June 5, 2024 18:16
Show Gist options
  • Save tcha-tcho/bdfa72c08113c3f9a8014b0b23ac956c to your computer and use it in GitHub Desktop.
Save tcha-tcho/bdfa72c08113c3f9a8014b0b23ac956c to your computer and use it in GitHub Desktop.
const https = require('https');
const http = require('http');
const getJSON = (options, onResult) => {
console.log('rest::getJSON');
const port = options.port == 443 ? https : http;
let output = '';
const req = port.request(options, (res) => {
console.log(`${options.host} : ${res.statusCode}`);
res.setEncoding('utf8');
res.on('data', (chunk) => {
output += chunk;
});
res.on('end', () => {
let obj = JSON.parse(output);
onResult(res.statusCode, obj);
});
});
req.on('error', (err) => {
// res.send('error: ' + err.message);
});
req.end();
};
const options = {
host: 'us-central1-tracker-net.cloudfunctions.net',
port: 443,
path: '/cache?imei=860896050989276',
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
};
setInterval(() => {
getJSON(options, (statusCode, res) => {
res.reverse();
let yamldata = res[0];
yamldata.ctrl = [];
let found_update = res[1]
console.log(yamldata.t > found_update.t)
try {
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 = yamldata.ft ?
Number(yamldata.ft) > maxdate :
yamldata.dt && Number(yamldata.dt) > maxdate;
if (in_the_future) yamldata.ctrl.push("fut");
if (offline_pos) yamldata.ctrl.push("off");
if (!yamldata.ok) yamldata.ctrl.push("!ok");
if (yamldata.lat == 0) yamldata.ctrl.push("!lat");
if (in_the_future || offline_pos) {
yamldata = { ...found_update, ctrl: yamldata.ctrl, t: yamldata.t };
} else if (!yamldata.ok || yamldata.lat == 0) {
yamldata.lat = found_update.lat;
yamldata.lng = found_update.lng;
};
console.log(yamldata)
} catch(e) {
console.error(e)
};
})
}, 5000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment