473,499 Members | 1,659 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

System Information

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.

Mar 20 '06 #1
5 1147
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

Mar 20 '06 #2
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

Mar 20 '06 #3
Mi*****@gmail.com wrote:
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.

Mar 20 '06 #4
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 :)

Mar 20 '06 #5
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!

Mar 20 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
16154
by: Phil Stanton | last post by:
When designing a new form or report, the Default ForeColor is often something like -2147483640 which is the colour of Windows text (possibly black) and the default backColor is -2147483643...
6
6728
by: Markus Wildgruber | last post by:
Hi! How can I get information about the system my application is running on? I found the System.Environment class that provides me with some of the desired information but some of them not in...
3
2063
by: Karl Hungus | last post by:
A cs file I compiled into an assembly dll is in my bin directory. In the cs file I have a using statement for System.Xml I compiled it using this command: csc /out:XmlContent.dll /t:library...
1
5033
by: Olav Tollefsen | last post by:
I get the included error message when trying to run my ASP.NET application under Windows Server 2003 (with all updates installed). How can I troubleshoot this? Olav File or assembly name...
1
4303
by: Patrick | last post by:
When Tracing in ASP.NET, the IIS process (on IIs5.1) is locking on the Trace file, and I can't read the trace file without restarting the IIS: Even the following does NOT work (how could I fix...
5
9624
by: petro | last post by:
Hello all, My asp.net web application works on my machine but I get the following error on our test web server, There is only one oracle home on the test server. Does anyone know how to resolve...
0
2195
by: Gursharan | last post by:
Hi, I get this error after I did a load test on my website (500 constant user load in team system for 35 minutes). This error is logged after about 1 minute of load testing and is logged every...
8
3202
by: Henrik | last post by:
Hi Is there any way to see what the System process is doing? We have developed an application running at a production site to measure and optimize the production. The application needs to be...
0
2381
by: howardr101 | last post by:
Hi, Have hunted around on the groups and can't find anything, hence. I've tried this against 2 mail servers (mailtraq and hmailserver) and it occus with both. The problems seems to be that...
1
4707
by: keke3905 | last post by:
I really would appreciate some help on this assignment. I need to make GUI boxes to display the system info such as on Microsoft Office. I have some code but not sure where to go with the rest of...
0
7014
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7229
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5485
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4921
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3108
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.