@@ -6,8 +6,9 @@ import * as url from 'url';
66import * as fs from 'fs'
77import { Project , ProjectObserser } from './project' ;
88import * as vscode from "vscode" ;
9- import Adb , { DeviceClient } from '@devicefarmer/adbkit' ;
9+ import Adb , { DeviceClient , Forward } from '@devicefarmer/adbkit' ;
1010import Tracker from '@devicefarmer/adbkit/dist/src/adb/tracker' ;
11+ import ADBDevice from '@devicefarmer/adbkit/dist/src/Device' ;
1112
1213const DEBUG = false ;
1314
@@ -22,14 +23,16 @@ const HANDSHAKE_TIMEOUT = 10 * 1000;
2223
2324export class Device extends EventEmitter {
2425 public name : string ;
25- public address : string ;
26+ public type : string ;
27+ public id : string ;
2628 private connection : ws . connection ;
27- private attached : boolean = false ;
29+ public attached : boolean = false ;
2830 public projectObserser : ProjectObserser ;
2931
30- constructor ( connection : ws . connection , address : string ) {
32+ constructor ( connection : ws . connection , type : string , id : string ) {
3133 super ( ) ;
32- this . address = address
34+ this . type = type
35+ this . id = id
3336 this . connection = connection ;
3437 this . read ( this . connection ) ;
3538 this . on ( 'data:hello' , data => {
@@ -51,6 +54,14 @@ export class Device extends EventEmitter {
5154 } , HANDSHAKE_TIMEOUT ) ;
5255 }
5356
57+ close ( ) {
58+ let message_id = `${ Date . now ( ) } _${ Math . random ( ) } ` ;
59+ let closeMessage = JSON . stringify ( { message_id, data : "close" , debug : false , type : 'close' } )
60+ this . connection . sendUTF ( closeMessage ) ;
61+ this . connection . close ( ) ;
62+ this . connection = null ;
63+ }
64+
5465 send ( type : string , data : any ) : void {
5566 let message_id = `${ Date . now ( ) } _${ Math . random ( ) } ` ;
5667 console . log ( data ) ;
@@ -85,9 +96,9 @@ export class Device extends EventEmitter {
8596
8697 public toString = ( ) : string => {
8798 if ( ! this . name ) {
88- return `Device (${ this . address } )` ;
99+ return `Device (${ this . type } : ${ this . id } )` ;
89100 }
90- return `Device ${ this . name } (${ this . address } )` ;
101+ return `Device ${ this . name } (${ this . type } : ${ this . id } )` ;
91102 }
92103
93104 private read ( connection : ws . connection ) {
@@ -148,18 +159,24 @@ export class AutoJsDebugServer extends EventEmitter {
148159 response . end ( ) ;
149160 }
150161 } ) ;
151- var wsServer = new ws . server ( { httpServer : this . httpServer } ) ;
162+ let wsServer = new ws . server ( { httpServer : this . httpServer } ) ;
152163 wsServer . on ( 'request' , request => {
153164 let connection = request . accept ( ) ;
154165 if ( ! connection ) {
155166 return ;
156167 }
157- this . newDevice ( connection , connection . remoteAddress )
168+ this . newDevice ( connection , "tcp" , connection . socket . remoteAddress + ":" + connection . socket . remotePort )
169+ } )
170+ }
171+
172+ getDeviceById ( id : string ) : Device {
173+ return this . devices . find ( ( value ) => {
174+ return value . id == id
158175 } )
159176 }
160177
161- private newDevice ( connection : ws . connection , address : string ) {
162- let device = new Device ( connection , address ) ;
178+ private newDevice ( connection : ws . connection , type : string , id : string ) {
179+ let device = new Device ( connection , type , id ) ;
163180 logDebug ( connection . state , "--->status" )
164181 device . on ( "attach" , ( device ) => {
165182 this . attachDevice ( device ) ;
@@ -183,11 +200,9 @@ export class AutoJsDebugServer extends EventEmitter {
183200
184201 client . on ( 'connect' , function ( connection ) {
185202 console . log ( "connected to " + url )
186- autoJsDebugServer . newDevice ( connection , "ADB: " + deviceId )
203+ autoJsDebugServer . newDevice ( connection , "adb" , deviceId )
187204 } ) ;
188-
189- //下面不能加'echo-protocol',否则会报Can`t connect due to "Sec-WebSocket-Protocol header"的错。因为服务器没有返回对应协议规定的信息
190- client . connect ( url ) ; //, 'echo-protocol');
205+ client . connect ( url ) ;
191206 }
192207
193208 listen ( ) : void {
@@ -208,31 +223,40 @@ export class AutoJsDebugServer extends EventEmitter {
208223 } ) ;
209224 }
210225
226+
227+ async listADBDevices ( ) : Promise < ADBDevice [ ] > {
228+ return this . adbClient . listDevices ( ) ;
229+ }
230+
211231 async trackADBDevices ( ) {
212232 let thisServer = this
233+ let devices = await thisServer . adbClient . listDevices ( )
234+ for ( let device0 of devices ) {
235+ await thisServer . connectDevice ( device0 . id )
236+ }
213237 if ( this . tracker ) {
214238 this . emit ( "adb:tracking_started" ) ;
215239 return
216240 }
217- let devices = await thisServer . adbClient . listDevices ( )
218- for ( let device0 of devices ) {
219- const device = thisServer . adbClient . getDevice ( device0 . id )
220- await thisServer . connectDevice ( device , device0 . id )
221- }
222241 try {
223242 let tracker = await thisServer . adbClient . trackDevices ( )
224- this . tracker = tracker
243+ thisServer . tracker = tracker
225244 tracker . on ( 'add' , async function ( device0 ) {
226245 console . log ( "adb device " + device0 . id + " added" )
227246 const device = thisServer . adbClient . getDevice ( device0 . id )
228247 await device . waitForDevice ( )
229- await thisServer . connectDevice ( device , device0 . id )
248+ await thisServer . connectDevice ( device0 . id , device )
230249 } )
231250 tracker . on ( 'remove' , function ( device ) {
232251 console . log ( "adb device " + device . id + " removed" )
252+ let wsDevice = thisServer . getDeviceById ( device . id )
253+ if ( wsDevice ) {
254+ wsDevice . close ( )
255+ }
256+
233257 } )
234258 tracker . on ( 'end' , function ( ) {
235- tracker = undefined
259+ thisServer . tracker = undefined
236260 console . log ( 'ADB Tracking stopped' )
237261 thisServer . emit ( "adb:tracking_stop" )
238262 } )
@@ -245,22 +269,36 @@ export class AutoJsDebugServer extends EventEmitter {
245269 this . emit ( "adb:tracking_start" ) ;
246270 }
247271
248- private async connectDevice ( device : DeviceClient , id : string ) {
249- let forwarded = await device . forward ( `tcp:0` , `tcp:9317` )
250- if ( forwarded ) {
251- let forwards = await device . listForwards ( )
252- if ( forwards . length > 0 ) {
253- let forward = forwards [ 0 ]
254- console . log ( `forward ${ id } : ${ forwards . toString ( ) } `)
255- let port = Number ( forward . local . replace ( "tcp:" , "" ) )
256- this . connectAutoxjsByADB ( port , id )
272+ async connectDevice ( id : string , device : DeviceClient = undefined ) {
273+ if ( ! device ) device = this . adbClient . getDevice ( id )
274+ let wsDevice = this . getDeviceById ( id )
275+ if ( wsDevice && wsDevice . attached ) return
276+ let forwards : Forward [ ] = await this . listForwards ( device , id )
277+ if ( forwards . length == 0 ) {
278+ let forwarded = await device . forward ( `tcp:0` , `tcp:9317 `)
279+ if ( forwarded ) {
280+ forwards = await this . listForwards ( device , id )
257281 }
258282 }
283+ if ( forwards . length > 0 ) {
284+ let forward = forwards [ 0 ]
285+ console . log ( `forward ${ id } : local -> ${ forward . local } , remote -> ${ forward . remote } ` )
286+ let port = Number ( forward . local . replace ( "tcp:" , "" ) )
287+ this . connectAutoxjsByADB ( port , id )
288+ }
289+ }
290+
291+ private async listForwards ( device : DeviceClient , id : string ) : Promise < Forward [ ] > {
292+ let forwards : Forward [ ] = await device . listForwards ( )
293+ return forwards . filter ( ( value ) => {
294+ return value . serial == id
295+ } )
259296 }
260297
261298 stopTrackADBDevices ( ) {
262299 if ( this . tracker ) {
263300 this . tracker . end ( )
301+ this . tracker = undefined
264302 }
265303 }
266304
@@ -314,6 +352,9 @@ export class AutoJsDebugServer extends EventEmitter {
314352 channel . dispose ( ) ;
315353 } ) ;
316354 this . logChannels . clear ( ) ;
355+ this . devices . forEach ( ( device ) => {
356+ device . close ( )
357+ } )
317358 }
318359
319360 /** 获取本地IP */
0 commit comments