@@ -30,42 +30,6 @@ struct Box<T: Any>: Hashable, Equatable {
30
30
}
31
31
}
32
32
33
- // MARK: Device Observer protocol
34
-
35
- public protocol DeviceObserver {
36
- func characteristicDidChange(
37
- _ accessory: Accessory ,
38
- serviceType: ServiceType ,
39
- characteristic: CharacteristicType )
40
- func characteristicListenerDidSubscribe(
41
- _ accessory: Accessory ,
42
- serviceType: ServiceType ,
43
- characteristic: CharacteristicType )
44
- func characteristicListenerDidUnsubscribe(
45
- _ accessory: Accessory ,
46
- serviceType: ServiceType ,
47
- characteristic: CharacteristicType )
48
- func didRequestIdentificationOf( _ accessory: Accessory )
49
- }
50
-
51
- public extension DeviceObserver {
52
- func characteristicDidChange( _ accessory: Accessory ,
53
- serviceType: ServiceType ,
54
- characteristic: CharacteristicType ) { }
55
- func characteristicListenerDidSubscribe(
56
- _ accessory: Accessory ,
57
- serviceType: ServiceType ,
58
- characteristic: CharacteristicType ) { }
59
- func characteristicListenerDidUnsubscribe(
60
- _ accessory: Accessory ,
61
- serviceType: ServiceType ,
62
- characteristic: CharacteristicType ) { }
63
- func didRequestIdentificationOf( _ accessory: Accessory ) { }
64
- }
65
-
66
- // MARK: Device class
67
-
68
- // swiftlint:disable:next type_body_length
69
33
public class Device {
70
34
public let name : String
71
35
public let isBridge : Bool
@@ -74,10 +38,10 @@ public class Device {
74
38
return configuration. setupCode
75
39
}
76
40
77
- public var observers : [ DeviceObserver ] = [ ]
78
-
79
41
public private( set) var accessories : [ Accessory ]
80
42
43
+ public var onIdentify : [ ( Accessory ? ) -> Void ] = [ ]
44
+
81
45
let storage : Storage
82
46
83
47
weak var server : Server ?
@@ -370,12 +334,6 @@ public class Device {
370
334
} else {
371
335
characteristicEventListeners [ Box ( characteristic) ] = [ listener]
372
336
}
373
- if let accessory = characteristic. service? . accessory {
374
- _ = observers. map { $0. characteristicListenerDidSubscribe ( accessory,
375
- serviceType: characteristic. service!. type,
376
- characteristic: characteristic. type)
377
- }
378
- }
379
337
}
380
338
381
339
@discardableResult
@@ -384,28 +342,11 @@ public class Device {
384
342
guard characteristicEventListeners [ Box ( characteristic) ] != nil else {
385
343
return nil
386
344
}
387
- let subscriber = characteristicEventListeners [ Box ( characteristic) ] !. remove ( connection)
388
- if let accessory = characteristic. service? . accessory {
389
- _ = observers. map { $0. characteristicListenerDidUnsubscribe ( accessory,
390
- serviceType: characteristic. service!. type,
391
- characteristic: characteristic. type)
392
- }
393
- }
394
- return subscriber
345
+ return characteristicEventListeners [ Box ( characteristic) ] !. remove ( connection)
395
346
}
396
347
397
- func notifyChange( of characteristic: Characteristic ,
398
- exceptToListener except: Server . Connection ? = nil ) {
399
- // Notify external listeners of a characteristic change
400
- // The characteristic itself is not passed to avoid Box'ing the object
401
- if let accessory = characteristic. service? . accessory {
402
- _ = observers. map { $0. characteristicDidChange ( accessory,
403
- serviceType: characteristic. service!. type,
404
- characteristic: characteristic. type)
405
-
406
- }
407
- }
408
- // Notify internal listeners (server connections) of changes to a characteristic
348
+ func notify( characteristicListeners characteristic: Characteristic ,
349
+ exceptListener except: Server . Connection ? = nil ) {
409
350
guard let listeners = characteristicEventListeners [ Box ( characteristic) ] ? . filter ( { $0 != except } ) ,
410
351
!listeners. isEmpty
411
352
else {
0 commit comments