Support for interface/custom files to listen directly to data coming from the Bangle

hrmaccevents now uses this feature rather than trying to access BLE directly
pull/3060/head
Gordon Williams 2023-10-20 17:04:39 +01:00
parent ded269bc3e
commit 77f925d49f
2 changed files with 41 additions and 61 deletions

View File

@ -5,7 +5,6 @@
</head> </head>
<body> <body>
<script src="../../core/lib/customize.js"></script> <script src="../../core/lib/customize.js"></script>
<script src="https://www.puck-js.com/puck.js"></script>
<p> <p>
<div class="form-group"> <div class="form-group">
<label class="form-switch"> <label class="form-switch">
@ -15,7 +14,7 @@
</div> </div>
</p> </p>
<p> <p>
<button id="btnConnect" class="btn btn-primary">Start</button> <button id="btnStart" class="btn btn-primary">Start</button>
<button id="btnStop" class="btn btn-secondary">Stop</button> <button id="btnStop" class="btn btn-secondary">Stop</button>
<button id="btnReset" class="btn btn-secondary">Reset</button> <button id="btnReset" class="btn btn-secondary">Reset</button>
<button id="btnSave" class="btn btn-primary">Save CSV</button> <button id="btnSave" class="btn btn-primary">Save CSV</button>
@ -181,15 +180,11 @@ function createCode(){
`; `;
} }
var connection; var isConnected = false;
var lineCount=-1; var lineCount=-1;
function stop (){ function stop (){
connection.reconnect((c)=>{ Puck.write("load();\n");
c.write("load();\n");
c.close();
connection = undefined;
});
} }
function updateButtons(){ function updateButtons(){
@ -206,24 +201,15 @@ document.getElementById("chkLocal").addEventListener("click", function() {
updateButtons(); updateButtons();
}); });
window.addEventListener("message", function(event) {
let msg = event.data;
if (msg.type=="readstoragefilersp") {
Util.saveCSV("log", msg.data);
}
}, false);
document.getElementById("btnDownload").addEventListener("click", function() { document.getElementById("btnDownload").addEventListener("click", function() {
if (connection) { stop();
stop();
}
console.log("Loading data from BangleJs..."); console.log("Loading data from BangleJs...");
try { isConnected = false;
window.postMessage({type:"readstoragefile",data:"log.csv",id:0}); setTimeout(function() {
} catch(ex) { Util.readStorageFile("log.csv",function(data) {
console.log("(Warning) Could not load apikey from BangleJs."); Util.saveCSV("log", data);
console.log(ex); });
} }, 1000);
}); });
document.getElementById("btnSave").addEventListener("click", function() { document.getElementById("btnSave").addEventListener("click", function() {
@ -235,63 +221,57 @@ function reset(){
} }
document.getElementById("btnReset").addEventListener("click", function() { document.getElementById("btnReset").addEventListener("click", function() {
if (connection) { Puck.write("load();\n");
stop(); isConnected = false;
}
lineCount=-1; lineCount=-1;
localStorage.removeItem("data"); localStorage.removeItem("data");
reset(); reset();
}); });
document.getElementById("btnStop").addEventListener("click", function() { document.getElementById("btnStop").addEventListener("click", function() {
if (connection) { stop();
stop(); isConnected = false;
}
}); });
function connect(connectionHandler){ document.getElementById("btnStart").addEventListener("click", function() {
Puck.connect(function(c) {
if (!c) {
console.log("Couldn't connect!\n");
return;
}
connection = c;
connectionHandler(c);
});
}
document.getElementById("btnConnect").addEventListener("click", function() {
localStorage.setItem("data", ""); localStorage.setItem("data", "");
lineCount=-1; lineCount=-1;
if (connection) { document.getElementById("result").innerText="0";
stop();
document.getElementById("result").innerText="0"; Puck.write("reset();\n", function() {
} setTimeout(function() {
connect(function(connection) { Puck.write("\x03\x10if(1){"+createCode()+"}\n",
var buf = ""; function() {
connection.on("data", function(d) { console.log("Ready...");
buf += d; isConnected = true;
var l = buf.split("\n"); }
buf = l.pop(); );
l.forEach(onLine); }, 1500);
});
connection.write("reset();\n", function() {
setTimeout(function() {
connection.write("\x03\x10if(1){"+createCode()+"}\n",
function() { console.log("Ready..."); });
}, 1500);
});
}); });
}); });
function onLine(line) { function onLine(line) {
console.log("RECEIVED:"+line); console.log("RECEIVED:"+line);
if (!isConnected) return;
if (line.startsWith("DATA:")){ if (line.startsWith("DATA:")){
localStorage.setItem("data", localStorage.getItem("data") + line.substr(5) + "\n"); localStorage.setItem("data", localStorage.getItem("data") + line.substr(5) + "\n");
lineCount++; lineCount++;
document.getElementById("result").innerText="Captured events: " + lineCount; document.getElementById("result").innerText="Captured events: " + lineCount;
} }
} }
var buf = "";
Puck.on("data", function(d) {
buf += d;
var l = buf.split("\n");
buf = l.pop();
l.forEach(onLine);
});
function onInit(info) {
}
</script> </script>
</body> </body>
</html> </html>

2
core

@ -1 +1 @@
Subproject commit 0f78c425bbf1e1947a4981232d2d80110fd04fb8 Subproject commit 4422e4a3e808c99e540dc86ac1e3cab0ccb23a82