Skip to content

Instantly share code, notes, and snippets.

@Pipeliner
Created September 25, 2012 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pipeliner/3784802 to your computer and use it in GitHub Desktop.
Save Pipeliner/3784802 to your computer and use it in GitHub Desktop.
Modem tray notifier
require 'fiddle'
def set_process_name name
RUBY_PLATFORM =~ /linux/ or return
Fiddle::Function.new(
DL::Handle['prctl'], [
Fiddle::TYPE_INT, Fiddle::TYPE_VOIDP,
Fiddle::TYPE_LONG, Fiddle::TYPE_LONG,
Fiddle::TYPE_LONG
], Fiddle::TYPE_INT
).call(15, name.to_s, 0, 0, 0)
end
#!/usr/bin/env ruby
# encoding: utf-8
trap ("HUP") { puts "Hupped!"; }
require 'Qt4'
require 'open-uri'
#require './prctl'
class HuaweiTrayIcon < Qt::SystemTrayIcon
def initialize()
super(Qt::Icon.new("smallhua.gif"))
@@me = self
menu = Qt::Menu.new("Huawei E5830 Status")
setContextMenu(menu)
ma = Qt::Action.new("&Quit", nil)
Qt::Object::connect(ma, SIGNAL('triggered()'), $qApp, SLOT('quit()'))
menu.addAction(ma)
timer = Qt::Timer.new(self) do
connect(SIGNAL :timeout) { @@me.show_notification }
end
timer.start 20_000
end
def event(e)
super
show_notification true
end
def show_notification(force=false)
network_types = ["No network available", "GSM","GPRS","EDGE","WCDMA","HSDPA","HSUPA","HSPA"]
begin
open("http://195.198.1.1/en/conn.asp").read.lines do |line|
if /^var sysinfo = \[\d,\d,\d,\d,\d,\d,(?<network_index>\d)\];$/ =~ line
@@network = network_types[network_index.to_i]
end
if /^var ppp_state = (?<ppp_state>\d);$/ =~ line
@@ppp = ppp_state == "1"
end
end
rescue Exception => exc
@@network = "No response from Huawei E5830: " + exc.message.force_encoding("UTF-8")
@@ppp = false
end
if force || @@network != @@old_network || @@ppp != @@old_ppp
message = @@network + (@@ppp ? ": <font color=green>✔" : ": <font color=red>✘")
%x{notify-send -i 'dialog-information' 'Huawei E5830 Status' '#{message}'}
@@old_ppp = @@ppp
@@old_network = @@network
end
end
end
a = Qt::Application.new(ARGV)
icon = HuaweiTrayIcon.new
icon.show
a.exec()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment