@@ -32,6 +32,13 @@ export interface MicrobitRadioBridgeConnectionOptions {
32
32
logging : Logging ;
33
33
}
34
34
35
+ interface ConnectCallbacks {
36
+ onConnecting : ( ) => void ;
37
+ onReconnecting : ( ) => void ;
38
+ onFail : ( ) => void ;
39
+ onSuccess : ( ) => void ;
40
+ }
41
+
35
42
/**
36
43
* Wraps around a USB connection to implement a subset of services over a serial protocol.
37
44
*
@@ -48,7 +55,7 @@ export class MicrobitRadioBridgeConnection
48
55
private disconnectPromise : Promise < void > | undefined ;
49
56
private serialSessionOpen = false ;
50
57
51
- private delegateStatusListner = ( e : ConnectionStatusEvent ) => {
58
+ private delegateStatusListener = ( e : ConnectionStatusEvent ) => {
52
59
const currentStatus = this . status ;
53
60
if ( e . status !== ConnectionStatus . CONNECTED ) {
54
61
this . setStatus ( e . status ) ;
@@ -84,11 +91,11 @@ export class MicrobitRadioBridgeConnection
84
91
async initialize ( ) : Promise < void > {
85
92
await this . delegate . initialize ( ) ;
86
93
this . setStatus ( this . statusFromDelegate ( ) ) ;
87
- this . delegate . addEventListener ( "status" , this . delegateStatusListner ) ;
94
+ this . delegate . addEventListener ( "status" , this . delegateStatusListener ) ;
88
95
}
89
96
90
97
dispose ( ) : void {
91
- this . delegate . removeEventListener ( "status" , this . delegateStatusListner ) ;
98
+ this . delegate . removeEventListener ( "status" , this . delegateStatusListener ) ;
92
99
this . delegate . dispose ( ) ;
93
100
}
94
101
@@ -124,36 +131,39 @@ export class MicrobitRadioBridgeConnection
124
131
this . remoteDeviceId ,
125
132
this . delegate ,
126
133
this . dispatchTypedEvent . bind ( this ) ,
127
- this . setStatus . bind ( this ) ,
128
- ( ) => {
129
- // Remote connection lost
130
- this . logging . event ( {
131
- type : "Serial" ,
132
- message : "Serial connection lost 1" ,
133
- } ) ;
134
- // This is the point we tell the consumer that we're trying to reconnect
135
- // in the background.
136
- // Leave serial connection running in case the remote device comes back.
137
- } ,
138
- ( ) => {
139
- // Remote connection... even more lost?
140
- this . logging . event ( {
141
- type : "Serial" ,
142
- message : "Serial connection lost 2" ,
143
- } ) ;
144
- this . serialSession ?. dispose ( ) ;
134
+ {
135
+ onConnecting : ( ) => this . setStatus ( ConnectionStatus . CONNECTING ) ,
136
+ onReconnecting : ( ) => {
137
+ // Leave serial connection running in case the remote device comes back.
138
+ if ( this . status !== ConnectionStatus . RECONNECTING ) {
139
+ this . setStatus ( ConnectionStatus . RECONNECTING ) ;
140
+ }
141
+ } ,
142
+ onFail : ( ) => {
143
+ if ( this . status !== ConnectionStatus . DISCONNECTED ) {
144
+ this . setStatus ( ConnectionStatus . DISCONNECTED ) ;
145
+ }
146
+ this . serialSession ?. dispose ( ) ;
147
+ this . serialSessionOpen = false ;
148
+ } ,
149
+ onSuccess : ( ) => {
150
+ if ( this . status !== ConnectionStatus . CONNECTED ) {
151
+ this . setStatus ( ConnectionStatus . CONNECTED ) ;
152
+ }
153
+ this . serialSessionOpen = true ;
154
+ } ,
145
155
} ,
146
156
) ;
147
157
148
158
await this . serialSession . connect ( ) ;
149
- this . serialSessionOpen = true ;
150
159
151
160
this . logging . event ( {
152
161
type : "Connect" ,
153
162
message : "Serial connect success" ,
154
163
} ) ;
155
164
return this . status ;
156
165
} catch ( e ) {
166
+ this . serialSessionOpen = false ;
157
167
this . logging . error ( "Failed to initialise serial protocol" , e ) ;
158
168
this . logging . event ( {
159
169
type : "Connect" ,
@@ -174,8 +184,13 @@ export class MicrobitRadioBridgeConnection
174
184
} ) ( ) ;
175
185
}
176
186
187
+ private log ( v : any ) {
188
+ this . logging . log ( v ) ;
189
+ }
190
+
177
191
private setStatus ( status : ConnectionStatus ) {
178
192
this . status = status ;
193
+ this . log ( "Radio connection status " + status ) ;
179
194
this . dispatchTypedEvent ( "status" , new ConnectionStatusEvent ( status ) ) ;
180
195
}
181
196
@@ -267,18 +282,15 @@ class RadioBridgeSerialSession {
267
282
private remoteDeviceId : number ,
268
283
private delegate : MicrobitWebUSBConnection ,
269
284
private dispatchTypedEvent : TypedServiceEventDispatcher ,
270
- private onStatusChanged : ( status : ConnectionStatus ) => void ,
271
- private onRemoteConnectionLost1 : ( ) => void ,
272
- private onRemoteConnectionLost2 : ( ) => void ,
285
+ private callbacks : ConnectCallbacks ,
273
286
) { }
274
287
275
288
async connect ( ) {
276
289
this . delegate . addEventListener ( "serialdata" , this . serialDataListener ) ;
277
290
this . delegate . addEventListener ( "serialerror" , this . serialErrorListener ) ;
278
-
279
291
try {
292
+ this . callbacks . onConnecting ( ) ;
280
293
await this . handshake ( ) ;
281
- this . onStatusChanged ( ConnectionStatus . CONNECTED ) ;
282
294
283
295
this . logging . log ( `Serial: using remote device id ${ this . remoteDeviceId } ` ) ;
284
296
const remoteMbIdCommand = protocol . generateCmdRemoteMbId (
@@ -319,8 +331,9 @@ class RadioBridgeSerialSession {
319
331
await periodicMessagePromise ;
320
332
321
333
this . startConnectionCheck ( ) ;
334
+ this . callbacks . onSuccess ( ) ;
322
335
} catch ( e ) {
323
- this . dispose ( ) ;
336
+ this . callbacks . onFail ( ) ;
324
337
}
325
338
}
326
339
@@ -335,8 +348,6 @@ class RadioBridgeSerialSession {
335
348
this . delegate . removeEventListener ( "serialdata" , this . serialDataListener ) ;
336
349
this . delegate . removeEventListener ( "serialerror" , this . serialErrorListener ) ;
337
350
await this . delegate . softwareReset ( ) ;
338
-
339
- this . onStatusChanged ( ConnectionStatus . DISCONNECTED ) ;
340
351
}
341
352
342
353
private async sendCmdWaitResponse (
@@ -358,19 +369,33 @@ class RadioBridgeSerialSession {
358
369
private startConnectionCheck ( ) {
359
370
// Check for connection lost
360
371
if ( this . connectionCheckIntervalId === undefined ) {
361
- this . connectionCheckIntervalId = setInterval ( async ( ) => {
372
+ this . connectionCheckIntervalId = setInterval ( ( ) => {
373
+ if (
374
+ this . lastReceivedMessageTimestamp &&
375
+ Date . now ( ) - this . lastReceivedMessageTimestamp <= 1_000
376
+ ) {
377
+ this . callbacks . onSuccess ( ) ;
378
+ }
362
379
if (
363
380
this . lastReceivedMessageTimestamp &&
364
381
Date . now ( ) - this . lastReceivedMessageTimestamp > 1_000
365
382
) {
366
- this . onRemoteConnectionLost1 ( ) ;
383
+ this . logging . event ( {
384
+ type : "Serial" ,
385
+ message : "Serial connection lost - attempting to reconnect" ,
386
+ } ) ;
387
+ this . callbacks . onReconnecting ( ) ;
367
388
}
368
389
if (
369
390
this . lastReceivedMessageTimestamp &&
370
391
Date . now ( ) - this . lastReceivedMessageTimestamp >
371
392
connectTimeoutDuration
372
393
) {
373
- this . onRemoteConnectionLost2 ( ) ;
394
+ this . logging . event ( {
395
+ type : "Serial" ,
396
+ message : "Serial connection lost" ,
397
+ } ) ;
398
+ this . callbacks . onFail ( ) ;
374
399
}
375
400
} , 1000 ) ;
376
401
}
0 commit comments