This repository has been archived by the owner on Sep 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CBDeviceClient.m
139 lines (111 loc) · 4.12 KB
/
CBDeviceClient.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//
// CBDeviceClient.m
// Cubicus
//
// Created by James Potter on 10/02/2012.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "CBDeviceClient.h"
#import "CBApplication.h"
@implementation CBDeviceClient
@synthesize delegate;
@synthesize guid;
- (id)initWithHost:(CBHost *)theHost guid:(NSString *)theGuid
{
self = [super initWithHost:theHost];
if (self) {
guid = theGuid;
}
return self;
}
#pragma mark -
#pragma mark Helper methods
+ (NSString *)generateGUID
{
CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
// __bridge_transfer means ARC takes care of release
NSString * uuidString = (__bridge_transfer NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuid);
CFRelease(uuid);
return uuidString;
}
#pragma mark -
#pragma mark Messages
- (void)switchApplication:(NSUInteger)applicationID context:(NSUInteger)contextID
{
NSDictionary *state = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:applicationID], @"current_application",
[NSNumber numberWithInt:contextID], @"current_context", nil];
[self sendMessage:@"state" content:state tag:0];
[self.delegate client:self didSwitchApplication:applicationID context:contextID];
}
- (void)sendEvent:(CBEvent *)event
{
NSLog(@"sending event; %@", event);
[self sendMessage:@"event" content:[event toJSON]];
}
- (void)sendPairResponse:(NSString *)pin
{
[self sendMessage:@"pair_response" content:pin];
}
#pragma mark -
#pragma mark AsyncSocketDelegate
//- (void)onSocketDidDisconnect:(AsyncSocket *)sock
//{
// NSLog(@"onSocketDidDisconnect called (failed to connect?)");
//}
- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
{
[super onSocket:sock didConnectToHost:host port:port];
// Step 1: send 'device_identify' message
[self sendMessage:@"device_identify" content:self.guid tag:CBDeviceClientTagIdentify];
}
- (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag
{
[super onSocket:sock didWriteDataWithTag:tag];
switch (tag) {
case CBDeviceClientTagIdentify:
// Step 2. wait for 'applications' message
[self readMessage];
break;
default:
NSLog(@"Unknown write tag: %li", tag);
break;
}
}
- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
[super onSocket:sock didReadData:data withTag:tag];
// Decode
NSDictionary *msg = (NSDictionary *)[self.parser objectWithData:data]; // assumes UTF-8
NSString *type = [msg objectForKey:@"type"];
id content = [msg objectForKey:@"content"];
NSLog(@"Received message type: %@", type);
if ([type isEqualToString:@"applications"]) {
// Convert to CBApplication's
NSArray *jsonArray = (NSArray *)content;
NSMutableArray *arr = [[NSMutableArray alloc] initWithCapacity:[jsonArray count]];
for (NSDictionary *dict in jsonArray) {
[arr addObject:[CBApplication fromJSON:dict]];
}
// Send to delegate
NSArray *applications = [[NSArray alloc] initWithArray:arr];
[self.delegate client:self didReceiveApplications:applications];
} else if ([type isEqualToString:@"state"]) {
// Send to delegate
NSDictionary *state = (NSDictionary *)content;
NSUInteger appID = [[state objectForKey:@"current_application"] intValue];
NSUInteger contextID = [[state objectForKey:@"current_context"] intValue];
[self.delegate client:self didSwitchApplication:appID context:contextID];
} else if ([type isEqualToString:@"event"]) {
// Deserialize and send to delegate
CBEvent *event = [CBEvent fromJSON:(NSDictionary *)content];
[self.delegate client:self didReceiveEvent:event];
} else if ([type isEqualToString:@"pair_request"]) {
[self.delegate clientDidReceivePairRequest:self];
} else {
NSLog(@"Unexpected message type: %@", type);
}
// Listen for the next message
[self readMessage];
}
@end