@@ -80,33 +80,76 @@ function setupAnalytics(result) {
8080
8181 analytics . sendEvent ( analytics . EVENT_CATEGORIES . APPLICATION , 'AppStart' , { sessionControl : 'start' } ) ;
8282
83- function sendCloseEvent ( ) {
84- analytics . sendEvent ( analytics . EVENT_CATEGORIES . APPLICATION , 'AppClose' , { sessionControl : 'end' } ) ;
85- }
83+ $ ( '.connect_b a.connect' ) . removeClass ( 'disabled' ) ;
84+ $ ( '.firmware_b a.flash' ) . removeClass ( 'disabled' ) ;
85+ }
8686
87- if ( GUI . isNWJS ( ) ) {
88- GUI . nwGui . Window . getAll ( function ( windows ) {
89- windows . forEach ( function ( win ) {
90- win . on ( 'close' , function ( ) {
91- sendCloseEvent ( ) ;
87+ function closeSerial ( ) {
88+ // automatically close the port when application closes
89+ const connectionId = serial . connectionId ;
9290
93- this . close ( true ) ;
94- } ) ;
95- win . on ( 'new-win-policy' , function ( frame , url , policy ) {
96- // do not open the window
97- policy . ignore ( ) ;
98- // and open it in external browser
99- GUI . nwGui . Shell . openExternal ( url ) ;
91+ if ( connectionId && CONFIGURATOR . connectionValid ) {
92+ // code below is handmade MSP message (without pretty JS wrapper), it behaves exactly like MSP.send_message
93+ // sending exit command just in case the cli tab was open.
94+ // reset motors to default (mincommand)
95+
96+ let bufferOut = new ArrayBuffer ( 5 ) ,
97+ bufView = new Uint8Array ( bufferOut ) ;
98+
99+ bufView [ 0 ] = 0x65 ; // e
100+ bufView [ 1 ] = 0x78 ; // x
101+ bufView [ 2 ] = 0x69 ; // i
102+ bufView [ 3 ] = 0x74 ; // t
103+ bufView [ 4 ] = 0x0D ; // enter
104+
105+ chrome . serial . send ( connectionId , bufferOut , function ( ) {
106+ console . log ( 'Send exit' ) ;
107+ } ) ;
108+
109+ setTimeout ( function ( ) {
110+ bufferOut = new ArrayBuffer ( 22 ) ;
111+ bufView = new Uint8Array ( bufferOut ) ;
112+ let checksum = 0 ;
113+
114+ bufView [ 0 ] = 36 ; // $
115+ bufView [ 1 ] = 77 ; // M
116+ bufView [ 2 ] = 60 ; // <
117+ bufView [ 3 ] = 16 ; // data length
118+ bufView [ 4 ] = 214 ; // MSP_SET_MOTOR
119+
120+ checksum = bufView [ 3 ] ^ bufView [ 4 ] ;
121+
122+ for ( let i = 0 ; i < 16 ; i += 2 ) {
123+ bufView [ i + 5 ] = MOTOR_CONFIG . mincommand & 0x00FF ;
124+ bufView [ i + 6 ] = MOTOR_CONFIG . mincommand >> 8 ;
125+
126+ checksum ^= bufView [ i + 5 ] ;
127+ checksum ^= bufView [ i + 6 ] ;
128+ }
129+
130+ bufView [ 5 + 16 ] = checksum ;
131+
132+ chrome . serial . send ( connectionId , bufferOut , function ( ) {
133+ chrome . serial . disconnect ( connectionId , function ( result ) {
134+ console . log ( `SERIAL: Connection closed - ${ result } ` ) ;
100135 } ) ;
101136 } ) ;
137+ } , 100 ) ;
138+ } else if ( connectionId ) {
139+ chrome . serial . disconnect ( connectionId , function ( result ) {
140+ console . log ( `SERIAL: Connection closed - ${ result } ` ) ;
102141 } ) ;
103- } else if ( ! GUI . isOther ( ) ) {
104- // Looks like we're in Chrome - but the event does not actually get fired
105- chrome . runtime . onSuspend . addListener ( sendCloseEvent ) ;
106142 }
143+ }
107144
108- $ ( '.connect_b a.connect' ) . removeClass ( 'disabled' ) ;
109- $ ( '.firmware_b a.flash' ) . removeClass ( 'disabled' ) ;
145+ function closeHandler ( ) {
146+ this . hide ( ) ;
147+
148+ analytics . sendEvent ( analytics . EVENT_CATEGORIES . APPLICATION , 'AppClose' , { sessionControl : 'end' } ) ;
149+
150+ closeSerial ( ) ;
151+
152+ this . close ( true ) ;
110153}
111154
112155//Process to execute to real start the app
@@ -119,6 +162,22 @@ function startProcess() {
119162 chromeVersion : window . navigator . appVersion . replace ( / .* C h r o m e \/ ( [ 0 - 9 . ] * ) .* / , "$1" ) ,
120163 configuratorVersion : CONFIGURATOR . version } ) ) ;
121164
165+ if ( GUI . isNWJS ( ) ) {
166+ let nwWindow = GUI . nwGui . Window . get ( ) ;
167+ nwWindow . on ( 'new-win-policy' , function ( frame , url , policy ) {
168+ // do not open the window
169+ policy . ignore ( ) ;
170+ // and open it in external browser
171+ GUI . nwGui . Shell . openExternal ( url ) ;
172+ } ) ;
173+ nwWindow . on ( 'close' , closeHandler ) ;
174+ } else if ( ! GUI . isOther ( ) ) {
175+ chrome . app . window . onClosed . addListener ( closeHandler ) ;
176+ // This event does not actually get fired:
177+ chrome . runtime . onSuspend . addListener ( closeHandler ) ;
178+ }
179+
180+ $ ( '.connect_b a.connect' ) . removeClass ( 'disabled' ) ;
122181 $ ( '#logo .version' ) . text ( CONFIGURATOR . version ) ;
123182 updateStatusBarVersion ( ) ;
124183 updateTopBarVersion ( ) ;
0 commit comments