@@ -177,7 +177,8 @@ const Comms = {
177177 char we use to signify echo(0) for a line */
178178 cmds = cmds . split ( "\x10" ) . filter ( l => l != "" ) . map ( l => "\x10" + l . trim ( ) ) ;
179179
180- return new Promise ( ( resolve , reject ) => {
180+ return ( Comms . espruinoDevice ?Promise . resolve ( ) :Comms . getDeviceInfo ( true /*noreset*/ ) ) // ensure Comms.espruinoDevice is set
181+ . then ( ( ) => new Promise ( ( resolve , reject ) => {
181182 // Function to upload a single line and wait for an 'OK' response
182183 function uploadCmd ( ) {
183184 if ( ! cmds . length ) return resolve ( ) ;
@@ -234,7 +235,7 @@ const Comms = {
234235 }
235236
236237 uploadCmd ( )
237- } ) ;
238+ } ) ) ;
238239 } ,
239240 /** Upload an app
240241 app : an apps.json structure (i.e. with `storage`)
@@ -373,8 +374,6 @@ const Comms = {
373374 return ;
374375 }
375376 }
376-
377-
378377 if ( Comms . getConnection ( ) . endpoint && Comms . getConnection ( ) . endpoint . name == "Web Serial" && Comms . espruinoDevice == "Bluetooth" ) {
379378 console . log ( "<COMMS> Using Web Serial, forcing Comms.espruinoDevice='USB'" , result ) ;
380379 // FIXME: won't work on ESP8266/ESP32!
@@ -441,20 +440,24 @@ const Comms = {
441440 // Get an app's info file from Bangle.js
442441 getAppInfo : app => {
443442 var cmd ;
444- if ( Const . FILES_IN_FS ) cmd = `\x10${ Comms . espruinoDevice } .println(require("fs").readFileSync(${ JSON . stringify ( AppInfo . getAppInfoFilename ( app ) ) } )||"null")\n` ;
445- else cmd = `\x10${ Comms . espruinoDevice } .println(require("Storage").read(${ JSON . stringify ( AppInfo . getAppInfoFilename ( app ) ) } )||"null")\n` ;
446- return Comms . write ( cmd ) .
447- then ( appJSON => {
448- let app ;
449- try {
450- app = JSON . parse ( appJSON ) ;
451- } catch ( e ) {
452- app = null ;
453- console . log ( "<COMMS> ERROR Parsing JSON" , e . toString ( ) ) ;
454- console . log ( "<COMMS> Actual response: " , JSON . stringify ( appJSON ) ) ;
455- throw new Error ( "Invalid JSON" ) ;
456- }
457- return app ;
443+
444+ ( Comms . espruinoDevice ?Promise . resolve ( ) :Comms . getDeviceInfo ( true /*noreset*/ ) ) // ensure Comms.espruinoDevice is set
445+ . then ( ( ) => {
446+ if ( Const . FILES_IN_FS ) cmd = `\x10${ Comms . espruinoDevice } .println(require("fs").readFileSync(${ JSON . stringify ( AppInfo . getAppInfoFilename ( app ) ) } )||"null")\n` ;
447+ else cmd = `\x10${ Comms . espruinoDevice } .println(require("Storage").read(${ JSON . stringify ( AppInfo . getAppInfoFilename ( app ) ) } )||"null")\n` ;
448+ return Comms . write ( cmd ) .
449+ then ( appJSON => {
450+ let app ;
451+ try {
452+ app = JSON . parse ( appJSON ) ;
453+ } catch ( e ) {
454+ app = null ;
455+ console . log ( "<COMMS> ERROR Parsing JSON" , e . toString ( ) ) ;
456+ console . log ( "<COMMS> Actual response: " , JSON . stringify ( appJSON ) ) ;
457+ throw new Error ( "Invalid JSON" ) ;
458+ }
459+ return app ;
460+ } ) ;
458461 } ) ;
459462 } ,
460463 /** Remove an app given an appinfo.id structure as JSON
@@ -514,7 +517,9 @@ const Comms = {
514517 removeAllApps : ( ) => {
515518 console . log ( "<COMMS> removeAllApps start" ) ;
516519 Progress . show ( { title :"Removing all apps" , percent :"animate" , sticky :true } ) ;
517- return new Promise ( ( resolve , reject ) => {
520+
521+ return ( Comms . espruinoDevice ?Promise . resolve ( ) :Comms . getDeviceInfo ( true /*noreset*/ ) ) // ensure Comms.espruinoDevice is set
522+ . then ( ( ) => new Promise ( ( resolve , reject ) => {
518523 let timeout = 5 ;
519524 function handleResult ( result , err ) {
520525 console . log ( "<COMMS> removeAllApps: received " + JSON . stringify ( result ) ) ;
@@ -537,21 +542,21 @@ const Comms = {
537542 } ) . then ( ( ) => new Promise ( resolve => {
538543 console . log ( "<COMMS> removeAllApps: Erase complete, waiting 500ms for 'reset()'" ) ;
539544 setTimeout ( resolve , 500 ) ;
540- } ) ) ; // now wait a second for the reset to complete
545+ } ) ) ) ; // now wait a second for the reset to complete
541546 } ,
542547 // Set the time on the device
543548 setTime : ( ) => {
544549 /* connect FIRST, then work out the time - otherwise
545550 we end up with a delay dependent on how long it took
546551 to open the device chooser. */
547- return Comms . write ( "\x03 " ) . then ( ( ) => {
552+ return Comms . write ( " \x08 " ) . then ( ( ) => { // send space+backspace (eg no-op)
548553 let d = new Date ( ) ;
549554 let tz = d . getTimezoneOffset ( ) / - 60
550555 let cmd = '\x10setTime(' + ( d . getTime ( ) / 1000 ) + ');' ;
551556 // in 1v93 we have timezones too
552557 cmd += 'E.setTimeZone(' + tz + ');' ;
553558 cmd += "(s=>s&&(s.timezone=" + tz + ",require('Storage').write('setting.json',s)))(require('Storage').readJSON('setting.json',1))\n" ;
554- Comms . write ( cmd ) ;
559+ return Comms . write ( cmd ) ;
555560 } ) ;
556561 } ,
557562 // Reset the device
@@ -587,17 +592,14 @@ const Comms = {
587592 // List all files on the device.
588593 // options can be undefined, or {sf:true} for only storage files, or {sf:false} for only normal files
589594 listFiles : ( options ) => {
590- return Comms . write ( " \x03" ) . then ( result => {
591- if ( result === null ) return Promise . reject ( "Ctrl-C failed" ) ;
592- let args = "" ;
593- if ( options && options . sf !== undefined ) args = `undefined,{sf:${ options . sf } }` ;
594- //use encodeURIComponent to serialize octal sequence of append files
595- return Comms . eval ( `require("Storage").list(${ args } ).map(encodeURIComponent)` , ( files , err ) => {
596- if ( files === null ) return Promise . reject ( err || "" ) ;
597- files = files . map ( decodeURIComponent ) ;
598- console . log ( "<COMMS> listFiles" , files ) ;
599- return files ;
600- } ) ;
595+ let args = "" ;
596+ if ( options && options . sf !== undefined ) args = `undefined,{sf:${ options . sf } }` ;
597+ //use encodeURIComponent to serialize octal sequence of append files
598+ return Comms . eval ( `require("Storage").list(${ args } ).map(encodeURIComponent)` , ( files , err ) => {
599+ if ( files === null ) return Promise . reject ( err || "" ) ;
600+ files = files . map ( decodeURIComponent ) ;
601+ console . log ( "<COMMS> listFiles" , files ) ;
602+ return files ;
601603 } ) ;
602604 } ,
603605 // Execute some code, and read back the block of text it outputs (first line is the size in bytes for progress)
@@ -643,27 +645,29 @@ const Comms = {
643645 Progress . show ( { title :`Reading ${ JSON . stringify ( filename ) } ` , percent :0 } ) ;
644646 console . log ( `<COMMS> readFile ${ JSON . stringify ( filename ) } ` ) ;
645647 const CHUNKSIZE = 384 ;
646- return Comms . readTextBlock ( `\x03\x10(function() {
648+ return ( Comms . espruinoDevice ?Promise . resolve ( ) :Comms . getDeviceInfo ( true /*noreset*/ ) ) // ensure Comms.espruinoDevice is set
649+ . then ( ( ) => Comms . readTextBlock ( `\x10(function() {
647650var s = require("Storage").read(${ JSON . stringify ( filename ) } );
648651if (s===undefined) s="";
649652${ Comms . espruinoDevice } .println(((s.length+2)/3)<<2);
650653for (var i=0;i<s.length;i+=${ CHUNKSIZE } ) ${ Comms . espruinoDevice } .print(btoa(s.substr(i,${ CHUNKSIZE } )));
651654${ Comms . espruinoDevice } .print("\\xFF");
652655})()\n` ) . then ( text => {
653656 return atobSafe ( text ) ;
654- } ) ;
657+ } ) ) ;
655658 } ,
656659 // Read a storagefile
657660 readStorageFile : ( filename ) => { // StorageFiles are different to normal storage entries
658661 Progress . show ( { title :`Reading ${ JSON . stringify ( filename ) } ` , percent :0 } ) ;
659662 console . log ( `<COMMS> readStorageFile ${ JSON . stringify ( filename ) } ` ) ;
660- return Comms . readTextBlock ( `\x03\x10(function() {
663+ return ( Comms . espruinoDevice ?Promise . resolve ( ) :Comms . getDeviceInfo ( true /*noreset*/ ) ) // ensure Comms.espruinoDevice is set
664+ . then ( ( ) => Comms . readTextBlock ( `\x10(function() {
661665 var f = require("Storage").open(${ JSON . stringify ( filename ) } ,"r");
662666 ${ Comms . espruinoDevice } .println(f.getLength());
663667 var l = f.readLine();
664668 while (l!==undefined) { ${ Comms . espruinoDevice } .print(l); l = f.readLine(); }
665669 ${ Comms . espruinoDevice } .print("\\xFF");
666- })()\n` ) ;
670+ })()\n` ) ) ;
667671 } ,
668672 // Read a non-storagefile file
669673 writeFile : ( filename , data ) => {
0 commit comments