@@ -6,13 +6,20 @@ import { MosDevice } from './MosDevice'
6
6
import { SocketServerEvent , SocketDescription , IncomingConnectionType } from './connection/socketConnection'
7
7
import { NCSServerConnection } from './connection/NCSServerConnection'
8
8
import { MosModel } from '@mos-connection/helper'
9
- import { EventEmitter } from 'events '
9
+ import { EventEmitter } from 'eventemitter3 '
10
10
import * as iconv from 'iconv-lite'
11
- import { MosMessageParser } from './connection/mosMessageParser'
11
+ import { ParsedMosMessage , MosMessageParser } from './connection/mosMessageParser'
12
12
import { IConnectionConfig , IMosConnection , IMOSDeviceConnectionOptions } from './api'
13
13
import { PROFILE_VALIDNESS_CHECK_WAIT_TIME } from './lib'
14
14
15
- export class MosConnection extends EventEmitter implements IMosConnection {
15
+ export interface MosConnectionEvents {
16
+ // Note: These match the events defined in IMosConnection
17
+ rawMessage : ( source : string , type : string , message : string ) => void
18
+ info : ( message : string , data ?: any ) => void
19
+ warning : ( message : string ) => void
20
+ error : ( error : Error ) => void
21
+ }
22
+ export class MosConnection extends EventEmitter < MosConnectionEvents > implements IMosConnection {
16
23
static readonly CONNECTION_PORT_LOWER = 10540
17
24
static readonly CONNECTION_PORT_UPPER = 10541
18
25
static readonly CONNECTION_PORT_QUERY = 10542
@@ -95,7 +102,8 @@ export class MosConnection extends EventEmitter implements IMosConnection {
95
102
this . emit ( 'warning' , 'primary: ' + str )
96
103
} )
97
104
primary . on ( 'error' , ( error : Error ) => {
98
- this . emit ( 'error' , 'primary: ' + error + ' ' + ( typeof error === 'object' ? error . stack : '' ) )
105
+ error . message = `primary: ${ error . message } `
106
+ this . emit ( 'error' , error )
99
107
} )
100
108
primary . on ( 'info' , ( str : string ) => {
101
109
this . emit ( 'info' , 'primary: ' + str )
@@ -140,7 +148,8 @@ export class MosConnection extends EventEmitter implements IMosConnection {
140
148
this . emit ( 'warning' , 'secondary: ' + str )
141
149
} )
142
150
secondary . on ( 'error' , ( error : Error ) => {
143
- this . emit ( 'error' , 'secondary: ' + error + ' ' + ( typeof error === 'object' ? error . stack : '' ) )
151
+ error . message = `secondary: ${ error . message } `
152
+ this . emit ( 'error' , error )
144
153
} )
145
154
secondary . on ( 'info' , ( str : string ) => {
146
155
this . emit ( 'info' , 'secondary: ' + str )
@@ -359,9 +368,9 @@ export class MosConnection extends EventEmitter implements IMosConnection {
359
368
socketServer . on ( SocketServerEvent . CLIENT_CONNECTED , ( e : SocketDescription ) =>
360
369
this . _registerIncomingClient ( e )
361
370
)
362
- socketServer . on ( SocketServerEvent . ERROR , ( e ) => {
371
+ socketServer . on ( SocketServerEvent . ERROR , ( err ) => {
363
372
// handle error
364
- this . emit ( 'error' , e )
373
+ this . emit ( 'error' , err )
365
374
} )
366
375
367
376
return socketServer
@@ -398,7 +407,7 @@ export class MosConnection extends EventEmitter implements IMosConnection {
398
407
`${ socketID } , ${ client . socket . remoteAddress } , ${ client . portDescription } `
399
408
)
400
409
// messageParser.debug = this._debug
401
- messageParser . on ( 'message' , ( message : any , messageString : string ) => {
410
+ messageParser . on ( 'message' , ( message : ParsedMosMessage , messageString : string ) => {
402
411
// Handle incoming data
403
412
handleMessage ( message , messageString ) . catch ( ( err ) => this . emit ( 'error' , err ) )
404
413
} )
@@ -423,18 +432,18 @@ export class MosConnection extends EventEmitter implements IMosConnection {
423
432
messageParser . debug = this . _debug
424
433
messageParser . parseMessage ( messageString )
425
434
} catch ( err ) {
426
- this . emit ( 'error' , err )
435
+ this . emit ( 'error' , err instanceof Error ? err : new Error ( ` ${ err } ` ) )
427
436
}
428
437
} )
429
- const handleMessage = async ( parsed : any , _messageString : string ) => {
438
+ const handleMessage = async ( parsed : ParsedMosMessage , _messageString : string ) => {
430
439
const remoteAddressContent = client . socket . remoteAddress
431
440
? client . socket . remoteAddress . split ( ':' )
432
441
: undefined
433
442
const remoteAddress = remoteAddressContent ? remoteAddressContent [ remoteAddressContent . length - 1 ] : ''
434
443
435
444
const ncsID = parsed . mos . ncsID
436
445
const mosID = parsed . mos . mosID
437
- const mosMessageId : number = parsed . mos . messageID
446
+ const mosMessageId = parsed . mos . messageID ? parseInt ( parsed . mos . messageID , 10 ) : undefined
438
447
439
448
let mosDevice = this . _mosDevices [ ncsID + '_' + mosID ] || this . _mosDevices [ mosID + '_' + ncsID ]
440
449
@@ -472,8 +481,9 @@ export class MosConnection extends EventEmitter implements IMosConnection {
472
481
primary . on ( 'warning' , ( str : string ) => {
473
482
this . emit ( 'warning' , 'primary: ' + str )
474
483
} )
475
- primary . on ( 'error' , ( str : string ) => {
476
- this . emit ( 'error' , 'primary: ' + str )
484
+ primary . on ( 'error' , ( err : Error ) => {
485
+ err . message = `primary: ${ err . message } `
486
+ this . emit ( 'error' , err )
477
487
} )
478
488
const openRelayOptions : IMOSDeviceConnectionOptions [ 'primary' ] | undefined =
479
489
typeof this . _conf . openRelay === 'object' ? this . _conf . openRelay . options : undefined
@@ -503,24 +513,36 @@ export class MosConnection extends EventEmitter implements IMosConnection {
503
513
}
504
514
if ( mosDevice ) {
505
515
mosDevice
506
- . routeData ( parsed , client . portDescription )
516
+ . routeData ( parsed . mos , client . portDescription )
517
+ . catch ( ( err ) => {
518
+ if ( MosModel . ParseError . isParseError ( err ) ) {
519
+ // Try to add the main mos message key to the error:
520
+ const breadcrumbs = Object . keys ( parsed . mos ) . filter (
521
+ ( key ) =>
522
+ ! [ 'messageID' , 'mosID' , 'ncsID' ] . includes ( key ) &&
523
+ typeof parsed . mos [ key ] === 'object'
524
+ )
525
+ throw MosModel . ParseError . handleCaughtError ( breadcrumbs . join ( '/' ) , err )
526
+ } else throw err
527
+ } )
507
528
. then ( ( message : MosModel . MosMessage ) => {
508
529
sendReply ( message )
509
530
} )
510
- . catch ( ( err : Error | MosModel . MosMessage ) => {
531
+ . catch ( ( err : Error | MosModel . MosMessage | MosModel . ParseError ) => {
511
532
// Something went wrong
512
533
if ( err instanceof MosModel . MosMessage ) {
513
534
sendReply ( err )
514
535
} else {
515
- // Unknown / internal error
536
+ // Internal/parsing error
537
+
516
538
// Log error:
517
539
this . emit ( 'warning' , `Error when handling incoming data: ${ err } ${ err ?. stack } ` )
518
540
// reply with NACK:
519
541
// TODO: implement ACK
520
542
// https://mosprotocol.com/wp-content/MOS-Protocol-Documents/MOS_Protocol_Version_2.8.5_Final.htm#mosAck
521
543
const msg = new MosModel . MOSAck (
522
544
{
523
- ID : this . mosTypes . mosString128 . create ( 0 ) ,
545
+ ID : this . mosTypes . mosString128 . create ( '0' ) ,
524
546
Revision : 0 ,
525
547
Description : this . mosTypes . mosString128 . create (
526
548
`MosDevice "${ ncsID + '_' + mosID } " not found`
@@ -538,7 +560,7 @@ export class MosConnection extends EventEmitter implements IMosConnection {
538
560
// We can't handle the message, reply with a NACK:
539
561
const msg = new MosModel . MOSAck (
540
562
{
541
- ID : this . mosTypes . mosString128 . create ( 0 ) ,
563
+ ID : this . mosTypes . mosString128 . create ( '0' ) ,
542
564
Revision : 0 ,
543
565
Description : this . mosTypes . mosString128 . create ( 'MosDevice not found' ) ,
544
566
Status : IMOSAckStatus . NACK ,
@@ -550,10 +572,8 @@ export class MosConnection extends EventEmitter implements IMosConnection {
550
572
}
551
573
}
552
574
client . socket . on ( 'error' , ( e : Error ) => {
553
- this . emit (
554
- 'error' ,
555
- `Socket had error (${ socketID } , ${ client . socket . remoteAddress } , ${ client . portDescription } ): ${ e } `
556
- )
575
+ e . message = `Socket had error (${ socketID } , ${ client . socket . remoteAddress } , ${ client . portDescription } ): ${ e . message } `
576
+ this . emit ( 'error' , e )
557
577
this . debugTrace (
558
578
`Socket had error (${ socketID } , ${ client . socket . remoteAddress } , ${ client . portDescription } ): ${ e } `
559
579
)
0 commit comments