slideshow/internal/slideshow.go

116 lines
3.2 KiB
Go

package internal
import (
"os/exec"
"strconv"
"sync"
"time"
"git.velvettear.de/velvettear/loggo"
"git.velvettear.de/velvettear/slideshow/internal/config"
)
var loop int64
// start the loop for the slideshow
func StartSlideshow() {
for {
timestamp := time.Now().UnixMilli()
loop++
imageLoop()
loggo.InfoTimed("slideshow loop #"+strconv.FormatInt(loop, 10)+" finished", timestamp)
loggo.Debug("sleeping for " + strconv.FormatFloat(config.Interval.Seconds(), 'f', 0, 64) + " seconds...")
time.Sleep(config.Interval)
}
}
// the image loop of the slideshoow
func imageLoop() {
name, error := requestImageName()
if error != nil {
loggo.Error("encountered an error requesting the name of an image", error.Error())
return
}
var waitgroup sync.WaitGroup
waitgroup.Add(2)
go setBackground(name, &waitgroup)
go exportColorPalette(name, &waitgroup)
waitgroup.Wait()
if !config.IsScriptStageSet() {
runScript(false)
}
}
// encapsulated method to set the background image in a goroutine
func setBackground(name string, waitgroup *sync.WaitGroup) {
timestamp := time.Now().UnixMilli()
defer waitgroup.Done()
if config.IsScriptStagePreImage() {
runScript(false)
}
stream, error := requestImageData(name)
if error != nil {
loggo.Error("encountered an error requesting the data stream for image '"+name+"'", error.Error())
return
}
error = streamToFeh(stream)
if error != nil {
loggo.Error("encountered an error piping the data stream for image '"+name+"' to 'feh'", error.Error())
}
loggo.InfoTimed("set new background image '"+name+"' via 'feh'", timestamp)
if config.IsScriptStagePostImage() {
runScript(false)
}
}
// encapsulated method to set the background image in a goroutine
func exportColorPalette(name string, waitgroup *sync.WaitGroup) {
timestamp := time.Now().UnixMilli()
if !config.IsPaletteSet() {
return
}
defer waitgroup.Done()
if config.IsScriptStagePrePalette() {
runScript(false)
}
stream, error := requestColorPalette(name)
if error != nil {
loggo.Error("encountered an error requesting the data stream for the color palette of image '"+name+"'", error.Error())
return
}
error = streamToFile(stream, config.PaletteFile)
if error != nil {
loggo.Error("encountered an error piping the data stream for the color palette of image '"+name+"' to file '"+config.PaletteFile+"'", error.Error())
}
loggo.InfoTimed("exported color palette of image '"+name+"' to '"+config.PaletteFile+"'", timestamp)
if config.IsScriptStagePostPalette() {
runScript(false)
}
}
// encapsulated method to run the defined script
func runScript(running bool) {
if !running && config.ScriptAsync {
go runScript(true)
return
}
timestamp := time.Now().UnixMilli()
if !config.IsScriptSet() {
return
}
cmd := exec.Command(config.Script, config.ScriptArgs...)
error := cmd.Run()
if error != nil {
loggo.Error("encountered an error executing the script '"+config.Script+"'", error.Error())
return
}
var extras []string
if config.ScriptAsync {
extras = append(extras, "async: "+strconv.FormatBool(config.ScriptAsync))
}
if config.IsScriptStageSet() {
extras = append(extras, "stage: "+config.ScriptStage)
}
loggo.InfoTimed("executed script '"+config.Script+"'", timestamp, extras...)
}