@@ -177,7 +177,8 @@ const Comms = {
177
177
char we use to signify echo(0) for a line */
178
178
cmds = cmds . split ( "\x10" ) . filter ( l => l != "" ) . map ( l => "\x10" + l . trim ( ) ) ;
179
179
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 ) => {
181
182
// Function to upload a single line and wait for an 'OK' response
182
183
function uploadCmd ( ) {
183
184
if ( ! cmds . length ) return resolve ( ) ;
@@ -234,7 +235,7 @@ const Comms = {
234
235
}
235
236
236
237
uploadCmd ( )
237
- } ) ;
238
+ } ) ) ;
238
239
} ,
239
240
/** Upload an app
240
241
app : an apps.json structure (i.e. with `storage`)
@@ -373,8 +374,6 @@ const Comms = {
373
374
return ;
374
375
}
375
376
}
376
-
377
-
378
377
if ( Comms . getConnection ( ) . endpoint && Comms . getConnection ( ) . endpoint . name == "Web Serial" && Comms . espruinoDevice == "Bluetooth" ) {
379
378
console . log ( "<COMMS> Using Web Serial, forcing Comms.espruinoDevice='USB'" , result ) ;
380
379
// FIXME: won't work on ESP8266/ESP32!
@@ -441,20 +440,24 @@ const Comms = {
441
440
// Get an app's info file from Bangle.js
442
441
getAppInfo : app => {
443
442
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
+ } ) ;
458
461
} ) ;
459
462
} ,
460
463
/** Remove an app given an appinfo.id structure as JSON
@@ -514,7 +517,9 @@ const Comms = {
514
517
removeAllApps : ( ) => {
515
518
console . log ( "<COMMS> removeAllApps start" ) ;
516
519
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 ) => {
518
523
let timeout = 5 ;
519
524
function handleResult ( result , err ) {
520
525
console . log ( "<COMMS> removeAllApps: received " + JSON . stringify ( result ) ) ;
@@ -537,21 +542,21 @@ const Comms = {
537
542
} ) . then ( ( ) => new Promise ( resolve => {
538
543
console . log ( "<COMMS> removeAllApps: Erase complete, waiting 500ms for 'reset()'" ) ;
539
544
setTimeout ( resolve , 500 ) ;
540
- } ) ) ; // now wait a second for the reset to complete
545
+ } ) ) ) ; // now wait a second for the reset to complete
541
546
} ,
542
547
// Set the time on the device
543
548
setTime : ( ) => {
544
549
/* connect FIRST, then work out the time - otherwise
545
550
we end up with a delay dependent on how long it took
546
551
to open the device chooser. */
547
- return Comms . write ( "\x03 " ) . then ( ( ) => {
552
+ return Comms . write ( " \x08 " ) . then ( ( ) => { // send space+backspace (eg no-op)
548
553
let d = new Date ( ) ;
549
554
let tz = d . getTimezoneOffset ( ) / - 60
550
555
let cmd = '\x10setTime(' + ( d . getTime ( ) / 1000 ) + ');' ;
551
556
// in 1v93 we have timezones too
552
557
cmd += 'E.setTimeZone(' + tz + ');' ;
553
558
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 ) ;
555
560
} ) ;
556
561
} ,
557
562
// Reset the device
@@ -587,17 +592,14 @@ const Comms = {
587
592
// List all files on the device.
588
593
// options can be undefined, or {sf:true} for only storage files, or {sf:false} for only normal files
589
594
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 ;
601
603
} ) ;
602
604
} ,
603
605
// 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 = {
643
645
Progress . show ( { title :`Reading ${ JSON . stringify ( filename ) } ` , percent :0 } ) ;
644
646
console . log ( `<COMMS> readFile ${ JSON . stringify ( filename ) } ` ) ;
645
647
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() {
647
650
var s = require("Storage").read(${ JSON . stringify ( filename ) } );
648
651
if (s===undefined) s="";
649
652
${ Comms . espruinoDevice } .println(((s.length+2)/3)<<2);
650
653
for (var i=0;i<s.length;i+=${ CHUNKSIZE } ) ${ Comms . espruinoDevice } .print(btoa(s.substr(i,${ CHUNKSIZE } )));
651
654
${ Comms . espruinoDevice } .print("\\xFF");
652
655
})()\n` ) . then ( text => {
653
656
return atobSafe ( text ) ;
654
- } ) ;
657
+ } ) ) ;
655
658
} ,
656
659
// Read a storagefile
657
660
readStorageFile : ( filename ) => { // StorageFiles are different to normal storage entries
658
661
Progress . show ( { title :`Reading ${ JSON . stringify ( filename ) } ` , percent :0 } ) ;
659
662
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() {
661
665
var f = require("Storage").open(${ JSON . stringify ( filename ) } ,"r");
662
666
${ Comms . espruinoDevice } .println(f.getLength());
663
667
var l = f.readLine();
664
668
while (l!==undefined) { ${ Comms . espruinoDevice } .print(l); l = f.readLine(); }
665
669
${ Comms . espruinoDevice } .print("\\xFF");
666
- })()\n` ) ;
670
+ })()\n` ) ) ;
667
671
} ,
668
672
// Read a non-storagefile file
669
673
writeFile : ( filename , data ) => {
0 commit comments