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

Will update schedule more often #31

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
62 changes: 38 additions & 24 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ import (
"github.com/TheCacophonyProject/audiobait/playlist"
)

const maxRetries = 4
const retryInterval = 30 * time.Second
const (
maxRetries = 4
retryInterval = 30 * time.Second
updateScheduleInterval = time.Minute * 100
)

var errNoSchedule = errors.New("no audio schedule for device, or no sounds to play in schedule")

// version is populated at link time via goreleaser
var version = "No version provided"
Expand Down Expand Up @@ -76,8 +81,11 @@ func runMain() error {

soundsDownloaded := false
for i := 1; i <= maxRetries; i++ {
err = DownloadAndPlaySounds(conf.AudioDir, soundCard)
if err != nil {
if err := DownloadAndPlaySounds(conf.AudioDir, soundCard); err == errNoSchedule {
log.Println(err)
log.Printf("waiting %s until updateing scheduel", updateScheduleInterval)
Copy link
Member

Choose a reason for hiding this comment

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

updating schedule

time.Sleep(updateScheduleInterval)
} else if err != nil {
log.Println("Error dowloading sounds and schedule:", err)
if i < maxRetries {
log.Println("Trying again in", retryInterval)
Expand All @@ -91,35 +99,41 @@ func runMain() error {
}

if !soundsDownloaded {
log.Println("Could not download sounds and schedule.")
log.Println("Could not download sounds and schedule. Will try again tomorrow")
}

// Wait until tomorrow.
playlist.WaitUntilNextDay()

}

}

func DownloadAndPlaySounds(audioDir string, soundCard playlist.AudioDevice) error {
downloader, err := NewDownloader(audioDir)
if err != nil {
return err
}
for {
downloader, err := NewDownloader(audioDir)
if err != nil {
return err
}

schedule := downloader.GetTodaysSchedule()
if len(schedule.Combos) == 0 {
return errors.New("No audio schedule for device, or no sounds to play in schedule.")
}
schedule := downloader.GetTodaysSchedule()
if len(schedule.Combos) == 0 {
return errNoSchedule
}

files, err := downloader.GetFilesForSchedule(schedule)
if err != nil {
return err
}
files, err := downloader.GetFilesForSchedule(schedule)
if err != nil {
return err
}

player := playlist.NewPlayer(soundCard, files, audioDir)
player.SetRecorder(AudioBaitEventRecorder{})
waitTime := player.TimeUntilNextCombo(schedule.Combos)

log.Printf("Playing todays audiobait schedule...")
player := playlist.NewPlayer(soundCard, files, audioDir)
player.SetRecorder(AudioBaitEventRecorder{})
player.PlayTodaysSchedule(schedule)
return nil
if waitTime > updateScheduleInterval {
time.Sleep(updateScheduleInterval)
Copy link
Member

Choose a reason for hiding this comment

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

how about just returning errNoSchedule here so that this sleep only happens in one place. You probably want to rename errNoSchedule to errTryLater or something (and change the error text)

} else {
log.Printf("Playing todays audiobait schedule...")
player.PlayTodaysSchedule(schedule)
return nil
}
}
}
5 changes: 5 additions & 0 deletions playlist/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ func (sp SchedulePlayer) createWindow(combo Combo) *window.Window {
return win
}

func (sp SchedulePlayer) TimeUntilNextCombo(combos []Combo) time.Duration {
count := sp.findNextCombo(combos)
return sp.createWindow(combos[count]).Until()
}

// playSounds plays the sounds for a combo.
func (sp SchedulePlayer) playSounds(combo Combo, chooser *SoundChooser) {
log.Print("Starting sound burst")
Expand Down