-
Notifications
You must be signed in to change notification settings - Fork 1
/
extension.js
169 lines (139 loc) · 4.54 KB
/
extension.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const { St } = imports.gi;
const Main = imports.ui.main;
const { Soup } = imports.gi;
const Lang = imports.lang;
const Mainloop = imports.mainloop;
const { Clutter } = imports.gi;
const PanelMenu = imports.ui.panelMenu;
const API_BASE = 'https://iceportal.de/api1/rs';
let session;
const ICEPortalIndicator = new Lang.Class({
Name: 'ICEPortal',
Extends: PanelMenu.Button,
_init() {
this.parent(0.0, 'ICE Portal indicator', false);
this.buttonText = new St.Label({
text: _(''),
y_align: Clutter.ActorAlign.CENTER,
});
this.actor.add_actor(this.buttonText);
this.refresh();
},
refresh() {
this.loadData(this.refreshUI);
this.removeTimeout();
this.timeout = Mainloop.timeout_add_seconds(
7,
Lang.bind(this, this.refresh),
);
return true;
},
loadData() {
session = new Soup.Session({
'user-agent': 'DB ICE Portal GNOME Shell extension',
});
const requestStatus = new Promise((resolve, reject) => {
const request = Soup.form_request_new_from_hash(
'GET',
`${API_BASE}/status`,
{},
);
session.queue_message(
request,
Lang.bind(this, (_, message) => {
if (message.status_code !== 200) {
global.log(
`ICE Portal ${message.status_code} ${message.response_body.data}`,
);
reject(message);
}
resolve(JSON.parse(message.response_body.data));
}),
);
});
const requestTrip = new Promise((resolve, reject) => {
const request = Soup.form_request_new_from_hash(
'GET',
`${API_BASE}/tripInfo/trip`,
{},
);
session.queue_message(
request,
Lang.bind(this, (_, message) => {
if (message.status_code !== 200) {
global.log(
`ICE Portal ${message.status_code} ${message.response_body.data}`,
);
reject(message);
}
resolve(JSON.parse(message.response_body.data));
}),
);
});
Promise.all([requestStatus, requestTrip]).then(
([resultStatus, resultTrip]) => {
// If there is an actual GPS signal, the gpsStatus is "VALID"
// If we don’t do this, we might set the displayed speed to 0 in tunnels
if (resultStatus.gpsStatus !== 'VALID') {
return;
}
const wifiString = this.wifiSymbol(resultStatus.internet);
const nextStop = resultTrip.trip.stops.find(
stop => stop.station.evaNr === resultTrip.trip.stopInfo.actualNext,
);
if (nextStop) {
const nextArrival = new Date(nextStop.timetable.actualArrivalTime);
const nextArrivalHour = nextArrival.getHours();
const nextArrivalMinute = nextArrival.getMinutes();
const delay = nextStop.timetable.arrivalDelay;
const delayString = delay === '' ? '' : ` (${delay})`;
const text = `${resultTrip.trip.trainType} ${resultTrip.trip.vzn} → ${resultTrip.trip.stopInfo.finalStationName} | WLAN: ${wifiString} | ${resultStatus.speed} km/h | ${nextStop.station.name} 🕒 ${nextArrivalHour}:${nextArrivalMinute}${delayString} 🛤 ${nextStop.track.actual}`;
this.refreshUI(text);
} else {
const text = `${resultTrip.trip.trainType} ${resultTrip.trip.vzn} → ${resultTrip.trip.stopInfo.finalStationName} | WLAN: ${wifiString} | ${resultStatus.speed} km/h`;
global.log('ICE Portal nextStop does not exist in trip.');
this.refreshUI(text);
}
},
);
},
// switch-case variable assignment? Nah.
wifiSymbol(internetStatus) {
return (
{
HIGH: 'Good', // Verified on board
MIDDLE: 'Meh', // Verified on board
LOW: 'De facto offline', // TODO: Verify existance on board
}[internetStatus] || '?'
);
},
refreshUI(txt) {
this.buttonText.set_text(txt);
},
removeTimeout() {
if (this.timeout) {
Mainloop.source_remove(this.timeout);
this.timeout = null;
}
},
stop() {
if (session !== undefined) session.abort();
session = undefined;
if (this.timeout) Mainloop.source_remove(this.timeout);
this.timeout = undefined;
this.menu.removeAll();
},
});
let iceMenu;
// eslint-disable-next-line no-unused-vars
function init() {}
// eslint-disable-next-line no-unused-vars
function enable() {
iceMenu = new ICEPortalIndicator();
Main.panel.addToStatusArea('iceportal-indicator', iceMenu);
}
// eslint-disable-next-line no-unused-vars
function disable() {
iceMenu.stop();
iceMenu.destroy();
}