Connecting with the Power Manager SDK

You can create tools and utilities that talk directly to Power Manager using the Software Development Kit (SDK). Connecting to Power Manager from your software is easy and requires only a few lines of code.

You can create tools and utilities that talk directly to Power Manager using the Software Development Kit (SDK). Connecting to Power Manager from your software is easy and requires only a few lines of code.

The Power Manager SDK is included as standard with Power Manager. There are no additional tools, software, or licences to deal with in order to start using the Power Manager SDK.

Let’s walk through the steps needed to create a connection from your program to Power Manager. We are going to use Xcode 4 and Objective-C/Cocoa for this example.

Create an Xcode Project

We need a project to add Power Manager support to. I recommend starting a small test project to experiment with first. You can later add Power Manager to existing projects.

  1. Create a new project with Xcode: File > New > Project…
  2. Select the Cocoa Application project template.
  3. Work through Xcode’s project creation steps.

Add the Power Manager Framework

We need to add the PowerManager.framework to our Xcode project. This framework includes everything you need to connect with Power Manager.

  1. Navigate to the project’s Build Phases in Xcode.

    Within Xcode, navigate to your project’s Build Phases.

  2. In the Link Binary With Libraries phase, click + to add a library.

    Add the framework called PowerManager.framework

  3. Add the PowerManager.framework.

    The Power Manager framework should now be listed and linked during builds.

If the PowerManager.framework is not listed, look for the framework in the folder /Library/Frameworks/. If the framework is missing, install Power Manager on your Mac.

Create a Connection

With the framework added to your project, you can now connect to Power Manager.

For this experiment, let’s add a Power Manager connection to the project’s application delegate. This is an object created when the application is launched. The files for this object should have been created by Xcode for you.

Look for a pair of files ending with AppDelegate; in my case, the file name is DSSWTestAppDelegate.

We are going to add a few bits and pieces to both the header (.h) and body (.m) files. Let’s look at the finished DSSWTestAppDelegate.h first:

#import <Cocoa/Cocoa.h>
#import <PowerManager/PowerManager.h>

@interface DSSWTestAppDelegate : NSObject 

@property (assign) IBOutlet NSWindow *window;
@property (retain) DSSWPMConnection *connection;
@property (retain) DSSWPMObserver *observer;

@end

In the header file:

  • I have added an import to open up access to the contents of PowerManager.framework.
  • I have added declarations for two new properties; a connection and an observer.

Let’s look at the finished DSSWTestAppDelegate.m next:

#import "DSSWTestAppDelegate.h"

@implementation DSSWTestAppDelegate

@synthesize window = _window;
@synthesize connection;
@synthesize observer;

- (void)dealloc
{
    self.observer = nil;
    self.connection = nil;
    [super dealloc];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    self.connection = DSSWPMConnection.connection;
    self.connection.keepAlive = YES;
    self.connection.autoReconnect = YES;
    self.observer = [[[DSSWPMObserver alloc] initWithConnection:self.connection] autorelease];
}

@end

In the body file:

  • I have added synthesize entries for the connection and observer.
  • I instigate the connection and observer when the application launches.
  • I nullify the connection and observer when the application delegate is deallocated.

This small body of code is all you need in order to set up and maintain a connection to Power Manager running on your Mac.

Next learn how to observe changes in Power Manager.