BangleApps/comms.js

113 lines
3.6 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
2019-11-06 17:25:02 +00:00
Promise.all(app.storage.map(storageFile => {
if (storageFile.content)
2019-11-07 21:08:33 +00:00
return Promise.resolve(storageFile);
2019-11-06 17:25:02 +00:00
else if (storageFile.url)
2019-11-07 21:08:33 +00:00
return httpGet("apps/"+storageFile.url).then(content => {
return {
name : storageFile.name,
content : content,
evaluate : storageFile.evaluate
}});
else return Promise.resolve();
})).then(fileContents => { // now we just have a list of files + contents...
// filter out empty files
fileContents = fileContents.filter(x=>x!==undefined);
2019-11-06 17:25:02 +00:00
// then map each file to a command to load into storage
2019-11-07 21:08:33 +00:00
fileContents.forEach(storageFile => {
// check if this is the JSON file
if (storageFile.name[0]=="+") {
storageFile.evaluate = true;
var json = {};
try {
json = JSON.parse(storageFile.content);
} catch (e) {
reject(storageFile.name+" is not valid JSON");
}
json.files = fileContents.map(storageFile=>storageFile.name).join(",");
storageFile.content = JSON.stringify(json);
}
// format ready for Espruino
var js = storageFile.evaluate ? storageFile.content.trim() : toJS(storageFile.content);
storageFile.cmd = `\x10require('Storage').write(${toJS(storageFile.name)},${js});`;
});
fileContents = fileContents.map(storageFile=>storageFile.cmd).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", (result) => {
if (result===null) return reject("");
setTimeout(() => { // wait for reset
Puck.write("\x10E.showMessage('Uploading...')\n"+fileContents+"load()\n",(result) => {
if (result===null) return reject("");
2019-11-03 11:13:21 +00:00
resolve();
});
},500);
2019-10-30 17:33:58 +00:00
});
});
});
},
getInstalledApps : () => {
return new Promise((resolve,reject) => {
Puck.write("\x03",(result) => {
if (result===null) return reject("");
Puck.eval('require("Storage").list().filter(f=>f[0]=="+").map(f=>f.substr(1))', (appList,err) => {
if (appList===null) return reject(err || "");
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) => {
2019-11-07 09:29:31 +00:00
Puck.write("\x03"+cmds+"\x10load()\n",(result) => {
if (result===null) return reject("");
2019-11-03 11:13:21 +00:00
resolve();
});
});
},
removeAllApps : () => {
return new Promise((resolve,reject) => {
// Use eval here so we wait for it to finish
Puck.eval('require("Storage").eraseAll()||true', (result,err) => {
if (result===null) return reject(err || "");
Puck.write('\x03\x10reset()\n',(result) => {
if (result===null) return reject("");
resolve();
});
});
});
},
setTime : () => {
return new Promise((resolve,reject) => {
Puck.setTime((result) => {
if (result===null) return reject("");
resolve();
});
});
2019-10-30 17:33:58 +00:00
}
};