pfSense Gold Subscription

Author Topic: Laptop Battery Level Widget  (Read 2041 times)

0 Members and 1 Guest are viewing this topic.

Offline daplumber

  • Jr. Member
  • **
  • Posts: 45
    • View Profile
Laptop Battery Level Widget
« on: May 25, 2012, 02:53:13 am »
Since I have a habit of regarding an old laptop as a compact server with a built-in UPS I'm running 2.1 on a surplus  elderly Toshiba Tecra M3.

(BTW Do Not use the Yukon GbE port, disable it in the BIOS, it has a nasty habit of hanging *BSD and Linux.)

So I thought it would be nice to see the battery level % (from sysctl) on the pfsense status page, so some quick and dirty cut-and-paste produced battery.widget.php:

Code: [Select]
<?php

require_once("guiconfig.inc");
require_once(
"pfsense-utils.inc");
require_once(
"functions.inc");
?>


<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td class="widgetsubheader"><b><center><?php echo gettext("Battery"?></center></b></td>
<td class="widgetsubheader"><b><center><?php echo gettext("% Charge"?></center></b></td>
</tr>

<?php

$dev_state exec("sysctl hw.acpi.battery.life | awk '{print $2}'"); ## get battery life from sysctl
$dev_ident =  "Internal";

echo '<tr><td class="listr"><center>' "\n";
echo $dev_ident ;
echo '</td>' "\n";

echo '<td class="listr"><center>';

if($dev_state == "100"
 echo "<span style=\"background-color:#00FF00\">$dev_state</span><br>";
else
echo "<span style=\"background-color:#FF8080\">$dev_state</span><br>"
echo '</td></tr>' "\n";

?>

</table>
<center><a href="battery.php" class="navlink">Battery Charge</a></center>

Comments, criticism, and above all better coding(!) welcomed!

I have no idea how portable (pun!) this is, but hw.acpi.battery.life seems to be pretty generic.

Picture attached. (Yes, same system as pfsense-ng-blue blue theme.)

Bonus [ER] (!) :
Sooner or later I'm going to try and plug a surplus (needs a new battery) APC UPS that I know works with apcupsd over USB into pfSense 2.1. Implementing the apcupsd FreeBSD port along with the neccessary php control and even a widget would be a nice-to-have...
« Last Edit: May 25, 2012, 02:56:11 am by daplumber »
----------
This user has been carbon dated to the 8-bit era...

Offline rcfa

  • Sr. Member
  • ****
  • Posts: 565
    • View Profile
Re: Laptop Battery Level Widget
« Reply #1 on: May 25, 2012, 06:39:04 am »
Did you check out the NUT package?

Seems like that would also be a candidate to consider laptop batteries as "local UPS"...

ThorstenK

  • Guest
Re: Laptop Battery Level Widget
« Reply #2 on: May 25, 2012, 06:51:33 am »
mmh- i recently worked in a similar area querying acpi wt sysctl.
Ive seen that things like this were implemented a bit different.
All the html is placed right directly in the widget, so everythings fine.
But i dont see hows your widget is going to update the batterys state over time.

If the NUT package is not an option, then you  may want to refactor your codes core functionality in functions.inc.php and return the data to the battery widget. If you are interested, then youll find some examples in system_information.widget.php.

Oh and i see a missing gettext($dev_ident)  ;)
But if you liked the experience of creating code and want to do the refactoring the i can help you
wt updating your widgets data with ajax.js/getstatus.php and to display a pretty battery bar :)

so - its up to you :)
« Last Edit: May 25, 2012, 06:53:14 am by ThorstenK »

Offline daplumber

  • Jr. Member
  • **
  • Posts: 45
    • View Profile
Re: Laptop Battery Level Widget
« Reply #3 on: May 25, 2012, 08:45:16 am »
Did you check out the NUT package?

Seems like that would also be a candidate to consider laptop batteries as "local UPS"...

I had a quick look at nut, but I didn't see anywhere it addressed non-networked UPS'. Unless I missed it?
----------
This user has been carbon dated to the 8-bit era...

Offline daplumber

  • Jr. Member
  • **
  • Posts: 45
    • View Profile
Re: Laptop Battery Level Widget
« Reply #4 on: May 25, 2012, 08:49:23 am »
mmh- i recently worked in a similar area querying acpi wt sysctl.
Ive seen that things like this were implemented a bit different.
All the html is placed right directly in the widget, so everythings fine.
But i dont see hows your widget is going to update the batterys state over time.

If the NUT package is not an option, then you  may want to refactor your codes core functionality in functions.inc.php and return the data to the battery widget. If you are interested, then youll find some examples in system_information.widget.php.

Oh and i see a missing gettext($dev_ident)  ;)
But if you liked the experience of creating code and want to do the refactoring the i can help you
wt updating your widgets data with ajax.js/getstatus.php and to display a pretty battery bar :)

so - its up to you :)

Er, no. :-) I'm dangerous when it comes to code. By all means show me how it should be done and made pretty!  8)

