From a02b5a6f56a02ccaf96a131cf23e66b711a179d1 Mon Sep 17 00:00:00 2001 From: Richard de Boer Date: Tue, 3 Dec 2024 21:30:24 +0100 Subject: [PATCH] hasensors: add step count sensor --- apps/hasensors/ChangeLog | 1 + apps/hasensors/README.md | 1 + apps/hasensors/boot.js | 9 +++++---- apps/hasensors/lib.js | 24 +++++++++++++++++++++--- 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/apps/hasensors/ChangeLog b/apps/hasensors/ChangeLog index e8b1934fc..77e8042a5 100644 --- a/apps/hasensors/ChangeLog +++ b/apps/hasensors/ChangeLog @@ -2,3 +2,4 @@ 0.02: Add sensor icons Customize code directly, remove config file 0.03: Add HRM sensor + Add step count sensor diff --git a/apps/hasensors/README.md b/apps/hasensors/README.md index c083a50dc..011f70844 100644 --- a/apps/hasensors/README.md +++ b/apps/hasensors/README.md @@ -23,3 +23,4 @@ Currently creates these sensors: * `_battery_level`: Your watch battery level as percentage * `_battery_state`: `charging` or `discharging` * `_hrm`: Heart rate (only if measured: this app doesn't enable/disable the sensor) +* `_steps`: Step Count diff --git a/apps/hasensors/boot.js b/apps/hasensors/boot.js index f031d7d7e..feb7d246c 100644 --- a/apps/hasensors/boot.js +++ b/apps/hasensors/boot.js @@ -1,7 +1,8 @@ (function () { - const sb = () => require("hasensors").sendBattery(); - Bangle.on("charging", sb); - NRF.on("connect", () => setTimeout(sb, 2000)); - setInterval(sb, 10 * 60 * 1000); + const su = () => require("hasensors").sendUpdate(); + Bangle.on("charging", su); + NRF.on("connect", () => setTimeout(su, 2000)); + su(); + setInterval(su, 10 * 60 * 1000); Bangle.on('HRM', h=>require("hasensors").sendHRM(h)); })(); diff --git a/apps/hasensors/lib.js b/apps/hasensors/lib.js index 56c609cee..624bc2dc6 100644 --- a/apps/hasensors/lib.js +++ b/apps/hasensors/lib.js @@ -12,8 +12,7 @@ function post(sensor, data) { }); } -exports.sendBattery = function () { - if (!NRF.getSecurityStatus().connected) return; +function sendBattery() { const b = E.getBattery(), c = Bangle.isCharging(); let i = "mdi:battery"; @@ -40,7 +39,26 @@ exports.sendBattery = function () { icon: i, } }); -}; +} + +function sendSteps() { + post("steps", { + state: Bangle.getStepCount(), + attributes: { + friendly_name: "{name} Step Count", + unit_of_measurement: "steps", + state_class: "total", + icon: "mdi:shoe-print", + } + }); +} + +exports.sendUpdate = function() { + if (!NRF.getSecurityStatus().connected) return; + sendBattery(); + sendSteps(); +} + let hrm_last = 0; const HRM_INTERVAL = 10*60*1000;