-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sunrise: Add sunrise module to pixlet. (#124)
* sunrise: Add sunrise module to pixlet. This commit adds the go-sunrise module to pixlet so that apps can make use of sunrise/sunset times. This has been a popular use case and hopefully this module will make it easier for more folks to take advantage of this information. * Fix accidental change * sunrise: Add better support for no sunrise/sunset. This commit adds the None return type for an empty time zone and added it to the example.
- Loading branch information
1 parent
2129361
commit c978cea
Showing
7 changed files
with
226 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
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,65 @@ | ||
load("render.star", "render") | ||
load("schema.star", "schema") | ||
load("time.star", "time") | ||
load("encoding/json.star", "json") | ||
load("sunrise.star", "sunrise") | ||
|
||
DEFAULT_LOCATION = """ | ||
{ | ||
"lat": "40.6781784", | ||
"lng": "-73.9441579", | ||
"description": "Brooklyn, NY, USA", | ||
"locality": "Brooklyn", | ||
"place_id": "ChIJCSF8lBZEwokRhngABHRcdoI", | ||
"timezone": "America/New_York" | ||
} | ||
""" | ||
|
||
def main(config): | ||
location = config.get("location", DEFAULT_LOCATION) | ||
loc = json.decode(location) | ||
lat, lng = float(loc["lat"]), float(loc["lng"]) | ||
|
||
now = time.now() | ||
rise = sunrise.sunrise(lat, lng, now) | ||
set = sunrise.sunset(lat, lng, now) | ||
|
||
# Check if the sun does not rise or set today. This would happen if the | ||
# location of the deivce is close to the north or south pole where there are | ||
# many days of light or darkness. Maybe someone brought their Tidbyt to the | ||
# Amundsen-Scott South Pole Station! How cool would that be? | ||
if rise == None or set == None: | ||
return render.Root( | ||
child = render.Column( | ||
children = [ | ||
render.Text("Now: %s" % now.in_location(loc["timezone"]).format("3:04 PM")), | ||
render.Marquee( | ||
width = 64, | ||
child = render.Text("Sun doesn't rise or set today."), | ||
), | ||
], | ||
), | ||
) | ||
|
||
return render.Root( | ||
child = render.Column( | ||
children = [ | ||
render.Text("Now: %s" % now.in_location(loc["timezone"]).format("3:04 PM")), | ||
render.Text("Rise: %s" % rise.in_location(loc["timezone"]).format("3:04 PM")), | ||
render.Text("Set: %s" % set.in_location(loc["timezone"]).format("3:04 PM")), | ||
], | ||
), | ||
) | ||
|
||
def get_schema(): | ||
return schema.Schema( | ||
version = "1", | ||
fields = [ | ||
schema.Location( | ||
id = "location", | ||
name = "Location", | ||
desc = "Location for which to display time.", | ||
icon = "place", | ||
), | ||
], | ||
) |
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
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
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,94 @@ | ||
package sunrise | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
gosunrise "github.com/nathan-osman/go-sunrise" | ||
startime "go.starlark.net/lib/time" | ||
"go.starlark.net/starlark" | ||
"go.starlark.net/starlarkstruct" | ||
) | ||
|
||
const ( | ||
ModuleName = "sunrise" | ||
) | ||
|
||
var ( | ||
once sync.Once | ||
module starlark.StringDict | ||
empty time.Time | ||
) | ||
|
||
func LoadModule() (starlark.StringDict, error) { | ||
once.Do(func() { | ||
module = starlark.StringDict{ | ||
ModuleName: &starlarkstruct.Module{ | ||
Name: ModuleName, | ||
Members: starlark.StringDict{ | ||
"sunrise": starlark.NewBuiltin("sunrise", sunrise), | ||
"sunset": starlark.NewBuiltin("sunset", sunset), | ||
}, | ||
}, | ||
} | ||
}) | ||
|
||
return module, nil | ||
} | ||
|
||
func sunrise(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { | ||
var ( | ||
starLat starlark.Float | ||
starLng starlark.Float | ||
starDate startime.Time | ||
) | ||
|
||
if err := starlark.UnpackArgs( | ||
"sunrise", | ||
args, kwargs, | ||
"lat", &starLat, | ||
"lng", &starLng, | ||
"date", &starDate, | ||
); err != nil { | ||
return nil, fmt.Errorf("unpacking arguments for sunrise: %s", err) | ||
} | ||
|
||
lat := float64(starLat) | ||
lng := float64(starLng) | ||
date := time.Time(starDate) | ||
rise, _ := gosunrise.SunriseSunset(lat, lng, date.Year(), date.Month(), date.Day()) | ||
if rise == empty { | ||
return starlark.None, nil | ||
} | ||
|
||
return startime.Time(rise), nil | ||
} | ||
|
||
func sunset(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { | ||
var ( | ||
starLat starlark.Float | ||
starLng starlark.Float | ||
starDate startime.Time | ||
) | ||
|
||
if err := starlark.UnpackArgs( | ||
"sunset", | ||
args, kwargs, | ||
"lat", &starLat, | ||
"lng", &starLng, | ||
"date", &starDate, | ||
); err != nil { | ||
return nil, fmt.Errorf("unpacking arguments for sunset: %s", err) | ||
} | ||
|
||
lat := float64(starLat) | ||
lng := float64(starLng) | ||
date := time.Time(starDate) | ||
_, set := gosunrise.SunriseSunset(lat, lng, date.Year(), date.Month(), date.Day()) | ||
if set == empty { | ||
return starlark.None, nil | ||
} | ||
|
||
return startime.Time(set), nil | ||
} |
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,47 @@ | ||
package sunrise_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"tidbyt.dev/pixlet/runtime" | ||
) | ||
|
||
var sunSource = ` | ||
load("time.star", "time") | ||
load("sunrise.star", "sunrise") | ||
def assert(success, message=None): | ||
if not success: | ||
fail(message or "assertion failed") | ||
# Setup. | ||
format = "2006-01-02T15:04:05" | ||
input = time.parse_time("2022-01-15T22:40:24", format = format) | ||
expectedRise = time.parse_time("2022-01-15T12:17:29", format = format) | ||
expectedSet = time.parse_time("2022-01-15T21:52:30", format = format) | ||
lat = 40.6781784 | ||
lng = -73.9441579 | ||
# Call methods. | ||
rise = sunrise.sunrise(lat, lng, input) | ||
set = sunrise.sunset(lat, lng, input) | ||
# Assert. | ||
assert(rise == expectedRise) | ||
assert(set == expectedSet) | ||
def main(): | ||
return [] | ||
` | ||
|
||
func TestSunrise(t *testing.T) { | ||
app := &runtime.Applet{} | ||
err := app.Load("sun.star", []byte(sunSource), nil) | ||
assert.NoError(t, err) | ||
|
||
screens, err := app.Run(map[string]string{}) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, screens) | ||
} |
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