-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
84 lines (73 loc) · 2.38 KB
/
Copy pathscript.js
File metadata and controls
84 lines (73 loc) · 2.38 KB
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
// const MDNS_ENDPOINT = "https://serviceberry-limeskey.local:8443/submit";
// const MDNS_ENDPOINT = "http://serviceberry-limeskey.local:8080/submit";
const MDNS_ENDPOINT = "http://192.168.0.251:8080/submit";
let logBuffer = "";
function log(msg) {
console.log(msg);
logBuffer += msg + "\n";
}
async function getPosition() {
try {
const loc = await Location.current();
log("[Location] Successfully retrieved GPS location.");
return {
latitude: loc.latitude ?? 0,
longitude: loc.longitude ?? 0,
accuracy: loc.horizontalAccuracy ?? 0,
altitude: loc.altitude ?? 0,
altitudeAccuracy: loc.verticalAccuracy ?? 0,
heading: loc.course ?? 0,
speed: loc.speed ?? 0,
source: "gnss",
};
} catch (e) {
log("[Location] Failed: " + e.toString());
throw e;
}
}
// Main
async function main() {
try {
// Only gather device GPS location and send a Position-only payload
const position = await getPosition();
const payload = {
position: {
latitude: position.latitude,
longitude: position.longitude,
accuracy: position.accuracy,
altitude: position.altitude,
altitudeAccuracy: position.altitudeAccuracy,
heading: position.heading,
speed: position.speed,
source: position.source,
},
};
log("[Payload] Position-only payload:\n" + JSON.stringify(payload, null, 2));
async function sendPayload(url, body) {
log(`[POST] Sending payload to: ${url}`);
let req = new Request(url);
req.method = "POST";
req.headers = { "Content-Type": "application/json" };
req.body = JSON.stringify(body);
try {
const res = await req.loadString();
const status = req.response && req.response.statusCode ? req.response.statusCode : null;
log(`[POST ${url}] Status: ${status}`);
log(`[POST ${url}] Response length: ${res.length}`);
} catch (e) {
if (req && req.response && req.response.statusCode) {
log(`[POST ${url}] HTTP Error Status: ${req.response.statusCode}`);
} else {
log(`[POST ${url}] Error: ${e.toString()}`);
}
}
}
// Send directly to IP
await sendPayload(MDNS_ENDPOINT, payload);
QuickLook.present(logBuffer);
} catch (e) {
QuickLook.present("[Error] " + e.toString() + "\n\nLogs:\n" + logBuffer);
}
}
await main();
Script.complete();