initial commit

This commit is contained in:
2026-10-31 13:28:34 +01:00
parent 189529e505
commit 40f1db79f0
2 changed files with 424 additions and 0 deletions
+418
View File
@@ -0,0 +1,418 @@
import Clutter from 'gi://Clutter';
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import NM from 'gi://NM';
import St from 'gi://St';
import {Extension} from 'resource:///org/gnome/shell/extensions/extension.js';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import * as PanelMenu from 'resource:///org/gnome/shell/ui/panelMenu.js';
const ICE_SSIDS = new Set([
'WIFIonICE',
'WIFI@DB',
]);
const STATUS_URL = 'https://iceportal.de/api1/rs/status';
const PORTAL_URL = 'https://iceportal.de/';
const UPDATE_INTERVAL_SECONDS = 10;
export default class ICESpeedExtension extends Extension {
enable() {
this._timerId = null;
this._requestProcess = null;
this._requestCancellable = null;
this._requestRunning = false;
this._requestGeneration = 0;
this._onIceWifi = false;
this._wifiDeviceSignals = new Map();
/*
* Panel indicator
*
* It exists internally, but is invisible unless we're connected
* to the ICE Wi-Fi.
*/
this._button = new PanelMenu.Button(
0.0,
this.metadata.name,
true
);
this._label = new St.Label({
text: '🚄 -- km/h',
y_align: Clutter.ActorAlign.CENTER,
});
this._button.add_child(this._label);
this._button.hide();
/*
* Left click -> open ICE Portal.
*/
this._buttonSignalId = this._button.connect(
'button-press-event',
(_actor, event) => {
if (event.get_button() === 1) {
Gio.AppInfo.launch_default_for_uri(
PORTAL_URL,
null
);
return Clutter.EVENT_STOP;
}
return Clutter.EVENT_PROPAGATE;
}
);
Main.panel.addToStatusArea(
this.uuid,
this._button,
0,
'right'
);
/*
* NetworkManager.
*
* No periodic polling takes place here. We only react to
* NetworkManager events.
*/
this._nmClient = NM.Client.new(null);
this._deviceAddedSignalId = this._nmClient.connect(
'device-added',
(_client, device) => {
this._watchDevice(device);
this._checkWifiState();
}
);
this._deviceRemovedSignalId = this._nmClient.connect(
'device-removed',
(_client, device) => {
this._unwatchDevice(device);
this._checkWifiState();
}
);
/*
* Watch all Wi-Fi devices already present.
*/
for (const device of this._nmClient.get_devices())
this._watchDevice(device);
/*
* One initial check, in case the extension gets enabled while
* we're already connected to WIFIonICE.
*/
this._checkWifiState();
}
_watchDevice(device) {
if (device.get_device_type() !== NM.DeviceType.WIFI)
return;
if (this._wifiDeviceSignals.has(device))
return;
/*
* This fires when a Wi-Fi device connects to/disconnects from
* an AP or switches APs.
*/
const signalId = device.connect(
'notify::active-access-point',
() => {
this._checkWifiState();
}
);
this._wifiDeviceSignals.set(device, signalId);
}
_unwatchDevice(device) {
if (!this._wifiDeviceSignals.has(device))
return;
const signalId = this._wifiDeviceSignals.get(device);
device.disconnect(signalId);
this._wifiDeviceSignals.delete(device);
}
_getCurrentWifiSsid() {
for (const device of this._nmClient.get_devices()) {
if (device.get_device_type() !== NM.DeviceType.WIFI)
continue;
const accessPoint = device.get_active_access_point();
if (!accessPoint)
continue;
const ssidBytes = accessPoint.get_ssid();
if (!ssidBytes)
continue;
try {
return new TextDecoder('utf-8').decode(
ssidBytes.get_data()
);
} catch (e) {
console.error(`ICE Speed: Could not decode SSID: ${e}`);
}
}
return null;
}
_checkWifiState() {
const ssid = this._getCurrentWifiSsid();
const isIceWifi =
ssid !== null &&
ICE_SSIDS.has(ssid);
/*
* Nothing changed -> do absolutely nothing.
*/
if (isIceWifi === this._onIceWifi)
return;
this._onIceWifi = isIceWifi;
if (isIceWifi)
this._startIceMode();
else
this._stopIceMode();
}
_startIceMode() {
/*
* Show indicator only while actually connected to ICE Wi-Fi.
*/
this._label.set_text('🚄 -- km/h');
this._button.show();
/*
* Query immediately instead of waiting 10 seconds.
*/
this._updateSpeed();
/*
* Only now do we create a repeating timer.
*/
this._timerId = GLib.timeout_add_seconds(
GLib.PRIORITY_DEFAULT,
UPDATE_INTERVAL_SECONDS,
() => {
this._updateSpeed();
return GLib.SOURCE_CONTINUE;
}
);
}
_stopIceMode() {
/*
* Stop timer completely.
*/
if (this._timerId !== null) {
GLib.Source.remove(this._timerId);
this._timerId = null;
}
/*
* Invalidate any result from an older request.
*/
this._requestGeneration++;
/*
* Cancel an in-progress curl invocation.
*/
if (this._requestCancellable !== null)
this._requestCancellable.cancel();
if (this._requestProcess !== null)
this._requestProcess.force_exit();
this._requestCancellable = null;
this._requestProcess = null;
this._requestRunning = false;
/*
* Completely disappear from panel.
*/
if (this._button !== null)
this._button.hide();
}
_updateSpeed() {
if (!this._onIceWifi)
return;
/*
* Don't start a second request if the previous one is still
* running.
*/
if (this._requestRunning)
return;
this._requestRunning = true;
const generation = ++this._requestGeneration;
const cancellable = new Gio.Cancellable();
this._requestCancellable = cancellable;
let process;
try {
process = Gio.Subprocess.new(
[
'curl',
'--silent',
'--show-error',
'--fail',
/*
* Never let a broken portal request hang around.
*/
'--connect-timeout',
'2',
'--max-time',
'4',
/*
* Avoid receiving a cached status response.
*/
'--header',
'Cache-Control: no-cache',
STATUS_URL,
],
Gio.SubprocessFlags.STDOUT_PIPE |
Gio.SubprocessFlags.STDERR_PIPE
);
} catch (e) {
this._requestRunning = false;
this._requestCancellable = null;
console.error(`ICE Speed: Could not start curl: ${e}`);
return;
}
this._requestProcess = process;
process.communicate_utf8_async(
null,
cancellable,
(proc, result) => {
let stdout = null;
let stderr = null;
try {
[, stdout, stderr] =
proc.communicate_utf8_finish(result);
} catch (e) {
/*
* Cancellation when leaving the Wi-Fi is expected,
* so don't spam the journal in that case.
*/
if (!cancellable.is_cancelled())
console.error(`ICE Speed: curl failed: ${e}`);
}
if (this._requestProcess === proc)
this._requestProcess = null;
if (this._requestCancellable === cancellable)
this._requestCancellable = null;
this._requestRunning = false;
/*
* The connection may have changed while curl was
* running. Never display a stale result.
*/
if (
!this._onIceWifi ||
generation !== this._requestGeneration
) {
return;
}
if (!proc.get_successful()) {
this._label.set_text('🚄 -- km/h');
if (stderr)
console.error(`ICE Speed: ${stderr.trim()}`);
return;
}
try {
const data = JSON.parse(stdout);
const speed = Number(data.speed);
if (Number.isFinite(speed)) {
this._label.set_text(
`🚄 ${Math.round(speed)} km/h`
);
} else {
this._label.set_text('🚄 -- km/h');
}
} catch (e) {
this._label.set_text('🚄 -- km/h');
console.error(`ICE Speed: Invalid JSON: ${e}`);
}
}
);
}
disable() {
this._onIceWifi = false;
this._stopIceMode();
if (this._nmClient !== null) {
if (this._deviceAddedSignalId !== null)
this._nmClient.disconnect(this._deviceAddedSignalId);
if (this._deviceRemovedSignalId !== null)
this._nmClient.disconnect(this._deviceRemovedSignalId);
}
for (const [device, signalId] of this._wifiDeviceSignals)
device.disconnect(signalId);
this._wifiDeviceSignals.clear();
if (this._buttonSignalId !== null)
this._button.disconnect(this._buttonSignalId);
this._button.destroy();
this._button = null;
this._label = null;
this._nmClient = null;
this._buttonSignalId = null;
this._deviceAddedSignalId = null;
this._deviceRemovedSignalId = null;
this._wifiDeviceSignals = null;
}
}
+6
View File
@@ -0,0 +1,6 @@
{
"uuid": "icespeed@local",
"name": "ICE Speed",
"description": "Shows the current ICE speed in the GNOME panel",
"shell-version": ["50"]
}