@@ -81,7 +81,7 @@ const Comms = {
81
81
handlers : { } ,
82
82
on : function ( id , callback ) { // calling with callback=undefined will disable
83
83
if ( id != "data" ) throw new Error ( "Only data callback is supported" ) ;
84
- var connection = Comms . getConnection ( ) ;
84
+ let connection = Comms . getConnection ( ) ;
85
85
if ( ! connection ) throw new Error ( "No active connection" ) ;
86
86
if ( "undefined" !== typeof Puck ) {
87
87
/* 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 = {
179
179
180
180
return ( Comms . espruinoDevice ?Promise . resolve ( ) :Comms . getDeviceInfo ( true /*noreset*/ ) ) // ensure Comms.espruinoDevice is set
181
181
. 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
+ }
199
199
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..." ) ;
205
209
ignore = true ;
206
210
}
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
+ }
230
231
}
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 ) ;
231
235
}
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
- }
236
236
237
- uploadCmd ( )
238
- } ) ) ;
237
+ uploadCmd ( )
238
+ } ) ) ;
239
239
} ,
240
240
/** Upload an app
241
241
app : an apps.json structure (i.e. with `storage`)
@@ -380,8 +380,8 @@ const Comms = {
380
380
Comms . espruinoDevice = device ;
381
381
else throw new Error ( "Unable to find Espruino console device" ) ;
382
382
console . log ( "<COMMS> Set console device to " + device ) ;
383
- } ) . then ( ( ) => Comms . getDeviceInfo ( true ) ) .
384
- then ( resolve ) ;
383
+ } ) . then ( ( ) => Comms . getDeviceInfo ( true ) )
384
+ . then ( resolve ) ;
385
385
return ;
386
386
}
387
387
}
@@ -406,14 +406,14 @@ const Comms = {
406
406
Comms . write ( cmd , { waitNewLine :true } ) . then ( appListStr => {
407
407
Progress . hide ( { sticky :true } ) ;
408
408
if ( ! appListStr ) appListStr = "" ;
409
- var connection = Comms . getConnection ( ) ;
409
+ let connection = Comms . getConnection ( ) ;
410
410
if ( connection ) {
411
411
appListStr = appListStr + "\n" + connection . received ; // add *any* information we have received so far, including what was returned
412
412
connection . received = "" ; // clear received data just in case
413
413
}
414
414
// 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 ] == "[" ) ;
417
417
// check to see if we got our data
418
418
if ( ! appListJSON ) {
419
419
console . log ( "No JSON, just got: " + JSON . stringify ( appListStr ) ) ;
@@ -433,7 +433,7 @@ const Comms = {
433
433
info . id = appList . pop ( ) ;
434
434
info . storageStats = appList . pop ( ) ; // how much storage has been used
435
435
if ( info . storageStats . totalBytes && ( info . storageStats . freeBytes * 10 < info . storageStats . totalBytes ) ) {
436
- var suggest = "" ;
436
+ let suggest = "" ;
437
437
if ( info . id . startsWith ( "BANGLEJS" ) && info . storageStats . trashBytes * 10 > info . storageStats . totalBytes )
438
438
suggest = "Try running 'Compact Storage' from Bangle.js 'Settings' -> 'Utils'." ;
439
439
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 = {
456
456
} ,
457
457
// Get an app's info file from Bangle.js
458
458
getAppInfo : app => {
459
- var cmd ;
459
+ let cmd ;
460
460
461
461
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
+ } ) ;
478
478
} ) ;
479
479
} ,
480
480
/** Remove an app given an appinfo.id structure as JSON
@@ -537,29 +537,29 @@ const Comms = {
537
537
538
538
return ( Comms . espruinoDevice ?Promise . resolve ( ) :Comms . getDeviceInfo ( true /*noreset*/ ) ) // ensure Comms.espruinoDevice is set
539
539
. 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
+ }
554
555
}
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
563
563
} ,
564
564
// Set the time on the device
565
565
setTime : ( ) => {
@@ -670,8 +670,8 @@ ${Comms.espruinoDevice}.println(((s.length+2)/3)<<2);
670
670
for (var i=0;i<s.length;i+=${ CHUNKSIZE } ) ${ Comms . espruinoDevice } .print(btoa(s.substr(i,${ CHUNKSIZE } )));
671
671
${ Comms . espruinoDevice } .print("\\xFF");
672
672
})()\n` ) . then ( text => {
673
- return atobSafe ( text ) ;
674
- } ) ) ;
673
+ return Utils . atobSafe ( text ) ;
674
+ } ) ) ;
675
675
} ,
676
676
// Read a storagefile
677
677
readStorageFile : ( filename ) => { // StorageFiles are different to normal storage entries
@@ -696,7 +696,7 @@ ${Comms.espruinoDevice}.print("\\xFF");
696
696
noACK : Const . PACKET_UPLOAD_NOACK
697
697
} ) ;
698
698
} else {
699
- var cmds = AppInfo . getFileUploadCommands ( filename , data ) ;
699
+ let cmds = AppInfo . getFileUploadCommands ( filename , data ) ;
700
700
return Comms . write ( "\x10" + Comms . getProgressCmd ( ) + "\n" ) . then ( ( ) =>
701
701
Comms . uploadCommandList ( cmds , 0 , cmds . length )
702
702
) ;
0 commit comments