nut/scripts/python/app/NUT-Monitor-py3qt5.in

945 lines
44 KiB
Text
Raw Normal View History

2022-06-29 10:37:36 +00:00
#!@PYTHON3@
2010-03-25 23:20:59 +00:00
# -*- coding: utf-8 -*-
2011-01-26 09:35:08 +00:00
# 2009-12-27 David Goncalves - Version 1.2
# Total rewrite of NUT-Monitor to optimize GUI interaction.
# Added favorites support (saved to user's home)
# Added status icon on the notification area
2010-03-25 23:20:59 +00:00
#
2011-01-26 09:35:08 +00:00
# 2010-02-26 David Goncalves
# Added UPS vars display and the possibility to change values
# when user double-clicks on a RW var.
2010-03-25 23:20:59 +00:00
#
2011-01-26 09:35:08 +00:00
# 2010-05-01 David Goncalves
# Added support for PyNotify (if available)
2010-03-25 23:20:59 +00:00
#
2011-01-26 09:35:08 +00:00
# 2010-05-05 David Goncalves
# Added support for command line options
# -> --start-hidden
# -> --favorite
#
# NUT-Monitor now tries to detect if there is a NUT server
# on localhost and if there is 1 UPS, connects to it.
#
# 2010-10-06 David Goncalves - Version 1.3
# Added localisation support
2015-04-30 13:53:36 +00:00
#
# 2015-02-14 Michal Fincham - Version 1.3.1
# Corrected unsafe permissions on ~/.nut-monitor (Debian #777706)
2022-06-29 10:37:36 +00:00
#
# 2022-02-20 Luke Dashjr - Version 2.0
# Port to Python 3 with PyQt5.
2010-03-25 23:20:59 +00:00
2022-06-29 10:37:36 +00:00
import PyQt5.uic
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
2011-01-26 09:35:08 +00:00
import sys
import base64
import os, os.path
2015-04-30 13:53:36 +00:00
import stat
2011-01-26 09:35:08 +00:00
import platform
import time
2010-03-25 23:20:59 +00:00
import threading
2011-01-26 09:35:08 +00:00
import optparse
2022-06-29 10:37:36 +00:00
import configparser
2011-01-26 09:35:08 +00:00
import locale
import gettext
2010-03-25 23:20:59 +00:00
import PyNUT
2011-01-26 09:35:08 +00:00
class interface :
2022-06-29 10:37:36 +00:00
DESIRED_FAVORITES_DIRECTORY_MODE = 0o700
2015-04-30 13:53:36 +00:00
__widgets = {}
__callbacks = {}
__favorites = {}
__favorites_file = None
__favorites_path = ""
__fav_menu_items = list()
__window_visible = True
2022-06-29 10:37:36 +00:00
__ui_file = None
2015-04-30 13:53:36 +00:00
__connected = False
__ups_handler = None
__ups_commands = None
__ups_vars = None
__ups_rw_vars = None
__gui_thread = None
__current_ups = None
2010-03-25 23:20:59 +00:00
2022-06-29 10:37:36 +00:00
def __init__( self, argv ) :
2010-03-25 23:20:59 +00:00
2011-01-26 09:35:08 +00:00
# Before anything, parse command line options if any present...
opt_parser = optparse.OptionParser()
opt_parser.add_option( "-H", "--start-hidden", action="store_true", default=False, dest="hidden", help="Start iconified in tray" )
opt_parser.add_option( "-F", "--favorite", dest="favorite", help="Load the specified favorite and connect to UPS" )
( cmd_opts, args ) = opt_parser.parse_args()
2022-06-29 10:37:36 +00:00
self.__app = QApplication( argv )
self.__ui_file = self.__find_res_file( 'ui', "window1.ui" )
self.__widgets["interface"] = PyQt5.uic.loadUi( self.__ui_file )
self.__widgets["main_window"] = self.__widgets["interface"]
self.__widgets["status_bar"] = self.__widgets["interface"].statusbar2
self.__widgets["ups_host_entry"] = self.__widgets["interface"].entry1
self.__widgets["ups_port_entry"] = self.__widgets["interface"].spinbutton1
self.__widgets["ups_refresh_button"] = self.__widgets["interface"].button1
self.__widgets["ups_authentication_check"] = self.__widgets["interface"].checkbutton1
self.__widgets["ups_authentication_frame"] = self.__widgets["interface"].hbox1
self.__widgets["ups_authentication_login"] = self.__widgets["interface"].entry2
self.__widgets["ups_authentication_password"] = self.__widgets["interface"].entry3
self.__widgets["ups_list_combo"] = self.__widgets["interface"].combobox1
self.__widgets["ups_commands_combo"] = self.__widgets["interface"].ups_commands_combo
self.__widgets["ups_commands_button"] = self.__widgets["interface"].button8
self.__widgets["ups_connect"] = self.__widgets["interface"].button2
self.__widgets["ups_disconnect"] = self.__widgets["interface"].button7
self.__widgets["ups_params_box"] = self.__widgets["interface"].vbox6
self.__widgets["ups_infos"] = self.__widgets["interface"].notebook1
self.__widgets["ups_vars_tree"] = self.__widgets["interface"].treeview1
self.__widgets["ups_vars_refresh"] = self.__widgets["interface"].button9
self.__widgets["ups_status_image"] = self.__widgets["interface"].image1
self.__widgets["ups_status_left"] = self.__widgets["interface"].label10
self.__widgets["ups_status_right"] = self.__widgets["interface"].label11
self.__widgets["ups_status_time"] = self.__widgets["interface"].label15
self.__widgets["menu_favorites_root"] = self.__widgets["interface"].menu2
self.__widgets["menu_favorites"] = self.__widgets["interface"].menu2
self.__widgets["menu_favorites_add"] = self.__widgets["interface"].menuitem4
self.__widgets["menu_favorites_del"] = self.__widgets["interface"].menuitem5
self.__widgets["progress_battery_charge"] = self.__widgets["interface"].progressbar1
self.__widgets["progress_battery_load"] = self.__widgets["interface"].progressbar2
2011-01-26 09:35:08 +00:00
# Create the tray icon and connect it to the show/hide method...
2022-06-29 10:37:36 +00:00
self.__widgets["status_icon"] = QSystemTrayIcon( QIcon( self.__find_res_file( "pixmaps", "on_line.png" ) ) )
self.__widgets["status_icon"].setVisible( True )
self.__widgets["status_icon"].activated.connect( self.tray_activated )
self.__widgets["ups_status_image"].setPixmap( QPixmap( self.__find_res_file( "pixmaps", "on_line.png" ) ) )
# Connect interface callbacks actions
self.__widgets["main_window"].destroyed.connect( self.quit )
self.__widgets["interface"].imagemenuitem1.triggered.connect( self.gui_about_dialog )
self.__widgets["interface"].imagemenuitem5.triggered.connect( self.quit )
self.__widgets["ups_host_entry"].textChanged.connect( self.__check_gui_fields )
self.__widgets["ups_authentication_login"].textChanged.connect( self.__check_gui_fields )
self.__widgets["ups_authentication_password"].textChanged.connect( self.__check_gui_fields )
self.__widgets["ups_authentication_check"].stateChanged.connect( self.__check_gui_fields )
self.__widgets["ups_port_entry"].valueChanged.connect( self.__check_gui_fields )
self.__widgets["ups_refresh_button"].clicked.connect( self.__update_ups_list )
self.__widgets["ups_connect"].clicked.connect( self.connect_to_ups )
self.__widgets["ups_disconnect"].clicked.connect( self.disconnect_from_ups )
self.__widgets["ups_vars_refresh"].clicked.connect( self.__gui_update_ups_vars_view )
self.__widgets["menu_favorites_add"].triggered.connect( self.__gui_add_favorite )
self.__widgets["menu_favorites_del"].triggered.connect( self.__gui_delete_favorite )
self.__widgets["ups_vars_tree"].doubleClicked.connect( self.__gui_ups_vars_selected )
2010-03-25 23:20:59 +00:00
2011-01-26 09:35:08 +00:00
# Remove the dummy combobox entry on UPS List and Commands
2022-06-29 10:37:36 +00:00
self.__widgets["ups_list_combo"].removeItem( 0 )
2011-01-26 09:35:08 +00:00
# Set UPS vars treeview properties -----------------------------
2022-06-29 10:37:36 +00:00
store = QStandardItemModel( 0, 3, self.__widgets["ups_vars_tree"] )
self.__widgets["ups_vars_tree"].setModel( store )
self.__widgets["ups_vars_tree"].setHeaderHidden( False )
self.__widgets["ups_vars_tree"].setRootIsDecorated( False )
2011-01-26 09:35:08 +00:00
# Column 0
2022-06-29 10:37:36 +00:00
store.setHeaderData( 0, Qt.Horizontal, '' )
2011-01-26 09:35:08 +00:00
# Column 1
2022-06-29 10:37:36 +00:00
store.setHeaderData( 1, Qt.Horizontal, _('Var name') )
2011-01-26 09:35:08 +00:00
# Column 2
2022-06-29 10:37:36 +00:00
store.setHeaderData( 2, Qt.Horizontal, _('Value') )
self.__widgets["ups_vars_tree"].header().setStretchLastSection( True )
2011-01-26 09:35:08 +00:00
2022-06-29 10:37:36 +00:00
self.__widgets["ups_vars_tree"].sortByColumn( 1, Qt.AscendingOrder )
2011-01-26 09:35:08 +00:00
self.__widgets["ups_vars_tree_store"] = store
2022-06-29 10:37:36 +00:00
self.__widgets["ups_vars_tree"].setMinimumSize( 0, 50 )
2011-01-26 09:35:08 +00:00
#---------------------------------------------------------------
# UPS Commands combo box creation ------------------------------
2022-06-29 10:37:36 +00:00
ups_commands_height = self.__widgets["ups_commands_combo"].size().height() * 2
self.__widgets["ups_commands_combo"].setMinimumSize(0, ups_commands_height)
self.__widgets["ups_commands_combo"].setCurrentIndex( 0 )
2011-01-26 09:35:08 +00:00
2022-06-29 10:37:36 +00:00
self.__widgets["ups_commands_button"].setMinimumSize(0, ups_commands_height)
self.__widgets["ups_commands_button"].clicked.connect( self.__gui_send_ups_command )
2011-01-26 09:35:08 +00:00
2022-06-29 10:37:36 +00:00
self.__widgets["ups_commands_combo_store"] = self.__widgets["ups_commands_combo"]
2011-01-26 09:35:08 +00:00
#---------------------------------------------------------------
2022-06-29 10:37:36 +00:00
self.gui_init_unconnected()
2011-01-26 09:35:08 +00:00
if ( cmd_opts.hidden != True ) :
self.__widgets["main_window"].show()
# Define favorites path and load favorites
if ( platform.system() == "Linux" ) :
self.__favorites_path = os.path.join( os.environ.get("HOME"), ".nut-monitor" )
elif ( platform.system() == "Windows" ) :
self.__favorites_path = os.path.join( os.environ.get("USERPROFILE"), "Application Data", "NUT-Monitor" )
self.__favorites_file = os.path.join( self.__favorites_path, "favorites.ini" )
self.__parse_favorites()
self.gui_status_message( _("Welcome to NUT Monitor") )
if ( cmd_opts.favorite != None ) :
2022-06-29 10:37:36 +00:00
if ( cmd_opts.favorite in self.__favorites ) :
2011-01-26 09:35:08 +00:00
self.__gui_load_favorite( fav_name=cmd_opts.favorite )
self.connect_to_ups()
else :
# Try to scan localhost for available ups and connect to it if there is only one
2022-06-29 10:37:36 +00:00
self.__widgets["ups_host_entry"].setText( "localhost" )
2011-01-26 09:35:08 +00:00
self.__update_ups_list()
2022-06-29 10:37:36 +00:00
if self.__widgets["ups_list_combo"].count() == 1:
2011-01-26 09:35:08 +00:00
self.connect_to_ups()
2022-06-29 10:37:36 +00:00
def exec( self ) :
self.__app.exec()
def __find_res_file( self, ftype, filename ) :
filename = os.path.join( ftype, filename )
# TODO: Skip checking application directory if installed
path = os.path.join( os.path.dirname( sys.argv[0] ), filename )
if os.path.exists(path):
return path
path = QStandardPaths.locate(QStandardPaths.AppDataLocation, filename)
if os.path.exists(path):
return path
raise RuntimeError("Cannot find %s resource %s" % (ftype, filename))
def __find_icon_file( self ) :
filename = 'nut-monitor.png'
# TODO: Skip checking application directory if installed
path = os.path.join( os.path.dirname( sys.argv[0] ), "icons", "256x256", filename )
if os.path.exists(path):
return path
path = QStandardPaths.locate(QStandardPaths.AppDataLocation, os.path.join( "icons", "hicolor", "256x256", "apps", filename ) )
if os.path.exists(path):
return path
raise RuntimeError("Cannot find %s resource %s" % ('icon', filename))
2011-01-26 09:35:08 +00:00
# Check if correct fields are filled to enable connection to the UPS
def __check_gui_fields( self, widget=None ) :
# If UPS list contains something, clear it
2022-06-29 10:37:36 +00:00
if self.__widgets["ups_list_combo"].currentIndex() != -1 :
self.__widgets["ups_list_combo"].clear()
self.__widgets["ups_connect"].setEnabled( False )
self.__widgets["menu_favorites_add"].setEnabled( False )
2011-01-26 09:35:08 +00:00
# Host/Port selection
2022-06-29 10:37:36 +00:00
if len( self.__widgets["ups_host_entry"].text() ) > 0 :
2011-01-26 09:35:08 +00:00
sensitive = True
# If authentication is selected, check that we have a login and password
2022-06-29 10:37:36 +00:00
if self.__widgets["ups_authentication_check"].isChecked() :
if len( self.__widgets["ups_authentication_login"].text() ) == 0 :
2011-01-26 09:35:08 +00:00
sensitive = False
2022-06-29 10:37:36 +00:00
if len( self.__widgets["ups_authentication_password"].text() ) == 0 :
2011-01-26 09:35:08 +00:00
sensitive = False
2022-06-29 10:37:36 +00:00
self.__widgets["ups_refresh_button"].setEnabled( sensitive )
2011-01-26 09:35:08 +00:00
if not sensitive :
2022-06-29 10:37:36 +00:00
self.__widgets["ups_connect"].setEnabled( False )
self.__widgets["menu_favorites_add"].setEnabled( False )
2011-01-26 09:35:08 +00:00
else :
2022-06-29 10:37:36 +00:00
self.__widgets["ups_refresh_button"].setEnabled( False )
self.__widgets["ups_connect"].setEnabled( False )
self.__widgets["menu_favorites_add"].setEnabled( False )
2010-03-25 23:20:59 +00:00
2011-01-26 09:35:08 +00:00
# Use authentication fields...
2022-06-29 10:37:36 +00:00
if self.__widgets["ups_authentication_check"].isChecked() :
self.__widgets["ups_authentication_frame"].setEnabled( True )
2011-01-26 09:35:08 +00:00
else :
2022-06-29 10:37:36 +00:00
self.__widgets["ups_authentication_frame"].setEnabled( False )
2011-01-26 09:35:08 +00:00
self.gui_status_message()
#-------------------------------------------------------------------
# This method is used to show/hide the main window when user clicks on the tray icon
def tray_activated( self, widget=None, data=None ) :
if self.__window_visible :
self.__widgets["main_window"].hide()
else :
self.__widgets["main_window"].show()
self.__window_visible = not self.__window_visible
#-------------------------------------------------------------------
# Change the status icon and tray icon
def change_status_icon( self, icon="on_line", blink=False ) :
2022-06-29 10:37:36 +00:00
self.__widgets["status_icon"].setIcon( QIcon( self.__find_res_file( "pixmaps", "%s.png" % icon ) ) )
self.__widgets["ups_status_image"].setPixmap( QPixmap( self.__find_res_file( "pixmaps", "%s.png" % icon ) ) )
# TODO self.__widgets["status_icon"].set_blinking( blink )
2011-01-26 09:35:08 +00:00
#-------------------------------------------------------------------
# This method connects to the NUT server and retrieve availables UPSes
# using connection parameters (host, port, login, pass...)
def __update_ups_list( self, widget=None ) :
2022-06-29 10:37:36 +00:00
host = self.__widgets["ups_host_entry"].text()
port = int( self.__widgets["ups_port_entry"].value() )
2011-01-26 09:35:08 +00:00
login = None
password = None
2022-06-29 10:37:36 +00:00
if self.__widgets["ups_authentication_check"].isChecked() :
login = self.__widgets["ups_authentication_login"].text()
password = self.__widgets["ups_authentication_password"].text()
2010-03-25 23:20:59 +00:00
try :
2011-01-26 09:35:08 +00:00
nut_handler = PyNUT.PyNUTClient( host=host, port=port, login=login, password=password )
upses = nut_handler.GetUPSList()
2022-06-29 10:37:36 +00:00
ups_list = list(key.decode('ascii') for key in upses.keys())
2011-01-26 09:35:08 +00:00
ups_list.sort()
# If UPS list contains something, clear it
2022-06-29 10:37:36 +00:00
self.__widgets["ups_list_combo"].clear()
2011-01-26 09:35:08 +00:00
for current in ups_list :
2022-06-29 10:37:36 +00:00
self.__widgets["ups_list_combo"].addItem( current )
2011-01-26 09:35:08 +00:00
2022-06-29 10:37:36 +00:00
self.__widgets["ups_list_combo"].setCurrentIndex( 0 )
2011-01-26 09:35:08 +00:00
2022-06-29 10:37:36 +00:00
self.__widgets["ups_connect"].setEnabled( True )
self.__widgets["menu_favorites_add"].setEnabled( True )
2011-01-26 09:35:08 +00:00
self.gui_status_message( _("Found {0} devices on {1}").format( len( ups_list ), host ) )
2010-03-25 23:20:59 +00:00
except :
2011-01-26 09:35:08 +00:00
error_msg = _("Error connecting to '{0}' ({1})").format( host, sys.exc_info()[1] )
self.gui_status_message( error_msg )
#-------------------------------------------------------------------
# Quit program
def quit( self, widget=None ) :
# If we are connected to an UPS, disconnect first...
if self.__connected :
self.gui_status_message( _("Disconnecting from device") )
self.disconnect_from_ups()
2010-03-25 23:20:59 +00:00
2022-06-29 10:37:36 +00:00
self.__app.quit()
2010-03-25 23:20:59 +00:00
2011-01-26 09:35:08 +00:00
#-------------------------------------------------------------------
# Method called when user wants to add a new favorite entry. It
# displays a dialog to enable user to select the name of the favorite
def __gui_add_favorite( self, widget=None ) :
2022-06-29 10:37:36 +00:00
dialog_ui_file = self.__find_res_file( 'ui', "dialog1.ui" )
dialog = PyQt5.uic.loadUi( dialog_ui_file )
2011-01-26 09:35:08 +00:00
# Define interface callbacks actions
2022-06-29 10:37:36 +00:00
def check_entry(val):
if self.__gui_add_favorite_check_gui_fields(val):
dialog.buttonBox.button(QDialogButtonBox.Ok).setEnabled( True )
else:
dialog.buttonBox.button(QDialogButtonBox.Ok).setEnabled( False )
dialog.entry4.textChanged.connect( check_entry )
self.__widgets["main_window"].setEnabled( False )
rc = dialog.exec()
if rc == QDialog.Accepted :
2011-01-26 09:35:08 +00:00
fav_data = {}
2022-06-29 10:37:36 +00:00
fav_data["host"] = self.__widgets["ups_host_entry"].text()
fav_data["port"] = "%d" % self.__widgets["ups_port_entry"].value()
fav_data["ups"] = self.__widgets["ups_list_combo"].currentText()
fav_data["auth"] = self.__widgets["ups_authentication_check"].isChecked()
2011-01-26 09:35:08 +00:00
if fav_data["auth"] :
2022-06-29 10:37:36 +00:00
fav_data["login"] = self.__widgets["ups_authentication_login"].text()
fav_data["password"] = base64.b64encode( self.__widgets["ups_authentication_password"].text().encode('ascii') ).decode('ascii')
2011-01-26 09:35:08 +00:00
2022-06-29 10:37:36 +00:00
fav_name = dialog.entry4.text()
2011-01-26 09:35:08 +00:00
self.__favorites[ fav_name ] = fav_data
self.__gui_refresh_favorites_menu()
# Save all favorites
self.__save_favorites()
2022-06-29 10:37:36 +00:00
self.__widgets["main_window"].setEnabled( True )
2011-01-26 09:35:08 +00:00
#-------------------------------------------------------------------
# Method called when user wants to delete an entry from favorites
def __gui_delete_favorite( self, widget=None ) :
2022-06-29 10:37:36 +00:00
dialog_ui_file = self.__find_res_file( 'ui', "dialog2.ui" )
dialog = PyQt5.uic.loadUi( dialog_ui_file )
2011-01-26 09:35:08 +00:00
# Remove the dummy combobox entry on list
2022-06-29 10:37:36 +00:00
dialog.combobox2.removeItem( 0 )
2011-01-26 09:35:08 +00:00
2022-06-29 10:37:36 +00:00
favs = list(self.__favorites.keys())
2011-01-26 09:35:08 +00:00
favs.sort()
for current in favs :
2022-06-29 10:37:36 +00:00
dialog.combobox2.addItem( current )
2011-01-26 09:35:08 +00:00
2022-06-29 10:37:36 +00:00
dialog.combobox2.setCurrentIndex( 0 )
2011-01-26 09:35:08 +00:00
2022-06-29 10:37:36 +00:00
self.__widgets["main_window"].setEnabled( False )
rc = dialog.exec()
fav_name = dialog.combobox2.currentText()
self.__widgets["main_window"].setEnabled( True )
2011-01-26 09:35:08 +00:00
2022-06-29 10:37:36 +00:00
if ( rc == QDialog.Accepted ) :
2011-01-26 09:35:08 +00:00
# Remove entry, show confirmation dialog
2022-06-29 10:37:36 +00:00
resp = QMessageBox.question( None, self.__widgets["main_window"].windowTitle(), _("Are you sure that you want to remove this favorite ?"), QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes )
2011-01-26 09:35:08 +00:00
2022-06-29 10:37:36 +00:00
if ( resp == QMessageBox.Yes ) :
2011-01-26 09:35:08 +00:00
del self.__favorites[ fav_name ]
self.__gui_refresh_favorites_menu()
self.__save_favorites()
self.gui_status_message( _("Removed favorite '%s'") % fav_name )
#-------------------------------------------------------------------
# Method called when user selects a favorite from the favorites menu
def __gui_load_favorite( self, fav_name="" ) :
2022-06-29 10:37:36 +00:00
if ( fav_name in self.__favorites ) :
2011-01-26 09:35:08 +00:00
# If auth is activated, process it before other fields to avoir weird
# reactions with the 'check_gui_fields' function.
if ( self.__favorites[fav_name].get("auth", False ) ) :
2022-06-29 10:37:36 +00:00
self.__widgets["ups_authentication_check"].setChecked( True )
self.__widgets["ups_authentication_login"].setText( self.__favorites[fav_name].get("login","") )
self.__widgets["ups_authentication_password"].setText( self.__favorites[fav_name].get("password","") )
2011-01-26 09:35:08 +00:00
2022-06-29 10:37:36 +00:00
self.__widgets["ups_host_entry"].setText( self.__favorites[fav_name].get("host","") )
self.__widgets["ups_port_entry"].setValue( int( self.__favorites[fav_name].get( "port", 3493 ) ) )
2011-01-26 09:35:08 +00:00
# Clear UPS list and add current UPS name
2022-06-29 10:37:36 +00:00
self.__widgets["ups_list_combo"].clear()
2011-01-26 09:35:08 +00:00
2022-06-29 10:37:36 +00:00
self.__widgets["ups_list_combo"].addItem( self.__favorites[fav_name].get("ups","") )
self.__widgets["ups_list_combo"].setCurrentIndex( 0 )
2011-01-26 09:35:08 +00:00
# Activate the connect button
2022-06-29 10:37:36 +00:00
self.__widgets["ups_connect"].setEnabled( True )
2011-01-26 09:35:08 +00:00
self.gui_status_message( _("Loaded '%s'") % fav_name )
#-------------------------------------------------------------------
# Send the selected command to the UPS
def __gui_send_ups_command( self, widget=None ) :
2022-06-29 10:37:36 +00:00
offset = self.__widgets["ups_commands_combo"].currentIndex()
cmd = self.__ups_commands[ offset ].decode('ascii')
2011-01-26 09:35:08 +00:00
2022-06-29 10:37:36 +00:00
self.__widgets["main_window"].setEnabled( False )
resp = QMessageBox.question( None, self.__widgets["main_window"].windowTitle(), _("Are you sure that you want to send '%s' to the device ?") % cmd, QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes )
self.__widgets["main_window"].setEnabled( True )
2011-01-26 09:35:08 +00:00
2022-06-29 10:37:36 +00:00
if ( resp == QMessageBox.Yes ) :
2011-01-26 09:35:08 +00:00
try :
self.__ups_handler.RunUPSCommand( self.__current_ups, cmd )
self.gui_status_message( _("Sent '{0}' command to {1}").format( cmd, self.__current_ups ) )
except :
self.gui_status_message( _("Failed to send '{0}' ({1})").format( cmd, sys.exc_info()[1] ) )
#-------------------------------------------------------------------
# Method called when user clicks on the UPS vars treeview. If the user
# performs a double click on a RW var, the GUI shows the update var dialog.
2022-06-29 10:37:36 +00:00
def __gui_ups_vars_selected( self, index ) :
if True :
model = self.__widgets["ups_vars_tree_store"]
2011-01-26 09:35:08 +00:00
try :
2022-06-29 10:37:36 +00:00
ups_var = model.data( index.siblingAtColumn(1) ).encode('ascii')
2011-01-26 09:35:08 +00:00
if ( ups_var in self.__ups_rw_vars ) :
# The selected var is RW, then we can show the update dialog
2022-06-29 10:37:36 +00:00
cur_val = self.__ups_rw_vars.get(ups_var).decode('ascii')
2011-01-26 09:35:08 +00:00
2022-06-29 10:37:36 +00:00
self.__widgets["main_window"].setEnabled( False )
new_val, rc = QInputDialog.getText( None, self.__widgets["main_window"].windowTitle(), _("Enter a new value for the variable.<br><br>{0} = {1} <font color=\"#606060\"><i>(current value)</i></font>").format( ups_var, cur_val), QLineEdit.Normal, cur_val )
self.__widgets["main_window"].setEnabled( True )
2011-01-26 09:35:08 +00:00
2022-06-29 10:37:36 +00:00
if ( rc ) :
2011-01-26 09:35:08 +00:00
try :
2022-06-29 10:37:36 +00:00
self.__ups_handler.SetRWVar( ups=self.__current_ups, var=ups_var.decode('ascii'), value=new_val )
2011-01-26 09:35:08 +00:00
self.gui_status_message( _("Updated variable on %s") % self.__current_ups )
# Change the value on the local dict to update the GUI
2022-06-29 10:37:36 +00:00
new_val = new_val.encode('ascii')
2011-01-26 09:35:08 +00:00
self.__ups_vars[ups_var] = new_val
self.__ups_rw_vars[ups_var] = new_val
self.__gui_update_ups_vars_view()
except :
error_msg = _("Error updating variable on '{0}' ({1})").format( self.__current_ups, sys.exc_info()[1] )
self.gui_status_message( error_msg )
else :
# User cancelled modification...
error_msg = _("No variable modified on %s - User cancelled") % self.__current_ups
self.gui_status_message( error_msg )
except :
2022-06-29 10:37:36 +00:00
# Failed to get information from the treeview... skip action
2011-01-26 09:35:08 +00:00
pass
2010-03-25 23:20:59 +00:00
2011-01-26 09:35:08 +00:00
#-------------------------------------------------------------------
# Refresh the content of the favorites menu according to the defined favorites
def __gui_refresh_favorites_menu( self ) :
for current in self.__fav_menu_items :
2022-06-29 10:37:36 +00:00
self.__widgets["menu_favorites"].removeAction(current)
2010-03-25 23:20:59 +00:00
2011-01-26 09:35:08 +00:00
self.__fav_menu_items = list()
2010-03-25 23:20:59 +00:00
2022-06-29 10:37:36 +00:00
items = list(self.__favorites.keys())
2011-01-26 09:35:08 +00:00
items.sort()
for current in items :
2022-06-29 10:37:36 +00:00
menu_item = QAction( current )
2011-01-26 09:35:08 +00:00
self.__fav_menu_items.append( menu_item )
2022-06-29 10:37:36 +00:00
self.__widgets["menu_favorites"].addAction( menu_item )
2011-01-26 09:35:08 +00:00
2022-06-29 10:37:36 +00:00
menu_item.triggered.connect( lambda: self.__gui_load_favorite( current ) )
2011-01-26 09:35:08 +00:00
if len( items ) > 0 :
2022-06-29 10:37:36 +00:00
self.__widgets["menu_favorites_del"].setEnabled( True )
2010-03-25 23:20:59 +00:00
else :
2022-06-29 10:37:36 +00:00
self.__widgets["menu_favorites_del"].setEnabled( False )
2011-01-26 09:35:08 +00:00
#-------------------------------------------------------------------
# In 'add favorites' dialog, this method compares the content of the
# text widget representing the name of the new favorite with existing
# ones. If they match, the 'add' button will be set to non sensitive
# to avoid creating entries with the same name.
2022-06-29 10:37:36 +00:00
def __gui_add_favorite_check_gui_fields( self, fav_name ) :
if ( len( fav_name ) > 0 ) and ( fav_name not in list(self.__favorites.keys()) ) :
return True
2010-03-25 23:20:59 +00:00
else :
2022-06-29 10:37:36 +00:00
return False
2010-03-25 23:20:59 +00:00
2011-01-26 09:35:08 +00:00
#-------------------------------------------------------------------
# Load and parse favorites
def __parse_favorites( self ) :
2010-03-25 23:20:59 +00:00
2011-01-26 09:35:08 +00:00
if ( not os.path.exists( self.__favorites_file ) ) :
# There is no favorites files, do nothing
return
2010-03-25 23:20:59 +00:00
try :
2015-04-30 13:53:36 +00:00
if ( not stat.S_IMODE( os.stat( self.__favorites_path ).st_mode ) == self.DESIRED_FAVORITES_DIRECTORY_MODE ) : # unsafe pre-1.2 directory found
os.chmod( self.__favorites_path, self.DESIRED_FAVORITES_DIRECTORY_MODE )
2022-06-29 10:37:36 +00:00
conf = configparser.ConfigParser()
2011-01-26 09:35:08 +00:00
conf.read( self.__favorites_file )
for current in conf.sections() :
# Check if mandatory fields are present
if ( conf.has_option( current, "host" ) and conf.has_option( current, "ups" ) ) :
# Valid entry found, add it to the list
fav_data = {}
fav_data["host"] = conf.get( current, "host" )
fav_data["ups"] = conf.get( current, "ups" )
if ( conf.has_option( current, "port" ) ) :
fav_data["port"] = conf.get( current, "port" )
else :
fav_data["port"] = "3493"
# If auth is defined the section must have login and pass defined
if ( conf.has_option( current, "auth" ) ) :
if( conf.has_option( current, "login" ) and conf.has_option( current, "password" ) ) :
# Add the entry
fav_data["auth"] = conf.getboolean( current, "auth" )
fav_data["login"] = conf.get( current, "login" )
try :
2022-06-29 10:37:36 +00:00
fav_data["password"] = base64.decodebytes( conf.get( current, "password" ).encode('ascii') ).decode('ascii')
2011-01-26 09:35:08 +00:00
except :
# If the password is not in base64, let the field empty
2022-06-29 10:37:36 +00:00
print(( _("Error parsing favorites, password for '%s' is not in base64\nSkipping password for this entry") % current ))
2011-01-26 09:35:08 +00:00
fav_data["password"] = ""
else :
fav_data["auth"] = False
2010-03-25 23:20:59 +00:00
2011-01-26 09:35:08 +00:00
self.__favorites[current] = fav_data
self.__gui_refresh_favorites_menu()
2010-03-25 23:20:59 +00:00
2011-01-26 09:35:08 +00:00
except :
self.gui_status_message( _("Error while parsing favorites file (%s)") % sys.exc_info()[1] )
2010-03-25 23:20:59 +00:00
2011-01-26 09:35:08 +00:00
#-------------------------------------------------------------------
# Save favorites to the defined favorites file using ini format
def __save_favorites( self ) :
2010-03-25 23:20:59 +00:00
2011-01-26 09:35:08 +00:00
# If path does not exists, try to create it
if ( not os.path.exists( self.__favorites_file ) ) :
try :
2022-06-29 10:37:36 +00:00
os.makedirs( self.__favorites_path, mode=self.DESIRED_FAVORITES_DIRECTORY_MODE, exist_ok=True )
2011-01-26 09:35:08 +00:00
except :
self.gui_status_message( _("Error while creating configuration folder (%s)") % sys.exc_info()[1] )
2022-06-29 10:37:36 +00:00
save_conf = configparser.ConfigParser()
for current in list(self.__favorites.keys()) :
2011-01-26 09:35:08 +00:00
save_conf.add_section( current )
2022-06-29 10:37:36 +00:00
for k, v in self.__favorites[ current ].items() :
if isinstance( v, bool ) :
v = str( v )
2011-01-26 09:35:08 +00:00
save_conf.set( current, k, v )
try :
fh = open( self.__favorites_file, "w" )
save_conf.write( fh )
fh.close()
self.gui_status_message( _("Saved favorites...") )
2010-03-25 23:20:59 +00:00
except :
2011-01-26 09:35:08 +00:00
self.gui_status_message( _("Error while saving favorites (%s)") % sys.exc_info()[1] )
#-------------------------------------------------------------------
# Display the about dialog
def gui_about_dialog( self, widget=None ) :
2022-06-29 10:37:36 +00:00
self.__widgets["main_window"].adjustSize()
dialog_ui_file = self.__find_res_file( 'ui', "aboutdialog1.ui" )
dialog = PyQt5.uic.loadUi( dialog_ui_file )
dialog.icon.setPixmap( QPixmap( self.__find_icon_file() ) )
credits_button = QPushButton( dialog )
credits_button.setText( _("C&redits") )
credits_button.setIcon( dialog.style().standardIcon( QStyle.SP_MessageBoxInformation ) )
credits_button.clicked.connect( self.gui_about_credits )
licence_button = QPushButton( dialog )
licence_button.setText( _("&Licence") )
licence_button.clicked.connect( self.gui_about_licence )
dialog.buttonBox.addButton( credits_button, QDialogButtonBox.HelpRole )
dialog.buttonBox.addButton( licence_button, QDialogButtonBox.HelpRole )
self.__widgets["main_window"].setEnabled( False )
dialog.exec()
self.__widgets["main_window"].setEnabled( True )
def gui_about_credits( self ) :
QMessageBox.about( None, _("Credits"), _("""
Written by:
David Goncalves <david@lestat.st>
Translated by:
David Goncalves <david@lestat.st> - Français
Daniele Pezzini <hyouko@gmail.com> - Italiano
""").strip() )
def gui_about_licence( self ) :
QMessageBox.about( None, _("Licence"), _("""
Copyright (C) 2010 David Goncalves <david@lestat.st>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
""").strip() )
2011-01-26 09:35:08 +00:00
#-------------------------------------------------------------------
# Display a message on the status bar. The message is also set as
# tooltip to enable users to see long messages.
def gui_status_message( self, msg="" ) :
2022-06-29 10:37:36 +00:00
text = msg
2010-03-25 23:20:59 +00:00
2022-06-29 10:37:36 +00:00
message_id = self.__widgets["status_bar"].showMessage( text.replace("\n", "") )
self.__widgets["status_bar"].setToolTip( text )
2010-03-25 23:20:59 +00:00
2011-01-26 09:35:08 +00:00
#-------------------------------------------------------------------
2022-06-29 10:37:36 +00:00
# Display a notification using QSystemTrayIcon with an optional icon
2011-01-26 09:35:08 +00:00
def gui_status_notification( self, message="", icon_file="" ) :
2022-06-29 10:37:36 +00:00
if ( icon_file != "" ) :
icon = QIcon( os.path.abspath( self.__find_res_file( "pixmaps", icon_file ) ) )
else :
icon = None
2010-03-25 23:20:59 +00:00
2022-06-29 10:37:36 +00:00
self.__widgets["status_icon"].showMessage( "NUT Monitor", message, icon )
2010-03-25 23:20:59 +00:00
2011-01-26 09:35:08 +00:00
#-------------------------------------------------------------------
# Connect to the selected UPS using parameters (host,port,login,pass)
def connect_to_ups( self, widget=None ) :
2010-03-25 23:20:59 +00:00
2022-06-29 10:37:36 +00:00
host = self.__widgets["ups_host_entry"].text()
port = int( self.__widgets["ups_port_entry"].value() )
2011-01-26 09:35:08 +00:00
login = None
password = None
2010-03-25 23:20:59 +00:00
2022-06-29 10:37:36 +00:00
if self.__widgets["ups_authentication_check"].isChecked() :
login = self.__widgets["ups_authentication_login"].text()
password = self.__widgets["ups_authentication_password"].text()
2010-03-25 23:20:59 +00:00
2011-01-26 09:35:08 +00:00
try :
self.__ups_handler = PyNUT.PyNUTClient( host=host, port=port, login=login, password=password )
2010-03-25 23:20:59 +00:00
2011-01-26 09:35:08 +00:00
except :
self.gui_status_message( _("Error connecting to '{0}' ({1})").format( host, sys.exc_info()[1] ) )
self.gui_status_notification( _("Error connecting to '{0}'\n{1}").format( host, sys.exc_info()[1] ), "warning.png" )
return
2010-03-25 23:20:59 +00:00
2011-01-26 09:35:08 +00:00
# Check if selected UPS exists on server...
srv_upses = self.__ups_handler.GetUPSList()
2022-06-29 10:37:36 +00:00
self.__current_ups = self.__widgets["ups_list_combo"].currentText()
2010-03-25 23:20:59 +00:00
2022-06-29 10:37:36 +00:00
if self.__current_ups.encode('ascii') not in srv_upses :
2011-01-26 09:35:08 +00:00
self.gui_status_message( _("Device '%s' not found on server") % self.__current_ups )
self.gui_status_notification( _("Device '%s' not found on server") % self.__current_ups, "warning.png" )
return
2010-03-25 23:20:59 +00:00
2011-01-26 09:35:08 +00:00
self.__connected = True
self.__widgets["ups_connect"].hide()
self.__widgets["ups_disconnect"].show()
self.__widgets["ups_infos"].show()
2022-06-29 10:37:36 +00:00
self.__widgets["ups_params_box"].setEnabled( False )
self.__widgets["menu_favorites_root"].setEnabled( False )
2011-01-26 09:35:08 +00:00
self.__widgets["ups_params_box"].hide()
2010-03-25 23:20:59 +00:00
2011-01-26 09:35:08 +00:00
commands = self.__ups_handler.GetUPSCommands( self.__current_ups )
2022-06-29 10:37:36 +00:00
self.__ups_commands = list(commands.keys())
2011-01-26 09:35:08 +00:00
self.__ups_commands.sort()
2010-03-25 23:20:59 +00:00
2011-01-26 09:35:08 +00:00
# Refresh UPS commands combo box
self.__widgets["ups_commands_combo_store"].clear()
for desc in self.__ups_commands :
2022-06-29 10:37:36 +00:00
# TODO: Style as "%s<br><font color=\"#707070\">%s</font>"
self.__widgets["ups_commands_combo_store"].addItem( "%s\n%s" % ( desc.decode('ascii'), commands[desc].decode('ascii') ) )
2010-03-25 23:20:59 +00:00
2022-06-29 10:37:36 +00:00
self.__widgets["ups_commands_combo"].setCurrentIndex( 0 )
2010-03-25 23:20:59 +00:00
2011-01-26 09:35:08 +00:00
# Update UPS vars manually before the thread
self.__ups_vars = self.__ups_handler.GetUPSVars( self.__current_ups )
self.__ups_rw_vars = self.__ups_handler.GetRWVars( self.__current_ups )
self.__gui_update_ups_vars_view()
2010-03-25 23:20:59 +00:00
2011-01-26 09:35:08 +00:00
# Try to resize the main window...
2022-06-29 10:37:36 +00:00
# FIXME: For some reason, calling this immediately doesn't work right
QTimer.singleShot(10, self.__widgets["main_window"].adjustSize)
2010-03-25 23:20:59 +00:00
2011-01-26 09:35:08 +00:00
# Start the GUI updater thread
self.__gui_thread = gui_updater( self )
self.__gui_thread.start()
2010-03-25 23:20:59 +00:00
2011-01-26 09:35:08 +00:00
self.gui_status_message( _("Connected to '{0}' on {1}").format( self.__current_ups, host ) )
2010-03-25 23:20:59 +00:00
2011-01-26 09:35:08 +00:00
#-------------------------------------------------------------------
# Refresh UPS vars in the treeview
def __gui_update_ups_vars_view( self, widget=None ) :
if self.__ups_handler :
vars = self.__ups_vars
rwvars = self.__ups_rw_vars
2010-03-25 23:20:59 +00:00
2022-06-29 10:37:36 +00:00
self.__widgets["ups_vars_tree_store"].removeRows(0, self.__widgets["ups_vars_tree_store"].rowCount())
2010-03-25 23:20:59 +00:00
2022-06-29 10:37:36 +00:00
for k,v in vars.items() :
if ( k in rwvars ) :
icon_file = self.__find_res_file( "pixmaps", "var-rw.png" )
2011-01-26 09:35:08 +00:00
else :
2022-06-29 10:37:36 +00:00
icon_file = self.__find_res_file( "pixmaps", "var-ro.png" )
2010-03-25 23:20:59 +00:00
2022-06-29 10:37:36 +00:00
icon = QIcon( icon_file )
item_icon = QStandardItem(icon, '')
item_icon.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemNeverHasChildren)
item_var_name = QStandardItem( k.decode('ascii') )
item_var_name.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemNeverHasChildren)
item_var_val = QStandardItem( v.decode('ascii') )
item_var_val.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemNeverHasChildren)
self.__widgets["ups_vars_tree_store"].appendRow( (item_icon, item_var_name, item_var_val) )
self.__widgets["ups_vars_tree"].resizeColumnToContents( 0 )
self.__widgets["ups_vars_tree"].resizeColumnToContents( 1 )
2010-03-25 23:20:59 +00:00
2022-06-29 10:37:36 +00:00
def gui_init_unconnected( self ) :
2011-01-26 09:35:08 +00:00
self.__connected = False
self.__widgets["ups_connect"].show()
self.__widgets["ups_disconnect"].hide()
self.__widgets["ups_infos"].hide()
2022-06-29 10:37:36 +00:00
self.__widgets["ups_params_box"].setEnabled( True )
self.__widgets["menu_favorites_root"].setEnabled( True )
self.__widgets["status_icon"].setToolTip( _("<i>Not connected</i>") )
2011-01-26 09:35:08 +00:00
self.__widgets["ups_params_box"].show()
2010-03-25 23:20:59 +00:00
2011-01-26 09:35:08 +00:00
# Try to resize the main window...
2022-06-29 10:37:36 +00:00
self.__widgets["main_window"].adjustSize()
#-------------------------------------------------------------------
# Disconnect from the UPS
def disconnect_from_ups( self, widget=None ) :
self.gui_init_unconnected()
2010-03-25 23:20:59 +00:00
2011-01-26 09:35:08 +00:00
# Stop the GUI updater thread
self.__gui_thread.stop_thread()
2010-03-25 23:20:59 +00:00
2011-01-26 09:35:08 +00:00
del self.__ups_handler
self.gui_status_message( _("Disconnected from '%s'") % self.__current_ups )
self.change_status_icon( "on_line", blink=False )
self.__current_ups = None
2010-03-25 23:20:59 +00:00
2011-01-26 09:35:08 +00:00
#-----------------------------------------------------------------------
# GUI Updater class
# This class updates the main gui with data from connected UPS
2022-06-29 10:37:36 +00:00
class gui_updater :
2010-03-25 23:20:59 +00:00
2011-01-26 09:35:08 +00:00
__parent_class = None
__stop_thread = False
2010-03-25 23:20:59 +00:00
2011-01-26 09:35:08 +00:00
def __init__( self, parent_class ) :
threading.Thread.__init__( self )
self.__parent_class = parent_class
2010-03-25 23:20:59 +00:00
2022-06-29 10:37:36 +00:00
def start( self ) :
self.__timer = QTimer()
self.__timer.timeout.connect(self.__update)
self.__timer.start(1000)
def __update( self ) :
2010-03-25 23:20:59 +00:00
2011-01-26 09:35:08 +00:00
ups = self.__parent_class._interface__current_ups
was_online = True
2010-03-25 23:20:59 +00:00
2011-01-26 09:35:08 +00:00
# Define a dict containing different UPS status
2022-06-29 10:37:36 +00:00
status_mapper = { b"LB" : "<font color=\"#BB0000\"><b>%s</b></font>" % _("Low batteries"),
b"RB" : "<font color=\"#FF0000\"><b>%s</b></font>" % _("Replace batteries !"),
b"BYPASS" : "<font color=\"#BB0000\">Bypass</font> <i>%s</i>" % _("(no battery protection)"),
b"CAL" : _("Performing runtime calibration"),
b"OFF" : "<font color=\"#000090\">%s</font> <i>(%s)</i>" % ( _("Offline"), _("not providing power to the load") ),
b"OVER" : "<font color=\"#BB0000\">%s</font> <i>(%s)</i>" % ( _("Overloaded !"), _("there is too much load for device") ),
b"TRIM" : _("Triming <i>(UPS is triming incoming voltage)</i>"),
b"BOOST" : _("Boost <i>(UPS is boosting incoming voltage)</i>")
2011-01-26 09:35:08 +00:00
}
2010-03-25 23:20:59 +00:00
2022-06-29 10:37:36 +00:00
if not self.__stop_thread :
2011-01-26 09:35:08 +00:00
try :
vars = self.__parent_class._interface__ups_handler.GetUPSVars( ups )
self.__parent_class._interface__ups_vars = vars
# Text displayed on the status frame
text_left = ""
text_right = ""
status_text = ""
2022-06-29 10:37:36 +00:00
text_left += "<b>%s</b><br>" % _("Device status :")
2011-01-26 09:35:08 +00:00
2022-06-29 10:37:36 +00:00
if ( vars.get(b"ups.status").find(b"OL") != -1 ) :
text_right += "<font color=\"#009000\"><b>%s</b></font>" % _("Online")
2011-01-26 09:35:08 +00:00
if not was_online :
self.__parent_class.change_status_icon( "on_line", blink=False )
was_online = True
2022-06-29 10:37:36 +00:00
if ( vars.get(b"ups.status").find(b"OB") != -1 ) :
text_right += "<font color=\"#900000\"><b>%s</b></font>" % _("On batteries")
2011-01-26 09:35:08 +00:00
if was_online :
self.__parent_class.change_status_icon( "on_battery", blink=True )
self.__parent_class.gui_status_notification( _("Device is running on batteries"), "on_battery.png" )
was_online = False
2022-06-29 10:37:36 +00:00
# Check for additionnal information
for k,v in status_mapper.items() :
if vars.get(b"ups.status").find(k) != -1 :
2011-01-26 09:35:08 +00:00
if ( text_right != "" ) :
text_right += " - %s" % v
else :
text_right += "%s" % v
# CHRG and DISCHRG cannot be trated with the previous loop ;)
2022-06-29 10:37:36 +00:00
if ( vars.get(b"ups.status").find(b"DISCHRG") != -1 ) :
2011-01-26 09:35:08 +00:00
text_right += " - <i>%s</i>" % _("discharging")
2022-06-29 10:37:36 +00:00
elif ( vars.get(b"ups.status").find(b"CHRG") != -1 ) :
2011-01-26 09:35:08 +00:00
text_right += " - <i>%s</i>" % _("charging")
2010-03-25 23:20:59 +00:00
2011-01-26 09:35:08 +00:00
status_text += text_right
2022-06-29 10:37:36 +00:00
text_right += "<br>"
2010-03-25 23:20:59 +00:00
2022-06-29 10:37:36 +00:00
if ( b"ups.mfr" in vars ) :
text_left += "<b>%s</b><br><br>" % _("Model :")
text_right += "%s<br>%s<br>" % (
vars.get(b"ups.mfr",b"").decode('ascii'),
vars.get(b"ups.model",b"").decode('ascii'),
)
2010-03-25 23:20:59 +00:00
2022-06-29 10:37:36 +00:00
if ( b"ups.temperature" in vars ) :
text_left += "<b>%s</b><br>" % _("Temperature :")
text_right += "%s<br>" % int( float( vars.get( b"ups.temperature", 0 ) ) )
2010-03-25 23:20:59 +00:00
2022-06-29 10:37:36 +00:00
if ( b"battery.voltage" in vars ) :
text_left += "<b>%s</b><br>" % _("Battery voltage :")
text_right += "%sv<br>" % (vars.get( b"battery.voltage", 0 ).decode('ascii'),)
2010-03-25 23:20:59 +00:00
2022-06-29 10:37:36 +00:00
self.__parent_class._interface__widgets["ups_status_left"].setText( text_left[:-4] )
self.__parent_class._interface__widgets["ups_status_right"].setText( text_right[:-4] )
2010-03-25 23:20:59 +00:00
2011-01-26 09:35:08 +00:00
# UPS load and battery charge progress bars
2022-06-29 10:37:36 +00:00
self.__parent_class._interface__widgets["progress_battery_charge"].setRange( 0, 100 )
if ( b"battery.charge" in vars ) :
charge = vars.get( b"battery.charge", "0" )
self.__parent_class._interface__widgets["progress_battery_charge"].setValue( int( float( charge ) ) )
self.__parent_class._interface__widgets["progress_battery_charge"].resetFormat()
status_text += "<br>%s %s%%" % ( _("Battery charge :"), int( float( charge ) ) )
2011-01-26 09:35:08 +00:00
else :
2022-06-29 10:37:36 +00:00
self.__parent_class._interface__widgets["progress_battery_charge"].setValue( 0 )
self.__parent_class._interface__widgets["progress_battery_charge"].setFormat( _("Not available") )
# FIXME: Some themes don't draw text, so swap it with a QLabel?
self.__parent_class._interface__widgets["progress_battery_load"].setRange( 0, 100 )
if ( b"ups.load" in vars ) :
load = vars.get( b"ups.load", "0" )
self.__parent_class._interface__widgets["progress_battery_load"].setValue( int( float( load ) ) )
self.__parent_class._interface__widgets["progress_battery_load"].resetFormat()
status_text += "<br>%s %s%%" % ( _("UPS load :"), int( float( load ) ) )
2011-01-26 09:35:08 +00:00
else :
2022-06-29 10:37:36 +00:00
self.__parent_class._interface__widgets["progress_battery_load"].setValue( 0 )
self.__parent_class._interface__widgets["progress_battery_load"].setFormat( _("Not available") )
# FIXME: Some themes don't draw text, so swap it with a QLabel?
2011-01-26 09:35:08 +00:00
2022-06-29 10:37:36 +00:00
if ( b"battery.runtime" in vars ) :
autonomy = int( float( vars.get( b"battery.runtime", 0 ) ) )
2011-01-26 09:35:08 +00:00
if ( autonomy >= 3600 ) :
info = time.strftime( _("<b>%H hours %M minutes %S seconds</b>"), time.gmtime( autonomy ) )
elif ( autonomy > 300 ) :
info = time.strftime( _("<b>%M minutes %S seconds</b>"), time.gmtime( autonomy ) )
else :
2022-06-29 10:37:36 +00:00
info = time.strftime( _("<b><font color=\"#DD0000\">%M minutes %S seconds</font></b>"), time.gmtime( autonomy ) )
2011-01-26 09:35:08 +00:00
else :
info = _("Not available")
2010-03-25 23:20:59 +00:00
2022-06-29 10:37:36 +00:00
self.__parent_class._interface__widgets["ups_status_time"].setText( info )
2010-03-25 23:20:59 +00:00
2011-01-26 09:35:08 +00:00
# Display UPS status as tooltip for tray icon
2022-06-29 10:37:36 +00:00
self.__parent_class._interface__widgets["status_icon"].setToolTip( status_text )
2010-03-25 23:20:59 +00:00
2011-01-26 09:35:08 +00:00
except :
self.__parent_class.gui_status_message( _("Error from '{0}' ({1})").format( ups, sys.exc_info()[1] ) )
self.__parent_class.gui_status_notification( _("Error from '{0}'\n{1}").format( ups, sys.exc_info()[1] ), "warning.png" )
2010-03-25 23:20:59 +00:00
def stop_thread( self ) :
2022-06-29 10:37:36 +00:00
self.__timer.stop()
2010-03-25 23:20:59 +00:00
2011-01-26 09:35:08 +00:00
#-----------------------------------------------------------------------
# The main program starts here :-)
2010-03-25 23:20:59 +00:00
if __name__ == "__main__" :
2011-01-26 09:35:08 +00:00
# Init the localisation
APP = "NUT-Monitor"
DIR = "locale"
gettext.bindtextdomain( APP, DIR )
gettext.textdomain( APP )
_ = gettext.gettext
2022-06-29 10:37:36 +00:00
for module in ( gettext, ) :
2011-01-26 09:35:08 +00:00
module.bindtextdomain( APP, DIR )
module.textdomain( APP )
2022-06-29 10:37:36 +00:00
gui = interface(sys.argv)
gui.exec()
2011-01-26 09:35:08 +00:00