From 77f925d49f9d2f356bf94aa8ec86df4a3419b946 Mon Sep 17 00:00:00 2001 From: Gordon Williams Date: Fri, 20 Oct 2023 17:04:39 +0100 Subject: [PATCH] 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 --- apps/hrmaccevents/custom.html | 100 ++++++++++++++-------------------- core | 2 +- 2 files changed, 41 insertions(+), 61 deletions(-) diff --git a/apps/hrmaccevents/custom.html b/apps/hrmaccevents/custom.html index 97cc7e0ab..c6776cb71 100644 --- a/apps/hrmaccevents/custom.html +++ b/apps/hrmaccevents/custom.html @@ -5,7 +5,6 @@ -

- + @@ -181,15 +180,11 @@ function createCode(){ `; } -var connection; +var isConnected = false; var lineCount=-1; function stop (){ - connection.reconnect((c)=>{ - c.write("load();\n"); - c.close(); - connection = undefined; - }); + Puck.write("load();\n"); } function updateButtons(){ @@ -206,24 +201,15 @@ document.getElementById("chkLocal").addEventListener("click", function() { 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() { - if (connection) { - stop(); - } + stop(); console.log("Loading data from BangleJs..."); - try { - window.postMessage({type:"readstoragefile",data:"log.csv",id:0}); - } catch(ex) { - console.log("(Warning) Could not load apikey from BangleJs."); - console.log(ex); - } + isConnected = false; + setTimeout(function() { + Util.readStorageFile("log.csv",function(data) { + Util.saveCSV("log", data); + }); + }, 1000); }); document.getElementById("btnSave").addEventListener("click", function() { @@ -235,63 +221,57 @@ function reset(){ } document.getElementById("btnReset").addEventListener("click", function() { - if (connection) { - stop(); - } + Puck.write("load();\n"); + isConnected = false; lineCount=-1; localStorage.removeItem("data"); reset(); }); document.getElementById("btnStop").addEventListener("click", function() { - if (connection) { - stop(); - } + stop(); + isConnected = false; }); -function connect(connectionHandler){ - Puck.connect(function(c) { - if (!c) { - console.log("Couldn't connect!\n"); - return; - } - connection = c; - connectionHandler(c); - }); -} - -document.getElementById("btnConnect").addEventListener("click", function() { +document.getElementById("btnStart").addEventListener("click", function() { localStorage.setItem("data", ""); lineCount=-1; - if (connection) { - stop(); - document.getElementById("result").innerText="0"; - } - connect(function(connection) { - var buf = ""; - connection.on("data", function(d) { - buf += d; - var l = buf.split("\n"); - buf = l.pop(); - l.forEach(onLine); - }); - connection.write("reset();\n", function() { - setTimeout(function() { - connection.write("\x03\x10if(1){"+createCode()+"}\n", - function() { console.log("Ready..."); }); - }, 1500); - }); + document.getElementById("result").innerText="0"; + + Puck.write("reset();\n", function() { + setTimeout(function() { + Puck.write("\x03\x10if(1){"+createCode()+"}\n", + function() { + console.log("Ready..."); + isConnected = true; + } + ); + }, 1500); }); }); function onLine(line) { console.log("RECEIVED:"+line); + if (!isConnected) return; if (line.startsWith("DATA:")){ localStorage.setItem("data", localStorage.getItem("data") + line.substr(5) + "\n"); 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) { + +} diff --git a/core b/core index 0f78c425b..4422e4a3e 160000 --- a/core +++ b/core @@ -1 +1 @@ -Subproject commit 0f78c425bbf1e1947a4981232d2d80110fd04fb8 +Subproject commit 4422e4a3e808c99e540dc86ac1e3cab0ccb23a82