Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
WanyiChen committed Jul 29, 2018
0 parents commit 744cefe
Show file tree
Hide file tree
Showing 9 changed files with 249 additions and 0 deletions.
111 changes: 111 additions & 0 deletions app/index.js
@@ -0,0 +1,111 @@
import document from "document";
import { HeartRateSensor } from "heart-rate";
import { user } from "user-profile";
import { vibration } from "haptics";
import { peerSocket } from "messaging";
import { geolocation } from "geolocation";

let hrmData = document.getElementById("hrm-data");
let circle = document.getElementById("circle");
let hrm = new HeartRateSensor();
let passThreshold = false;
let passThresholdPrev = false;
let ok = false;
let help = false;
let positions;
var timer;
hrm.start();

//refresh every second
function refreshData() {
//reading heart rate and location data
geolocation.getCurrentPosition(locationSuccess, locationError);
let data = {
heartRate: hrm.heartRate ? hrm.heartRate : 0,
location: {
latitude: positions ? positions.coords.latitude : 0,
longitude: positions ? positions.coords.longitude : 0
},
help: help
};

//setting threshold and alerts
if (data.heartRate > 220 - user.age){
circle.style.fill = "red";
hrmData.style.fill = "white";
passThreshold = true;
if (passThresholdPrev == false){
startTimer();
vibration.start("ring");
}
} else if (data.heartRate > 220 - user.age - 10){
circle.style.fill = "yellow";
hrmData.style.fill = "black";
passThreshold = false;
} else {
circle.style.fill = "green";
hrmData.style.fill = "white";
passThreshold = false;
}

passThresholdPrev = passThreshold;

//display on screen
hrmData.text = data.heartRate + "";
return data;
}

//if heartrate doesn't go down in 5s and the user didn't press "ok", notification will be sent
function startTimer(){
ok = false; //reset "ok" button
timer = setTimeout(function(){ help = true; }, 5000);
}

//refresh data every second to see if we need to send message
function sendMessage(){
refreshData();
if (passThreshold == false || ok == true){
clearTimeout(timer); //cancel sending message if notification was already sent or if the user pressed "ok"
vibration.stop();
}
message();
help = false;
}
setInterval(sendMessage, 1000);

//send message to companion app
function message(){
if (peerSocket.readyState === peerSocket.OPEN) {
peerSocket.send(refreshData());
}
}

//buttons
let okButton = document.getElementById("ok-button");
okButton.onactivate = function(evt) {
ok = true;
console.log("stopped");
let takeCarePage = document.getElementById("take-care");
takeCarePage.style.display = "inline"; //pop-up the take care page and auto disappears after 3s
setTimeout(function(){ takeCarePage.style.display = "none"; }, 3000);
}
//send help immediately if pressed HELP button
let helpButton = document.getElementById("help-button");
let helpComingPage = document.getElementById("help-coming");
let gotItButton = helpComingPage.getElementById("got-it-button");
helpButton.onactivate = function(evt) {
help = true;
clearTimeout(timer); //message is already sent, no need to send again after 5s
helpComingPage.style.display = "inline"; //pop-up the "help on the way" page
vibration.stop();
}
gotItButton.onclick = function(evt) {
helpComingPage.style.display = "none"; //hide the "help on the way" page
}

//getting location
function locationSuccess(position) {
positions = position;
}

function locationError(error) {}
49 changes: 49 additions & 0 deletions companion/index.js
@@ -0,0 +1,49 @@
import { me } from "companion";
import * as messaging from "messaging";
import { settingsStorage } from "settings";

var notificationCount = 0;

if (!me.permissions.granted("access_internet")) {
console.log("We're not allowed to access the internet :-(");
}

