luci-app-battstatus: initial release
authorRussell Morris <rmorris@rkmorris.us>
Thu, 23 Jan 2020 03:10:13 +0000 (21:10 -0600)
committerJo-Philipp Wich <jo@mein.io>
Sat, 18 Jul 2020 13:28:36 +0000 (15:28 +0200)
Introduce a new package luci-app-battstatus which queries battery charge
information using i2c commands and displays a charge indicator in the
upper right notification area.

So far this package has been tested to work with the HooToo HT-TM05
travel router but might be extended to cover further models in the
future.

Signed-off-by: Russell Morris <rmorris@rkmorris.us>
[rebase on top of master, rewrite commit message, adjust copyright,
 convert some ubus fields to boolean values]
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
modules/luci-mod-battstatus/Makefile [new file with mode: 0644]
modules/luci-mod-battstatus/htdocs/luci-static/resources/preload/battstatus.js [new file with mode: 0755]
modules/luci-mod-battstatus/root/usr/libexec/rpcd/luci.battstatus [new file with mode: 0755]
modules/luci-mod-battstatus/root/usr/share/rpcd/acl.d/luci-mod-battstatus.json [new file with mode: 0755]

diff --git a/modules/luci-mod-battstatus/Makefile b/modules/luci-mod-battstatus/Makefile
new file mode 100644 (file)
index 0000000..179ede9
--- /dev/null
@@ -0,0 +1,19 @@
+#
+# Copyright (C) 2020 Russell Morris <rmorris@rkmorris.us>
+#
+# This is free software, licensed under the Apache License, Version 2.0 .
+#
+
+include $(TOPDIR)/rules.mk
+
+LUCI_TITLE:=LuCI Battery Status
+LUCI_DESCRIPTION:=Provides a battery charge indicator in LuCI. Currently only supports the HooToo HT-TM05 travel router.
+
+LUCI_DEPENDS:=+luci-base +libiwinfo-lua +rpcd-mod-iwinfo +libi2c +i2c-tools
+
+PKG_LICENSE:=Apache-2.0
+
+include ../../luci.mk
+
+# call BuildPackage - OpenWrt buildroot signature
+
diff --git a/modules/luci-mod-battstatus/htdocs/luci-static/resources/preload/battstatus.js b/modules/luci-mod-battstatus/htdocs/luci-static/resources/preload/battstatus.js
new file mode 100755 (executable)
index 0000000..d895c36
--- /dev/null
@@ -0,0 +1,55 @@
+'use strict';
+'require ui';
+'require rpc';
+'require poll';
+'require baseclass';
+
+var callBatteryStatus = rpc.declare({
+       object: 'luci.battstatus',
+       method: 'getBatteryStatus',
+       expect: { '': {} }
+});
+
+var devices = {};
+
+return baseclass.extend({
+       __init__: function() {
+               this.updateIndicator();
+               poll.add(L.bind(this.updateIndicator, this), 5);
+       },
+
+       updateIndicator: function() {
+               return callBatteryStatus().then(L.bind(function(devs) {
+                       for (var dev in devs) {
+                               var info = devs[dev];
+                               if (info.valid) {
+                                       info.status = (info.charging ? _('Charging') : _('Not Charging')) + ": " + info.percentage + "%";
+                                       info.state = "active";
+                                       if (info.percentage <= 20)
+                                               info.color = "Red";
+                                       else if (info.percentage <= 30)
+                                               info.color = "GoldenRod";
+                               } else {
+                                       info.status = info.message;
+                                       info.state = "inactive";
+                               }
+
+                               info.name = "battery-" + dev.replace(" ", "-");
+                               ui.showIndicator(info.name, info.status, null, info.state);
+                               if (typeof info.color != 'undefined') {
+                                       info.element = document.querySelector(`[data-indicator="${info.name}"]`);
+                                       info.element.innerHTML = `<span style="color:${info.color}">${info.status}</span>`;
+                               }
+
+                               devices[dev] = info;
+                       }
+
+                       for (var dev in devices) {
+                               if (!devs.hasOwnProperty(dev)) {
+                                       ui.hideIndicator('battery-%s'.format(dev));
+                                       delete devices[dev];
+                               }
+                       }
+               }, this));
+       }
+});
diff --git a/modules/luci-mod-battstatus/root/usr/libexec/rpcd/luci.battstatus b/modules/luci-mod-battstatus/root/usr/libexec/rpcd/luci.battstatus
new file mode 100755 (executable)
index 0000000..d3534b4
--- /dev/null
@@ -0,0 +1,39 @@
+#!/bin/sh
+
+. /usr/share/libubox/jshn.sh
+
+case "$1" in
+       list)
+               printf '{ "getBatteryStatus": {} }'
+       ;;
+       call)
+               case "$2" in
+                       getBatteryStatus)
+                               json_init
+
+                               eval $(/bin/ubus call system board 2>/dev/null | /usr/bin/jsonfilter -e 'MODEL=@.model')
+                               json_add_object "$MODEL"
+
+                               if [ -f /usr/sbin/i2cset ] && [ -f /usr/sbin/i2cget ]; then
+                                       json_add_boolean valid 1
+                                       if [ $(i2cset -y 0 0x0a 0x0a 0x01 && i2cget -y 0 0x0a 0x0a) = 0x40 ]; then
+                                               json_add_boolean charging 1
+                                       else
+                                               json_add_boolean charging 0
+                                       fi
+                                       json_add_int percentage $(i2cset -y 0 0x0a 0x0a 0x10 && i2cget -y 0 0x0a 0x0a | xargs printf %d)
+                               else
+                                       json_add_boolean valid 0
+                                       if [ ! -f /usr/sbin/i2cset ]; then
+                                               json_add_string message "Need i2cset"
+                                       else
+                                               json_add_string message "Need i2cget"
+                                       fi
+                               fi
+
+                               json_close_object
+                               json_dump
+                       ;;
+               esac
+       ;;
+esac
diff --git a/modules/luci-mod-battstatus/root/usr/share/rpcd/acl.d/luci-mod-battstatus.json b/modules/luci-mod-battstatus/root/usr/share/rpcd/acl.d/luci-mod-battstatus.json
new file mode 100755 (executable)
index 0000000..4babc31
--- /dev/null
@@ -0,0 +1,10 @@
+{
+       "luci-mod-battstatus": {
+               "description": "Grant access to battery status",
+               "read": {
+                       "ubus": {
+                               "luci.battstatus": [ "getBatteryStatus" ]
+                       }
+               }
+       }
+}