Click here to Skip to main content
11,489,693 members (69,060 online)
Click here to Skip to main content

Using WM_POWER events to monitor a UPS

, 1 Dec 2011 CPOL 12.2K 691 15
Rate this:
Please Sign up or sign in to vote.
Shows how to monitor a UPS, graph the details and shutdown the system

Introduction

A client wanted an app to monitor and log the UPS for an embedded windows controller.

This toolbar widget provides a framework to capture win32 WM_POWER events, leverage them to draw a graph of UPS state and optionally shutdown/suspend or hibernate the system.

The app is very simple and should be a good framework for other, related projects.

Background

As provided to our client, the application does more than as described here, however the additional functions were client specific and not pertinent.

When looking for a similar project, I found pieces, but nothing specific. As such, this project derives from projects by maharishi_b and lcady.

Using the Code

The app builds to a toolbar app that runs all the time and monitors windows power events. At each event, the app logs the event to a CSV log file and if the AC goes offline, the app pops up and shows a graph of the UPS capacity and the time left in mins.

All these features depend on your UPS being plugged in and supporting the standard windows HID power device profile.

During normal operations, the app may be viewed using the toolbar context menu (right click on the toolbar icon). Using 'show' will bring up the normal operations window:

This shows that the UPS is online with 100% battery capacity. The 'High' at the bottom is the windows power systems determination of the battery capacity. Full scale for the battery capacity is 100%.

When the AC line is lost, the main screen will popup and display the power status:

This shows that the AC is now offline with 67% battery capacity remaining. The system still regards this capacity as 'High'. Now that the AC is offline, the time remaining becomes relevant and is shown in red. Full scale for the time remaining is hard coded as 60mins.

After restoring power, the time remaining is no longer relevant and the system begins re-charging the battery:

Each event that occurs, is logged to the log.csv file in the directory from which PowerMonitor was started. The format is as follows:

Each event is logged by time, date and event type. Details of the event follow as per the system power status structure:

[StructLayout(LayoutKind.Sequential)]
public class SystemPowerStatus
    {
    public ACLineStatus ACLineStatus;
    public BatteryFlag BatteryFlag;
    public Byte BatteryLifePercent;
    public Byte Reserved1;
    public Int32 BatteryLifeTime;
    public Int32 BatteryFullLifeTime;

Most of the code is self explanatory. The strip chart is probably the most interesting - it is an evolution of lcady's strip chart. I am in the process of making it a general purpose strip chart class, but for now it is something of a simple hack.

I like the fact that this class draws to a BMP and then blits the BMP into a picture view. It means the chart construction is mostly independent of the viewing process and can be offloaded to other threads. This is particularly pertinent when doing data acquisition via potentially slow devices.

Points of Interest

It is exceptionally hard to map battery capacity to time left. UPS are getting better, but they mostly use battery voltage and this is a very poor indicator, especially when the battery is old, has been cycled a lot or has been recently cycled.

Time left is only an indicator and actions you take should be based on your own analysis of data collected during AC outages and recharge times.

History

  • 30th November, 2011: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

klinkenbecker
Software Developer (Senior)
United States United States
No Biography provided

Comments and Discussions

 
QuestionPower Management in Windows Forms Pin
Kim Togo6-Dec-11 1:30
memberKim Togo6-Dec-11 1:30 
Take a look at Power Management in Windows Forms[^] and PowerStatus class[^] all managed.
General[My vote of 2] Not very accurate and useful, needs improvements in all areas. Pin
Galatei5-Dec-11 12:36
memberGalatei5-Dec-11 12:36 
GeneralRe: [My vote of 2] Not very accurate and useful, needs improvements in all areas. Pin
klinkenbecker8-Dec-11 19:41
memberklinkenbecker8-Dec-11 19:41 
QuestionThis assumes that UPS software is installed, right? Pin
Paulo Morgado5-Dec-11 10:28
memberPaulo Morgado5-Dec-11 10:28 
AnswerRe: This assumes that UPS software is installed, right? Pin
klinkenbecker8-Dec-11 19:32
memberklinkenbecker8-Dec-11 19:32 
GeneralRe: This assumes that UPS software is installed, right? Pin
Paulo Morgado12-Dec-11 0:20
memberPaulo Morgado12-Dec-11 0:20 
GeneralMy vote of 2 Pin
Dave Kreskowiak1-Dec-11 5:00
mvpDave Kreskowiak1-Dec-11 5:00 
QuestionWhoops... Pin
Dave Kreskowiak1-Dec-11 4:59
mvpDave Kreskowiak1-Dec-11 4:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 1 Dec 2011
Article Copyright 2011 by klinkenbecker
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid