106 lines
3 KiB
Go
106 lines
3 KiB
Go
|
package images
|
||
|
|
||
|
// import (
|
||
|
// "runtime"
|
||
|
// "strconv"
|
||
|
// "sync"
|
||
|
// "time"
|
||
|
|
||
|
// "git.velvettear.de/velvettear/image-frame/config"
|
||
|
// "git.velvettear.de/velvettear/loggo"
|
||
|
// )
|
||
|
|
||
|
// // cache for scaled images
|
||
|
// var cache []scaledImage
|
||
|
|
||
|
// // timestamp of the next cache rotation
|
||
|
// var NextRotation time.Time
|
||
|
|
||
|
// // get the previous image from the history and set it as the first scaled image in the cache
|
||
|
// func SetPreviousImage() error {
|
||
|
// previousImage, error := getLatestFromHistory()
|
||
|
// if error != nil {
|
||
|
// return error
|
||
|
// }
|
||
|
// var tmpCache []scaledImage
|
||
|
// tmpCache = append(tmpCache, previousImage)
|
||
|
// tmpCache = append(tmpCache, cache...)
|
||
|
// cache = tmpCache
|
||
|
// tmpCache = nil
|
||
|
// return nil
|
||
|
// }
|
||
|
|
||
|
// // get the first scaled image from the cache
|
||
|
// func GetCachedImage() scaledImage {
|
||
|
// return cache[0]
|
||
|
// }
|
||
|
|
||
|
// // replace the first element in the cache with the last one and add a new scaled image to the cache
|
||
|
// func RotateCache() {
|
||
|
// addToHistory(GetCachedImage())
|
||
|
// loggo.Debug("removing first element from image cache...")
|
||
|
// cacheSize := len(cache)
|
||
|
// if cacheSize == 1 {
|
||
|
// cache = nil
|
||
|
// cacheImages()
|
||
|
// return
|
||
|
// }
|
||
|
// cacheSize--
|
||
|
// cache[0] = cache[cacheSize]
|
||
|
// cache = cache[:cacheSize]
|
||
|
// go cacheImages()
|
||
|
// }
|
||
|
|
||
|
// // start the "slideshow" in a goroutine - rotate the cache based on the set interval
|
||
|
// func startSlideshow() {
|
||
|
// go func() {
|
||
|
// interval := time.Duration(config.GetImageSlideshowInterval()) * time.Second
|
||
|
// for {
|
||
|
// NextRotation = time.Now().Add(interval)
|
||
|
// time.Sleep(interval)
|
||
|
// RotateCache()
|
||
|
// }
|
||
|
// }()
|
||
|
// }
|
||
|
|
||
|
// // fill the cache with scaled images
|
||
|
// func cacheImages() {
|
||
|
// timestamp := time.Now().UnixMilli()
|
||
|
// imageCount := len(images)
|
||
|
// cacheSize := len(cache)
|
||
|
// cacheLimit := config.GetImageCache()
|
||
|
// if imageCount < cacheLimit {
|
||
|
// cacheLimit = imageCount
|
||
|
// }
|
||
|
// imagesToCache := cacheLimit - cacheSize
|
||
|
// if imagesToCache <= 0 {
|
||
|
// return
|
||
|
// }
|
||
|
// concurrency := runtime.NumCPU()
|
||
|
// if imagesToCache < concurrency {
|
||
|
// concurrency = imagesToCache
|
||
|
// }
|
||
|
// loggo.Debug("filling image cache with "+strconv.Itoa(imagesToCache)+" element(s)", "concurrency: "+strconv.Itoa(concurrency))
|
||
|
// var waitgroup sync.WaitGroup
|
||
|
// waitgroup.Add(imagesToCache)
|
||
|
// channel := make(chan struct{}, concurrency)
|
||
|
// var cached int
|
||
|
// for cached = 0; cached < imagesToCache; cached++ {
|
||
|
// channel <- struct{}{}
|
||
|
// var randomScaledImage scaledImage
|
||
|
// randomScaledImage.Name = getRandomImage()
|
||
|
// if randomScaledImage.isCached() {
|
||
|
// continue
|
||
|
// }
|
||
|
// go func(randomScaledImage scaledImage) {
|
||
|
// randomScaledImage.Data = scale(randomScaledImage.Name, config.GetImageWidth(), config.GetImageHeight())
|
||
|
// cache = append(cache, randomScaledImage)
|
||
|
// loggo.Debug("added scaled image '" + randomScaledImage.Name + "' to cache")
|
||
|
// <-channel
|
||
|
// waitgroup.Done()
|
||
|
// }(randomScaledImage)
|
||
|
// }
|
||
|
// waitgroup.Wait()
|
||
|
// loggo.DebugTimed("filled image cache with "+strconv.Itoa(cached)+" images", timestamp)
|
||
|
// }
|