feat: add color delta checker to limit requests

This commit is contained in:
Simon Vieille 2025-12-07 11:10:32 +01:00
commit 7f7d60ac3c
Signed by: deblan
GPG key ID: 579388D585F70417
6 changed files with 42 additions and 7 deletions

View file

@ -5,9 +5,9 @@ import (
"os"
"time"
"gitnet.fr/deblan/ha-rgb-screen/internal/color"
"gitnet.fr/deblan/ha-rgb-screen/internal/config"
"gitnet.fr/deblan/ha-rgb-screen/internal/ha"
"gitnet.fr/deblan/ha-rgb-screen/internal/img"
"gitnet.fr/deblan/ha-rgb-screen/internal/screen"
)
@ -21,14 +21,23 @@ func main() {
s := screen.NewScreen(params)
client := ha.NewClient(params)
var lastValue color.RGB
for {
capture, err := s.Capture()
if err == nil {
if err := client.Update(img.GetRgbAverage(capture)); err != nil {
log.Println("Error: %s", err.Error())
newValue := color.GetRgbAverage(capture)
delta := color.Delta(newValue, lastValue)
if delta > params.Delta {
lastValue = newValue
if err := client.Update(newValue); err != nil && !params.Debug {
log.Printf("Error: %s\n", err.Error())
}
}
} else {
log.Fatalf("Fatal error: %s", err.Error())
}

1
go.mod
View file

@ -8,6 +8,7 @@ require (
github.com/gen2brain/shm v0.1.0 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/jezek/xgb v1.1.1 // indirect
github.com/lucasb-eyer/go-colorful v1.3.0 // indirect
github.com/lxn/win v0.0.0-20210218163916-a377121e959e // indirect
golang.org/x/sys v0.24.0 // indirect
)

2
go.sum
View file

@ -6,6 +6,8 @@ github.com/jezek/xgb v1.1.1 h1:bE/r8ZZtSv7l9gk6nU0mYx51aXrvnyb44892TwSaqS4=
github.com/jezek/xgb v1.1.1/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk=
github.com/kbinani/screenshot v0.0.0-20250624051815-089614a94018 h1:NQYgMY188uWrS+E/7xMVpydsI48PMHcc7SfR4OxkDF4=
github.com/kbinani/screenshot v0.0.0-20250624051815-089614a94018/go.mod h1:Pmpz2BLf55auQZ67u3rvyI2vAQvNetkK/4zYUmpauZQ=
github.com/lucasb-eyer/go-colorful v1.3.0 h1:2/yBRLdWBZKrf7gB40FoiKfAWYQ0lqNcbuQwVHXptag=
github.com/lucasb-eyer/go-colorful v1.3.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/lxn/win v0.0.0-20210218163916-a377121e959e h1:H+t6A/QJMbhCSEH5rAuRxh+CtW96g0Or0Fxa9IKr4uc=
github.com/lxn/win v0.0.0-20210218163916-a377121e959e/go.mod h1:KxxjdtRkfNoYDCUP5ryK7XJJNTnpC8atvtmTheChOtk=
golang.org/x/sys v0.0.0-20201018230417-eeed37f84f13/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

View file

@ -1,6 +1,16 @@
package img
package color
import "image"
import (
"image"
"github.com/lucasb-eyer/go-colorful"
)
type XYZ struct {
X float64
Y float64
Z float64
}
type RGB struct {
R float64
@ -8,6 +18,13 @@ type RGB struct {
B float64
}
func Delta(color1, color2 RGB) float64 {
c1 := colorful.Color{color1.R, color1.G, color1.B} // valeurs en [0..1], donc 1.0 = 255
c2 := colorful.Color{color2.R, color2.G, color2.B} // valeurs en [0..1], donc 1.0 = 255
return c1.DistanceCIE76(c2)
}
func GetRgbAverage(i *image.RGBA) RGB {
bounds := i.Bounds()

View file

@ -6,7 +6,9 @@ type Config struct {
Url string
Delay int
Screen int
Delta float64
Verbose bool
Debug bool
Usage func()
}
@ -22,7 +24,9 @@ func GetConfig() *Config {
url := flag.String("url", "", "Webhook URL")
delay := flag.Int("delay", 500, "Delay in ms")
screen := flag.Int("screen", 0, "Screen index")
delta := flag.Float64("delta", 7.0, "Delta")
verbose := flag.Bool("v", false, "Verbose mode")
debug := flag.Bool("debug", false, "Debug mode (no http request)")
flag.Parse()
@ -30,7 +34,9 @@ func GetConfig() *Config {
Url: *url,
Delay: *delay,
Screen: *screen,
Delta: *delta,
Verbose: *verbose,
Debug: *debug,
Usage: flag.Usage,
}
}

View file

@ -6,8 +6,8 @@ import (
"log"
"net/http"
"gitnet.fr/deblan/ha-rgb-screen/internal/color"
"gitnet.fr/deblan/ha-rgb-screen/internal/config"
"gitnet.fr/deblan/ha-rgb-screen/internal/img"
)
type Client struct {
@ -22,7 +22,7 @@ func NewClient(params *config.Config) *Client {
}
}
func (c *Client) Update(rgb img.RGB) error {
func (c *Client) Update(rgb color.RGB) error {
data := fmt.Sprintf(`{"rgb": [%.0f, %.0f, %.0f]}`, rgb.R, rgb.G, rgb.B)
if c.params.Verbose {