有没有功耗指标?

我很想看看我的笔记本电脑一直消耗多少电量。

因此,我想要一个持续(可能间隔1秒)的指示器监视当前功耗(以mA为单位测量的电流或以W测量的电功率)并将其显示在我的Ubuntu 16.04的Unity面板中。

注意: 截至目前,无法通过软件方式测量笔记本电脑在AC适配器或台式计算机上的功耗。 甚至其他操作系统(如Windows)也需要外部硬件传感器来提供该信息。 如果您需要台式计算机的指示器,此答案将不适合您

我写了一个指示器,用于监测瓦特和毫安的能耗。 用法非常简单:

python /path/to/power-flow-indicator 

或者与脚本位于同一目录:

  ./power-flow-indicator 

默认情况下,输出以瓦特为单位显示,但--amps选项允许以毫安为单位显示输出:

 python power-flow-indicator --amps 

-h将显示相同的信息:

 usage: power-flow-indicator [-h] [--amps] optional arguments: -h, --help show this help message and exit --amps display output in milliamps 

获取指标

指标源代码位于GitHub上 。 如果您安装了git ,则可以通过以下方式克隆存储库:

 git clone https://github.com/SergKolo/power-flow-indicator.git 

或者,可以通过获取zip文件

  wget https://github.com/SergKolo/power-flow-indicator/archive/master.zip 

源代码

注意 :指示器需要一个图标。 您最好克隆git存储库或获取zip包,而不是仅仅复制源代码。

 #!/usr/bin/env python # -*- coding: utf-8 -*- # # Author: Serg Kolo , contact: 1047481448@qq.com # Date: July 21, 2012 # Purpose: Indicator that displays power # consumption of laptop battery # # Written for: http://askubuntu.com/q/801003/295286 # Tested on: Ubuntu 16.04 LTS # # # # Licensed under The MIT License (MIT). # See included LICENSE file or the notice below. # # Copyright © 2016 Sergiy Kolodyazhnyy # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included # in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. import gi gi.require_version('AppIndicator3', '0.1') from gi.repository import GLib as glib from gi.repository import AppIndicator3 as appindicator from gi.repository import Gtk as gtk import os import dbus import subprocess import argparse class LockKeyStatusIndicator(object): def __init__(self, show_amps): self.show_amps = show_amps self.app = appindicator.Indicator.new( 'power-flow-indicator', "", appindicator.IndicatorCategory.APPLICATION_STATUS) self.app.set_status(appindicator.IndicatorStatus.ACTIVE) self.icon_path = os.path.dirname( os.path.realpath(__file__) ) self.app.set_icon( os.path.join(self.icon_path,"pwi_icon.png" )) self.update_label() self.app_menu = gtk.Menu() self.quit_app = gtk.MenuItem('Quit') self.quit_app.connect('activate', self.quit) self.quit_app.show() self.app_menu.append(self.quit_app) self.app.set_menu(self.app_menu) def run(self): try: gtk.main() except KeyboardInterrupt: pass def quit(self, data=None): gtk.main_quit() def run_cmd(self, cmdlist): new_env = dict( os.environ ) new_env['LC_ALL'] = 'C' try: stdout = subprocess.check_output(cmdlist,env=new_env) except subprocess.CalledProcessError: pass else: if stdout: return stdout def run_dbus_method(self, bus_type, obj, path, interface, method, arg): if bus_type == "session": bus = dbus.SessionBus() if bus_type == "system": bus = dbus.SystemBus() proxy = bus.get_object(obj, path) method = proxy.get_dbus_method(method, interface) if arg: return method(arg) else: return method() def get_power_info(self): battery_path = None battery_info = [] energy_rate = None voltage = None current = None on_battery = None for line in self.run_cmd(['upower', '-e']).decode().split('\n'): if 'battery_BAT' in line: battery_path = line break self.run_dbus_method('system', 'org.freedesktop.UPower', battery_path, 'org.freedesktop.UPower.Device', 'Refresh', 'None') for entry in self.run_cmd( ['upower', '-i', battery_path]).decode().split('\n'): if 'state' in entry: if entry.replace(" ", "").split(':')[1] == 'discharging': on_battery = True if 'energy-rate' in entry: energy_rate = entry.replace(" ", "").split(':')[1][:-1] print energy_rate if 'voltage' in entry: voltage = entry.replace(" ", "").split(':')[1] current = round( 1000 * float(energy_rate[:-1]) / float(voltage[:-1]), 4) if on_battery: return str(round(float(energy_rate),2)) + 'W', str(int(current)) + 'mA' else: return 'on ac', 'on ac' def update_label(self): cwd = os.getcwd() red_icon = os.path.join(cwd, 'red.png') green_icon = os.path.join(cwd, 'green.png') if self.show_amps: label_text = self.get_power_info()[1] else: label_text = self.get_power_info()[0] self.app.set_label(label_text, "") glib.timeout_add_seconds(1, self.set_app_label) def set_app_label(self): self.update_label() def main(): parser = argparse.ArgumentParser() parser.add_argument( "--amps", help="display output in milliamps", action="store_true") args = parser.parse_args() indicator = LockKeyStatusIndicator(args.amps) indicator.run() if __name__ == '__main__': main()