messaging.peerSocket.addEventListener("message", (evt) => {
//getting today's date
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;
var yyyy = today.getFullYear();

if(dd<10) {
dd = '0'+dd
}

if(mm<10) {
mm = '0'+mm
}

today = yyyy+'-'+mm+'-'+dd;

if (evt.data) {
let message = {
appId: me.applicationId,
notificationId: notificationCount,
date: today,
notification: evt.data
}

notificationCount++;

let url = `https://naajw2ldia.execute-api.us-east-2.amazonaws.com/prod`
fetch(url, {
method: "POST",
body: JSON.stringify(message)
}).then(res => {return res.json()})
.then(body => console.log(body))
.catch(error => console.error('Error:', error))
.then(response => console.log('Success:', response));
console.log(JSON.stringify(message));
//console.log("notified");
}
});
25 changes: 25 additions & 0 deletions package.json
@@ -0,0 +1,25 @@
{
"fitbit": {
"appUUID": "b9f430bf-0317-4966-a307-f3e9c1d5efc4",
"appType": "app",
"appDisplayName": "BeeHeart",
"iconFile": "resources/icon.png",
"wipeColor": "#607d8b",
"requestedPermissions": [
"access_heart_rate",
"run_background",
"access_user_profile",
"access_internet",
"access_location"
],
"buildTargets": [
"higgs",
"meson"
],
"i18n": {
"en": {
"name": "BeeHeart"
}
}
}
}
Binary file added resources/heart.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/icon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 45 additions & 0 deletions resources/index.gui
@@ -0,0 +1,45 @@
<svg>
<!-- imports -->
<defs>
<link rel="stylesheet" href="styles.css" />
<link rel="import" href="/mnt/sysassets/widgets_common.gui" />
<link rel="stylesheet" href="/mnt/sysassets/widgets/dynamic_textarea.css" />
<link rel="import" href="/mnt/sysassets/widgets/dynamic_textarea.gui" />
<link rel="import" href="/mnt/sysassets/widgets/square_button_widget.gui" />
<link rel="import" href="/mnt/sysassets/widgets/mixed_text_widget.gui" />
</defs>

<!-- background -->
<circle id="circle" cx="50%" cy="35%" r="75" fill="green" />
<image x="47%" y="15%" width="20" height="20" href="heart.png" load="sync" />

<!-- texts -->
<text id="hrm-data" class="sensor-data" x="50%" y="45%" fill="white" font-size="50" font-weight="bold"></text>

<!-- button -->
<use id="ok-button" href="#square-button" class="dialog-half-button-left" fill="fb-green">
<set href="#text" attributeName="text-buffer" to="I'm OK" />
</use>
<use id="help-button" href="#square-button" class="dialog-half-button-right" fill="fb-red">
<set href="#text" attributeName="text-buffer" to="HELP" />
</use>

<!-- Take Care page -->
<svg id="take-care" display="none">
<rect class="full-size" fill="fb-black" />
<image x="35%" y="15%" width="120" height="120" href="smile.png" load="sync" />
<text id="hrm-label" class="sensor-label" x="50%" y="200" font-size="50" font-weight="bold">Take care</text>
</svg>

<!-- Help on the way page -->
<svg id="help-coming" display="none">
<rect class="full-size" fill="fb-black" />
<text id="hrm-label" class="sensor-label" x="50%" y="50" font-weight="bold">Help is on the way</text>
<text id="hrm-label" class="sensor-label" x="50%" y="100" font-weight="bold">Log on to</text>
<text id="hrm-label" class="sensor-label" x="50%" y="$" font-weight="bold">www.beeheart.com</text>
<use id="got-it-button" href="#square-button-positive" y="70%">
<set href="#text" attributeName="text-buffer" to="Got it" />
</use>
</svg>

</svg>
Binary file added resources/smile.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions resources/styles.css
@@ -0,0 +1,13 @@
.sensor-label {
font-family: System-Regular;
fill: white;
text-anchor: middle;
text-length: 32;
}

.sensor-data {
font-family: System-Light;
fill: yellow;
text-anchor: middle;
text-length: 32;
}
6 changes: 6 additions & 0 deletions resources/widgets.gui
@@ -0,0 +1,6 @@
<svg>
<defs>
<link rel="stylesheet" href="styles.css" />
<link rel="import" href="/mnt/sysassets/widgets_common.gui" />
</defs>
</svg>

0 comments on commit 744cefe

Please sign in to comment.