Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add toggle to optionally skip taking photos at night #60

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package conf
const (
DefaultListenAddress = ":8080"
DefaultLogToFile = false
DefaultSkipPhotosAtNight = false
DefaultSecondsBetweenCaptures = 1800 // 30min
DefaultOffsetWithinHour = 900 // 15min
MaxFileSizeBytes = 100485760 // 100 MB
Expand Down
2 changes: 2 additions & 0 deletions conf/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var initialConfiguration = Settings{
RotateBy: 0,
ResolutionSetting: 0,
Quality: 100,
SkipPhotosAtNight: DefaultSkipPhotosAtNight,
}

type Settings struct {
Expand All @@ -39,6 +40,7 @@ type Settings struct {
ResolutionSetting int
Quality int
DebugEnabled bool
SkipPhotosAtNight bool
}

func (s Settings) String() string {
Expand Down
2 changes: 1 addition & 1 deletion conf/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ func TestAreSettingsMissing(t *testing.T) {
}

func TestSettingsToString(t *testing.T) {
expected := `{"SecondsBetweenCaptures":0,"OffsetWithinHour":0,"PhotoResolutionWidth":0,"PhotoResolutionHeight":0,"PreviewResolutionWidth":0,"PreviewResolutionHeight":0,"RotateBy":0,"ResolutionSetting":0,"Quality":0,"DebugEnabled":false}`
expected := `{"SecondsBetweenCaptures":0,"OffsetWithinHour":0,"PhotoResolutionWidth":0,"PhotoResolutionHeight":0,"PreviewResolutionWidth":0,"PreviewResolutionHeight":0,"RotateBy":0,"ResolutionSetting":0,"Quality":0,"DebugEnabled":false,"SkipPhotosAtNight":false}`
ensure.DeepEqual(t, expected, Settings{}.String())
}
5 changes: 5 additions & 0 deletions timelapse/camera.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,8 @@ func getFileName(t time.Time) string {
func (c *Camera) getAbsoluteFilepath() string {
return filepath.Join(c.savePath, getFileName(time.Now()))
}

func isNightPhoto(now time.Time) bool {
h := now.Hour()
return h > 22 || h < 6
Comment on lines +84 to +86
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to make this configurable and surface the cfg from the web front end

}
13 changes: 13 additions & 0 deletions timelapse/camera_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,16 @@ func TestCreateFileName(t *testing.T) {
ensure.DeepEqual(t, getFileName(time.Date(1974, time.May, 19, 1, 2, 3, 0, time.UTC)), "19740519-010203.jpg")
ensure.DeepEqual(t, getFileName(time.Date(1974, time.December, 19, 1, 2, 3, 4, time.UTC)), "19741219-010203.jpg")
}

func TestIsNightTime(t *testing.T) {
tenPM := time.Date(2000, time.January, 1, 22, 0, 0, 0, time.UTC)
elevenPM := time.Date(2000, time.January, 1, 23, 0, 0, 0, time.UTC)
fourAM := time.Date(2000, time.January, 1, 4, 0, 0, 0, time.UTC)
sixAM := time.Date(2000, time.January, 1, 6, 0, 0, 0, time.UTC)

ensure.True(t, isNightPhoto(elevenPM))
ensure.True(t, isNightPhoto(fourAM))

ensure.False(t, isNightPhoto(tenPM))
ensure.False(t, isNightPhoto(sixAM))
}
34 changes: 21 additions & 13 deletions timelapse/timelapse.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@ func (t Timelapse) CapturePeriodically() {
log.Printf("Error instantiating camera: %s\n", err)
// Sleep for a bit and create a new camera instance on the next iteration.
} else {
s, err := camera.Capture()
if err != nil {
log.Printf("Error during capture: %s\n", err.Error())
if t.Settings.SkipPhotosAtNight && isNightPhoto(time.Now()) {
log.Printf("Skipping night time photo\n")
} else {
log.Printf("Photo stored in '%s'\n", s)
s, err := camera.Capture()
if err != nil {
log.Printf("Error during capture: %s\n", err.Error())
} else {
log.Printf("Photo stored in '%s'\n", s)
}
log.Printf("Sleeping for %d seconds.\n", t.Settings.SecondsBetweenCaptures)
}
log.Printf("Sleeping for %d seconds.\n", t.Settings.SecondsBetweenCaptures)
}
time.Sleep(time.Duration(t.Settings.SecondsBetweenCaptures) * time.Second)
}
Expand All @@ -62,15 +66,19 @@ func (t Timelapse) CapturePeriodically() {
log.Printf("Error instantiating camera: %s\n", err)
// Sleep for a bit and create a new camera instance on the next iteration.
} else {
photoPath, err := camera.Capture()
if err != nil {
log.Printf("Error during capture: %s\n", err.Error())

// Sleep for 1s after an error to ensure time changed sufficiently before next invocation of WaitForCapture
time.Sleep(time.Duration(1 * time.Second))
continue
if t.Settings.SkipPhotosAtNight && isNightPhoto(time.Now()) {
log.Printf("Skipping night time photo\n")
} else {
photoPath, err := camera.Capture()
if err != nil {
log.Printf("Error during capture: %s\n", err.Error())

// Sleep for 1s after an error to ensure time changed sufficiently before next invocation of WaitForCapture
time.Sleep(time.Duration(1 * time.Second))
continue
}
log.Printf("Photo stored in '%s'\n", photoPath)
}
log.Printf("Photo stored in '%s'\n", photoPath)
}
timeToCaptureSeconds := time.Now().Unix() - beforeCapture.Unix()
log.Printf("Capture took %d seconds\n", timeToCaptureSeconds)
Expand Down
Loading