@@ -81,7 +81,7 @@ const Comms = {
8181 handlers : { } ,
8282 on : function ( id , callback ) { // calling with callback=undefined will disable
8383 if ( id != "data" ) throw new Error ( "Only data callback is supported" ) ;
84- var connection = Comms . getConnection ( ) ;
84+ let connection = Comms . getConnection ( ) ;
8585 if ( ! connection ) throw new Error ( "No active connection" ) ;
8686 if ( "undefined" !== typeof Puck ) {
8787 /* This is a bit of a mess - the Puck.js lib only supports one callback with `.on`. If you
@@ -179,63 +179,63 @@ const Comms = {
179179
180180 return ( Comms . espruinoDevice ?Promise . resolve ( ) :Comms . getDeviceInfo ( true /*noreset*/ ) ) // ensure Comms.espruinoDevice is set
181181 . then ( ( ) => new Promise ( ( resolve , reject ) => {
182- // Function to upload a single line and wait for an 'OK' response
183- function uploadCmd ( ) {
184- if ( ! cmds . length ) return resolve ( ) ;
185- let cmd = cmds . shift ( ) ;
186- Progress . show ( {
187- min :currentBytes / maxBytes ,
188- max :( currentBytes + cmd . length ) / maxBytes } ) ;
189- currentBytes += cmd . length ;
190- function responseHandler ( result ) {
191- console . log ( "<COMMS> Response: " , JSON . stringify ( result ) ) ;
192- var ignore = false ;
193- if ( result !== undefined ) {
194- result = result . trim ( ) ;
195- if ( result == "OK" ) {
196- uploadCmd ( ) ; // all as expected - send next
197- return ;
198- }
182+ // Function to upload a single line and wait for an 'OK' response
183+ function uploadCmd ( ) {
184+ if ( ! cmds . length ) return resolve ( ) ;
185+ let cmd = cmds . shift ( ) ;
186+ Progress . show ( {
187+ min :currentBytes / maxBytes ,
188+ max :( currentBytes + cmd . length ) / maxBytes } ) ;
189+ currentBytes += cmd . length ;
190+ function responseHandler ( result ) {
191+ console . log ( "<COMMS> Response: " , JSON . stringify ( result ) ) ;
192+ let ignore = false ;
193+ if ( result !== undefined ) {
194+ result = result . trim ( ) ;
195+ if ( result == "OK" ) {
196+ uploadCmd ( ) ; // all as expected - send next
197+ return ;
198+ }
199199
200- if ( result . startsWith ( "{" ) && result . endsWith ( "}" ) ) {
201- console . log ( "<COMMS> JSON response received (Gadgetbridge?) - ignoring..." ) ;
202- ignore = true ;
203- } else if ( result == "" ) {
204- console . log ( "<COMMS> Blank line received - ignoring..." ) ;
200+ if ( result . startsWith ( "{" ) && result . endsWith ( "}" ) ) {
201+ console . log ( "<COMMS> JSON response received (Gadgetbridge?) - ignoring..." ) ;
202+ ignore = true ;
203+ } else if ( result == "" ) {
204+ console . log ( "<COMMS> Blank line received - ignoring..." ) ;
205+ ignore = true ;
206+ }
207+ } else { // result===undefined
208+ console . log ( "<COMMS> No response received - ignoring..." ) ;
205209 ignore = true ;
206210 }
207- } else { // result===undefined
208- console . log ( "<COMMS> No response received - ignoring..." ) ;
209- ignore = true ;
210- }
211- if ( ignore ) {
212- /* Here we have to poke around inside the Comms library internals. Basically
213- it just gave us the first line in the input buffer, but there may have been more.
214- We take the next line (or undefined) and call ourselves again to handle that.
215- Just in case, delay a little to give our previous command time to finish.*/
216- setTimeout ( function ( ) {
217- let connection = Comms . getConnection ( ) ;
218- let newLineIdx = connection . received . indexOf ( "\n" ) ;
219- let l = undefined ;
220- if ( newLineIdx >= 0 ) {
221- l = connection . received . substr ( 0 , newLineIdx ) ;
222- connection . received = connection . received . substr ( newLineIdx + 1 ) ;
223- }
224- responseHandler ( l ) ;
225- } , 500 ) ;
226- } else {
227- // Not a response we expected and we're not ignoring!
228- Progress . hide ( { sticky :true } ) ;
229- return reject ( "Unexpected response " + ( result ?JSON . stringify ( result ) :"<empty>" ) ) ;
211+ if ( ignore ) {
212+ /* Here we have to poke around inside the Comms library internals. Basically
213+ it just gave us the first line in the input buffer, but there may have been more.
214+ We take the next line (or undefined) and call ourselves again to handle that.
215+ Just in case, delay a little to give our previous command time to finish.*/
216+ setTimeout ( function ( ) {
217+ let connection = Comms . getConnection ( ) ;
218+ let newLineIdx = connection . received . indexOf ( "\n" ) ;
219+ let l = undefined ;
220+ if ( newLineIdx >= 0 ) {
221+ l = connection . received . substr ( 0 , newLineIdx ) ;
222+ connection . received = connection . received . substr ( newLineIdx + 1 ) ;
223+ }
224+ responseHandler ( l ) ;
225+ } , 500 ) ;
226+ } else {
227+ // Not a response we expected and we're not ignoring!
228+ Progress . hide ( { sticky :true } ) ;
229+ return reject ( "Unexpected response " + ( result ?JSON . stringify ( result ) :"<empty>" ) ) ;
230+ }
230231 }
232+ // Actually write the command with a 'print OK' at the end, and use responseHandler
233+ // to deal with the response. If OK we call uploadCmd to upload the next block
234+ return Comms . write ( `${ cmd } ;${ Comms . getProgressCmd ( currentBytes / maxBytes ) } ${ Comms . espruinoDevice } .println("OK")\n` , { waitNewLine :true } ) . then ( responseHandler ) ;
231235 }
232- // Actually write the command with a 'print OK' at the end, and use responseHandler
233- // to deal with the response. If OK we call uploadCmd to upload the next block
234- return Comms . write ( `${ cmd } ;${ Comms . getProgressCmd ( currentBytes / maxBytes ) } ${ Comms . espruinoDevice } .println("OK")\n` , { waitNewLine :true } ) . then ( responseHandler ) ;
235- }
236236
237- uploadCmd ( )
238- } ) ) ;
237+ uploadCmd ( )
238+ } ) ) ;
239239 } ,
240240 /** Upload an app
241241 app : an apps.json structure (i.e. with `storage`)
@@ -380,8 +380,8 @@ const Comms = {
380380 Comms . espruinoDevice = device ;
381381 else throw new Error ( "Unable to find Espruino console device" ) ;
382382 console . log ( "<COMMS> Set console device to " + device ) ;
383- } ) . then ( ( ) => Comms . getDeviceInfo ( true ) ) .
384- then ( resolve ) ;
383+ } ) . then ( ( ) => Comms . getDeviceInfo ( true ) )
384+ . then ( resolve ) ;
385385 return ;
386386 }
387387 }
@@ -406,14 +406,14 @@ const Comms = {
406406 Comms . write ( cmd , { waitNewLine :true } ) . then ( appListStr => {
407407 Progress . hide ( { sticky :true } ) ;
408408 if ( ! appListStr ) appListStr = "" ;
409- var connection = Comms . getConnection ( ) ;
409+ let connection = Comms . getConnection ( ) ;
410410 if ( connection ) {
411411 appListStr = appListStr + "\n" + connection . received ; // add *any* information we have received so far, including what was returned
412412 connection . received = "" ; // clear received data just in case
413413 }
414414 // we may have received more than one line - we're looking for an array (starting with '[')
415- var lines = appListStr ? appListStr . split ( "\n" ) . map ( l => l . trim ( ) ) : [ ] ;
416- var appListJSON = lines . find ( l => l [ 0 ] == "[" ) ;
415+ let lines = appListStr ? appListStr . split ( "\n" ) . map ( l => l . trim ( ) ) : [ ] ;
416+ let appListJSON = lines . find ( l => l [ 0 ] == "[" ) ;
417417 // check to see if we got our data
418418 if ( ! appListJSON ) {
419419 console . log ( "No JSON, just got: " + JSON . stringify ( appListStr ) ) ;
@@ -433,7 +433,7 @@ const Comms = {
433433 info . id = appList . pop ( ) ;
434434 info . storageStats = appList . pop ( ) ; // how much storage has been used
435435 if ( info . storageStats . totalBytes && ( info . storageStats . freeBytes * 10 < info . storageStats . totalBytes ) ) {
436- var suggest = "" ;
436+ let suggest = "" ;
437437 if ( info . id . startsWith ( "BANGLEJS" ) && info . storageStats . trashBytes * 10 > info . storageStats . totalBytes )
438438 suggest = "Try running 'Compact Storage' from Bangle.js 'Settings' -> 'Utils'." ;
439439 showToast ( `Low Disk Space: ${ Math . round ( info . storageStats . freeBytes / 1000 ) } k of ${ Math . round ( info . storageStats . totalBytes / 1000 ) } k remaining on this device.${ suggest } See 'More...' -> 'Device Info' for more information.` , "warning" ) ;
@@ -456,25 +456,25 @@ const Comms = {
456456 } ,
457457 // Get an app's info file from Bangle.js
458458 getAppInfo : app => {
459- var cmd ;
459+ let cmd ;
460460
461461 return ( Comms . espruinoDevice ?Promise . resolve ( ) :Comms . getDeviceInfo ( true /*noreset*/ ) ) // ensure Comms.espruinoDevice is set
462- . then ( ( ) => {
463- if ( Const . FILES_IN_FS ) cmd = `\x10${ Comms . espruinoDevice } .println(require("fs").readFileSync(${ JSON . stringify ( AppInfo . getAppInfoFilename ( app ) ) } )||"null")\n` ;
464- else cmd = `\x10${ Comms . espruinoDevice } .println(require("Storage").read(${ JSON . stringify ( AppInfo . getAppInfoFilename ( app ) ) } )||"null")\n` ;
465- return Comms . write ( cmd ) .
466- then ( appJSON => {
467- let app ;
468- try {
469- app = JSON . parse ( appJSON ) ;
470- } catch ( e ) {
471- app = null ;
472- console . log ( "<COMMS> ERROR Parsing JSON" , e . toString ( ) ) ;
473- console . log ( "<COMMS> Actual response: " , JSON . stringify ( appJSON ) ) ;
474- throw new Error ( "Invalid JSON" ) ;
475- }
476- return app ;
477- } ) ;
462+ . then ( ( ) => {
463+ if ( Const . FILES_IN_FS ) cmd = `\x10${ Comms . espruinoDevice } .println(require("fs").readFileSync(${ JSON . stringify ( AppInfo . getAppInfoFilename ( app ) ) } )||"null")\n` ;
464+ else cmd = `\x10${ Comms . espruinoDevice } .println(require("Storage").read(${ JSON . stringify ( AppInfo . getAppInfoFilename ( app ) ) } )||"null")\n` ;
465+ return Comms . write ( cmd ) .
466+ then ( appJSON => {
467+ let app ;
468+ try {
469+ app = JSON . parse ( appJSON ) ;
470+ } catch ( e ) {
471+ app = null ;
472+ console . log ( "<COMMS> ERROR Parsing JSON" , e . toString ( ) ) ;
473+ console . log ( "<COMMS> Actual response: " , JSON . stringify ( appJSON ) ) ;
474+ throw new Error ( "Invalid JSON" ) ;
475+ }
476+ return app ;
477+ } ) ;
478478 } ) ;
479479 } ,
480480 /** Remove an app given an appinfo.id structure as JSON
@@ -537,29 +537,29 @@ const Comms = {
537537
538538 return ( Comms . espruinoDevice ?Promise . resolve ( ) :Comms . getDeviceInfo ( true /*noreset*/ ) ) // ensure Comms.espruinoDevice is set
539539 . then ( ( ) => new Promise ( ( resolve , reject ) => {
540- let timeout = 5 ;
541- function handleResult ( result , err ) {
542- console . log ( "<COMMS> removeAllApps: received " + JSON . stringify ( result ) ) ;
543- if ( result == "" && ( timeout -- ) ) {
544- console . log ( "<COMMS> removeAllApps: no result - waiting some more (" + timeout + ")." ) ;
545- // send space and delete - so it's something, but it should just cancel out
546- Comms . write ( " \u0008" , { waitNewLine :true } ) . then ( handleResult ) ;
547- } else {
548- Progress . hide ( { sticky :true } ) ;
549- if ( ! result || result . trim ( ) != "OK" ) {
550- if ( ! result ) result = "No response" ;
551- else result = "Got " + JSON . stringify ( result . trim ( ) ) ;
552- return reject ( err || result ) ;
553- } else resolve ( ) ;
540+ let timeout = 5 ;
541+ function handleResult ( result , err ) {
542+ console . log ( "<COMMS> removeAllApps: received " + JSON . stringify ( result ) ) ;
543+ if ( result == "" && ( timeout -- ) ) {
544+ console . log ( "<COMMS> removeAllApps: no result - waiting some more (" + timeout + ")." ) ;
545+ // send space and delete - so it's something, but it should just cancel out
546+ Comms . write ( " \u0008" , { waitNewLine :true } ) . then ( handleResult ) ;
547+ } else {
548+ Progress . hide ( { sticky :true } ) ;
549+ if ( ! result || result . trim ( ) != "OK" ) {
550+ if ( ! result ) result = "No response" ;
551+ else result = "Got " + JSON . stringify ( result . trim ( ) ) ;
552+ return reject ( err || result ) ;
553+ } else resolve ( ) ;
554+ }
554555 }
555- }
556- // Use write with newline here so we wait for it to finish
557- let cmd = `\x10E.showMessage("Erasing...");require("Storage").eraseAll();${ Comms . espruinoDevice } .println("OK");reset()\n` ;
558- Comms . write ( cmd , { waitNewLine :true } ) . then ( handleResult ) ;
559- } ) . then ( ( ) => new Promise ( resolve => {
560- console . log ( "<COMMS> removeAllApps: Erase complete, waiting 500ms for 'reset()'" ) ;
561- setTimeout ( resolve , 500 ) ;
562- } ) ) ) ; // now wait a second for the reset to complete
556+ // Use write with newline here so we wait for it to finish
557+ let cmd = `\x10E.showMessage("Erasing...");require("Storage").eraseAll();${ Comms . espruinoDevice } .println("OK");reset()\n` ;
558+ Comms . write ( cmd , { waitNewLine :true } ) . then ( handleResult ) ;
559+ } ) . then ( ( ) => new Promise ( resolve => {
560+ console . log ( "<COMMS> removeAllApps: Erase complete, waiting 500ms for 'reset()'" ) ;
561+ setTimeout ( resolve , 500 ) ;
562+ } ) ) ) ; // now wait a second for the reset to complete
563563 } ,
564564 // Set the time on the device
565565 setTime : ( ) => {
@@ -670,8 +670,8 @@ ${Comms.espruinoDevice}.println(((s.length+2)/3)<<2);
670670for (var i=0;i<s.length;i+=${ CHUNKSIZE } ) ${ Comms . espruinoDevice } .print(btoa(s.substr(i,${ CHUNKSIZE } )));
671671${ Comms . espruinoDevice } .print("\\xFF");
672672})()\n` ) . then ( text => {
673- return atobSafe ( text ) ;
674- } ) ) ;
673+ return Utils . atobSafe ( text ) ;
674+ } ) ) ;
675675 } ,
676676 // Read a storagefile
677677 readStorageFile : ( filename ) => { // StorageFiles are different to normal storage entries
@@ -696,7 +696,7 @@ ${Comms.espruinoDevice}.print("\\xFF");
696696 noACK : Const . PACKET_UPLOAD_NOACK
697697 } ) ;
698698 } else {
699- var cmds = AppInfo . getFileUploadCommands ( filename , data ) ;
699+ let cmds = AppInfo . getFileUploadCommands ( filename , data ) ;
700700 return Comms . write ( "\x10" + Comms . getProgressCmd ( ) + "\n" ) . then ( ( ) =>
701701 Comms . uploadCommandList ( cmds , 0 , cmds . length )
702702 ) ;
0 commit comments