-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
144 lines (121 loc) · 3.99 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const Gpsd = require('node-gpsd-client');
const tiny = require('tiny-json-http');
const haversine = require("haversine-distance");
const { EOL } = require("os");
let config = null;
try{
config = require('./config.json');
}
catch (e){
throw new Error("FAILED TO LOAD config.json FILE" + EOL + e);
}
const client = new Gpsd({
port: config.gpsd_port,
hostname: config.gpsd_host,
parse: true
})
client.on('connected', () => {
console.log('Gpsd connected')
client.watch({
class: 'WATCH',
json: true,
scaled: true
})
})
client.on('error', err => {
console.log(`Gpsd error: ${err.message}`)
})
let historicTPV = []
let cachedTPV = null;
let lastMessageTime = new Date(0);
client.on('TPV', data => {
//first check mode, 1 = no fix, 2 = 2d fix, 3 = 3d fix
if(data.device == config.gps_device){
let now = new Date(Date.now());
let cutoffTime = new Date(Date.now() - config.static_distance_measure_time);
if(data.mode > 1){
cachedTPV = data;
historicTPV.push(data);
while(historicTPV.length > 0 && new Date(historicTPV[0].time) < cutoffTime)
{
historicTPV.shift();
}
}
else{
cachedTPV = null;
previousTPV= null;
}
lastMessageTime = now;
}
})
let cachedSKY = null;
client.on('SKY', data => {
cachedSKY = data;
})
client.connect()
let previousSendTime = 0;
let hasExceededStaticDistance = false;
let toSend = [];
const delayTimer = 10000;
const loopTimer = 1000;
checkInterval();
async function checkInterval(){
console.log("call checkInterval()");
console.log(`historicTPV.length = ${historicTPV.length}`);
if(historicTPV.length > 1){
let totalDistance = 0;
let first = historicTPV[0];
let last = historicTPV[historicTPV.length-1];
const a = { lat: first.lat, lon: first.lon }
const b = { lat: last.lat, lon: last.lon }
totalDistance = haversine(a, b);
if(totalDistance > config.static_distance_threshold){
hasExceededStaticDistance = true;
}
}
console.log(`hasExceededStaticDistance = ${hasExceededStaticDistance}`);
var waitTime = hasExceededStaticDistance ? config.send_interval : config.static_send_interval;
console.log(`waitTime = ${waitTime}`);
console.log(`previousSendTime = ${previousSendTime}`);
console.log(`Date.now() - waitTime = ${Date.now() - waitTime}`);
if(previousSendTime < Date.now() - waitTime){
console.log(`Send time has elapsed!`);
if(cachedTPV != null && (cachedTPV.lat !== 0 && cachedTPV.lon !== 0)){
saveLocation();
hasExceededStaticDistance = false;
previousSendTime = Date.now();
}
else{
console.log(`Send time has elapsed but waiting for fix.. ${cachedSKY == null ? 'no data yet' : `satellite count = ${cachedSKY.satellites.length}` }, last GPSD update ${lastMessageTime}`);
}
}
await sendMessages();
setTimeout(checkInterval, cachedTPV != null ? loopTimer : delayTimer);
}
function saveLocation(){
let lat = cachedTPV.lat;
let lon = cachedTPV.lon;
let speed = cachedTPV.speed;
let hdop = cachedSKY.hdop;
let time = cachedTPV.time;
let epx = cachedTPV.epx;
let epy = cachedTPV.epy;
let accuracy = (epx + epy)/2
let url = `${config.server_url}/?id=${config.device_id}&lat=${lat}&lon=${lon}&hdop=${hdop}&speed=${speed}×tamp=${time}&accuracy=${Math.round(accuracy * 100) / 100}`;
toSend.push(url);
}
async function sendMessages(){
while(toSend.length > 0){
var url = toSend[0];
try {
console.log(`sending '${url}', last GPSD update ${lastMessageTime}`);
await tiny.get({url});
console.log("success");
toSend.shift();
}
catch(e){
console.error(`FAILED TO UPDATE LOCATION, #${toSend.length} UPDATES IN QUEUE`, e);
break;
}
}
}