【问题标题】:Monitor battery status on Linux在 Linux 上监控电池状态
【发布时间】:2016-07-13 19:23:59
【问题描述】:

我正在开发一个Qt 应用程序,并希望监控电池状态(充电、百分比等)。在/sys/class/power_supply/battery 目录中有statusuevent 之类的文件,其中包含我需要的所有信息。这些文件在连接/断开充电器时得到更新。我尝试使用 QFileSystemWatcher 类,该类不起作用,因为这些文件的时间戳不会随其内容而改变。有没有其他方法可以监控电池状态?

我可以使用计时器/单独的线程来执行此操作,但想探索通过操作系统提供的其他选项(Arm 上的Debian)。

【问题讨论】:

  • this questionthat questionuevent 接口是你需要的,我想。
  • QFileSystemWatcher 依赖于 Linux 上的 inotify,is not supported for sysfs and procfs。您需要在适当的文件上 selectpoll 以获得您想要的行为。
  • 你也可以看看 D-Bus,里面有很多信息——也许对你有帮助。
  • @Adam 谢谢,你能举一些例子吗?快速谷歌搜索没有显示任何有用的信息。

标签: c++ linux qt


【解决方案1】:

你也可以看看我在评论中所说的 D-Bus。

特别是你应该看看:

  • signal Changed() - 当有变化时它会通知你 [3]

和属性 [2]

  • 能源
  • EnergyEmpty
  • 充满能量

QT5 支持 D-Bus [4] [5]。

要测试的shell命令:

dbus-send --print-reply \
          --system \
          --dest=org.freedesktop.UPower \
          /org/freedesktop/UPower/devices/battery_BAT0 \ # change BAT0 to proper value - you can check it with d-feet [6]
          org.freedesktop.DBus.Properties.GetAll \
          string:org.freedesktop.UPower.Device

  1. https://upower.freedesktop.org/docs/UPower.html
  2. https://upower.freedesktop.org/docs/Device.html
  3. https://upower.freedesktop.org/docs/Device.html#Device::Changed
  4. http://doc.qt.io/qt-5/qtdbus-index.html
  5. http://doc.qt.io/qt-5/examples-dbus.html
  6. https://wiki.gnome.org/action/show/Apps/DFeet?action=show&redirect=DFeet

【讨论】:

  • 似乎没有为电池或线路电源对象发出信号。这让我走了 90% 的路,但最后,你仍然需要在计时器上进行轮询,这意味着它并不比按照提问者所描述的那样检查文件更好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-26
  • 1970-01-01
  • 2022-12-17
相关资源
最近更新 更多