flightnethelper 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -0
- package/dist/Main.d.ts +12 -11
- package/dist/Main.js +26 -13
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
usage:
|
|
2
|
+
|
|
3
|
+
import { FlightnetPredictions } from "flightnethelper";
|
|
4
|
+
|
|
5
|
+
const predictions = new FlightnetPredictions("ws://localhost:9080");
|
|
6
|
+
|
|
7
|
+
setInterval(() => {
|
|
8
|
+
if (predictions.data) {
|
|
9
|
+
// predictions.data is the 24data { "t": "ACFT_DATA", d: { } }
|
|
10
|
+
}
|
|
11
|
+
}, 200);
|
package/dist/Main.d.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
data:
|
|
9
|
-
constructor(url:
|
|
10
|
-
|
|
11
|
-
|
|
1
|
+
import WebSocket from 'ws';
|
|
2
|
+
declare class FlightnetPredictions {
|
|
3
|
+
ws: WebSocket;
|
|
4
|
+
latestPayload: any;
|
|
5
|
+
snapshot: any;
|
|
6
|
+
idx: number;
|
|
7
|
+
timer: NodeJS.Timer | null | any;
|
|
8
|
+
data: any;
|
|
9
|
+
constructor(url: any, intervalMs?: number);
|
|
10
|
+
start(intervalMs: any): void;
|
|
11
|
+
stop(): void;
|
|
12
12
|
}
|
|
13
|
+
export { FlightnetPredictions };
|
package/dist/Main.js
CHANGED
|
@@ -8,34 +8,47 @@ const ws_1 = __importDefault(require("ws"));
|
|
|
8
8
|
const ORDER = ["1", "2", "3", "4", "5"];
|
|
9
9
|
class FlightnetPredictions {
|
|
10
10
|
constructor(url, intervalMs = 200) {
|
|
11
|
+
this.ws = new ws_1.default(url);
|
|
11
12
|
this.latestPayload = null;
|
|
13
|
+
this.snapshot = null;
|
|
12
14
|
this.idx = 0;
|
|
13
15
|
this.timer = null;
|
|
14
|
-
|
|
16
|
+
// Client reads this: { t, d }
|
|
15
17
|
this.data = null;
|
|
16
|
-
this.ws
|
|
17
|
-
this.ws.on("message", raw => {
|
|
18
|
+
this.ws.on('message', (raw) => {
|
|
18
19
|
try {
|
|
19
|
-
|
|
20
|
+
const parsed = JSON.parse(raw.toString());
|
|
21
|
+
this.latestPayload = parsed;
|
|
22
|
+
try {
|
|
23
|
+
this.snapshot = JSON.parse(JSON.stringify(parsed));
|
|
24
|
+
}
|
|
25
|
+
catch (e) {
|
|
26
|
+
this.snapshot = parsed;
|
|
27
|
+
}
|
|
28
|
+
this.idx = 0; // resync rotation on each new push
|
|
20
29
|
}
|
|
21
|
-
catch {
|
|
30
|
+
catch (e) {
|
|
22
31
|
// ignore malformed frames
|
|
23
32
|
}
|
|
24
33
|
});
|
|
25
|
-
this.ws.on(
|
|
26
|
-
this.ws.on(
|
|
34
|
+
this.ws.on('open', () => this.start(intervalMs));
|
|
35
|
+
this.ws.on('close', () => this.stop());
|
|
27
36
|
}
|
|
28
37
|
start(intervalMs) {
|
|
29
38
|
if (this.timer)
|
|
30
39
|
return;
|
|
31
40
|
this.timer = setInterval(() => {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
const msg = this.latestPayload[ORDER[this.idx]];
|
|
35
|
-
if (!msg)
|
|
41
|
+
const payload = this.snapshot;
|
|
42
|
+
if (!payload)
|
|
36
43
|
return;
|
|
37
|
-
|
|
38
|
-
|
|
44
|
+
const key = ORDER[this.idx];
|
|
45
|
+
const msg = payload[key];
|
|
46
|
+
if (!msg) {
|
|
47
|
+
this.data = payload['1'] || null;
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
this.data = msg;
|
|
51
|
+
}
|
|
39
52
|
this.idx = (this.idx + 1) % ORDER.length;
|
|
40
53
|
}, intervalMs);
|
|
41
54
|
}
|