diff --git a/controller/controller.go b/controller/controller.go index 278663f..3e40773 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -16,7 +16,7 @@ func TurnOn(ip string, port string, temperature int, dimming int) { panic("Unable to connect to light bulp!") } - c.Write([]byte(`{"method": "setPilot", "params":{"state": true, "temp": temperature, "dimming": dimming}}`)) + c.Write([]byte(`{"method": "setPilot", "params":{"state": true, "temp": 2700, "dimming": 100}}`)) } func TurnOff(ip string, port string) { diff --git a/data.go b/data.go index dc08d3d..21f3195 100644 --- a/data.go +++ b/data.go @@ -13,4 +13,5 @@ type BulbStatus struct { type GroupStatus struct { Name string Bulbs []BulbStatus + AllOn bool } diff --git a/devenv.lock b/devenv.lock index eec00f3..982f184 100644 --- a/devenv.lock +++ b/devenv.lock @@ -31,10 +31,31 @@ "type": "github" } }, + "git-hooks": { + "inputs": { + "flake-compat": "flake-compat", + "gitignore": "gitignore", + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1737465171, + "owner": "cachix", + "repo": "git-hooks.nix", + "rev": "9364dc02281ce2d37a1f55b6e51f7c0f65a75f17", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "git-hooks.nix", + "type": "github" + } + }, "gitignore": { "inputs": { "nixpkgs": [ - "pre-commit-hooks", + "git-hooks", "nixpkgs" ] }, @@ -66,32 +87,14 @@ "type": "github" } }, - "pre-commit-hooks": { - "inputs": { - "flake-compat": "flake-compat", - "gitignore": "gitignore", - "nixpkgs": [ - "nixpkgs" - ] - }, - "locked": { - "lastModified": 1737465171, - "owner": "cachix", - "repo": "pre-commit-hooks.nix", - "rev": "9364dc02281ce2d37a1f55b6e51f7c0f65a75f17", - "type": "github" - }, - "original": { - "owner": "cachix", - "repo": "pre-commit-hooks.nix", - "type": "github" - } - }, "root": { "inputs": { "devenv": "devenv", + "git-hooks": "git-hooks", "nixpkgs": "nixpkgs", - "pre-commit-hooks": "pre-commit-hooks" + "pre-commit-hooks": [ + "git-hooks" + ] } } }, diff --git a/devenv.nix b/devenv.nix index cd841a1..6c09f3e 100644 --- a/devenv.nix +++ b/devenv.nix @@ -13,6 +13,7 @@ # https://devenv.sh/languages/ languages.go.enable = true; + languages.go.enableHardeningWorkaround = true; # https://devenv.sh/processes/ # processes.cargo-watch.exec = "cargo-watch"; diff --git a/main.go b/main.go index a5b35ed..3ce1ff8 100644 --- a/main.go +++ b/main.go @@ -27,6 +27,7 @@ func main() { // Für jede Gruppe in den Konfigurationsdaten: Status der Bulbs abfragen. for groupName, group := range config.Groups { var bulbs []BulbStatus + allOn := true // Annahme: Alle Glühbirnen sind eingeschaltet for _, bulp := range group.Bulps { // Abfrage des Status, hier wird angenommen, dass controller.GetStatus(ip, port) einen Status zurückgibt @@ -39,9 +40,13 @@ func main() { if err != nil { bulb.Message = fmt.Sprintf("Fehler: %v", err) + allOn = false // Fehler: Nicht alle Glühbirnen sind eingeschaltet } else { // Beispiel: Wir nutzen status.Result.State um zu bestimmen, ob das Licht an ist. bulb.IsOn = status.Result.State + if !status.Result.State { + allOn = false // Mindestens eine Glühbirne ist ausgeschaltet + } } bulbs = append(bulbs, bulb) @@ -50,6 +55,7 @@ func main() { groupStatus := GroupStatus{ Name: groupName, Bulbs: bulbs, + AllOn: allOn, // Setze den Gesamtstatus der Gruppe } groups = append(groups, groupStatus) } @@ -59,6 +65,37 @@ func main() { }) }) + r.POST( + "/toggle-group", + func(c *gin.Context) { + var request struct { + GroupName string `json:"groupName"` + TurnOn bool `json:"turnOn"` + } + + if err := c.BindJSON(&request); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + group, exists := config.Groups[request.GroupName] + if !exists { + c.JSON(http.StatusNotFound, gin.H{"error": "Gruppe nicht gefunden"}) + return + } + + for _, bulb := range group.Bulps { + if request.TurnOn { + controller.TurnOn(bulb.IP, bulb.Port, config.Defaults.Temperatur, config.Defaults.Dimming) + } else { + controller.TurnOff(bulb.IP, bulb.Port) + } + } + + c.JSON(http.StatusOK, gin.H{"message": "Gruppe erfolgreich geschaltet"}) + }, + ) + // static files r.StaticFile("/styles.css", "static/styles.css") r.StaticFile("/favicon.ico", "static/favicon.ico") diff --git a/misc/sample-config.yml b/misc/sample-config.yml index 3b2271e..25fe024 100644 --- a/misc/sample-config.yml +++ b/misc/sample-config.yml @@ -2,11 +2,24 @@ defaults: temperatur: 2700 dimming: 100 groups: - wohnzimmer: + alle: bulps: - ip: "192.168.0.151" name: "Wohnzimmer gross" port: "38899" + - ip: "192.168.0.152" + name: "Wohnzimmer klein 1" + port: "38899" + - ip: "192.168.0.153" + name: "Wohnzimmer klein 2" + port: "38899" + tisch: + bulps: + - ip: "192.168.0.151" + name: "Wohnzimmer gross" + port: "38899" + tv: + bulps: - ip: "192.168.0.152" name: "Wohnzimmer klein 1" port: "38899" diff --git a/static/styles.css b/static/styles.css index fd2365d..2032835 100644 --- a/static/styles.css +++ b/static/styles.css @@ -159,4 +159,64 @@ footer { .bulb-list { flex-direction: column; } +} + +/* The switch - the box around the slider */ +.switch { + position: relative; + display: inline-block; + width: 60px; + height: 34px; +} + +/* Hide default HTML checkbox */ +.switch input { + opacity: 0; + width: 0; + height: 0; +} + +.switch { + position: relative; + display: inline-block; + width: 60px; + height: 34px; +} + +.switch input { + opacity: 0; + width: 0; + height: 0; +} + +.slider { + position: absolute; + cursor: pointer; + top: 0; + left: 0; + right: 0; + bottom: 0; + background-color: #ccc; + transition: .4s; + border-radius: 34px; +} + +.slider:before { + position: absolute; + content: ""; + height: 26px; + width: 26px; + left: 4px; + bottom: 4px; + background-color: white; + transition: .4s; + border-radius: 50%; +} + +input:checked+.slider { + background-color: #2196F3; +} + +input:checked+.slider:before { + transform: translateX(26px); } \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index 9ce6179..f27ff9e 100644 --- a/templates/index.html +++ b/templates/index.html @@ -15,6 +15,39 @@ +
@@ -32,12 +65,19 @@
{{.Name}}
+
+
+
+