Skip to content

Commit 9d04708

Browse files
committed
[darwin-framework-tool] Add some logging code to HomeKit support on iOS
1 parent c02aac6 commit 9d04708

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

examples/darwin-framework-tool/commands/common/xpc/HomeKitConnector.mm

+69
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ - (void)start
5959

6060
// Wait until homeManagerDidUpdateHomes is called or timeout
6161
dispatch_group_wait(_homeManagerReadyGroup, dispatch_time(DISPATCH_TIME_NOW, kHomeManagerSetupTimeout));
62+
63+
[self printHomes];
6264
}
6365

6466
- (void)stop
@@ -100,6 +102,73 @@ - (HMHome *)homeFor:(NSString *)controllerID
100102
return homes[index];
101103
}
102104

105+
- (NSString *)paddedString:(NSString *)string width:(NSUInteger)width
106+
{
107+
// Using length might not account for all unicode width details, but it's a simple approximation.
108+
NSUInteger length = string.length;
109+
if (length >= width) {
110+
return string;
111+
}
112+
NSMutableString * result = [NSMutableString stringWithString:string];
113+
for (NSUInteger i = 0; i < (width - length); i++) {
114+
[result appendString:@" "];
115+
}
116+
return result;
117+
}
118+
119+
- (NSString *)repeatString:(NSString *)string count:(NSUInteger)count
120+
{
121+
NSMutableString * result = [NSMutableString string];
122+
for (NSUInteger i = 0; i < count; i++) {
123+
[result appendString:string];
124+
}
125+
return result;
126+
}
127+
128+
- (void)printHomes
129+
{
130+
for (HMHome * home in _homeManager.homes) {
131+
NSUInteger maxNameLength = 0;
132+
NSUInteger maxNodeIDLength = 0;
133+
NSUInteger maxManufacturerLength = 0;
134+
NSUInteger maxModelLength = 0;
135+
136+
__auto_type * sortedAccessories = [home.accessories sortedArrayUsingComparator:^NSComparisonResult(HMAccessory * a, HMAccessory * b) {
137+
return [a.name localizedCaseInsensitiveCompare:b.name];
138+
}];
139+
140+
for (HMAccessory * accessory in sortedAccessories) {
141+
maxNameLength = MAX(maxNameLength, accessory.name.length);
142+
maxManufacturerLength = MAX(maxManufacturerLength, accessory.manufacturer.length);
143+
maxModelLength = MAX(maxModelLength, accessory.model.length);
144+
maxNodeIDLength = MAX(maxNodeIDLength, [accessory.matterNodeID stringValue].length);
145+
}
146+
147+
__auto_type * rows = [NSMutableArray arrayWithCapacity:sortedAccessories.count];
148+
[sortedAccessories enumerateObjectsUsingBlock:^(HMAccessory * accessory, NSUInteger idx, BOOL * stop) {
149+
if (accessory.matterNodeID == nil || [accessory.matterNodeID integerValue] == 0) {
150+
return;
151+
}
152+
153+
__auto_type * name = [self paddedString:accessory.name width:maxNameLength];
154+
__auto_type * manufacturer = [self paddedString:accessory.manufacturer width:maxManufacturerLength];
155+
__auto_type * model = [self paddedString:accessory.model width:maxModelLength];
156+
__auto_type * nodeID = [self paddedString:[accessory.matterNodeID stringValue] width:maxNodeIDLength];
157+
__auto_type * formattedString = [NSString stringWithFormat:@" %@%@%@%@ ", name, manufacturer, model, nodeID];
158+
[rows addObject:formattedString];
159+
}];
160+
161+
NSUInteger tableWidth = 1 + maxNameLength + 3 + maxManufacturerLength + 3 + maxModelLength + 3 + maxNodeIDLength + 1;
162+
NSLog(@"%@", [self repeatString:@"" count:tableWidth]);
163+
NSLog(@"%@", [self paddedString:[NSString stringWithFormat:@" %@ [%@] ", home.name, home.matterControllerID] width:tableWidth]);
164+
NSLog(@"%@", [self repeatString:@"" count:tableWidth]);
165+
for (NSString * row in rows) {
166+
NSLog(@"%@", row);
167+
}
168+
NSLog(@"%@", [self repeatString:@"" count:tableWidth]);
169+
}
170+
}
171+
103172
- (NSString *)homeControllerIDFor:(NSString *)controllerID
104173
{
105174
__auto_type * home = [self homeFor:controllerID];

0 commit comments

Comments
 (0)