Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

App to verify if user is asleep

ANSWERED

Hi all! My JS experience is close to zero and I need help for a small app:
My goal is to ping the user with a vibration every X-minutes. If they don't respond for 3 pings then they are asleep. If they want pings to stop, they can just use the button.
Now, when pings stop, I want to start observing the user's sleep (awake or asleep) and I would like to record the timestamps and the sleep.state. What is the easiest way to save the sleep.state observation?
here is my draft:

// console.log("App code started");
import { vibration } from "haptics";
import * as document from "document";
import { HeartRateSensor } from "heart-rate";
import sleep from "sleep";

const hrm = new HeartRateSensor();
var secInterval = 5;
var noResponse = 0;
const myButton = document.getElementById("button");
var stop = false;

if (sleep) {
  sleep.addEventListener("change", () => {
     console.log(`User sleep state is: ${sleep.state}`);
   });
} else {
   console.warn("Sleep API not supported on this device, or no permission")
}

// tried clock.ontick but it won't work when screen is off
var pingInterval = setInterval(pingUser, secInterval*1000);


function pingUser(){
  myButton.addEventListener("click", (evt) => {
    stop = true;
  }) 
  
  if (stop || noResponse>=3){
    vibration.stop();
    console.log(sleep.state);
    // >>>> save sleep state observation
  }
  else {
    noResponse = noResponse + 1;
    vibration.start("nudge");
  }
}

 

Best Answer
1 BEST ANSWER

Accepted Solutions

Hi @ac_devel  - several options spring to mind depending on how you want to view the information;

  • write to a file in the watch space, [for pre Versa 2, memory space may become more of an issue],
  • write a file for each observation,
  • use a companion and save in preferences [there may be limits to the number of entries].

Author | ch, passion for improvement.

View best answer in original post

Best Answer
0 Votes
2 REPLIES 2

Hi @ac_devel  - several options spring to mind depending on how you want to view the information;

  • write to a file in the watch space, [for pre Versa 2, memory space may become more of an issue],
  • write a file for each observation,
  • use a companion and save in preferences [there may be limits to the number of entries].

Author | ch, passion for improvement.

Best Answer
0 Votes

I ended up using companion to see the messages but in the future I'll probably use a API communication to send the observation to a server.

Best Answer