zenbus-next-bus 1.0.0
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/LICENSE +21 -0
- package/README.md +77 -0
- package/examples/debug-next-bus.json +39 -0
- package/icons/zenbus.png +0 -0
- package/package.json +52 -0
- package/zenbus-core.mjs +82 -0
- package/zenbus-next-bus.html +77 -0
- package/zenbus-next-bus.mjs +50 -0
- package/zenbus-node-red.cjs +41 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 gautric
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# zenbus-next-bus
|
|
2
|
+
|
|
3
|
+
Real-time next bus ETA from [Zenbus](https://zenbus.net) networks, streamed to your terminal.
|
|
4
|
+
|
|
5
|
+
Calls the Zenbus API directly using protobuf — no browser, no scraping.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install -g zenbus-next-bus
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## CLI Usage
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
zenbus-next-bus --alias gpso --itinerary 5426824545828864 --stop 5366312231501824
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
### Options
|
|
20
|
+
|
|
21
|
+
| Option | Env var | Description | Required |
|
|
22
|
+
|---|---|---|---|
|
|
23
|
+
| `--alias` | `ZENBUS_ALIAS` | Network alias (e.g. `gpso`) | yes |
|
|
24
|
+
| `--itinerary` | `ZENBUS_ITINERARY` | Itinerary ID | yes |
|
|
25
|
+
| `--stop` | `ZENBUS_STOP` | Stop ID | yes |
|
|
26
|
+
| `--interval` | `ZENBUS_INTERVAL` | Poll interval in seconds (default: 10) | no |
|
|
27
|
+
| `--output` | | Output format: `terminal` or `json` (default: terminal) | no |
|
|
28
|
+
|
|
29
|
+
### Terminal output (default)
|
|
30
|
+
|
|
31
|
+
Live-updating display with real-time ETA, distance, scheduled time, and 2nd bus when available.
|
|
32
|
+
|
|
33
|
+
### JSON output
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
zenbus-next-bus --alias gpso --itinerary ... --stop ... --output json
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Outputs one JSON object per poll cycle to stdout — ideal for piping to other tools.
|
|
40
|
+
|
|
41
|
+
## Programmatic Usage
|
|
42
|
+
|
|
43
|
+
```js
|
|
44
|
+
import { createClient } from 'zenbus-next-bus';
|
|
45
|
+
|
|
46
|
+
const client = await createClient({ alias: 'gpso', itinerary: '...', stop: '...' });
|
|
47
|
+
const data = await client.poll();
|
|
48
|
+
console.log(data.next); // { etaMinutes, distanceM, estimatedArrival, scheduledTime, isLive }
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Node-RED
|
|
52
|
+
|
|
53
|
+
Install in your Node-RED directory:
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
npm install zenbus-next-bus
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
The `zenbus-next-bus` node appears in the palette. Configure alias, itinerary, stop, and interval. It outputs `msg.payload` with the full poll data on each cycle.
|
|
60
|
+
|
|
61
|
+
### Sample flow
|
|
62
|
+
|
|
63
|
+
A ready-to-use example is bundled with the package. In Node-RED, go to **Menu → Import → Examples → zenbus-next-bus → debug-next-bus** to import a flow that wires the node to a debug output.
|
|
64
|
+
|
|
65
|
+
You can also import it manually from `examples/debug-next-bus.json`.
|
|
66
|
+
|
|
67
|
+
## Finding your IDs
|
|
68
|
+
|
|
69
|
+
Open your stop on [zenbus.net](https://zenbus.net), the URL contains the IDs:
|
|
70
|
+
|
|
71
|
+
```
|
|
72
|
+
https://zenbus.net/publicapp/web/{alias}?line=...&stop={stop}&itinerary={itinerary}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## License
|
|
76
|
+
|
|
77
|
+
MIT
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"id": "zenbus1",
|
|
4
|
+
"type": "zenbus-next-bus",
|
|
5
|
+
"z": "flow1",
|
|
6
|
+
"name": "Zenbus ETA",
|
|
7
|
+
"alias": "gpso",
|
|
8
|
+
"itinerary": "5426824545828864",
|
|
9
|
+
"stop": "5366312231501824",
|
|
10
|
+
"interval": "10",
|
|
11
|
+
"x": 200,
|
|
12
|
+
"y": 200,
|
|
13
|
+
"wires": [["debug1"]]
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
"id": "debug1",
|
|
17
|
+
"type": "debug",
|
|
18
|
+
"z": "flow1",
|
|
19
|
+
"name": "Bus ETA Debug",
|
|
20
|
+
"active": true,
|
|
21
|
+
"tosidebar": true,
|
|
22
|
+
"console": false,
|
|
23
|
+
"tostatus": true,
|
|
24
|
+
"complete": "payload",
|
|
25
|
+
"targetType": "msg",
|
|
26
|
+
"statusVal": "payload.next.etaMinutes",
|
|
27
|
+
"statusType": "auto",
|
|
28
|
+
"x": 450,
|
|
29
|
+
"y": 200,
|
|
30
|
+
"wires": []
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
"id": "flow1",
|
|
34
|
+
"type": "tab",
|
|
35
|
+
"label": "Zenbus Next Bus",
|
|
36
|
+
"disabled": false,
|
|
37
|
+
"info": ""
|
|
38
|
+
}
|
|
39
|
+
]
|
package/icons/zenbus.png
ADDED
|
Binary file
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "zenbus-next-bus",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Real-time next bus ETA from Zenbus networks via direct API (protobuf)",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"zenbus-next-bus": "./zenbus-next-bus.mjs"
|
|
8
|
+
},
|
|
9
|
+
"exports": {
|
|
10
|
+
".": "./zenbus-core.mjs",
|
|
11
|
+
"./core": "./zenbus-core.mjs",
|
|
12
|
+
"./node-red": "./zenbus-node-red.mjs"
|
|
13
|
+
},
|
|
14
|
+
"node-red": {
|
|
15
|
+
"nodes": {
|
|
16
|
+
"zenbus-next-bus": "zenbus-node-red.cjs"
|
|
17
|
+
},
|
|
18
|
+
"examples": {
|
|
19
|
+
"zenbus-next-bus": ["examples/debug-next-bus.json"]
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"start": "node zenbus-next-bus.mjs --alias gpso --itinerary 5426824545828864 --stop 5366312231501824"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"zenbus-core.mjs",
|
|
27
|
+
"zenbus-next-bus.mjs",
|
|
28
|
+
"zenbus-node-red.cjs",
|
|
29
|
+
"zenbus-next-bus.html",
|
|
30
|
+
"icons",
|
|
31
|
+
"examples"
|
|
32
|
+
],
|
|
33
|
+
"keywords": [
|
|
34
|
+
"zenbus",
|
|
35
|
+
"bus",
|
|
36
|
+
"transit",
|
|
37
|
+
"real-time",
|
|
38
|
+
"eta",
|
|
39
|
+
"protobuf",
|
|
40
|
+
"cli",
|
|
41
|
+
"node-red"
|
|
42
|
+
],
|
|
43
|
+
"author": "gautric",
|
|
44
|
+
"license": "MIT",
|
|
45
|
+
"engines": {
|
|
46
|
+
"node": ">=18"
|
|
47
|
+
},
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"protobufjs": "^8.0.1",
|
|
50
|
+
"yargs": "^17.7.2"
|
|
51
|
+
}
|
|
52
|
+
}
|
package/zenbus-core.mjs
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import protobuf from 'protobufjs';
|
|
2
|
+
|
|
3
|
+
const BASE = 'https://zenbus.net';
|
|
4
|
+
|
|
5
|
+
async function fetchProto(url, Type) {
|
|
6
|
+
const buf = Buffer.from(await (await fetch(url)).arrayBuffer());
|
|
7
|
+
return Type.decode(buf);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function secsToHHMM(secs) {
|
|
11
|
+
const h = Math.floor(secs / 3600);
|
|
12
|
+
const m = Math.floor((secs % 3600) / 60);
|
|
13
|
+
return `${h}h${String(m).padStart(2, '0')}`;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export async function createClient({ alias, itinerary, stop }) {
|
|
17
|
+
const protoText = await (await fetch(`${BASE}/poll/cdn/zenbus.proto`)).text();
|
|
18
|
+
const { root } = protobuf.parse(protoText);
|
|
19
|
+
const StaticMessage = root.lookupType('zenbus_realtime.StaticMessage');
|
|
20
|
+
const LiveMessage = root.lookupType('zenbus_realtime.LiveMessage');
|
|
21
|
+
|
|
22
|
+
const staticData = await fetchProto(`${BASE}/publicapp/static-data?alias=${alias}`, StaticMessage);
|
|
23
|
+
const shape = staticData.shape?.find(s => s.itineraryId?.toString() === itinerary);
|
|
24
|
+
const stopAnchor = shape?.anchor?.find(a => a.stopId?.toString() === stop);
|
|
25
|
+
const stopIndex = stopAnchor?.stopIndexInItinerary ?? -1;
|
|
26
|
+
const stopDistanceM = stopAnchor?.distanceTravelled ?? 0;
|
|
27
|
+
const stopName = staticData.stop?.find(s => s.stopId?.toString() === stop)?.name || 'Unknown';
|
|
28
|
+
const itin = staticData.itinerary?.find(i => i.itineraryId?.toString() === itinerary);
|
|
29
|
+
const lineCode = staticData.line?.find(l => l.lineId?.toString() === itin?.lineId?.toString())?.code || 'Unknown';
|
|
30
|
+
|
|
31
|
+
const pollUrl = `${BASE}/publicapp/poll?alias=${alias}&itinerary=${itinerary}`;
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
stopName, lineCode,
|
|
35
|
+
async poll() {
|
|
36
|
+
const liveData = await fetchProto(pollUrl, LiveMessage);
|
|
37
|
+
const now = new Date();
|
|
38
|
+
const midnightUtcSecs = liveData.timetable?.[0]?.midnight?.toNumber?.()
|
|
39
|
+
?? Math.floor(new Date(now).setHours(0, 0, 0, 0) / 1000);
|
|
40
|
+
const nowSecs = Math.floor(now.getTime() / 1000) - midnightUtcSecs;
|
|
41
|
+
|
|
42
|
+
const allColumns = [...(liveData.tripColumn || [])];
|
|
43
|
+
for (const tt of liveData.timetable || []) {
|
|
44
|
+
for (const col of tt.column || []) allColumns.push(col);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const candidates = [];
|
|
48
|
+
for (const tc of allColumns) {
|
|
49
|
+
const est = tc.estimactual?.find(s => s.stopIndexInItinerary === stopIndex);
|
|
50
|
+
const etaSecs = est?.arrival || est?.departure || 0;
|
|
51
|
+
if (!etaSecs || etaSecs <= nowSecs) continue;
|
|
52
|
+
|
|
53
|
+
const vehicleDist = tc.distanceTravelled || 0;
|
|
54
|
+
const hasStarted = tc.previousIndexInItinerary >= 0 && tc.pos?.length > 0;
|
|
55
|
+
if (hasStarted && vehicleDist > stopDistanceM) continue;
|
|
56
|
+
|
|
57
|
+
const remainingDist = hasStarted ? Math.max(0, stopDistanceM - vehicleDist) : stopDistanceM;
|
|
58
|
+
const aimed = tc.aimed?.find(s => s.stopIndexInItinerary === stopIndex);
|
|
59
|
+
const schedSecs = aimed?.arrival || aimed?.departure || aimed?.arriparture || 0;
|
|
60
|
+
|
|
61
|
+
candidates.push({
|
|
62
|
+
etaMinutes: Math.round((etaSecs - nowSecs) / 60),
|
|
63
|
+
distanceM: Math.round(remainingDist),
|
|
64
|
+
estimatedArrival: secsToHHMM(etaSecs),
|
|
65
|
+
scheduledTime: schedSecs ? secsToHHMM(schedSecs) : null,
|
|
66
|
+
isLive: hasStarted,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
candidates.sort((a, b) => a.etaMinutes - b.etaMinutes);
|
|
71
|
+
const best = candidates[0] || null;
|
|
72
|
+
const second = candidates.find((c, i) => i > 0 && c.isLive) || null;
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
stop: stopName, line: lineCode,
|
|
76
|
+
timestamp: now.toISOString(),
|
|
77
|
+
now: secsToHHMM(nowSecs),
|
|
78
|
+
next: best, secondBus: second,
|
|
79
|
+
};
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
<script type="text/javascript">
|
|
2
|
+
RED.nodes.registerType('zenbus-next-bus', {
|
|
3
|
+
category: 'transport',
|
|
4
|
+
color: '#8e6538',
|
|
5
|
+
defaults: {
|
|
6
|
+
name: { value: '' },
|
|
7
|
+
alias: { value: '', required: true },
|
|
8
|
+
itinerary: { value: '', required: true },
|
|
9
|
+
stop: { value: '', required: true },
|
|
10
|
+
interval: { value: 10, validate: RED.validators.number() }
|
|
11
|
+
},
|
|
12
|
+
inputs: 0,
|
|
13
|
+
outputs: 1,
|
|
14
|
+
icon: 'zenbus.png',
|
|
15
|
+
label: function () {
|
|
16
|
+
return this.name || 'zenbus ' + (this.alias || '');
|
|
17
|
+
},
|
|
18
|
+
paletteLabel: 'zenbus next bus'
|
|
19
|
+
});
|
|
20
|
+
</script>
|
|
21
|
+
|
|
22
|
+
<script type="text/html" data-template-name="zenbus-next-bus">
|
|
23
|
+
<div class="form-row">
|
|
24
|
+
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
|
25
|
+
<input type="text" id="node-input-name" placeholder="Name">
|
|
26
|
+
</div>
|
|
27
|
+
<div class="form-row">
|
|
28
|
+
<label for="node-input-alias"><i class="fa fa-globe"></i> Alias</label>
|
|
29
|
+
<input type="text" id="node-input-alias" placeholder="e.g. gpso">
|
|
30
|
+
</div>
|
|
31
|
+
<div class="form-row">
|
|
32
|
+
<label for="node-input-itinerary"><i class="fa fa-road"></i> Itinerary</label>
|
|
33
|
+
<input type="text" id="node-input-itinerary" placeholder="Itinerary ID">
|
|
34
|
+
</div>
|
|
35
|
+
<div class="form-row">
|
|
36
|
+
<label for="node-input-stop"><i class="fa fa-map-marker"></i> Stop</label>
|
|
37
|
+
<input type="text" id="node-input-stop" placeholder="Stop ID">
|
|
38
|
+
</div>
|
|
39
|
+
<div class="form-row">
|
|
40
|
+
<label for="node-input-interval"><i class="fa fa-clock-o"></i> Interval (s)</label>
|
|
41
|
+
<input type="number" id="node-input-interval" min="5" step="1">
|
|
42
|
+
</div>
|
|
43
|
+
</script>
|
|
44
|
+
|
|
45
|
+
<script type="text/html" data-help-name="zenbus-next-bus">
|
|
46
|
+
<p>Polls the <a href="https://zenbus.net">Zenbus</a> real-time API and outputs next bus ETA.</p>
|
|
47
|
+
|
|
48
|
+
<h3>Configuration</h3>
|
|
49
|
+
<dl class="message-properties">
|
|
50
|
+
<dt>Alias <span class="property-type">string</span></dt>
|
|
51
|
+
<dd>Network alias (e.g. <code>gpso</code>). Found in the Zenbus URL.</dd>
|
|
52
|
+
<dt>Itinerary <span class="property-type">string</span></dt>
|
|
53
|
+
<dd>Itinerary ID from the Zenbus URL.</dd>
|
|
54
|
+
<dt>Stop <span class="property-type">string</span></dt>
|
|
55
|
+
<dd>Stop ID from the Zenbus URL.</dd>
|
|
56
|
+
<dt>Interval <span class="property-type">number</span></dt>
|
|
57
|
+
<dd>Poll interval in seconds (default 10).</dd>
|
|
58
|
+
</dl>
|
|
59
|
+
|
|
60
|
+
<h3>Output</h3>
|
|
61
|
+
<dl class="message-properties">
|
|
62
|
+
<dt>payload.stop <span class="property-type">string</span></dt>
|
|
63
|
+
<dd>Stop name.</dd>
|
|
64
|
+
<dt>payload.line <span class="property-type">string</span></dt>
|
|
65
|
+
<dd>Line code.</dd>
|
|
66
|
+
<dt>payload.next <span class="property-type">object | null</span></dt>
|
|
67
|
+
<dd>Next bus: <code>etaMinutes</code>, <code>distanceM</code>, <code>estimatedArrival</code>, <code>scheduledTime</code>, <code>isLive</code>.</dd>
|
|
68
|
+
<dt>payload.secondBus <span class="property-type">object | null</span></dt>
|
|
69
|
+
<dd>Second tracked bus (same fields), only when live distance is available.</dd>
|
|
70
|
+
<dt>payload.timestamp <span class="property-type">string</span></dt>
|
|
71
|
+
<dd>ISO timestamp of the poll.</dd>
|
|
72
|
+
</dl>
|
|
73
|
+
|
|
74
|
+
<h3>Finding your IDs</h3>
|
|
75
|
+
<p>Open your stop on <a href="https://zenbus.net">zenbus.net</a>. The URL contains:</p>
|
|
76
|
+
<pre>https://zenbus.net/publicapp/web/{alias}?line=...&stop={stop}&itinerary={itinerary}</pre>
|
|
77
|
+
</script>
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import yargs from 'yargs';
|
|
3
|
+
import { hideBin } from 'yargs/helpers';
|
|
4
|
+
import { createClient } from './zenbus-core.mjs';
|
|
5
|
+
|
|
6
|
+
const argv = yargs(hideBin(process.argv))
|
|
7
|
+
.env('ZENBUS')
|
|
8
|
+
.option('alias', { type: 'string', demandOption: true, describe: 'Network alias (e.g. gpso)' })
|
|
9
|
+
.option('itinerary', { type: 'string', demandOption: true, describe: 'Itinerary ID' })
|
|
10
|
+
.option('stop', { type: 'string', demandOption: true, describe: 'Stop ID' })
|
|
11
|
+
.option('interval', { type: 'number', default: 10, describe: 'Poll interval in seconds' })
|
|
12
|
+
.option('output', { type: 'string', choices: ['terminal', 'json'], default: 'terminal', describe: 'Output format' })
|
|
13
|
+
.strict()
|
|
14
|
+
.parseSync();
|
|
15
|
+
|
|
16
|
+
const client = await createClient(argv);
|
|
17
|
+
|
|
18
|
+
function renderTerminal(data) {
|
|
19
|
+
process.stdout.write('\x1B[2J\x1B[H');
|
|
20
|
+
console.log('🚌 ZENBUS - Live Stream (Ctrl+C to quit)\n');
|
|
21
|
+
console.log('═══════════════════════════════════════');
|
|
22
|
+
console.log(` 🚏 Stop: ${data.stop}`);
|
|
23
|
+
console.log(` 🚌 Line: ${data.line}`);
|
|
24
|
+
if (data.next) {
|
|
25
|
+
const n = data.next;
|
|
26
|
+
console.log(` ⏱️ ETA: ${n.etaMinutes} min${n.isLive ? ' (real-time)' : ' (scheduled)'}`);
|
|
27
|
+
console.log(` 📏 Distance: ${n.distanceM} m away`);
|
|
28
|
+
console.log(` 🕐 Arrival: ${n.estimatedArrival}`);
|
|
29
|
+
if (n.scheduledTime) console.log(` 📅 Scheduled: ${n.scheduledTime}`);
|
|
30
|
+
if (data.secondBus) {
|
|
31
|
+
const s = data.secondBus;
|
|
32
|
+
console.log(` ───────────────────────────────────`);
|
|
33
|
+
console.log(` 2️⃣ 2nd bus: ${s.etaMinutes} min (${s.distanceM} m away)`);
|
|
34
|
+
console.log(` 🕐 Arrival: ${s.estimatedArrival}`);
|
|
35
|
+
}
|
|
36
|
+
} else {
|
|
37
|
+
console.log(` ⏱️ ETA: No upcoming bus`);
|
|
38
|
+
}
|
|
39
|
+
console.log(` 🕑 Now: ${data.now}`);
|
|
40
|
+
console.log('═══════════════════════════════════════');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const render = argv.output === 'json'
|
|
44
|
+
? (data) => console.log(JSON.stringify(data))
|
|
45
|
+
: renderTerminal;
|
|
46
|
+
|
|
47
|
+
while (true) {
|
|
48
|
+
try { render(await client.poll()); } catch (e) { console.error('⚠️', e.message); }
|
|
49
|
+
await new Promise(r => setTimeout(r, argv.interval * 1000));
|
|
50
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
module.exports = function (RED) {
|
|
2
|
+
function ZenbusNextBusNode(config) {
|
|
3
|
+
RED.nodes.createNode(this, config);
|
|
4
|
+
const node = this;
|
|
5
|
+
let timer = null;
|
|
6
|
+
|
|
7
|
+
const interval = (config.interval || 10) * 1000;
|
|
8
|
+
|
|
9
|
+
import('./zenbus-core.mjs').then(({ createClient }) => {
|
|
10
|
+
createClient({
|
|
11
|
+
alias: config.alias,
|
|
12
|
+
itinerary: config.itinerary,
|
|
13
|
+
stop: config.stop,
|
|
14
|
+
}).then(client => {
|
|
15
|
+
async function tick() {
|
|
16
|
+
try {
|
|
17
|
+
const data = await client.poll();
|
|
18
|
+
node.send({ payload: data });
|
|
19
|
+
node.status({
|
|
20
|
+
fill: data.next?.isLive ? 'green' : 'yellow',
|
|
21
|
+
shape: 'dot',
|
|
22
|
+
text: data.next ? `${data.next.etaMinutes} min – ${data.next.distanceM} m` : 'no bus',
|
|
23
|
+
});
|
|
24
|
+
} catch (e) {
|
|
25
|
+
node.error(e.message);
|
|
26
|
+
node.status({ fill: 'red', shape: 'ring', text: e.message });
|
|
27
|
+
}
|
|
28
|
+
timer = setTimeout(tick, interval);
|
|
29
|
+
}
|
|
30
|
+
tick();
|
|
31
|
+
}).catch(e => {
|
|
32
|
+
node.error(e.message);
|
|
33
|
+
node.status({ fill: 'red', shape: 'ring', text: 'init failed' });
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
node.on('close', () => { if (timer) clearTimeout(timer); });
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
RED.nodes.registerType('zenbus-next-bus', ZenbusNextBusNode);
|
|
41
|
+
};
|