@@ -109,6 +109,13 @@ extension Ocp1Connection {
109109 }
110110 }
111111
112+ private func _didConnect( ) async {
113+ _updateConnectionState ( . connected)
114+ await _refreshDeviceTreeWithPolicy ( )
115+ await refreshSubscriptions ( )
116+ await refreshCachedObjectProperties ( )
117+ }
118+
112119 /// reconnect to the OCA device with exponential backoff, updating
113120 /// connectionState
114121 private func reconnectDeviceWithBackoff( ) async throws {
@@ -118,7 +125,7 @@ extension Ocp1Connection {
118125 for i in 0 ..< options. reconnectMaxTries {
119126 do {
120127 try await _connectDeviceWithTimeout ( )
121- _updateConnectionState ( . connected )
128+ await _didConnect ( )
122129 break
123130 } catch {
124131 lastError = error
@@ -139,10 +146,6 @@ extension Ocp1Connection {
139146 _updateConnectionState ( . notConnected)
140147 throw Ocp1Error . notConnected
141148 }
142-
143- await _refreshDeviceTreeWithPolicy ( )
144- await refreshSubscriptions ( )
145- await refreshCachedObjectProperties ( )
146149 }
147150
148151 private var _needsReconnect : Bool {
@@ -236,9 +239,9 @@ public extension Ocp1Connection {
236239 if connectionState == . connecting {
237240 logger. trace ( " connected " )
238241 _updateConnectionState ( . connected)
239- await _refreshDeviceTreeWithPolicy ( )
242+ await _didConnect ( )
240243 } else if connectionState != . connected {
241- logger. trace ( " connection failed whilst connecting : \( connectionState) " )
244+ logger. trace ( " connection failed whilst attempting to connect : \( connectionState) " )
242245 throw connectionState. error ?? . notConnected
243246 }
244247 }
@@ -248,9 +251,7 @@ public extension Ocp1Connection {
248251
249252 _updateConnectionState ( . notConnected)
250253
251- try await disconnectDevice (
252- clearObjectCache: !options. flags
253- . contains ( . retainObjectCacheAfterDisconnect)
254- )
254+ let clearObjectCache = !options. flags. contains ( . retainObjectCacheAfterDisconnect)
255+ try await disconnectDevice ( clearObjectCache: clearObjectCache)
255256 }
256257}
0 commit comments