Repository navigation
Expand file tree
/
Copy pathHAWatchManager.m
More file actions
134 lines (120 loc) · 4.32 KB
/
Copy pathHAWatchManager.m
File metadata and controls
134 lines (120 loc) · 4.32 KB
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
#import "HAWatchManager.h"
#import "HAHomeManager.h"
#import "HAWatchServiceRequest.h"
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 90000
#import <WatchConnectivity/WatchConnectivity.h>
@interface HAWatchManager () <WCSessionDelegate>
@property(nonatomic, retain) WCSession *session;
@property(nonatomic, retain) NSArray *entities;
@end
#endif
@implementation HAWatchManager
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 90000
@synthesize session = _session;
@synthesize entities = _entities;
#endif
+ (HAWatchManager *)sharedManager {
static HAWatchManager *manager = nil;
if (manager == nil) {
manager = [[HAWatchManager alloc] init];
}
return manager;
}
- (void)start {
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 90000
if (NSClassFromString(@"WCSession") == nil || ![WCSession isSupported]) {
return;
}
self.session = [WCSession defaultSession];
self.session.delegate = self;
[self.session activateSession];
#endif
}
- (void)updateEntities:(NSArray *)entities {
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 90000
NSMutableArray *watchEntities = [NSMutableArray array];
for (NSDictionary *entity in entities) {
NSString *entityID = [entity objectForKey:@"entity_id"];
NSString *state = [entity objectForKey:@"state"];
NSDictionary *attributes = [entity objectForKey:@"attributes"];
if ([entityID length] == 0 || [state length] == 0) {
continue;
}
NSMutableDictionary *watchEntity = [NSMutableDictionary dictionaryWithObjectsAndKeys:
entityID, @"entity_id",
state, @"state",
[attributes objectForKey:@"friendly_name"] ?: entityID, @"name",
nil];
id brightness = [attributes objectForKey:@"brightness"];
if ([brightness isKindOfClass:[NSNumber class]]) {
[watchEntity setObject:brightness forKey:@"brightness"];
}
[watchEntities addObject:watchEntity];
}
self.entities = watchEntities;
[self syncCurrentContext];
#endif
}
- (void)syncCurrentContext {
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 90000
if (self.session == nil) {
return;
}
NSDictionary *home = [HAHomeManager selectedHome];
if (home == nil) {
return;
}
NSDictionary *context = [NSDictionary dictionaryWithObjectsAndKeys:
@1, @"protocol_version",
[home objectForKey:HAHomeIdentifierKey] ?: @"", @"home_id",
[home objectForKey:HAHomeNameKey] ?: @"Home", @"home_name",
[HAHomeManager selectedEntityIDs], @"selected_entity_ids",
self.entities ?: [NSArray array], @"entities",
nil];
[self.session updateApplicationContext:context error:nil];
#endif
}
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 90000
- (void)session:(WCSession *)session
activationDidCompleteWithState:(WCSessionActivationState)activationState
error:(NSError *)error {
if (error == nil) {
[self syncCurrentContext];
}
}
- (void)sessionDidBecomeInactive:(WCSession *)session {
}
- (void)sessionDidDeactivate:(WCSession *)session {
[session activateSession];
}
- (void)session:(WCSession *)session
didReceiveMessage:(NSDictionary<NSString *, id> *)message
replyHandler:(void (^)(NSDictionary<NSString *, id> *replyMessage))replyHandler {
if (![[message objectForKey:@"type"] isEqualToString:@"call_service"]) {
replyHandler([NSDictionary dictionaryWithObjectsAndKeys:
@NO, @"ok", @"Unsupported watch message.", @"message", nil]);
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
NSDictionary *home = [HAHomeManager selectedHome];
NSString *requestedHomeID = [message objectForKey:@"home_id"];
if (home == nil || ![[home objectForKey:HAHomeIdentifierKey] isEqualToString:requestedHomeID]) {
replyHandler([NSDictionary dictionaryWithObjectsAndKeys:
@NO, @"ok", @"Open the selected home on the iPhone first.", @"message", nil]);
return;
}
HAWatchServiceRequest *request = [[[HAWatchServiceRequest alloc]
initWithHome:home message:message replyHandler:replyHandler] autorelease];
[request start];
});
}
#endif
- (void)dealloc {
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 90000
_session.delegate = nil;
[_session release];
[_entities release];
#endif
[super dealloc];
}
@end