Skip to content

Commit

Permalink
First impl draft; Updated icons
Browse files Browse the repository at this point in the history
  • Loading branch information
smirko-dev committed Nov 19, 2021
1 parent ce72b72 commit 9a4762d
Show file tree
Hide file tree
Showing 38 changed files with 312 additions and 121 deletions.
19 changes: 11 additions & 8 deletions app/appointment.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { inbox } from "file-transfer";
import { readFileSync } from "fs";

import { dataFile, dataType } from "../common/constants";
import { calendarFile, calendarType } from "../common/constants";
import { toEpochSec } from "../common/utils";

let data;
let handleCalendarUpdatedCallback;
let handleCallback;

export function initialize(callback) {
handleCalendarUpdatedCallback = callback;
handleCallback = callback;
data = loadData();
inbox.addEventListener("newfile", fileHandler);
fileHandler();
Expand Down Expand Up @@ -43,14 +43,17 @@ function fileHandler() {
let fileName;
do {
fileName = inbox.nextFile();
data = loadData();
updatedData();
if (fileName === calendarFile) {
console.log('Load ' + fileName);
data = loadData();
updatedData();
}
} while (fileName);
}

function loadData() {
try {
return readFileSync(`/private/data/${dataFile}`, dataType);
return readFileSync(`/private/data/${calendarFile}`, calendarType);
} catch (ex) {
console.error(`Appointment: loadData() failed. ${ex}`);
return;
Expand All @@ -66,7 +69,7 @@ function existsData() {
}

function updatedData() {
if (typeof handleCalendarUpdatedCallback === "function" && existsData()) {
handleCalendarUpdatedCallback();
if (typeof handleCallback === "function" && existsData()) {
handleCallback();
}
}
90 changes: 54 additions & 36 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import { battery } from "power";
import { display } from "display";
import { today } from 'user-activity';
import { me as device } from "device";
import { units } from "user-settings";
import * as fs from "fs";
import * as appointment from "./appointment";
import * as weather from "./weather";
import * as clock from "./clock";
import * as messaging from "messaging";
import { fromEpochSec, timeString } from "../common/utils";
Expand All @@ -19,10 +21,8 @@ const appointmentsLabel = document.getElementById("appointmentsLabel");
const batteryImage = document.getElementById("batteryImage");
const batteryLabel = document.getElementById("batteryLabel");

const activityIcon = document.getElementById("activityIcon");
const activityLabel = document.getElementById("activityLabel");

//TODO: let activityIntervalID = 0;
const infoIcon = document.getElementById("infoIcon");
const infoLabel = document.getElementById("infoLabel");

const INVISIBLE = 0.0;
const VISIBLE = 0.8;
Expand All @@ -40,17 +40,22 @@ me.onunload = saveSettings;

// Load settings at startup
let settings = loadSettings();
applySettings(settings.activity, settings.color);
applySettings(settings.info, settings.color);

// Apply and store settings
function applySettings(activity, color) {
if (typeof activity !== 'undefined') {
activityIcon.image = `${activity}.png`;
settings.activity = activity;
function applySettings(info, color) {
if (typeof info !== 'undefined') {
if (info === 'weather') {
renderWeather(weather.current());
}
else {
infoIcon.image = `${info}.png`;
settings.info = info;
}
}
if (typeof color !== 'undefined') {
hourLabel.style.fill = color;
activityIcon.style.fill = color;
infoIcon.style.fill = color;
settings.color = color;
}
}
Expand All @@ -63,7 +68,7 @@ function loadSettings() {
catch (ex) {
// Default values
return {
activity: "steps",
info: "steps",
color: "#2490DD"
};
}
Expand All @@ -76,11 +81,11 @@ function saveSettings() {

// Update settings
messaging.peerSocket.onmessage = (evt) => {
if (evt.data.key === "activity") {
if (evt.data.key === "info") {
applySettings(evt.data.value, settings.color);
}
else if (evt.data.key === "color") {
applySettings(settings.activity, evt.data.value);
applySettings(settings.info, evt.data.value);
}
renderAppointment();
}
Expand All @@ -103,22 +108,27 @@ appointment.initialize(() => {
renderAppointment();
});

weather.initialize(data => {
// Update weather with new data
renderWeather(data);
});

display.addEventListener("change", () => {
if (display.on) {
// Update appointment and battery on display on
renderAppointment();
renderBattery();
}
else {
// Stop updating activity info
hideActivity();
// Stop updating info
hideInfo();
}
});

// Hide event when touched
appointmentsLabel.addEventListener("mousedown", () => {
showActivity();
updateActivity();
showInfo();
updateInfo();
})

function renderAppointment() {
Expand All @@ -127,40 +137,48 @@ function renderAppointment() {
if (event) {
const date = fromEpochSec(event.startDate);
appointmentsLabel.text = timeString(date) + " " + event.title;
hideActivity();
hideInfo();
}
else {
showActivity();
updateActivity();
showInfo();
updateInfo();
}
}

function hideActivity() {
activityIcon.style.opacity = INVISIBLE;
activityLabel.style.opacity = INVISIBLE;
function renderWeather(data) {
data = units.temperature === "F" ? toFahrenheit(data) : data;
infoLabel.text = `${data.temperature}\u00B0 ${data.unit}`;
infoIcon.image = `${data.icon}`;
}

function hideInfo() {
infoIcon.style.opacity = INVISIBLE;
infoLabel.style.opacity = INVISIBLE;
appointmentsLabel.style.opacity = VISIBLE;
//TODO: clearInterval(activityIntervalID);
}

function showActivity() {
activityIcon.style.opacity = VISIBLE;
activityLabel.style.opacity = VISIBLE;
function showInfo() {
infoIcon.style.opacity = VISIBLE;
infoLabel.style.opacity = VISIBLE;
appointmentsLabel.style.opacity = INVISIBLE;
//TODO: activityIntervalID = setInterval(updateActivity, 1500);
}

function updateActivity() {
if (settings.activity === 'distance') {
activityLabel.text = today.adjusted.distance;
function updateInfo() {
if (settings.info === 'distance') {
infoLabel.text = today.adjusted.distance;
}
else if (settings.info === 'floors') {
infoLabel.text = today.adjusted.elevationGain;
}
else if (settings.activity === 'floors') {
activityLabel.text = today.adjusted.elevationGain;
else if (settings.info === 'calories') {
infoLabel.text = today.adjusted.calories;
}
else if (settings.activity === 'calories') {
activityLabel.text = today.adjusted.calories;
else if (settings.info === 'weather') {
// TODO weather
console.log('updateInfo -> weather not implemented yet');
}
else {
activityLabel.text = today.adjusted.steps;
infoLabel.text = today.adjusted.steps;
}
}

Expand Down
54 changes: 54 additions & 0 deletions app/weather.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { inbox } from "file-transfer";
import { readFileSync } from "fs";

import { weatherFile, weatherType } from "../common/constants";

let data;
let handleCallback;

export function current() {
return data;
}

export function initialize(callback) {
handleCallback = callback;
data = loadData();
inbox.addEventListener("newfile", fileHandler);
fileHandler();
updatedData();
}

function fileHandler() {
let fileName;
do {
fileName = inbox.nextFile();
if (fileName === weatherFile) {
console.log('Load ' + fileName);
data = loadData();
updatedData();
}
} while (fileName);
}

function loadData() {
try {
return readFileSync(`/private/data/${weatherFile}`, weatherType);
} catch (ex) {
console.error(`loadData() failed. ${ex}`);
return;
}
}

function existsData() {
if (data === undefined) {
console.warn("No data found.");
return false;
}
return true;
}

function updatedData() {
if (typeof handleCallback === "function" && existsData()) {
handleCallback(data);
}
}
6 changes: 4 additions & 2 deletions common/constants.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export const dataType = "cbor";
export const dataFile = "appointments.cbor";
export const calendarType = "cbor";
export const calendarFile = "appointments.cbor";
export const settingsType = "cbor";
export const settingsFile = "settings.cbor";
export const weatherType = "cbor";
export const weatherFile = "weather.cbor";
export const millisecondsPerMinute = 1000 * 60;
20 changes: 8 additions & 12 deletions common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ export function zeroPad(i) {
return i;
}

// Convert celcius to fahrenheit
export function toFahrenheit(data) {
if (data.unit.toLowerCase() === "celsius") {
data.temperature = Math.round((data.temperature * 1.8) + 32.0);
data.unit = "Fahrenheit";
}
return data;
}
// Return day as a string
export function dayString(day) {
if (day == 1) {
Expand All @@ -20,18 +28,6 @@ export function dayString(day) {
else if (day == 3) {
return "3rd"
}
if (day == 21) {
return "21st"
}
else if (day == 22) {
return "22nd"
}
else if (day == 23) {
return "23rd"
}
if (day == 31) {
return "31st"
}
return day.toString() + "th";
}

Expand Down
Loading

0 comments on commit 9a4762d

Please sign in to comment.