wip
This commit is contained in:
@@ -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) {
|
||||
|
||||
1
data.go
1
data.go
@@ -13,4 +13,5 @@ type BulbStatus struct {
|
||||
type GroupStatus struct {
|
||||
Name string
|
||||
Bulbs []BulbStatus
|
||||
AllOn bool
|
||||
}
|
||||
|
||||
49
devenv.lock
49
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"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
@@ -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";
|
||||
|
||||
37
main.go
37
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")
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -15,6 +15,39 @@
|
||||
<link rel="stylesheet" href="/styles.css">
|
||||
<!-- Optional: Favicon -->
|
||||
<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>
|
||||
|
||||
<body>
|
||||
@@ -32,12 +65,19 @@
|
||||
<img src="/room.png" alt="Raum Symbol" style="width:24px;height:24px;vertical-align:middle;" />
|
||||
{{.Name}}
|
||||
</h2>
|
||||
|
||||
<!-- Toggle-Button -->
|
||||
<label class="switch">
|
||||
<input type="checkbox" {{if .AllOn}}checked{{end}}>
|
||||
<span class="slider"></span>
|
||||
</label>
|
||||
|
||||
<ul>
|
||||
{{/* Iteriere über die Bulbs innerhalb der aktuellen Gruppe */}}
|
||||
{{range .Bulbs}}
|
||||
<li>
|
||||
<!-- IP und Port anzeigen -->
|
||||
Name: {{.Name}}
|
||||
{{.Name}}
|
||||
{{if .Message}}
|
||||
Fehler: {{.Message}}
|
||||
{{else}}
|
||||
|
||||
Reference in New Issue
Block a user