@@ -136,16 +136,16 @@ extension Ocp1Connection {
136136 }
137137 }
138138
139- /// functions with `_` prefix (with the exception of `_updateConnectionState()`) expect the
140- /// caller to update the connection state
141-
142- private func _updateConnectionState( _ connectionState: Ocp1ConnectionState ) {
139+ func updateConnectionState( _ connectionState: Ocp1ConnectionState ) {
143140 logger. trace ( " _updateConnectionState: \( _connectionState. value) => \( connectionState) " )
144141 _connectionState. send ( connectionState)
145142 }
146143
147144 private func _didConnectDevice( ) async throws {
148- _updateConnectionState ( . connected)
145+ if !isDatagram {
146+ // otherwise, set connected state when we receive first keepAlive PDU
147+ updateConnectionState ( . connected)
148+ }
149149
150150 _startMonitor ( )
151151
@@ -168,13 +168,13 @@ extension Ocp1Connection {
168168 public func connect( ) async throws {
169169 logger. trace ( " connecting... " )
170170
171- _updateConnectionState ( . connecting)
171+ updateConnectionState ( . connecting)
172172
173173 do {
174174 try await _connectDeviceWithTimeout ( )
175175 } catch {
176176 logger. trace ( " connection failed: \( error) " )
177- _updateConnectionState ( error. ocp1ConnectionState)
177+ updateConnectionState ( error. ocp1ConnectionState)
178178 throw error
179179 }
180180
@@ -186,6 +186,14 @@ extension Ocp1Connection {
186186 throw connectionState. error ?? . notConnected
187187 }
188188 }
189+
190+ var isConnecting : Bool {
191+ _connectionState. value == . connecting || _connectionState. value == . reconnecting
192+ }
193+
194+ public var isConnected : Bool {
195+ _connectionState. value == . connected
196+ }
189197}
190198
191199// MARK: - disconnection handling
@@ -213,7 +221,7 @@ extension Ocp1Connection {
213221 public func disconnect( ) async throws {
214222 await removeSubscriptions ( )
215223
216- _updateConnectionState ( . notConnected)
224+ updateConnectionState ( . notConnected)
217225
218226 let clearObjectCache = !options. flags. contains ( . retainObjectCacheAfterDisconnect)
219227 try await _disconnectDevice ( clearObjectCache: clearObjectCache)
@@ -257,7 +265,7 @@ extension Ocp1Connection {
257265 var lastError : Error ?
258266 var backoff : Duration = options. reconnectPauseInterval
259267
260- _updateConnectionState ( . reconnecting)
268+ updateConnectionState ( . reconnecting)
261269
262270 logger
263271 . trace (
@@ -283,11 +291,11 @@ extension Ocp1Connection {
283291
284292 if let lastError {
285293 logger. trace ( " reconnection abandoned: \( lastError) " )
286- _updateConnectionState ( lastError. ocp1ConnectionState)
294+ updateConnectionState ( lastError. ocp1ConnectionState)
287295 throw lastError
288- } else if !isConnected {
296+ } else if !isDatagram && ! isConnected {
289297 logger. trace ( " reconnection abandoned after too many tries " )
290- _updateConnectionState ( . notConnected)
298+ updateConnectionState ( . notConnected)
291299 throw Ocp1Error . notConnected
292300 }
293301 }
@@ -325,7 +333,7 @@ extension Ocp1Connection {
325333 " failed to send message: error \( error) , new connection state \( connectionState) ; disconnecting "
326334 )
327335 if isConnected {
328- _updateConnectionState ( connectionState)
336+ updateConnectionState ( connectionState)
329337 try await _disconnectDeviceAfterConnectionFailure ( )
330338 }
331339 }
@@ -336,15 +344,11 @@ extension Ocp1Connection {
336344
337345 logger. trace ( " expiring connection with policy \( _reconnectionPolicy) , error \( error) " )
338346
339- _updateConnectionState ( error. ocp1ConnectionState)
347+ updateConnectionState ( error. ocp1ConnectionState)
340348
341349 if _reconnectionPolicy == . reconnectInMonitor {
342350 try await _disconnectDeviceAfterConnectionFailure ( )
343351 Task { try await reconnectDeviceWithBackoff ( ) }
344352 }
345353 }
346-
347- public var isConnected : Bool {
348- _connectionState. value == . connected
349- }
350354}
0 commit comments