Binding with the Power Manager SDK

Binding makes it easy to track changes in the Power Manager engine. Binding is part of Objective-C/Cocoa's Key Value Observation (KVO) methodology, and Power Manager SDK provides full support for this sophisticated ability.

Binding makes it easy to track changes in the Power Manager engine. Binding is part of Objective-C/Cocoa’s Key Value Observation (KVO) methodology, and Power Manager SDK provides full support for this sophisticated ability.

Previously we walked through how to bind a piece of user interface to Power Manager. This time we will write a small tool that binds programmatically and runs without a graphical interface.

This recipe will highlight just how seamlessly you can integrate with Power Manager’s engine. All the networking, notifications, and requests are handled for you, all thanks to DSSWPMObserver.

Binding to the Events

We are going to create a small tool that binds an array to Power Manager’s events. The events we want to bind to those returned by the eventstore.events variable.

The resulting tool will have an NSArray of NSDictionaries managed and kept in sync with Power Manager’s events. As changes occur in Power Manager’s events, those changes will be instantly and automatically reflected in the tool.

  1. Create a new command line project in Xcode 4.

    Create a new command line project in Xcode.

  2. Select the Foundation type of project; this includes the Foundation framework.

    Select the Foundation type of project.

  3. Add the PowerManager.framework to your project.

  4. Copy and paste the following code into your main.m file:

    #import <Cocoa/Cocoa.h>
    #import <PowerManager/PowerManager.h>
    
    @interface Bound : NSObject {
        NSArray* events;
    }
    @property (retain) DSSWPMConnection* connection;
    @property (retain) DSSWPMObserver* observer;
    @property (retain) NSArray* events;
    @end
    
    @implementation Bound
    @synthesize connection;
    @synthesize observer;
    
    - (id)init
    {
        if (self = [super init])
        {
            self.connection = DSSWPMConnection.connection;
            self.connection.keepAlive = YES; // Stop idle connections disconnecting
            self.connection.autoReconnect = YES; // Reconnect if connection dropped
    
            self.observer = [[[DSSWPMObserver alloc] initWithConnection:connection] autorelease];
    
            // Bind the events in this object to Power Manager's eventstore.events
            [self bind:@"events" toObject:self.observer withKeyPath:(NSString*)kPMRPCEventStoreEvents options:nil];
        }
        return self;
    }
    
    - (void)dealloc
    {
        [self unbind:@"events"];
        self.events = nil;
        self.observer = nil;
        self.connection = nil;
        [super dealloc];
    }
    
    - (NSArray*)events
    {
        return [[events retain] autorelease];
    }
    
    - (void)setEvents:(NSArray *)inEvents
    {
        if (inEvents != events)
        {
            [events release];
            events = [inEvents retain];
    
            // Print a list of the event names
            NSLog(@"setEvents called with %d events:",(int)events.count);
            for(NSDictionary* event in events)
            {
                NSLog(@"- %@",[event objectForKey:(NSString*)kPMEventName]);
            }
        }
    }
    
    @end
    
    int main(int argc, const char * argv[])
    {
        @autoreleasepool {
            Bound* bound = [[Bound alloc] init];
    
            // Run until terminated
            [[NSRunLoop mainRunLoop] run];
    
            [bound release];
        }
        return 0;
    }
    
  5. Build and run the project.

With the tool running add, edit, and remove events from Power Manager. The tool will automatically print an updated list of events with each change.

The command line tool prints the names of your Power Manager events.

This code works by setting up an association between your local bound object with the corresponding eventstore.events variable being managed by Power Manager. The DSSWPMObserver object is responsible for performing the steps required to make this happen.

You can use this technique to bind to any variable provided by the Power Manager SDK.