@@ -34,8 +34,8 @@ import {
3434 BehaviorSubject ,
3535 interval
3636} from 'rxjs' ;
37- import { parseMessage , initSocket , initPluginUrl } from './readMessages ' ;
38- import { debug } from 'util' ;
37+ import ReaderWriter from './reader-writer ' ;
38+
3939// Required agent version
4040const MIN_VERSION = '1.1.71' ;
4141
@@ -64,7 +64,33 @@ export default class SocketDaemon {
6464
6565 this . agentDiscoveryStatus = new BehaviorSubject ( AGENT_STATUS_NOT_FOUND ) ;
6666 this . wsConnectionStatus = new BehaviorSubject ( WS_STATUS_DISCONNECTED ) ;
67+ this . devicesListStatus = new Subject ( ) ;
6768 this . wsError = new Subject ( ) ;
69+
70+ this . devicesList = {
71+ serial : [ ] ,
72+ network : [ ]
73+ } ;
74+
75+ this . devicesListStatus . subscribe ( devicesInfo => {
76+ if ( devicesInfo . Network ) {
77+ this . devicesList . network = devicesInfo . Ports ;
78+ }
79+ else {
80+ this . devicesList . serial = devicesInfo . Ports ;
81+ }
82+ console . log ( this . devicesList ) ;
83+ } ) ;
84+
85+ this . readerWriter = null ;
86+ this . wsConnectionStatus . subscribe ( status => {
87+ if ( status === WS_STATUS_CONNECTED ) {
88+ this . readerWriter = new ReaderWriter ( this . socket , this . agentInfo [ this . selectedProtocol ] , this . devicesListStatus ) ;
89+ }
90+ else {
91+ this . readerWriter = null ;
92+ }
93+ } ) ;
6894 }
6995
7096 /**
@@ -100,7 +126,6 @@ export default class SocketDaemon {
100126 * @return {object } info - The agent info values.
101127 */
102128 tryPorts ( hostname ) {
103- console . log ( 'tryPorts\n' ) ;
104129 const pluginLookups = [ ] ;
105130
106131 for ( let port = LOOKUP_PORT_START ; port < LOOKUP_PORT_END ; port += 1 ) {
@@ -121,7 +146,6 @@ export default class SocketDaemon {
121146 if ( r . response . url . indexOf ( PROTOCOL . HTTPS ) === 0 ) {
122147 this . selectedProtocol = PROTOCOL . HTTPS ;
123148 }
124- initPluginUrl ( this . agentInfo [ this . selectedProtocol ] ) ;
125149 return true ;
126150 }
127151 return false ;
@@ -149,8 +173,6 @@ export default class SocketDaemon {
149173 this . socket = io ( address , { reconnection : false , forceNew : true } ) ;
150174
151175 this . socket . on ( 'connect' , ( ) => {
152- initSocket ( this . socket ) ;
153-
154176 // On connect download windows drivers which are indispensable for detection of boards
155177 this . socket . emit ( 'command' , 'downloadtool windows-drivers latest arduino keep' ) ;
156178 this . socket . emit ( 'command' , 'downloadtool bossac 1.7.0 arduino keep' ) ;
@@ -173,9 +195,6 @@ export default class SocketDaemon {
173195 this . wsConnectionStatus . next ( WS_STATUS_DISCONNECTED ) ;
174196 this . findAgent ( ) ;
175197 } ) ;
176-
177- // Parse messages
178- this . socket . on ( 'message' , parseMessage ) ;
179198 }
180199
181200 /**
@@ -188,7 +207,10 @@ export default class SocketDaemon {
188207 }
189208
190209 return fetch ( `${ this . agentInfo [ this . selectedProtocol ] } /update` , {
191- method : 'POST'
210+ method : 'POST' ,
211+ headers : {
212+ 'Content-Type' : 'text/plain; charset=utf-8'
213+ }
192214 } ) . then ( ( ) => reject ( ) ) ; // We reject the promise because the daemon will be restarted, we need to continue looking for the port
193215 } ) ;
194216 }
0 commit comments