reformatted json clock
parent
eebfc56b95
commit
81755f82e8
|
|
@ -12,17 +12,17 @@ var prevVals = {};
|
||||||
let drawTimeout;
|
let drawTimeout;
|
||||||
|
|
||||||
var settings = {
|
var settings = {
|
||||||
hr_12: global_settings["12hour"] !== undefined ? global_settings["12hour"] : false,
|
hr_12: global_settings["12hour"] !== undefined ? global_settings["12hour"] : false,
|
||||||
dark_mode: g.theme.dark
|
dark_mode: g.theme.dark
|
||||||
};
|
};
|
||||||
|
|
||||||
let clrs = {
|
let clrs = {
|
||||||
tab : "#505050", // grey
|
tab: "#505050", // grey
|
||||||
keys: settings.dark_mode ? "#4287f5" : "#0000FF", // blue
|
keys: settings.dark_mode ? "#4287f5" : "#0000FF", // blue
|
||||||
strings: settings.dark_mode ? "#F0A000" : "#FF0000", // orange or red
|
strings: settings.dark_mode ? "#F0A000" : "#FF0000", // orange or red
|
||||||
ints: settings.dark_mode ? "#00FF00" : "#005F00", // green
|
ints: settings.dark_mode ? "#00FF00" : "#005F00", // green
|
||||||
bg: g.theme.bg,
|
bg: g.theme.bg,
|
||||||
brackets: g.theme.fg,
|
brackets: g.theme.fg,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -43,222 +43,226 @@ var numWidth = 0;
|
||||||
|
|
||||||
// requires the myLocation app
|
// requires the myLocation app
|
||||||
function loadLocation() {
|
function loadLocation() {
|
||||||
location = require("Storage").readJSON(LOCATION_FILE,1)||{};
|
location = require("Storage").readJSON(LOCATION_FILE, 1) || {};
|
||||||
location.lat = location.lat||0;
|
location.lat = location.lat || 0;
|
||||||
location.lon = location.lon||0;
|
location.lon = location.lon || 0;
|
||||||
location.location = location.location||null;
|
location.location = location.location || null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getHr(h) {
|
function getHr(h) {
|
||||||
var amPm = "";
|
var amPm = "";
|
||||||
if (settings.hr_12) {
|
if (settings.hr_12) {
|
||||||
amPm = h < 12 ? "AM" : "PM";
|
amPm = h < 12 ? "AM" : "PM";
|
||||||
h = h % 12;
|
h = h % 12;
|
||||||
if (h == 0) h = 12;
|
if (h == 0) h = 12;
|
||||||
}
|
}
|
||||||
return [h, amPm];
|
return [h, amPm];
|
||||||
}
|
}
|
||||||
|
|
||||||
function extractTime(d){
|
function extractTime(d) {
|
||||||
const out = getHr(d.getHours());
|
const out = getHr(d.getHours());
|
||||||
const hr = out[0]; const amPm = out[1];
|
const hr = out[0];
|
||||||
|
const amPm = out[1];
|
||||||
const m = d.getMinutes();
|
const m = d.getMinutes();
|
||||||
return `${h}:${("0"+m).substr(-2)}${amPm}`;
|
return `${h}:${("0"+m).substr(-2)}${amPm}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function extractDate(d) {
|
function extractDate(d) {
|
||||||
const weekdays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
const weekdays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
||||||
const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
||||||
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
|
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
|
||||||
|
];
|
||||||
|
|
||||||
const weekday = weekdays[d.getDay()];
|
const weekday = weekdays[d.getDay()];
|
||||||
const month = months[d.getMonth()];
|
const month = months[d.getMonth()];
|
||||||
const day = d.getDate();
|
const day = d.getDate();
|
||||||
|
|
||||||
return `${weekday} ${month} ${day}`;
|
return `${weekday} ${month} ${day}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSteps() {
|
function getSteps() {
|
||||||
try {
|
try {
|
||||||
return Bangle.getHealthStatus("day").steps;
|
return Bangle.getHealthStatus("day").steps;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (WIDGETS.wpedom !== undefined)
|
if (WIDGETS.wpedom !== undefined)
|
||||||
return WIDGETS.wpedom.getSteps();
|
return WIDGETS.wpedom.getSteps();
|
||||||
else
|
else
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getVal(now, loc) {
|
function getVal(now, loc) {
|
||||||
const vals = {};
|
const vals = {};
|
||||||
const currentDateStr = extractDate(now);
|
const currentDateStr = extractDate(now);
|
||||||
if (loc.location) {
|
if (loc.location) {
|
||||||
if (lastSunCalcDate !== currentDateStr) {
|
if (lastSunCalcDate !== currentDateStr) {
|
||||||
cachedSunTimes = SunCalc.getTimes(now, location.lat, location.lon);
|
cachedSunTimes = SunCalc.getTimes(now, location.lat, location.lon);
|
||||||
lastSunCalcDate = currentDateStr;
|
lastSunCalcDate = currentDateStr;
|
||||||
}
|
}
|
||||||
vals.rise = extractTime(cachedSunTimes.sunrise);
|
vals.rise = extractTime(cachedSunTimes.sunrise);
|
||||||
vals.set = extractTime(cachedSunTimes.sunset);
|
vals.set = extractTime(cachedSunTimes.sunset);
|
||||||
}
|
}
|
||||||
vals.time = extractTime(now);
|
vals.time = extractTime(now);
|
||||||
vals.date = currentDateStr;
|
vals.date = currentDateStr;
|
||||||
vals.batt_pct = E.getBattery();
|
vals.batt_pct = E.getBattery();
|
||||||
vals.steps = getSteps();
|
vals.steps = getSteps();
|
||||||
return vals;
|
return vals;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function loadJson() {
|
function loadJson() {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
vals = getVal(now, location);
|
vals = getVal(now, location);
|
||||||
//vals.steps = null; // For testing; uncomment to see the steps not appear
|
//vals.steps = null; // For testing; uncomment to see the steps not appear
|
||||||
//location.location = null; // For testing, if null, the time becomes an struct to take up sun's struct
|
//location.location = null; // For testing, if null, the time becomes an struct to take up sun's struct
|
||||||
let raw;
|
let raw;
|
||||||
|
|
||||||
if (location.location !== null) {
|
if (location.location !== null) {
|
||||||
raw = {
|
raw = {
|
||||||
time: vals.time,
|
time: vals.time,
|
||||||
dt: vals.date,
|
dt: vals.date,
|
||||||
sun: {
|
sun: {
|
||||||
rise: vals.rise,
|
rise: vals.rise,
|
||||||
set: vals.set,
|
set: vals.set,
|
||||||
},
|
},
|
||||||
"batt_%": vals.batt_pct,
|
"batt_%": vals.batt_pct,
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
raw = {
|
raw = {
|
||||||
time: {
|
time: {
|
||||||
hr: getHr(now.getHours())[0],
|
hr: getHr(now.getHours())[0],
|
||||||
min: now.getMinutes(),
|
min: now.getMinutes(),
|
||||||
},
|
},
|
||||||
dt: vals.date,
|
dt: vals.date,
|
||||||
"batt_%": vals.batt_pct,
|
"batt_%": vals.batt_pct,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (vals.steps != null) raw.steps = vals.steps;
|
if (vals.steps != null) raw.steps = vals.steps;
|
||||||
|
|
||||||
jsonText = JSON.stringify(raw, null, 2); // just stringify the object
|
jsonText = JSON.stringify(raw, null, 2); // just stringify the object
|
||||||
lines = jsonText.split("\n");
|
lines = jsonText.split("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
function draw() {
|
function draw() {
|
||||||
g.clear();
|
g.clear();
|
||||||
g.setFontAlign(-1, -1);
|
g.setFontAlign(-1, -1);
|
||||||
g.setFont("Vector", 10);
|
g.setFont("Vector", 10);
|
||||||
valuePositions = [];
|
valuePositions = [];
|
||||||
|
|
||||||
g.setColor(clrs.tab);
|
g.setColor(clrs.tab);
|
||||||
|
|
||||||
g.fillRect(90, 0, w, headerHeight);
|
|
||||||
loadJson();
|
|
||||||
g.setColor(clrs.brackets);
|
|
||||||
g.drawString("clockface.json",3,3);
|
|
||||||
|
|
||||||
g.setFont("Vector", buttonHeight);
|
g.fillRect(90, 0, w, headerHeight);
|
||||||
g.drawString("X",buttonX,buttonY);
|
loadJson();
|
||||||
g.setFont("Vector", fontSize);
|
g.setColor(clrs.brackets);
|
||||||
|
g.drawString("clockface.json", 3, 3);
|
||||||
|
|
||||||
for (let i = 0; i < maxLines; i++) {
|
g.setFont("Vector", buttonHeight);
|
||||||
const y = headerHeight + i * lineHeight;
|
g.drawString("X", buttonX, buttonY);
|
||||||
const lineNumberStr = (i + 1).toString().padStart(2, " ") + " ";
|
g.setFont("Vector", fontSize);
|
||||||
g.drawString(lineNumberStr, 0, y);
|
|
||||||
numWidth = Math.max(numWidth, g.stringWidth(lineNumberStr));
|
|
||||||
}
|
|
||||||
|
|
||||||
redraw();
|
for (let i = 0; i < maxLines; i++) {
|
||||||
|
const y = headerHeight + i * lineHeight;
|
||||||
|
const lineNumberStr = (i + 1).toString().padStart(2, " ") + " ";
|
||||||
|
g.drawString(lineNumberStr, 0, y);
|
||||||
|
numWidth = Math.max(numWidth, g.stringWidth(lineNumberStr));
|
||||||
|
}
|
||||||
|
|
||||||
|
redraw();
|
||||||
}
|
}
|
||||||
|
|
||||||
function redraw() {
|
function redraw() {
|
||||||
for (let i = 0; i < maxLines; i++) {
|
for (let i = 0; i < maxLines; i++) {
|
||||||
const lineIndex = i;
|
const lineIndex = i;
|
||||||
const line = lines[lineIndex];
|
const line = lines[lineIndex];
|
||||||
if (!line) continue;
|
if (!line) continue;
|
||||||
const y = headerHeight + i * lineHeight;
|
const y = headerHeight + i * lineHeight;
|
||||||
|
|
||||||
const indentMatch = line.match(/^(\s*)/);
|
const indentMatch = line.match(/^(\s*)/);
|
||||||
const indent = indentMatch ? indentMatch[1] : "";
|
const indent = indentMatch ? indentMatch[1] : "";
|
||||||
|
|
||||||
const kvMatch = line.trim().match(/^"([^"]+)":\s*(.+)$/);
|
const kvMatch = line.trim().match(/^"([^"]+)":\s*(.+)$/);
|
||||||
if (kvMatch) {
|
if (kvMatch) {
|
||||||
const key = kvMatch[1];
|
const key = kvMatch[1];
|
||||||
let value = kvMatch[2];
|
let value = kvMatch[2];
|
||||||
|
|
||||||
if (prevVals.key == value) continue;
|
if (prevVals.key == value) continue;
|
||||||
prevVals.key = value;
|
prevVals.key = value;
|
||||||
|
|
||||||
// Key
|
// Key
|
||||||
g.setColor(clrs.keys);
|
g.setColor(clrs.keys);
|
||||||
g.drawString(indent + `"${key}"`, numWidth, y);
|
g.drawString(indent + `"${key}"`, numWidth, y);
|
||||||
const keyWidth = g.stringWidth(indent + `"${key}"`);
|
const keyWidth = g.stringWidth(indent + `"${key}"`);
|
||||||
const valueX = numWidth + keyWidth;
|
const valueX = numWidth + keyWidth;
|
||||||
const valueText = ": " + value;
|
const valueText = ": " + value;
|
||||||
|
|
||||||
// Value color
|
// Value color
|
||||||
if (value.startsWith('"')) {
|
if (value.startsWith('"')) {
|
||||||
g.setColor(clrs.strings);
|
g.setColor(clrs.strings);
|
||||||
}
|
} else if (value.startsWith('{') || value.startsWith('}')) {
|
||||||
else if (value.startsWith('{') || value.startsWith('}')) {
|
g.setColor(clrs.brackets);
|
||||||
g.setColor(clrs.brackets);
|
} else {
|
||||||
}
|
g.setColor(clrs.ints);
|
||||||
else {
|
}
|
||||||
g.setColor(clrs.ints);
|
g.drawString(valueText, valueX, y);
|
||||||
}
|
|
||||||
g.drawString(valueText, valueX, y);
|
|
||||||
|
|
||||||
valuePositions.push({ key, x: valueX, y, text: value });
|
valuePositions.push({
|
||||||
|
key,
|
||||||
|
x: valueX,
|
||||||
|
y,
|
||||||
|
text: value
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
g.setColor(clrs.brackets);
|
||||||
|
g.drawString(line, numWidth, y);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
Bangle.drawWidgets();
|
||||||
g.setColor(clrs.brackets);
|
|
||||||
g.drawString(line, numWidth, y);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Bangle.drawWidgets();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function clearVals() {
|
function clearVals() {
|
||||||
g.setFont("Vector", fontSize);
|
g.setFont("Vector", fontSize);
|
||||||
g.setFontAlign(-1, -1);
|
g.setFontAlign(-1, -1);
|
||||||
valuePositions.forEach(pos => {
|
valuePositions.forEach(pos => {
|
||||||
g.setColor(clrs.bg);
|
g.setColor(clrs.bg);
|
||||||
g.fillRect(pos.x, pos.y, w, pos.y + lineHeight);
|
g.fillRect(pos.x, pos.y, w, pos.y + lineHeight);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function redrawValues(){
|
function redrawValues() {
|
||||||
loadJson();
|
loadJson();
|
||||||
clearVals();
|
clearVals();
|
||||||
redraw();
|
redraw();
|
||||||
if (drawTimeout) clearTimeout(drawTimeout);
|
if (drawTimeout) clearTimeout(drawTimeout);
|
||||||
drawTimeout = setTimeout(function() {
|
drawTimeout = setTimeout(function() {
|
||||||
drawTimeout = undefined;
|
drawTimeout = undefined;
|
||||||
redrawValues();
|
redrawValues();
|
||||||
}, 60000 - (Date.now() % 60000));
|
}, 60000 - (Date.now() % 60000));
|
||||||
}
|
}
|
||||||
|
|
||||||
Bangle.on('touch', (zone, e) => {
|
Bangle.on('touch', (zone, e) => {
|
||||||
if (e.x >= (buttonY - buttonHeight) && e.x <= (buttonX + buttonHeight)
|
if (e.x >= (buttonY - buttonHeight) && e.x <= (buttonX + buttonHeight) &&
|
||||||
&& (e.y >= (buttonY - buttonHeight) && e.y <= (buttonY + buttonHeight))) {
|
(e.y >= (buttonY - buttonHeight) && e.y <= (buttonY + buttonHeight))) {
|
||||||
load(); // Exit app
|
load(); // Exit app
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Bangle.on('backlight', function(on) {
|
Bangle.on('backlight', function(on) {
|
||||||
if (on) {
|
if (on) {
|
||||||
redrawValues(); // or just draw() if you want full re-render
|
redrawValues(); // or just draw() if you want full re-render
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Bangle.setUI({
|
Bangle.setUI({
|
||||||
mode: "clock",
|
mode: "clock",
|
||||||
remove: function () {
|
remove: function() {
|
||||||
if (drawTimeout) clearTimeout(drawTimeout);
|
if (drawTimeout) clearTimeout(drawTimeout);
|
||||||
drawTimeout = undefined;
|
drawTimeout = undefined;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
loadLocation();
|
loadLocation();
|
||||||
Bangle.loadWidgets();
|
Bangle.loadWidgets();
|
||||||
widget_utils.hide();
|
widget_utils.hide();
|
||||||
draw();
|
draw();
|
||||||
setTimeout(Bangle.drawWidgets,0);
|
setTimeout(Bangle.drawWidgets, 0);
|
||||||
Loading…
Reference in New Issue