I'm more of a lover than a fighter and more of a tester than a coder!  :D  ;D
----------
This user has been carbon dated to the 8-bit era...

Offline rcfa

  • Sr. Member
  • ****
  • Posts: 565
    • View Profile
Re: Laptop Battery Level Widget
« Reply #5 on: May 25, 2012, 01:54:56 pm »
Did you check out the NUT package?

Seems like that would also be a candidate to consider laptop batteries as "local UPS"...

I had a quick look at nut, but I didn't see anywhere it addressed non-networked UPS'. Unless I missed it?


I haven't used NUT yet myself, just installed it to have a look, because I'm considering getting some UPS for my network equipment, that said, however, if you go to the Services:NUT:NUT Settings tab, the first line in "General Settings" has a pop-up selector "UPS Monitoring" and among the choices is "Local UPS", and further down on the page are settings for local UPS devices, including USB devices, etc.

I guess that would have to be extended to include "built-in" UPSs, i.e. laptop batteries and such, but that would likely have to be done at the NUT level, rather than in pfSense to be done properly, everything else would be a hack.

http://www.networkupstools.org/docs/developer-guide.chunked/ar01s04.html

If a hack were to be made, likely the way to do it would be to fake an SNMP UPS and then use a remote SNMP UPS, but that would be pretty ugly.

Offline stephenw10

  • Hero Member
  • *****
  • Posts: 8094
    • View Profile
Re: Laptop Battery Level Widget
« Reply #6 on: May 25, 2012, 05:24:32 pm »
BTW Do Not use the Yukon GbE port, disable it in the BIOS, it has a nasty habit of hanging *BSD and Linux

Quite a lot of work has gone into the msk(4) driver between FreeBSD 8.1 ans 8.3 including code to handle some watchdog errors. You may want to re-try it.
What chip is used in your laptop NIC?

Steve

Offline daplumber

  • Jr. Member
  • **
  • Posts: 45
    • View Profile
Re: Laptop Battery Level Widget
« Reply #7 on: May 26, 2012, 09:34:47 pm »
BTW Do Not use the Yukon GbE port, disable it in the BIOS, it has a nasty habit of hanging *BSD and Linux

Quite a lot of work has gone into the msk(4) driver between FreeBSD 8.1 ans 8.3 including code to handle some watchdog errors. You may want to re-try it.
What chip is used in your laptop NIC?

Steve

It is (I quote): "Marvell Yukon 88E8053 PCI-E Gigabit Ethernet Controller"

Given that everything Open Source had to be reverse engineered (Thank you Marvell, Not!) I'm not convinced. Most of the hangs I've seen on various laptops of the era were IRQ handling related. But I'll give it a try and see.
----------
This user has been carbon dated to the 8-bit era...

Offline daplumber

  • Jr. Member
  • **
  • Posts: 45
    • View Profile
Re: Laptop Battery Level Widget
« Reply #8 on: May 26, 2012, 11:04:23 pm »
BTW Do Not use the Yukon GbE port, disable it in the BIOS, it has a nasty habit of hanging *BSD and Linux

Quite a lot of work has gone into the msk(4) driver between FreeBSD 8.1 ans 8.3 including code to handle some watchdog errors. You may want to re-try it.
What chip is used in your laptop NIC?

Steve

It is (I quote): "Marvell Yukon 88E8053 PCI-E Gigabit Ethernet Controller"

Given that everything Open Source had to be reverse engineered (Thank you Marvell, Not!) I'm not convinced. Most of the hangs I've seen on various laptops of the era were IRQ handling related. But I'll give it a try and see.

Nope. Re-enabling the Yukon in the BIOS still hangs pfsense/FreeBSD 8.3 an indeterminate number of minutes after bringing the msk driver up.

Fortunately this laptop has an ExpressCard slot, so I've ordered a cheap Realtek 8111 based GbE card.
----------
This user has been carbon dated to the 8-bit era...

Offline stephenw10

  • Hero Member
  • *****
  • Posts: 8094
    • View Profile
Re: Laptop Battery Level Widget
« Reply #9 on: May 27, 2012, 01:21:42 am »
Well that's disapointing.  :(
I updated the 'firmware' on that same chip which seemed to remove some trouble I was having. See:
http://forum.pfsense.org/index.php/topic,20095.msg207289.html#msg207289
Another user wrote up how to do it better than me!:
http://forum.pfsense.org/index.php/topic,20095.msg250430/topicseen.html#msg250430

What 'rev' do you have?

Steve

Offline daplumber

  • Jr. Member
  • **
  • Posts: 45
    • View Profile
Re: Laptop Battery Level Widget
« Reply #10 on: September 27, 2012, 01:28:07 pm »
So "sysctl hw.acpi.battery.life" has been returning "-1" for quite a few version of Beta0 now. Sorry to say I didn't notice when exactly. Any ideas on what broke?
----------
This user has been carbon dated to the 8-bit era...