#!/usr/bin/env ruby
dir = File.dirname(__FILE__) + '/../lib'
$LOAD_PATH << dir unless $LOAD_PATH.include?(dir)
require 'gtk2'
require 'rubygems'
require 'currentcost'
require 'optparse'
# If EEML is available, start a simple server which will serve power information
begin
require 'eeml'
require 'webrick'
$eeml_server_enabled = true
rescue LoadError
$eeml_server_enabled = false
end
# Command-line options
options = {:port => '/dev/ttyS0'}
OptionParser.new do |opts|
opts.on("-p", "--serial_port SERIAL_PORT", "serial port") do |p|
options[:port] = p
end
if $eeml_server_enabled == true
opts.on("-h", "--http_port HTTP_PORT", "http port") do |h|
options[:http_port] = h
end
end
end.parse!
# Pixbuf images - generated from PNG files with gdk-pixbuf-csource --raw
GREY =
# Pixbuf magic (0x47646b50)
"GdkP" <<
# length: header (24) + pixel_data (1024)
"\0\0\4\30" <<
# pixdata_type (0x1010002)
"\1\1\0\2" <<
# rowstride (64)
"\0\0\0@" <<
# width (16)
"\0\0\0\20" <<
# height (16)
"\0\0\0\20" <<
# pixel_data:
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\351\352\351J\360\360\360\262\353\353" <<
"\353\371\347\347\347\377\337\337\337\377\322\323\322\371\310\310\310" <<
"\262\267\267\267J\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\336" <<
"\336\336\30\366\366\366\270\371\370\370\377\360\360\360\377\350\350\347" <<
"\377\337\337\337\377\327\327\327\377\316\317\317\377\306\306\306\377" <<
"\275\275\276\377\260\257\257\270\232\232\232\30\0\0\0\0\0\0\0\0\0\0\0" <<
"\0\336\336\336\30\371\370\370\335\370\370\370\377\365\365\365\377\347" <<
"\347\350\377\337\337\337\377\327\327\327\377\317\316\316\377\306\306" <<
"\306\377\275\275\276\377\265\265\265\377\255\255\255\377\241\241\241" <<
"\335\213\214\214\30\0\0\0\0\0\0\0\0\366\366\366\270\370\370\370\377\366" <<
"\366\366\377\377\377\377\377\355\355\355\377\327\326\327\377\316\316" <<
"\316\377\306\306\306\377\275\275\275\377\265\265\265\377\255\255\254" <<
"\377\244\244\244\377\234\234\234\377\217\217\217\270\0\0\0\0\351\350" <<
"\350J\370\370\370\377\360\360\360\377\374\374\374\377\377\377\377\377" <<
"\365\366\366\377\316\316\316\377\306\306\306\377\275\275\275\377\265" <<
"\265\265\377\255\255\255\377\244\244\244\377\234\234\234\377\223\223" <<
"\223\377\213\213\213\377}||J\357\357\357\262\360\360\360\377\354\354" <<
"\354\377\377\377\377\377\377\377\377\377\377\377\377\377\314\314\314" <<
"\377\275\275\275\377\275\275\275\377\276\276\276\377\244\244\244\377" <<
"\234\233\233\377\223\223\223\377\213\213\213\377\202\202\203\377www\262" <<
"\352\352\352\371\347\347\347\377\364\364\364\377\377\377\377\377\377" <<
"\377\377\377\377\377\377\377\332\332\332\377\265\265\265\377\354\354" <<
"\354\377\377\377\377\377\275\274\275\377\267\267\267\377\261\261\261" <<
"\377\254\253\253\377\246\246\246\377\237\237\237\373\347\347\347\377" <<
"\342\342\342\377\377\377\377\377\377\377\377\377\377\377\377\377\377" <<
"\377\377\377\356\356\356\377\276\276\277\377\377\377\377\377\377\377" <<
"\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" <<
"\377\377\377\377\377\377\377\377\377\336\336\336\377\354\354\355\377" <<
"\377\377\377\377\377\377\377\377\323\323\323\377\377\377\377\377\377" <<
"\377\377\377\340\340\340\377\377\377\377\377\377\377\377\377\377\377" <<
"\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" <<
"\377\377\377\377\377\325\325\325\372\371\371\371\377\377\377\377\377" <<
"\341\341\341\377\265\265\265\377\377\377\377\377\377\377\377\377\377" <<
"\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\322\322" <<
"\322\377qqr\377iii\377``a\377UUU\371\335\335\335\314\377\377\377\377" <<
"\377\377\377\377\275\275\275\377\254\254\254\377\352\352\352\377\377" <<
"\377\377\377\377\377\377\377\377\377\377\377\271\271\271\377\322\322" <<
"\322\377\200\200\200\377iih\377`aa\377XXX\377NNN\262\343\343\343\206" <<
"\377\377\377\377\356\356\356\377\254\254\254\377\244\243\243\377\274" <<
"\274\274\377\377\377\377\377\377\377\377\377\343\343\343\377yyy\377q" <<
"qq\377iii\377```\377XXX\377OOO\377GGGJ\377\377\377\34\351\351\351\347" <<
"\265\265\265\377\243\244\244\377\233\233\233\377\223\223\223\377\377" <<
"\377\377\377\377\377\377\377\246\246\245\377qqq\377iii\377```\377XXX" <<
"\377OOO\377EEE\270\0\0\0\0\0\0\0\0\224\223\224\30\237\237\237\335\233" <<
"\233\233\377\223\223\223\377\212\212\212\377\307\307\307\377\360\360" <<
"\360\377qqq\377ihh\377```\377XWX\377OPO\377EEE\335=>=\30\0\0\0\0\0\0" <<
"\0\0\0\0\0\0\204\205\205\30\214\215\214\270\212\212\212\377\201\202\202" <<
"\377yyy\377qqq\377hhh\377```\377XXX\377OOO\377EEE\270===\30\0\0\0\0\0" <<
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0yxyJuuu\262lll\371ggg\377^_^\377" <<
"TTS\371MMM\262EFFJ\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
GREEN =
# Pixbuf magic (0x47646b50)
"GdkP" <<
# length: header (24) + pixel_data (1024)
"\0\0\4\30" <<
# pixdata_type (0x1010002)
"\1\1\0\2" <<
# rowstride (64)
"\0\0\0@" <<
# width (16)
"\0\0\0\20" <<
# height (16)
"\0\0\0\20" <<
# pixel_data:
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0a\352aJ7\3607\262\5\353\5\371\0\347\0" <<
"\377\0\337\0\377\4\323\4\371.\310.\262L\267LJ\0\0\0\0\0\0\0\0\0\0\0\0" <<
"\0\0\0\0\0\0\0\0\0\0\0\0j\336j\30""5\3665\270\0\370\0\377\0\360\0\377" <<
"\0\350\0\377\0\337\0\377\0\327\0\377\0\317\0\377\0\306\0\377\0\275\0" <<
"\377&\257%\270I\232I\30\0\0\0\0\0\0\0\0\0\0\0\0j\336j\30\34\370\34\335" <<
"\0\370\0\377U\365U\377\0\347\0\377\0\337\0\377\0\327\0\377\0\316\0\377" <<
"\0\306\0\377\0\275\0\377\0\265\0\377\0\255\0\377\22\241\22\335B\214B" <<
"\30\0\0\0\0\0\0\0\0""5\3665\270\0\370\0\377q\366q\377\377\377\377\377" <<
"q\355q\377\0\326\0\377\0\316\0\377\0\306\0\377\0\275\0\377\0\265\0\377" <<
"\0\255\0\377\0\244\0\377\0\234\0\377\37\217\37\270\0\0\0\0a\350`J\0\370" <<
"\0\377\0\360\0\377\343\374\343\377\377\377\377\377\306\366\306\377\0" <<
"\316\0\377\0\306\0\377\0\275\0\377\0\265\0\377\0\255\0\377\0\244\0\377" <<
"\0\234\0\377\0\223\0\377\0\213\0\3774|3J7\3577\262\0\360\0\3779\3549" <<
"\377\377\377\377\377\377\377\377\377\377\377\377\377\34\314\34\377\0" <<
"\275\0\377\34\275\34\3779\2769\377\0\244\0\377\0\233\0\377\0\223\0\377" <<
"\0\213\0\377\0\202\0\377\33w\33\262\5\352\5\371\0\347\0\377\252\364\252" <<
"\377\377\377\377\377\377\377\377\377\377\377\377\377q\332q\377\0\265" <<
"\0\377\306\354\306\377\377\377\377\377U\274U\377U\267U\377U\261U\377" <<
"U\253U\377U\246U\377W\237W\373\0\347\0\377\34\342\34\377\377\377\377" <<
"\377\377\377\377\377\377\377\377\377\377\377\377\377\306\356\306\377" <<
"9\2769\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" <<
"\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" <<
"\0\336\0\377\216\354\216\377\377\377\377\377\377\377\377\377U\323U\377" <<
"\377\377\377\377\377\377\377\377\252\340\252\377\377\377\377\377\377" <<
"\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" <<
"\377\377\377\377\377\377\377\377\377\377\40\325\40\372\343\371\343\377" <<
"\377\377\377\377\216\341\216\377\0\265\0\377\377\377\377\377\377\377" <<
"\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" <<
"\377\252\322\252\377\0q\0\377\0i\0\377\0`\0\377\2U\2\371\204\335\204" <<
"\314\377\377\377\377\377\377\377\377\34\275\34\377\0\254\0\377\306\352" <<
"\306\377\377\377\377\377\377\377\377\377\377\377\377\377q\271q\377\252" <<
"\322\252\377\34\200\34\377\0i\0\377\0a\0\377\0X\0\377\22N\22\262\274" <<
"\343\274\206\377\377\377\377\306\356\306\377\0\254\0\377\0\243\0\377" <<
"U\274U\377\377\377\377\377\377\377\377\377\306\343\306\377\0y\0\377\0" <<
"q\0\377\0i\0\377\0`\0\377\0X\0\377\0O\0\377\35G\35J\377\377\377\34\305" <<
"\351\305\347\34\265\34\377\0\244\0\377\0\233\0\377\0\223\0\377\377\377" <<
"\377\377\377\377\377\377U\246U\377\0q\0\377\0i\0\377\0`\0\377\0X\0\377" <<
"\0O\0\377\16E\16\270\0\0\0\0\0\0\0\0F\223F\30\22\237\22\335\0\233\0\377" <<
"\0\223\0\377\0\212\0\377\216\307\216\377\343\360\343\377\0q\0\377\0h" <<
"\0\377\0`\0\377\0W\0\377\0P\0\377\10E\10\335\35>\35\30\0\0\0\0\0\0\0" <<
"\0\0\0\0\0\77\205\77\30\36\215\36\270\0\212\0\377\0\202\0\377\0y\0\377" <<
"\0q\0\377\0h\0\377\0`\0\377\0X\0\377\0O\0\377\16E\16\270\35=\35\30\0" <<
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0""2x2J\33u\33\262\2l\2" <<
"\371\0g\0\377\0_\0\377\1T\1\371\21M\21\262\34F\35J\0\0\0\0\0\0\0\0\0" <<
"\0\0\0\0\0\0\0"
RED =
# Pixbuf magic (0x47646b50)
"GdkP" <<
# length: header (24) + pixel_data (1024)
"\0\0\4\30" <<
# pixdata_type (0x1010002)
"\1\1\0\2" <<
# rowstride (64)
"\0\0\0@" <<
# width (16)
"\0\0\0\20" <<
# height (16)
"\0\0\0\20" <<
# pixel_data:
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\351aaJ\36077\262\353\5\5\371\347\0\0" <<
"\377\337\0\0\377\322\4\4\371\310..\262\267LLJ\0\0\0\0\0\0\0\0\0\0\0\0" <<
"\0\0\0\0\0\0\0\0\0\0\0\0\336jj\30\36655\270\371\0\0\377\360\0\0\377\350" <<
"\0\0\377\337\0\0\377\327\0\0\377\316\0\0\377\306\0\0\377\275\0\0\377" <<
"\260%%\270\232II\30\0\0\0\0\0\0\0\0\0\0\0\0\336jj\30\371\34\34\335\370" <<
"\0\0\377\365UU\377\347\0\0\377\337\0\0\377\327\0\0\377\317\0\0\377\306" <<
"\0\0\377\275\0\0\377\265\0\0\377\255\0\0\377\241\22\22\335\213BB\30\0" <<
"\0\0\0\0\0\0\0\36655\270\370\0\0\377\366qq\377\377\377\377\377\355qq" <<
"\377\327\0\0\377\316\0\0\377\306\0\0\377\275\0\0\377\265\0\0\377\255" <<
"\0\0\377\244\0\0\377\234\0\0\377\217\37\37\270\0\0\0\0\351``J\370\0\0" <<
"\377\360\0\0\377\374\343\343\377\377\377\377\377\365\306\306\377\316" <<
"\0\0\377\306\0\0\377\275\0\0\377\265\0\0\377\255\0\0\377\244\0\0\377" <<
"\234\0\0\377\223\0\0\377\213\0\0\377}33J\35777\262\360\0\0\377\35499" <<
"\377\377\377\377\377\377\377\377\377\377\377\377\377\314\34\34\377\275" <<
"\0\0\377\275\34\34\377\27699\377\244\0\0\377\234\0\0\377\223\0\0\377" <<
"\213\0\0\377\202\0\0\377w\33\33\262\352\5\5\371\347\0\0\377\364\252\252" <<
"\377\377\377\377\377\377\377\377\377\377\377\377\377\332qq\377\265\0" <<
"\0\377\354\306\306\377\377\377\377\377\275UU\377\267UU\377\261UU\377" <<
"\254UU\377\246UU\377\237WW\373\347\0\0\377\342\34\34\377\377\377\377" <<
"\377\377\377\377\377\377\377\377\377\377\377\377\377\356\306\306\377" <<
"\27699\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" <<
"\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" <<
"\336\0\0\377\354\216\216\377\377\377\377\377\377\377\377\377\323UU\377" <<
"\377\377\377\377\377\377\377\377\340\252\252\377\377\377\377\377\377" <<
"\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" <<
"\377\377\377\377\377\377\377\377\377\377\325\40\40\372\371\343\343\377" <<
"\377\377\377\377\341\216\216\377\265\0\0\377\377\377\377\377\377\377" <<
"\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" <<
"\377\322\252\252\377q\0\0\377i\0\0\377`\0\0\377U\2\2\371\335\204\204" <<
"\314\377\377\377\377\377\377\377\377\275\34\34\377\254\0\0\377\352\306" <<
"\306\377\377\377\377\377\377\377\377\377\377\377\377\377\271qq\377\322" <<
"\252\252\377\200\34\34\377i\0\0\377`\0\0\377X\0\0\377N\22\22\262\343" <<
"\274\274\206\377\377\377\377\356\306\306\377\254\0\0\377\244\0\0\377" <<
"\274UU\377\377\377\377\377\377\377\377\377\343\306\306\377y\0\0\377q" <<
"\0\0\377i\0\0\377`\0\0\377X\0\0\377O\0\0\377G\35\35J\377\377\377\34\351" <<
"\305\305\347\265\34\34\377\243\0\0\377\233\0\0\377\223\0\0\377\377\377" <<
"\377\377\377\377\377\377\246UU\377q\0\0\377i\0\0\377`\0\0\377X\0\0\377" <<
"O\0\0\377E\16\16\270\0\0\0\0\0\0\0\0\224FF\30\237\22\22\335\233\0\0\377" <<
"\223\0\0\377\212\0\0\377\307\216\216\377\360\343\343\377q\0\0\377i\0" <<
"\0\377`\0\0\377X\0\0\377O\0\0\377E\10\10\335=\35\35\30\0\0\0\0\0\0\0" <<
"\0\0\0\0\0\204\77\77\30\214\36\36\270\212\0\0\377\201\0\0\377y\0\0\377" <<
"q\0\0\377h\0\0\377`\0\0\377X\0\0\377O\0\0\377E\16\16\270=\35\35\30\0" <<
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0y22Ju\33\33\262l\2\2\371" <<
"g\0\0\377^\0\0\377T\1\1\371M\21\21\262E\35\35J\0\0\0\0\0\0\0\0\0\0\0" <<
"\0\0\0\0\0"
YELLOW =
# Pixbuf magic (0x47646b50)
"GdkP" <<
# length: header (24) + pixel_data (1024)
"\0\0\4\30" <<
# pixdata_type (0x1010002)
"\1\1\0\2" <<
# rowstride (64)
"\0\0\0@" <<
# width (16)
"\0\0\0\20" <<
# height (16)
"\0\0\0\20" <<
# pixel_data:
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\351\352aJ\360\3607\262\353\353\5\371" <<
"\347\347\0\377\337\337\0\377\322\323\4\371\310\310.\262\267\267LJ\0\0" <<
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\336\336j\30\366\3665\270" <<
"\371\370\0\377\360\360\0\377\350\350\0\377\337\337\0\377\327\327\0\377" <<
"\316\317\0\377\306\306\0\377\275\275\0\377\260\257%\270\232\232I\30\0" <<
"\0\0\0\0\0\0\0\0\0\0\0\336\336j\30\371\370\34\335\370\370\0\377\365\365" <<
"U\377\347\347\0\377\337\337\0\377\327\327\0\377\317\316\0\377\306\306" <<
"\0\377\275\275\0\377\265\265\0\377\255\255\0\377\241\241\22\335\213\214" <<
"B\30\0\0\0\0\0\0\0\0\366\3665\270\370\370\0\377\366\366q\377\377\377" <<
"\377\377\355\355q\377\327\326\0\377\316\316\0\377\306\306\0\377\275\275" <<
"\0\377\265\265\0\377\255\255\0\377\244\244\0\377\234\234\0\377\217\217" <<
"\37\270\0\0\0\0\351\350`J\370\370\0\377\360\360\0\377\374\374\343\377" <<
"\377\377\377\377\365\366\306\377\316\316\0\377\306\306\0\377\275\275" <<
"\0\377\265\265\0\377\255\255\0\377\244\244\0\377\234\234\0\377\223\223" <<
"\0\377\213\213\0\377}|3J\357\3577\262\360\360\0\377\354\3549\377\377" <<
"\377\377\377\377\377\377\377\377\377\377\377\314\314\34\377\275\275\0" <<
"\377\275\275\34\377\276\2769\377\244\244\0\377\234\233\0\377\223\223" <<
"\0\377\213\213\0\377\202\202\0\377ww\33\262\352\352\5\371\347\347\0\377" <<
"\364\364\252\377\377\377\377\377\377\377\377\377\377\377\377\377\332" <<
"\332q\377\265\265\0\377\354\354\306\377\377\377\377\377\275\274U\377" <<
"\267\267U\377\261\261U\377\254\253U\377\246\246U\377\237\237W\373\347" <<
"\347\0\377\342\342\34\377\377\377\377\377\377\377\377\377\377\377\377" <<
"\377\377\377\377\377\356\356\306\377\276\2769\377\377\377\377\377\377" <<
"\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" <<
"\377\377\377\377\377\377\377\377\377\377\336\336\0\377\354\354\216\377" <<
"\377\377\377\377\377\377\377\377\323\323U\377\377\377\377\377\377\377" <<
"\377\377\340\340\252\377\377\377\377\377\377\377\377\377\377\377\377" <<
"\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" <<
"\377\377\377\377\325\325\40\372\371\371\343\377\377\377\377\377\341\341" <<
"\216\377\265\265\0\377\377\377\377\377\377\377\377\377\377\377\377\377" <<
"\377\377\377\377\377\377\377\377\377\377\377\377\322\322\252\377qq\0" <<
"\377ii\0\377``\0\377UU\2\371\335\335\204\314\377\377\377\377\377\377" <<
"\377\377\275\275\34\377\254\254\0\377\352\352\306\377\377\377\377\377" <<
"\377\377\377\377\377\377\377\377\271\271q\377\322\322\252\377\200\200" <<
"\34\377ii\0\377`a\0\377XX\0\377NN\22\262\343\343\274\206\377\377\377" <<
"\377\356\356\306\377\254\254\0\377\244\243\0\377\274\274U\377\377\377" <<
"\377\377\377\377\377\377\343\343\306\377yy\0\377qq\0\377ii\0\377``\0" <<
"\377XX\0\377OO\0\377GG\35J\377\377\377\34\351\351\305\347\265\265\34" <<
"\377\243\244\0\377\233\233\0\377\223\223\0\377\377\377\377\377\377\377" <<
"\377\377\246\246U\377qq\0\377ii\0\377``\0\377XX\0\377OO\0\377EE\16\270" <<
"\0\0\0\0\0\0\0\0\224\223F\30\237\237\22\335\233\233\0\377\223\223\0\377" <<
"\212\212\0\377\307\307\216\377\360\360\343\377qq\0\377ih\0\377``\0\377" <<
"XW\0\377OP\0\377EE\10\335=>\35\30\0\0\0\0\0\0\0\0\0\0\0\0\204\205\77" <<
"\30\214\215\36\270\212\212\0\377\201\202\0\377yy\0\377qq\0\377hh\0\377" <<
"``\0\377XX\0\377OO\0\377EE\16\270==\35\30\0\0\0\0\0\0\0\0\0\0\0\0\0\0" <<
"\0\0\0\0\0\0\0\0\0\0yx2Juu\33\262ll\2\371gg\0\377^_\0\377TT\1\371MM\21" <<
"\262EF\35J\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
$grey_pixbuf = Gdk::Pixbuf.new(GREY.unpack("C*"), true)
$green_pixbuf = Gdk::Pixbuf.new(GREEN.unpack("C*"), true)
$red_pixbuf = Gdk::Pixbuf.new(RED.unpack("C*"), true)
$yellow_pixbuf = Gdk::Pixbuf.new(YELLOW.unpack("C*"), true)
# build the UI
$tray = Gtk::StatusIcon.new
$tray.pixbuf = $grey_pixbuf
$menu = Gtk::Menu.new
about_item = Gtk::ImageMenuItem.new(Gtk::Stock::ABOUT)
quit_item = Gtk::ImageMenuItem.new(Gtk::Stock::QUIT)
$menu.append(about_item)
$menu.append(Gtk::SeparatorMenuItem.new)
$menu.append(quit_item)
$menu.show_all
about_item.signal_connect('activate') {
about = Gtk::AboutDialog.new
about.signal_connect('response') {
about.destroy
}
about.name = 'CurrentCost Tray Monitor'
about.version = '0.1'
about.copyright = '© 2008 James Smith'
about.comments = 'Heavily based on CCTrayRb: http://rubyforge.org/projects/cctrayrb/'
about.license = 'MIT License - see http://github.com/Floppy/currentcost-ruby/tree/master/COPYING'
about.website = 'http://github.com/Floppy/currentcost-ruby/wikis/currentcosttraymonitor'
about.authors = ['James Smith - james@floppy.org.uk']
about.show_all
}
quit_item.signal_connect('activate') {
$meter.close
$eeml_server.shutdown if $eeml_server_enabled && $eeml_server.status == :Running
Gtk.main_quit
}
$tray.signal_connect('popup_menu') { |icon, button, time|
if button == 3 # right mouse button
$menu.popup(nil, nil, button, time)
end
}
# Meter observer
class MeterObserver
def update(reading)
# Add all channels to get real figure
watts = 0
reading.channels.each { |c| watts += c[:watts] }
# Change icon colour based on power usage
case watts
when 0..349
$tray.pixbuf = $green_pixbuf
when 350..999
$tray.pixbuf = $yellow_pixbuf
else
$tray.pixbuf = $red_pixbuf
end
# Set tooltip
$tray.tooltip = "#{watts} watts"
# Update EEML data
if $eeml_server_enabled && $eeml_server.status == :Running
$eeml_environment[0].value = watts
$eeml_environment.set_updated!
end
rescue
$tray.pixbuf = $grey_pixbuf
end
end
# Create CurrentCost meter connection
$meter = CurrentCost::Meter.new(options[:port])
obs = MeterObserver.new
$meter.add_observer(obs)
# Create EEML HTTP server if options are set
if $eeml_server_enabled && options[:http_port]
# Create EEML environment
$eeml_environment = EEML::Environment.new
# Create data object
data = EEML::Data.new(0)
data.unit = EEML::Unit.new("Watts", :symbol => 'W', :type => :derivedSI)
$eeml_environment << data
# Create WEBrick server
$eeml_server = WEBrick::HTTPServer.new( :Port => options[:http_port] )
# Create a simple webrick servlet for index.eeml
class EEMLServlet < WEBrick::HTTPServlet::AbstractServlet
def do_GET(request, response)
response.status = 200
response['Content-Type'] = "text/xml"
response.body = $eeml_environment.to_eeml
end
end
$eeml_server.mount("/index.eeml", EEMLServlet)
trap("INT") {$eeml_server.shutdown}
Thread.new{$eeml_server.start}
end
# Go
Gtk.main