commit 6634cc3890a20396855562248e550141b4089954
Author: ctucx <c@ctu.cx>
Date: Mon, 17 Aug 2020 17:28:43 +0200
Author: ctucx <c@ctu.cx>
Date: Mon, 17 Aug 2020 17:28:43 +0200
init
2 files changed, 206 insertions(+), 0 deletions(-)
diff --git a/extension.js b/extension.js @@ -0,0 +1,195 @@ +const Clutter = imports.gi.Clutter; +const St = imports.gi.St; +const Main = imports.ui.main; +const PanelMenu = imports.ui.panelMenu; +const PopupMenu = imports.ui.popupMenu; +const Me = imports.misc.extensionUtils.getCurrentExtension(); +const Mainloop = imports.mainloop; +const Lang = imports.lang; +const Soup = imports.gi.Soup; + +let indicatorItem, backgroudTask, vendorText, cellBand, linkSpeed, cellDistance, modemTemperature; +const httpSession = new Soup.SessionAsync(); + +function init (extensionMeta) { } + +function enable() { + indicatorItem = new FritzboxMenu(); + + updateMenubarState(); + + Main.panel.addToStatusArea('fritzbox-status-menu', indicatorItem, 1); + + backgroudTask = Mainloop.timeout_add_seconds(5, Lang.bind(this, function (){ + updateMenubarState(); + return true; + })); +} + +function disable() { + indicatorItem.destroy(); + Mainloop.source_remove(backgroudTask); +} + +//get data from fritzbox +function getData () { + return new Promise(function(resolve, reject) { + let message = Soup.Message.new('GET', 'http://192.168.178.1:1234/status.json'); + + httpSession.queue_message(message, function(session, message) { + if (message.status_code === 200) { + resolve(JSON.parse(message.response_body.data)); + } else { + reject(Error("FritzBox not reachable")); + } + }); + }); +} + +//pulls data with getData() and updates the data in the applet and menu +function updateMenubarState() { + getData().then(function(response) { + let signalIcon = 'network-cellular-no-route-symbolic'; + let technology = response['data']['modem']['connectedCells'][0]['technology'] + let signalLevel = 0; + + if (technology == 'lte') { + indicatorItem.changeTechnologyIcon('network-cellular-4g-symbolic'); + cellBand.changeIcon('network-cellular-4g-symbolic') + + signalLevel = response['data']['modem']['connectedCells'][0]['rsrp'] + + if (signalLevel > -84) { + signalIcon = 'network-cellular-signal-excellent-symbolic'; + } else if (signalLevel > -85``) { + signalIcon = 'network-cellular-signal-good-symbolic'; + } else if (signalLevel > -103) { + signalIcon = 'network-cellular-signal-ok-symbolic'; + } else if (signalLevel < -111) { + signalIcon = 'network-cellular-signal-weak-symbolic'; + } + } else if (technology == 'umts') { + indicatorItem.changeTechnologyIcon('network-cellular-3g-symbolic'); + cellBand.changeIcon('network-cellular-3g-symbolic') + + signalLevel = response['data']['modem']['connectedCells'][0]['rscp'] + + if (signalLevel < -60) { + signalIcon = 'network-cellular-signal-excellent-symbolic'; + } else if (signalLevel < -75) { + signalIcon = 'network-cellular-signal-good-symbolic'; + } else if (signalLevel < -85) { + signalIcon = 'network-cellular-signal-ok-symbolic'; + } else if (signalLevel > -85) { + signalIcon = 'network-cellular-signal-weak-symbolic'; + } + } else { + indicatorItem.changeTechnologyIcon('network-cellular-no-route-symbolic'); + cellBand.changeIcon('network-cellular-no-route-symbolic'); + } + + indicatorItem.changeSignalIcon(signalIcon); + indicatorItem.changeSignalLabel(signalLevel + 'dBm'); + + //Menu items + vendorText.changeLabel(response["data"]['modem']["vendorText"]); + linkSpeed.changeLabel((response['data']['modem']['linkSpeedRx']/1000) + " / " + (response['data']['modem']['linkSpeedTx']/1000) + " Mbit/s"); + cellBand.changeLabel('Band: ' + response["data"]['modem']['connectedCells'][0]['band']); + cellDistance.changeLabel('Cell distance: ' + (response['data']['modem']['connectedCells'][0]['distance']/1000) + 'km'); + modemTemperature.changeLabel('Temperature: ' + response["data"]['modem']['temperature'] + ' °C'); + + indicatorItem.show(); + }, function(error) { + global.log("BIG OOOF"); + indicatorItem.hide(); + }); +} + +//the menu and applet +const FritzboxMenu = new Lang.Class({ + Name: 'FritzboxStatusMenu', + Extends: PanelMenu.Button, + + _init: function() { + this.parent(0.0, "FritzBox Status"); + + let hbox = new St.BoxLayout({ style_class: 'panel-status-menu-box' }); + + this.technologyIcon = new St.Icon({ icon_name: 'network-cellular-no-route-symbolic', style_class: 'system-status-icon' }); + this.signalIcon = new St.Icon({ icon_name: 'network-cellular-no-route-symbolic', style_class: 'system-status-icon' }); + this.signalLabel = new St.Label({ text: _("no data"), + y_expand: true, y_align: Clutter.ActorAlign.CENTER + }); + + hbox.add_child(this.technologyIcon); + hbox.add_child(this.signalIcon); + hbox.add_child(this.signalLabel); + + this.add_actor(hbox); + + vendorText = new FritzboxMenuItem({icon: "preferences-system-network-symbolic", label: _("NO DATA"), class: "system-apps-icon"}); + this.menu.addMenuItem(vendorText); + + linkSpeed = new FritzboxMenuItem({icon: "network-transmit-receive-symbolic", label: _("NO DATA"), class: "system-actions-icon"}); + this.menu.addMenuItem(linkSpeed); + + cellBand = new FritzboxMenuItem({icon: "network-cellular-no-route-symbolic", label: _("NO DATA"), class: "system-status-icon"}); + this.menu.addMenuItem(cellBand); + + cellDistance = new FritzboxMenuItem({icon: "mark-location-symbolic", label: _("NO DATA"), class: "system-status-icon"}); + this.menu.addMenuItem(cellDistance); + + modemTemperature = new FritzboxMenuItem({icon: "emblem-important-symbolic", label: _("NO DATA"), class: "system-emblems-icon"}); + this.menu.addMenuItem(modemTemperature); + }, + + changeTechnologyIcon: function(newIcon) { + this.technologyIcon.set_icon_name(newIcon); + }, + + changeSignalIcon: function(newIcon) { + this.signalIcon.set_icon_name(newIcon); + }, + + changeSignalLabel: function(text) { + this.signalLabel.set_text(_(text)); + }, + + destroy: function() { + this.parent(); + } +}); + +//helper for items in the menu +const FritzboxMenuItem = new Lang.Class({ + Name: 'FritzboxMenuItem', + Extends: PopupMenu.PopupBaseMenuItem, + + _init: function(info) { + this.parent(); + this._info = info; + this._icon = new St.Icon({ icon_name: info.icon, + style_class: info.class, + icon_size: 16 }); + this.add_child(this._icon); + + this._label = new St.Label({ text: info.label }); + this.actor.add_child(this._label); + }, + + changeIcon: function(newIconName) { + this._icon.set_icon_name(newIconName); + }, + + changeLabel: function(newLabelText) { + this._label.set_text(newLabelText); + }, + + activate: function(event) { + this.parent(event); + }, + + destroy: function() { + this.parent(); + } +});
diff --git a/metadata.json b/metadata.json @@ -0,0 +1,10 @@ +{ + "description": "Shows the status of a FritzBoxLTE with fb-exporter in the Gnome Statusbar.", + "name": "FritzBoxLTE Status Indicator", + "shell-version": [ + "3.20.3" + ], + "url": "https://cgit.ctucx/fritzbox-exporter", + "uuid": "fb_exporter@ctu.cx", + "version": 1 +}+ \ No newline at end of file