-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jon Barlow
committed
Dec 18, 2019
1 parent
683fccc
commit 81d65c3
Showing
16 changed files
with
3,527 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2019 Fitbit, Inc | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,18 @@ | ||
# sdk-calendar-clock | ||
|
||
Fitbit SDK example application which demonstrates the Companion Calendar API. | ||
|
||
 | ||
|
||
The companion fetches calendar data using the Companion Calendar API and sends it to the | ||
device using the File Transfer API. The application decides which event to display. | ||
|
||
> Note: Only a subset of the calendar data is sent to the device, if you need to | ||
> use different fields, then you need to include them in the file transfer. | ||
Find out more information on the | ||
[Fitbit Developer Website](https://dev.fitbit.com). | ||
|
||
## License | ||
|
||
This example is licensed under the [MIT License](./LICENSE). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { inbox } from "file-transfer"; | ||
import { readFileSync } from "fs"; | ||
|
||
import { dataFile, dataType } from "../common/constants"; | ||
import { toEpochSec } from "../common/utils"; | ||
|
||
let data = {}; | ||
let cb; | ||
|
||
export function initialize(callback) { | ||
cb = callback; | ||
data = loadData(); | ||
updatedData(); | ||
|
||
fileHandler(); | ||
inbox.addEventListener("newfile", fileHandler); | ||
} | ||
|
||
export function next() { | ||
if (existsData()) { | ||
// Exclude all-day events | ||
let events = data.filter(event => { | ||
return !event.isAllDay; | ||
}); | ||
|
||
if (events && events.length > 0) { | ||
const currentDate = toEpochSec(new Date()); | ||
|
||
// Get all future events | ||
let futureEvents = events.filter(event => { | ||
return event.startDate > currentDate; | ||
}); | ||
|
||
if (futureEvents && futureEvents.length > 0) { | ||
// Get the first future appointment | ||
return futureEvents[0]; | ||
} | ||
} | ||
} | ||
return; | ||
} | ||
|
||
function fileHandler() { | ||
let fileName; | ||
do { | ||
fileName = inbox.nextFile(); | ||
data = loadData(); | ||
updatedData(); | ||
} while (fileName); | ||
} | ||
|
||
function loadData() { | ||
try { | ||
return readFileSync(`/private/data/${dataFile}`, dataType); | ||
} catch (ex) { | ||
console.error(`Appointment: loadData() failed. ${ex}`); | ||
return {}; | ||
} | ||
} | ||
|
||
function existsData() { | ||
if (Object.keys(data).length === 0 && data.constructor === Object) { | ||
console.warn("Appointment: No data found."); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
function updatedData() { | ||
if (typeof cb === "function" && existsData()) { | ||
cb(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import clock from "clock"; | ||
import { preferences } from "user-settings"; | ||
import * as util from "../common/utils"; | ||
|
||
let cb; | ||
|
||
export function initialize(granularity, callback) { | ||
clock.granularity = granularity ? granularity : "minutes"; | ||
cb = callback; | ||
clock.addEventListener("tick", tick); | ||
} | ||
|
||
export function tick(evt) { | ||
const today = evt ? evt.date : new Date(); | ||
const mins = util.zeroPad(today.getMinutes()); | ||
let hours = today.getHours(); | ||
|
||
if (preferences.clockDisplay === "12h") { | ||
// 12h format | ||
hours = hours % 12 || 12; | ||
} else { | ||
// 24h format | ||
hours = util.zeroPad(hours); | ||
} | ||
|
||
if (typeof cb === "function") { | ||
cb({ time: `${hours}:${mins}` }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
console.log("Hello world!"); | ||
// basic clock | ||
// show next appointment title.substr, location,/in 1 day/hours/mins | ||
|
||
/* | ||
Meeting with blah blah blah | ||
at BUH-q3wewe | ||
in 3 hours (12:15pm) | ||
*/ | ||
|
||
import document from "document"; | ||
|
||
import * as appointment from "./appointment"; | ||
import * as clock from "./clock"; | ||
import { fromEpochSec, timeUntil, trimString } from "../common/utils"; | ||
|
||
const time = document.getElementById("time"); | ||
const title = document.getElementById("title"); | ||
const details = document.getElementById("details"); | ||
|
||
clock.initialize("minutes", data => { | ||
// Clock ticked, update UI | ||
time.text = data.time; | ||
renderAppointment(); | ||
}); | ||
|
||
appointment.initialize(() => { | ||
// We have fresh calendar data | ||
clock.tick(); | ||
}); | ||
|
||
function renderAppointment() { | ||
let event = appointment.next(); | ||
if (event) { | ||
title.text = event.title.substr(0, 20); | ||
details.text = `${timeUntil(fromEpochSec(event.startDate))} @ ${ | ||
event.location | ||
}`; | ||
} else { | ||
title.text = "No appointments"; | ||
location.text = ""; | ||
date.text = ""; | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const dataType = "cbor"; | ||
export const dataFile = "appointments.cbor"; | ||
export const millisecondsPerMinute = 1000 * 60; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* Add zero in front of numbers < 10 | ||
* @param {number} i | ||
*/ | ||
export function zeroPad(i) { | ||
if (i < 10) { | ||
i = "0" + i; | ||
} | ||
return i; | ||
} | ||
|
||
/** | ||
* Convert a Date object to seconds since January 1, 1970 | ||
* @param {Date} date | ||
*/ | ||
export function toEpochSec(date) { | ||
return Math.floor(date.getTime() / 1000); | ||
} | ||
|
||
/** | ||
* Convert a seconds since January 1, 1970 to a Date object | ||
* @param {Number} seconds | ||
*/ | ||
export function fromEpochSec(seconds) { | ||
return new Date(seconds * 1000); | ||
} | ||
|
||
/** | ||
* Returns a friendly time until a specified date | ||
* @param {Date} date | ||
*/ | ||
export function timeUntil(date) { | ||
const today = new Date(); | ||
const days = parseInt((date - today) / (1000 * 60 * 60 * 24)); | ||
const hours = parseInt((Math.abs(date - today) / (1000 * 60 * 60)) % 24); | ||
const minutes = parseInt( | ||
(Math.abs(date.getTime() - today.getTime()) / (1000 * 60)) % 60 | ||
); | ||
|
||
if (days > 0) { | ||
return `in ${days} day${pluralize(days)}`; | ||
} | ||
|
||
if (hours > 0) { | ||
return `in ${hours}hr${pluralize(hours)}${ | ||
hours < 3 && minutes > 0 ? ` ${minutes}min${pluralize(minutes)}` : "" | ||
}`; | ||
} | ||
|
||
if (minutes > 0) { | ||
return `in ${minutes}min${pluralize(minutes)}`; | ||
} | ||
|
||
return "Now"; | ||
} | ||
|
||
/** | ||
* Returns an `s` if the number is plural | ||
* @param {Number} number | ||
*/ | ||
function pluralize(number) { | ||
return number === 1 ? "" : "s"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import calendars from "calendars"; | ||
import * as cbor from "cbor"; | ||
import { me as companion } from "companion"; | ||
import { outbox } from "file-transfer"; | ||
|
||
import { toEpochSec } from "../common/utils"; | ||
import { dataFile, millisecondsPerMinute } from "../common/constants"; | ||
|
||
companion.wakeInterval = 15 * millisecondsPerMinute; | ||
companion.addEventListener("wakeinterval", refreshData); | ||
|
||
refreshData(); | ||
|
||
function refreshData() { | ||
let dataCalendars = [], | ||
dataEvents = []; | ||
|
||
calendars | ||
.searchSources() | ||
.then(results => { | ||
return calendars.searchCalendars(); | ||
}) | ||
.then(results => { | ||
results.forEach(calendar => { | ||
// console.log( | ||
// `> calendar: ${calendar.title} (${calendar.sourceId}/${calendar.id})` | ||
// ); | ||
dataCalendars.push(calendar); | ||
}); | ||
|
||
// Filter events to 48hr window | ||
const start = new Date(); | ||
const end = new Date(); | ||
start.setHours(0, 0, 0, 0); | ||
end.setHours(128, 59, 59, 999); | ||
const eventsQuery = { | ||
startDate: start, | ||
endDate: end | ||
}; | ||
|
||
return calendars.searchEvents(eventsQuery); | ||
}) | ||
.then(results => { | ||
results.forEach(event => { | ||
// console.log(`> event: ${event.title} (${event.startDate})`); | ||
dataEvents.push({ | ||
title: event.title, | ||
location: event.location, | ||
startDate: toEpochSec(event.startDate), | ||
endDate: toEpochSec(event.endDate), | ||
isAllDay: event.isAllDay | ||
}); | ||
}); | ||
if (dataEvents && dataEvents.length > 0) { | ||
sendData(dataEvents); | ||
} | ||
}) | ||
.catch(error => { | ||
console.error(error); | ||
console.error(error.stack); | ||
}); | ||
} | ||
|
||
function sendData(data) { | ||
outbox.enqueue(dataFile, cbor.encode(data)).catch(error => { | ||
console.warn(`Failed to enqueue data. Error: ${error}`); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"name": "sdk-calendar-clock", | ||
"version": "0.1.0", | ||
"private": true, | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@fitbit/sdk": "~4.1.0", | ||
"@fitbit/sdk-cli": "^1.7.1" | ||
}, | ||
"fitbit": { | ||
"appUUID": "24855175-1a50-42f2-8178-019cb091a886", | ||
"appType": "clockface", | ||
"appDisplayName": "SDK Calendar Clock", | ||
"iconFile": "resources/icon.png", | ||
"wipeColor": "#ffffff", | ||
"requestedPermissions": [ | ||
"access_calendar", | ||
"run_background" | ||
], | ||
"buildTargets": [ | ||
"higgs", | ||
"meson", | ||
"gemini", | ||
"mira" | ||
], | ||
"i18n": {}, | ||
"defaultLanguage": "en-US" | ||
}, | ||
"scripts": { | ||
"build": "fitbit-build", | ||
"debug": "fitbit" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<svg> | ||
<rect id="background" /> | ||
<text id="time">13:37</text> | ||
<text id="title">Appointment Title</text> | ||
<textarea id="details">Appointment Date @ Appointment Location</textarea> | ||
</svg> |
Oops, something went wrong.