Thursday, 20 May 2021

Fitbit - timer for medication - how to show current time on application

I'm trying to use my fitbit to help know how far into my medication I am more easily.

I'm using a fitbit and some basic JS.

The current functionality is below for the countDownTimer

    function CountDownTimer(duration, granularity) {
  this.duration = duration;
  this.granularity = granularity || 1000;
  this.tickFtns = [];
  this.running = false;
}

CountDownTimer.prototype.start = function() {
  if (this.running) {
    return;
  }
  this.running = true;
  var start = Date.now(),
      that = this,
      diff, obj;

  (function timer() {
    diff = that.duration - (((Date.now() - start) / 1000) | 0);

    if (diff > 0) {
      setTimeout(timer, that.granularity);
    } else {
      diff = 0;
      that.running = false;
    }

    obj = CountDownTimer.parse(diff);
    that.tickFtns.forEach(function(ftn) {
      ftn.call(this, obj.minutes, obj.seconds);
    }, that);
  }());
};

CountDownTimer.prototype.onTick = function(ftn) {
  if (typeof ftn === 'function') {
    this.tickFtns.push(ftn);
  }
  return this;
};

CountDownTimer.prototype.expired = function() {
  return !this.running;
};

CountDownTimer.parse = function(seconds) {
  return {
      'hours': (seconds / 3600)|0,
    'minutes': (seconds / 60) | 0,
    'seconds': (seconds % 60) | 0
  };
};



export default CountDownTimer;

This is the function to format the output

function Format(display) {
return function (hours,minutes, seconds) {
        hours = hours < 10 ? "0" + hours : 0;
        minutes = minutes < 10 ? "0" + minutes : 0;
        seconds = seconds < 10 ? "0" + seconds : 0;
display.text  = hours + ":" + minutes + ":" + seconds;
};
}

export default Format;

This is the main app.js file that runs them all,

import CountDownTimer from"../common/CountDownTimer";
import Format from "../common/Format";
const display = document.getElementById("time");
const myButton = document.getElementById("button-1");
myButton.text = "button";

var Timing = 6000000*1

myButton.addEventListener("click", (evt) => {

  myButton.text = "Started";
  var timer = new CountDownTimer(Timing);

timer.onTick(Format(display)).start(); 
  localStorage.setItem("myKey", "myValue");
  console.log(localStorage.getItem("myKey"));
  console.log("CLICKED");
});

With the index.html page being something like,

    <svg>
    <text id="myLabel" />
<text id="time"/>
<use  id="button-1" href="#icon-text-button" class="icon-text-button bottom application-fill" />

</svg>

At the moment the countdown is working well when just using minutes and seconds but it breaks when I try to use hours as well, in a XX:XX:XX format.

Additionally, I want to store the date time when I am clicking the timer in the companion storage and to then send to my own person dataset.

The companion storage I think is like this,

import * as messaging from "messaging";
import { settingsStorage } from "settings";

import { localStorage } from "local-storage";

localStorage.setItem("myKey", "myValue");
console.log(localStorage.getItem("myKey"));

Would love to get some guidance - Thanks!



from Fitbit - timer for medication - how to show current time on application

No comments:

Post a Comment