BangleApps/comms.js

59 lines
1.7 KiB
JavaScript
Raw Normal View History

2019-10-30 17:33:58 +00:00
Puck.debug=3;
2019-11-03 11:13:21 +00:00
/* 'app' is of the form:
{ name: "T-Rex",
icon: "trex.png",
description: "T-Rex game in the style of Chrome's offline game",
storage: [
{name:"+trex",file:"trex.json"},
{name:"-trex",file:"trex.js"},
{name:"*trex",file:"trex-icon.js"}
]
}
*/
// FIXME: use UART lib so that we handle errors properly
2019-10-30 17:33:58 +00:00
var Comms = {
uploadApp : app => {
return new Promise((resolve,reject) => {
// Load all files
Promise.all(app.storage.map(storageFile => httpGet("apps/"+storageFile.file)
// map each file to a command to load into storage
2019-11-04 15:03:50 +00:00
.then(contents=>`\x10require('Storage').write(${toJS(storageFile.name)},${storageFile.evaluate ? contents.trim() : toJS(contents)});`)))
2019-10-30 17:33:58 +00:00
.then(function(fileContents) {
2019-11-04 15:03:50 +00:00
fileContents = fileContents.join("\n")+"\n";
2019-11-03 11:13:21 +00:00
console.log("uploadApp",fileContents);
// reset to ensure we have enough memory to upload what we need to
Puck.write("\x03reset();\n", function() {
setTimeout(function() { // wait for reset
Puck.write(fileContents,function() {
resolve();
});
},500);
2019-10-30 17:33:58 +00:00
});
});
});
},
getInstalledApps : () => {
return new Promise((resolve,reject) => {
Puck.write("\x03",() => {
Puck.eval('require("Storage").list().filter(f=>f[0]=="+").map(f=>f.substr(1))', appList => {
2019-11-03 11:13:21 +00:00
console.log("getInstalledApps", appList);
2019-10-30 17:33:58 +00:00
resolve(appList);
});
});
});
2019-11-03 11:13:21 +00:00
},
removeApp : app => { // expects an app structure
var cmds = app.storage.map(file=>{
return `\x10require("Storage").erase(${toJS(file.name)});\n`;
}).join("");
console.log("removeApp", cmds);
return new Promise((resolve,reject) => {
Puck.write("\x03"+cmds,() => {
resolve();
});
});
2019-10-30 17:33:58 +00:00
}
};