Skip to content

Commit

Permalink
Use 'modules/suncalc.js' to avoid it being copied 8 times for differe…
Browse files Browse the repository at this point in the history
…nt apps

+      Add a 'time' clockinfo that also displays a percentage of day left
  • Loading branch information
gfwilliams committed Dec 9, 2022
1 parent 7a2af0e commit a0d3d15
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 15 deletions.
2 changes: 2 additions & 0 deletions apps/clkinfosunrise/ChangeLog
@@ -1 +1,3 @@
0.01: New App!
0.02: Use 'modules/suncalc.js' to avoid it being copied 8 times for different apps
Add a 'time' clockinfo that also displays a percentage of day left
64 changes: 50 additions & 14 deletions apps/clkinfosunrise/clkinfo.js
@@ -1,32 +1,68 @@
(function() {
// get today's sunlight times for lat/lon
var sunrise, sunset;
var sunrise, sunset, date;
var SunCalc = require("suncalc"); // from modules folder
const locale = require("locale");

function calculate() {
var SunCalc = require("https://raw.githubusercontent.com/mourner/suncalc/master/suncalc.js");
const locale = require("locale");
var location = require("Storage").readJSON("mylocation.json",1)||{};
location.lat = location.lat||51.5072;
location.lon = location.lon||0.1276;
location.location = location.location||"London";
var times = SunCalc.getTimes(new Date(), location.lat, location.lon);
sunrise = locale.time(times.sunrise,1);
sunset = locale.time(times.sunset,1);
location.lon = location.lon||0.1276; // London
date = new Date(Date.now());
var times = SunCalc.getTimes(date, location.lat, location.lon);
sunrise = times.sunrise;
sunset = times.sunset;
/* do we want to re-calculate this every day? Or we just assume
that 'show' will get called once a day? */
}

function show() {
this.interval = setTimeout(()=>{
this.emit("redraw");
this.interval = setInterval(()=>{
this.emit("redraw");
}, 60000);
}, 60000 - (Date.now() % 60000));
}
function hide() {
clearInterval(this.interval);
this.interval = undefined;
}

return {
name: "Bangle",
items: [
{ name : "Sunrise",
get : () => ({ text : sunrise,
img : atob("GBiBAAAAAAAAAAAAAAAYAAA8AAB+AAD/AAAAAAAAAAAAAAAYAAAYAAQYIA4AcAYAYAA8AAB+AAD/AAH/gD///D///AAAAAAAAAAAAA==") }),
show : calculate, hide : () => {}
get : () => { calculate();
return { text : locale.time(sunrise,1),
img : atob("GBiBAAAAAAAAAAAAAAAYAAA8AAB+AAD/AAAAAAAAAAAAAAAYAAAYAAQYIA4AcAYAYAA8AAB+AAD/AAH/gD///D///AAAAAAAAAAAAA==") }},
show : show, hide : hide
}, { name : "Sunset",
get : () => ({ text : sunset,
img : atob("GBiBAAAAAAAAAAAAAAB+AAA8AAAYAAAYAAAAAAAAAAAAAAAYAAAYAAQYIA4AcAYAYAA8AAB+AAD/AAH/gD///D///AAAAAAAAAAAAA==") }),
show : calculate, hide : () => {}
get : () => { calculate();
return { text : locale.time(sunset,1),
img : atob("GBiBAAAAAAAAAAAAAAB+AAA8AAAYAAAYAAAAAAAAAAAAAAAYAAAYAAQYIA4AcAYAYAA8AAB+AAD/AAH/gD///D///AAAAAAAAAAAAA==") }},
show : show, hide : hide
}, { name : "Time", // Time in day (uses v/min/max to show percentage through day)
get : () => {
calculate();
let day = true;
let v = 0;
let d = date.getTime();
let dayLength = sunset.getTime()-sunrise.getTime();
if (d < sunrise.getTime()) {
day = false; // early morning
v = 100 - Math.round(100 * (sunrise.getTime()-d) / (86400000-dayLength));
} else if (d > sunset.getTime()) {
day = false; // evening
v = Math.round(100 * (d-sunset.getTime()) / (86400000-dayLength));
} else { // day!
v = Math.round(100 * (d-sunrise.getTime()) / dayLength);
}
return { text : locale.time(date,1), v : v, min : 0, max : 100,
img : day ? atob("GBiBAAAYAAAYAAAYAAgAEBwAOAx+MAD/AAH/gAP/wAf/4Af/4Of/5+f/5wf/4Af/4AP/wAH/gAD/AAx+MBwAOAgAEAAYAAAYAAAYAA==") : atob("GBiBAAfwAA/8AAP/AAH/gAD/wAB/wAB/4AA/8AA/8AA/8AAf8AAf8AAf8AAf8AA/8AA/8AA/4AB/4AB/wAD/wAH/gAf/AA/8AAfwAA==")
}
},
show : show, hide : hide
}
]
};
Expand Down
2 changes: 1 addition & 1 deletion apps/clkinfosunrise/metadata.json
@@ -1,6 +1,6 @@
{ "id": "clkinfosunrise",
"name": "Sunrise Clockinfo",
"version":"0.01",
"version":"0.02",
"description": "For clocks that display 'clockinfo' (messages that can be cycled through using the clock_info module) this displays sunrise and sunset based on the location from the 'My Location' app",
"icon": "app.png",
"type": "clkinfo",
Expand Down

0 comments on commit a0d3d15

Please sign in to comment.