System Information

M

MidiBot

Hello. I want to write a program in which the main functionality will
depend on whether or not my
computer is connected to a power adapter(this is for laptops). I looked

in the online-documentation for this and had no luck. I imagine it is
quite easy and may even be as simple as a boolean variable...?

All I want to be able to tell is whether or not my computer is being
externally powered. More specifically
I want to be able to have my program running, and be alerted if I
un-plug my power adapter.

I am using a Mac(powerbook) with if that makes any
difference.

Thank you very much.
 
T

totalgeekdom

I'm not sure about osx, but I know linux uses a proc virtual
filesystem. ( freebsd is probably closer, and it doesn't either but you
can mount it via mount_linprocfs none /proc)

But if you know if you have a proc file system, you can check it to see
if you are under power...

IE if I remember right, on my laptop it would be
'/proc/acpi/battery/BAT1'
From there I would grep / sed it until I got the results I wanted, then
do
if os.popen("cat /proc[...]") == 'power':
[program]
else:
print "not plugged in"



Not the prettiest of solutions, but it's worth a shot
 
T

totalgeekdom

Oops -- just asked a buddy, osx doesn't have /proc

But you could look into sysctl....
I'll boot up my laptop and check out if there is anything regarding the
battery, but I suspect you should investigate
sysctl -a | grep acpi
 
S

Serge Orlov

in the online-documentation for this and had no luck. I imagine it is
quite easy and may even be as simple as a boolean variable...?

In general it's not so simple, since a computer can have many power
sources.
All I want to be able to tell is whether or not my computer is being
externally powered. More specifically
I want to be able to have my program running, and be alerted if I
un-plug my power adapter.

I am using a Mac(powerbook) with if that makes any
difference.

It makes big difference, device API on different operating systems is
*absolutely* different. On Mac OS X, start from ioreg:
http://www.hmug.org/man/8/ioreg.php,
also take a look at command line utility for querying battery info:
http://www.mitt-eget.com/software/macosx/
These two pages should help you get started.

-- Serge.
 
T

totalgeekdom

When I did
"
sysctl -a > sysctl-plugged-in.txt
sysctl -a > sysctl-unplugged.txt
diff sysctl-plugged-in.txt sysctl-unplugged.txt
"

I didn't get anything of interest...

Maybe there is something I missed, or maybe it's just because I was
using a live linux distro, or maybe sysctl isn't the way to go :)
 
T

totalgeekdom

Good news!
I asked a buddy that runs osx, and he whipped up a script for you :)

I understand it's not python, but it gets you close, you should be able
to use popen or subprocess.call to complete this.

function is_charger_plugged_in()
{
sleep 3;

BATTINFO=`ioreg -l -w 0 | grep IOBatteryInfo | head -n 1 | sed
-e 's/[( ) { }]//g'`
AMP=`echo $BATTINFO | awk -F, '{print $2}' | cut -f 2 -d =`
CAP=`echo $BATTINFO | awk -F, '{print $7}' | cut -f 2 -d =`

if [ "$AMP" -lt "$CAP" ]; then
return 0
else
return 1
fi > /dev/null 2>&1
}

is_charger_plugged_in

if [ "$?" -eq "0" ]; then
echo "charger is plugged in";
else
echo "charger is not plugged in";
fi


If I had to guess for the python script I'd do --
#!/usr/bin/env python
import time,os

def pluggedin():
# ioreg is for osx only
time.sleep(3)
battInfo = "ioreg -l -w 0 | grep IOBatteryInfo | head -n 1 | sed -e
's/[( ) { }]//g'"
amp = "echo $BATTINFO | awk -F, '{print $2}' | cut -f 2 -d ="
cap = "echo $BATTINFO | awk -F, '{print $7}' | cut -f 2 -d ="
if amp < cap:
return 0
else:
return 1
if pluggedin() == 1:
print "It is plugged in"
else:
print "It is not plugged in"


Original code was hooked up to me by prism =) Cause he's a cool guy

Hope that helps!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top