This commit is contained in:
Michael Lehmann
2025-02-18 22:55:03 +01:00
parent f34cf6da8b
commit 5399bc9a8a
8 changed files with 181 additions and 26 deletions

View File

@@ -16,7 +16,7 @@ func TurnOn(ip string, port string, temperature int, dimming int) {
panic("Unable to connect to light bulp!") 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) { func TurnOff(ip string, port string) {

View File

@@ -13,4 +13,5 @@ type BulbStatus struct {
type GroupStatus struct { type GroupStatus struct {
Name string Name string
Bulbs []BulbStatus Bulbs []BulbStatus
AllOn bool
} }

View File

@@ -31,10 +31,31 @@
"type": "github" "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": { "gitignore": {
"inputs": { "inputs": {
"nixpkgs": [ "nixpkgs": [
"pre-commit-hooks", "git-hooks",
"nixpkgs" "nixpkgs"
] ]
}, },
@@ -66,32 +87,14 @@
"type": "github" "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": { "root": {
"inputs": { "inputs": {
"devenv": "devenv", "devenv": "devenv",
"git-hooks": "git-hooks",
"nixpkgs": "nixpkgs", "nixpkgs": "nixpkgs",
"pre-commit-hooks": "pre-commit-hooks" "pre-commit-hooks": [
"git-hooks"
]
} }
} }
}, },

View File

@@ -13,6 +13,7 @@
# https://devenv.sh/languages/ # https://devenv.sh/languages/
languages.go.enable = true; languages.go.enable = true;
languages.go.enableHardeningWorkaround = true;
# https://devenv.sh/processes/ # https://devenv.sh/processes/
# processes.cargo-watch.exec = "cargo-watch"; # processes.cargo-watch.exec = "cargo-watch";

37
main.go
View File

@@ -27,6 +27,7 @@ func main() {
// Für jede Gruppe in den Konfigurationsdaten: Status der Bulbs abfragen. // Für jede Gruppe in den Konfigurationsdaten: Status der Bulbs abfragen.
for groupName, group := range config.Groups { for groupName, group := range config.Groups {
var bulbs []BulbStatus var bulbs []BulbStatus
allOn := true // Annahme: Alle Glühbirnen sind eingeschaltet
for _, bulp := range group.Bulps { for _, bulp := range group.Bulps {
// Abfrage des Status, hier wird angenommen, dass controller.GetStatus(ip, port) einen Status zurückgibt // Abfrage des Status, hier wird angenommen, dass controller.GetStatus(ip, port) einen Status zurückgibt
@@ -39,9 +40,13 @@ func main() {
if err != nil { if err != nil {
bulb.Message = fmt.Sprintf("Fehler: %v", err) bulb.Message = fmt.Sprintf("Fehler: %v", err)
allOn = false // Fehler: Nicht alle Glühbirnen sind eingeschaltet
} else { } else {
// Beispiel: Wir nutzen status.Result.State um zu bestimmen, ob das Licht an ist. // Beispiel: Wir nutzen status.Result.State um zu bestimmen, ob das Licht an ist.
bulb.IsOn = status.Result.State bulb.IsOn = status.Result.State
if !status.Result.State {
allOn = false // Mindestens eine Glühbirne ist ausgeschaltet
}
} }
bulbs = append(bulbs, bulb) bulbs = append(bulbs, bulb)
@@ -50,6 +55,7 @@ func main() {
groupStatus := GroupStatus{ groupStatus := GroupStatus{
Name: groupName, Name: groupName,
Bulbs: bulbs, Bulbs: bulbs,
AllOn: allOn, // Setze den Gesamtstatus der Gruppe
} }
groups = append(groups, groupStatus) 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 // static files
r.StaticFile("/styles.css", "static/styles.css") r.StaticFile("/styles.css", "static/styles.css")
r.StaticFile("/favicon.ico", "static/favicon.ico") r.StaticFile("/favicon.ico", "static/favicon.ico")

View File

@@ -2,7 +2,7 @@ defaults:
temperatur: 2700 temperatur: 2700
dimming: 100 dimming: 100
groups: groups:
wohnzimmer: alle:
bulps: bulps:
- ip: "192.168.0.151" - ip: "192.168.0.151"
name: "Wohnzimmer gross" name: "Wohnzimmer gross"
@@ -13,3 +13,16 @@ groups:
- ip: "192.168.0.153" - ip: "192.168.0.153"
name: "Wohnzimmer klein 2" name: "Wohnzimmer klein 2"
port: "38899" 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"
- ip: "192.168.0.153"
name: "Wohnzimmer klein 2"
port: "38899"

View File

@@ -160,3 +160,63 @@ footer {
flex-direction: column; 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);
}

View File

@@ -15,6 +15,39 @@
<link rel="stylesheet" href="/styles.css"> <link rel="stylesheet" href="/styles.css">
<!-- Optional: Favicon --> <!-- Optional: Favicon -->
<link rel="icon" href="/favicon.ico" type="image/x-icon"> <link rel="icon" href="/favicon.ico" type="image/x-icon">
<script>
document.addEventListener("DOMContentLoaded", function () {
document.querySelectorAll('.group .switch input').forEach(function (checkbox) {
checkbox.addEventListener('change', function () {
const groupName = this.closest('.group').querySelector('h2').textContent.trim();
const turnOn = this.checked;
fetch('/toggle-group', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
groupName: groupName,
turnOn: turnOn
})
})
.then(response => response.json())
.then(data => {
if (data.error) {
alert(data.error);
} else {
// Optional: Seite neu laden oder Status aktualisieren
location.reload();
}
})
.catch(error => {
console.error('Fehler:', error);
});
});
});
});
</script>
</head> </head>
<body> <body>
@@ -32,12 +65,19 @@
<img src="/room.png" alt="Raum Symbol" style="width:24px;height:24px;vertical-align:middle;" /> <img src="/room.png" alt="Raum Symbol" style="width:24px;height:24px;vertical-align:middle;" />
{{.Name}} {{.Name}}
</h2> </h2>
<!-- Toggle-Button -->
<label class="switch">
<input type="checkbox" {{if .AllOn}}checked{{end}}>
<span class="slider"></span>
</label>
<ul> <ul>
{{/* Iteriere über die Bulbs innerhalb der aktuellen Gruppe */}} {{/* Iteriere über die Bulbs innerhalb der aktuellen Gruppe */}}
{{range .Bulbs}} {{range .Bulbs}}
<li> <li>
<!-- IP und Port anzeigen --> <!-- IP und Port anzeigen -->
Name: {{.Name}} {{.Name}}
{{if .Message}} {{if .Message}}
Fehler: {{.Message}} Fehler: {{.Message}}
{{else}} {{else}}