@@ -36,34 +36,34 @@ type PortOptions = {
36
36
*/
37
37
export class MinimalAdapter {
38
38
public readonly driver : OTRCPDriver ;
39
- private readonly portOptions : PortOptions ;
40
- private serialPort ?: SerialPort ;
41
- private socketPort ?: Socket ;
39
+ readonly # portOptions: PortOptions ;
40
+ # serialPort?: SerialPort ;
41
+ # socketPort?: Socket ;
42
42
/** True when serial/socket is currently closing */
43
- private closing : boolean ;
43
+ # closing: boolean ;
44
44
45
- private wiresharkSeqNum : number ;
46
- private wiresharkPort : number ;
47
- private wiresharkAddress : string ;
48
- private readonly wiresharkSocket : DgramSocket ;
45
+ # wiresharkSeqNum: number ;
46
+ # wiresharkPort: number ;
47
+ # wiresharkAddress: string ;
48
+ readonly # wiresharkSocket: DgramSocket ;
49
49
50
50
constructor ( portOptions : PortOptions , streamRawConfig : StreamRawConfig , netParams : NetworkParameters , sendMACToZEP : boolean ) {
51
- this . wiresharkSeqNum = 0 ; // start at 1
52
- this . wiresharkSocket = createSocket ( "udp4" ) ;
53
- this . wiresharkPort = process . env . WIRESHARK_ZEP_PORT ? Number . parseInt ( process . env . WIRESHARK_ZEP_PORT ) : DEFAULT_ZEP_UDP_PORT ;
54
- this . wiresharkAddress = process . env . WIRESHARK_ADDRESS ? process . env . WIRESHARK_ADDRESS : DEFAULT_WIRESHARK_IP ;
55
- this . wiresharkSocket . bind ( this . wiresharkPort ) ;
51
+ this . # wiresharkSeqNum = 0 ; // start at 1
52
+ this . # wiresharkSocket = createSocket ( "udp4" ) ;
53
+ this . # wiresharkPort = process . env . WIRESHARK_ZEP_PORT ? Number . parseInt ( process . env . WIRESHARK_ZEP_PORT ) : DEFAULT_ZEP_UDP_PORT ;
54
+ this . # wiresharkAddress = process . env . WIRESHARK_ADDRESS ? process . env . WIRESHARK_ADDRESS : DEFAULT_WIRESHARK_IP ;
55
+ this . # wiresharkSocket. bind ( this . # wiresharkPort) ;
56
56
57
57
this . driver = new OTRCPDriver ( streamRawConfig , netParams , "." , sendMACToZEP ) ;
58
58
59
- this . portOptions = portOptions ;
60
- this . closing = false ;
59
+ this . # portOptions = portOptions ;
60
+ this . # closing = false ;
61
61
62
62
if ( sendMACToZEP ) {
63
63
this . driver . on ( "macFrame" , ( payload , rssi ) => {
64
64
const wsZEPFrame = createWiresharkZEPFrame ( this . driver . netParams . channel , 1 , 0 , rssi ?? 0 , this . nextWiresharkSeqNum ( ) , payload ) ;
65
65
66
- this . wiresharkSocket . send ( wsZEPFrame , this . wiresharkPort , this . wiresharkAddress ) ;
66
+ this . # wiresharkSocket. send ( wsZEPFrame , this . # wiresharkPort, this . # wiresharkAddress) ;
67
67
} ) ;
68
68
}
69
69
@@ -75,21 +75,21 @@ export class MinimalAdapter {
75
75
* Check if port is valid, open, and not closing.
76
76
*/
77
77
get portOpen ( ) : boolean {
78
- if ( this . closing ) {
78
+ if ( this . # closing) {
79
79
return false ;
80
80
}
81
81
82
- if ( isTcpPath ( this . portOptions . path ! ) ) {
83
- return this . socketPort ? ! this . socketPort . closed : false ;
82
+ if ( isTcpPath ( this . # portOptions. path ! ) ) {
83
+ return this . # socketPort ? ! this . # socketPort. closed : false ;
84
84
}
85
85
86
- return this . serialPort ? this . serialPort . isOpen : false ;
86
+ return this . # serialPort ? this . # serialPort. isOpen : false ;
87
87
}
88
88
89
89
private nextWiresharkSeqNum ( ) : number {
90
- this . wiresharkSeqNum = ( this . wiresharkSeqNum + 1 ) & 0xffffffff ;
90
+ this . # wiresharkSeqNum = ( this . # wiresharkSeqNum + 1 ) & 0xffffffff ;
91
91
92
- return this . wiresharkSeqNum + 1 ;
92
+ return this . # wiresharkSeqNum + 1 ;
93
93
}
94
94
95
95
/**
@@ -98,19 +98,19 @@ export class MinimalAdapter {
98
98
public async initPort ( ) : Promise < void > {
99
99
await this . closePort ( ) ; // will do nothing if nothing's open
100
100
101
- if ( isTcpPath ( this . portOptions . path ! ) ) {
102
- const pathUrl = new URL ( this . portOptions . path ! ) ;
101
+ if ( isTcpPath ( this . # portOptions. path ! ) ) {
102
+ const pathUrl = new URL ( this . # portOptions. path ! ) ;
103
103
const hostname = pathUrl . hostname ;
104
104
const port = Number . parseInt ( pathUrl . port , 10 ) ;
105
105
106
106
logger . debug ( ( ) => `Opening TCP socket with ${ hostname } :${ port } ` , NS ) ;
107
107
108
- this . socketPort = new Socket ( ) ;
108
+ this . # socketPort = new Socket ( ) ;
109
109
110
- this . socketPort . setNoDelay ( true ) ;
111
- this . socketPort . setKeepAlive ( true , 15000 ) ;
112
- this . driver . writer . pipe ( this . socketPort ) ;
113
- this . socketPort . pipe ( this . driver . parser ) ;
110
+ this . # socketPort. setNoDelay ( true ) ;
111
+ this . # socketPort. setKeepAlive ( true , 15000 ) ;
112
+ this . driver . writer . pipe ( this . # socketPort) ;
113
+ this . # socketPort. pipe ( this . driver . parser ) ;
114
114
this . driver . parser . on ( "data" , this . driver . onFrame . bind ( this . driver ) ) ;
115
115
116
116
return await new Promise ( ( resolve , reject ) : void => {
@@ -120,27 +120,27 @@ export class MinimalAdapter {
120
120
reject ( err ) ;
121
121
} ;
122
122
123
- this . socketPort ! . on ( "connect" , ( ) => {
123
+ this . # socketPort! . on ( "connect" , ( ) => {
124
124
logger . debug ( ( ) => "Socket connected" , NS ) ;
125
125
} ) ;
126
- this . socketPort ! . on ( "ready" , ( ) : void => {
126
+ this . # socketPort! . on ( "ready" , ( ) : void => {
127
127
logger . info ( "Socket ready" , NS ) ;
128
- this . socketPort ! . removeListener ( "error" , openError ) ;
129
- this . socketPort ! . once ( "close" , this . onPortClose . bind ( this ) ) ;
130
- this . socketPort ! . on ( "error" , this . onPortError . bind ( this ) ) ;
128
+ this . # socketPort! . removeListener ( "error" , openError ) ;
129
+ this . # socketPort! . once ( "close" , this . onPortClose . bind ( this ) ) ;
130
+ this . # socketPort! . on ( "error" , this . onPortError . bind ( this ) ) ;
131
131
132
132
resolve ( ) ;
133
133
} ) ;
134
- this . socketPort ! . once ( "error" , openError ) ;
134
+ this . # socketPort! . once ( "error" , openError ) ;
135
135
136
- this . socketPort ! . connect ( port , hostname ) ;
136
+ this . # socketPort! . connect ( port , hostname ) ;
137
137
} ) ;
138
138
}
139
139
140
140
const serialOpts = {
141
- path : this . portOptions . path ! ,
142
- baudRate : typeof this . portOptions . baudRate === "number" ? this . portOptions . baudRate : 115200 ,
143
- rtscts : typeof this . portOptions . rtscts === "boolean" ? this . portOptions . rtscts : false ,
141
+ path : this . # portOptions. path ! ,
142
+ baudRate : typeof this . # portOptions. baudRate === "number" ? this . # portOptions. baudRate : 115200 ,
143
+ rtscts : typeof this . # portOptions. rtscts === "boolean" ? this . # portOptions. rtscts : false ,
144
144
autoOpen : false ,
145
145
parity : "none" as const ,
146
146
stopBits : 1 as const ,
@@ -156,21 +156,21 @@ export class MinimalAdapter {
156
156
}
157
157
158
158
logger . debug ( ( ) => `Opening serial port with [path=${ serialOpts . path } baudRate=${ serialOpts . baudRate } rtscts=${ serialOpts . rtscts } ]` , NS ) ;
159
- this . serialPort = new SerialPort ( serialOpts ) ;
159
+ this . # serialPort = new SerialPort ( serialOpts ) ;
160
160
161
- this . driver . writer . pipe ( this . serialPort ) ;
162
- this . serialPort . pipe ( this . driver . parser ) ;
161
+ this . driver . writer . pipe ( this . # serialPort) ;
162
+ this . # serialPort. pipe ( this . driver . parser ) ;
163
163
this . driver . parser . on ( "data" , this . driver . onFrame . bind ( this . driver ) ) ;
164
164
165
165
try {
166
166
await new Promise < void > ( ( resolve , reject ) : void => {
167
- this . serialPort ! . open ( ( err ) => ( err ? reject ( err ) : resolve ( ) ) ) ;
167
+ this . # serialPort! . open ( ( err ) => ( err ? reject ( err ) : resolve ( ) ) ) ;
168
168
} ) ;
169
169
170
170
logger . info ( "Serial port opened" , NS ) ;
171
171
172
- this . serialPort . once ( "close" , this . onPortClose . bind ( this ) ) ;
173
- this . serialPort . on ( "error" , this . onPortError . bind ( this ) ) ;
172
+ this . # serialPort. once ( "close" , this . onPortClose . bind ( this ) ) ;
173
+ this . # serialPort. on ( "error" , this . onPortError . bind ( this ) ) ;
174
174
} catch ( error ) {
175
175
await this . stop ( ) ;
176
176
@@ -207,11 +207,11 @@ export class MinimalAdapter {
207
207
throw new Error ( "Invalid call to start" ) ;
208
208
}
209
209
210
- if ( this . serialPort ) {
210
+ if ( this . # serialPort) {
211
211
// try clearing read/write buffers
212
212
try {
213
213
await new Promise < void > ( ( resolve , reject ) : void => {
214
- this . serialPort ! . flush ( ( err ) => ( err ? reject ( err ) : resolve ( ) ) ) ;
214
+ this . # serialPort! . flush ( ( err ) => ( err ? reject ( err ) : resolve ( ) ) ) ;
215
215
} ) ;
216
216
} catch ( err ) {
217
217
logger . error ( `Error while flushing serial port before start: ${ err } ` , NS ) ;
@@ -231,35 +231,35 @@ export class MinimalAdapter {
231
231
}
232
232
233
233
public async stop ( ) : Promise < void > {
234
- this . closing = true ;
234
+ this . # closing = true ;
235
235
236
236
await this . driver . stop ( ) ;
237
- this . wiresharkSocket . close ( ) ;
237
+ this . # wiresharkSocket. close ( ) ;
238
238
await this . closePort ( ) ;
239
239
}
240
240
241
241
public async closePort ( ) : Promise < void > {
242
- if ( this . serialPort ?. isOpen ) {
242
+ if ( this . # serialPort?. isOpen ) {
243
243
try {
244
244
await new Promise < void > ( ( resolve , reject ) : void => {
245
- this . serialPort ! . flush ( ( err ) => ( err ? reject ( err ) : resolve ( ) ) ) ;
245
+ this . # serialPort! . flush ( ( err ) => ( err ? reject ( err ) : resolve ( ) ) ) ;
246
246
} ) ;
247
247
248
248
await new Promise < void > ( ( resolve , reject ) : void => {
249
- this . serialPort ! . close ( ( err ) => ( err ? reject ( err ) : resolve ( ) ) ) ;
249
+ this . # serialPort! . close ( ( err ) => ( err ? reject ( err ) : resolve ( ) ) ) ;
250
250
} ) ;
251
251
} catch ( err ) {
252
252
logger . error ( `Failed to close serial port ${ err } .` , NS ) ;
253
253
}
254
254
255
- this . serialPort . removeAllListeners ( ) ;
255
+ this . # serialPort. removeAllListeners ( ) ;
256
256
257
- this . serialPort = undefined ;
258
- } else if ( this . socketPort != null && ! this . socketPort . closed ) {
259
- this . socketPort . destroy ( ) ;
260
- this . socketPort . removeAllListeners ( ) ;
257
+ this . # serialPort = undefined ;
258
+ } else if ( this . # socketPort != null && ! this . # socketPort. closed ) {
259
+ this . # socketPort. destroy ( ) ;
260
+ this . # socketPort. removeAllListeners ( ) ;
261
261
262
- this . socketPort = undefined ;
262
+ this . # socketPort = undefined ;
263
263
}
264
264
}
265
265
0 commit comments