Skip to content

Instantly share code, notes, and snippets.

@0xc010d
Forked from xslim/TVRemote.m
Created March 15, 2012 15:41
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save 0xc010d/2044858 to your computer and use it in GitHub Desktop.
TVRemote
@interface TVRemote : NSObject
@property (nonatomic, retain) TVRemoteAbstract *currentRemote;
- (void)createSocketRemote;
@end
@implementation TVRemote
- (void)createSocketRemote {
self.currentRemote = [[TVRemoteSocket alloc] init];
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
{
NSMethodSignature *signature = [super methodSignatureForSelector:aSelector];
if(!signature) {
if ([self.currentRemote respondsToSelector:aSelector]) {
return [self.currentRemote methodSignatureForSelector:aSelector];
}
signature = [NSMethodSignature signatureWithObjCTypes: "@^v^c"];
}
return signature;
}
- (void)forwardInvocation:(NSInvocation *)anInvocation
{
if ([self.currentRemote respondsToSelector:[anInvocation selector]]) {
[anInvocation invokeWithTarget:self.currentRemote];
}
}
@end
@protocol TVRemoteAbstract <NSObject>
@property (nonatomic, assign) BOOL isTCP;
@property (nonatomic, assign) BOOL isHTTP;
@property (nonatomic, assign) BOOL isJSON;
@property (nonatomic, assign) BOOL requiresPersistantConnection;
@property (nonatomic, copy) NSString *host;
@property (nonatomic, assign) NSUInteger port;
@property (nonatomic, readonly) BOOL isConnected;
@property (nonatomic, readonly) BOOL finishedSending;
- (void)connect;
- (void)connectToHost:(NSString *)host port:(NSUInteger)port;
- (void)disconnect;
- (void)disconnectNow:(BOOL)now;
- (void)authorize;
- (void)sendNextProgram;
- (void)sendPreviousProgram;
- (void)sendVolumeUp;
- (void)sendVolumeDown;
- (void)sendOK;
- (void)selectProgramNumber:(NSUInteger)programNumber;
@end
@interface TVRemoteAbstract : NSObject <TVRemoteAbstract>
@end
@interface TVRemoteSocket : TVRemoteAbstract
@end
@implementation TVRemoteSocket
static const NSTimeInterval kPacketSendingTimerInterval = 0.250;
- (id)init
{
self = [super init];
if (self) {
self.packetsToSend = [NSMutableArray array];
_socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
//[_socket synchronouslySetDelegate:self];
//[_socket setAutoDisconnectOnClosedReadStream:NO];
}
return self;
}
- (void)testAuthorizeAndVolumeUp
{
TVRemote<TVRemoteAbstract> *remote = [[TVRemote alloc] init];
[remote createSocketRemote];
[remote connectToHost:@"10.10.10.87" port:55000];
[remote authorize];
/*
[remote sendVolumeUp];
[remote sendVolumeUp];
[remote sendVolumeUp];
[remote sendVolumeUp];
*/
[remote selectProgramNumber:1];
[remote sendVolumeUp];
[remote sendVolumeUp];
[remote sendVolumeUp];
[remote sendVolumeDown];
[remote sendVolumeDown];
/*
[remote sendPreviousProgram];
*/
//[remote selectProgramNumber:21];
while (remote.finishedSending) {
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0]];
NSLog(@"Polling...");
}
[remote disconnect];
while (remote.isConnected) {
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:2.0]];
NSLog(@"Disconnecting...");
}
//[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:3.0]];
//NSLog(@"Done..?");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment