Beispiel: Dashboard
In diesem Beispiel wird gezeigt, wie man ein Dashboard erstellt. In diesem Dashboard wird der Status von Lampen, Meldern und Sensoren in einer Liste dargestellt. Die Gerätenamen und Gruppenadressen müssen für das jeweilige ETS Projekt noch angepasst werden.
Nach dem Start des Skripts ist das Dashboard unter der URL der HomeKit Brücke für KNX und dem Port 8081 verfügbar – zB http://hkknx.local:8081
1var hkknx = import("hkknx")
2var template = import("html/template")
3var http = import("net/http")
4
5// lightbulbs references readable group addresses.
6// - *1* if the light is turned on
7// - *0* if the light is turned off
8lightbulbs = map[string]string{
9 "WC": "0/0/2",
10 "Büro": "0/0/2",
11}
12
13// motionSensors references readable group addresses.
14// - *1* if the sensor detects motion
15// - *0* if the sensor detects no motion
16motionSensors = map[string]string{
17 "Eingang": "0/1/2",
18 "Wohnzimmer": "0/1/2",
19 "Esszimmer": "0/1/2",
20}
21
22// lightLevels references readable group addresses which return
23// DPT 7.013 encoded light level values.
24lightLevels = map[string]string{
25 "Eingang": "0/1/1",
26 "Carport": "0/1/1",
27}
28
29// currents references readable group addresses which return
30// DPT 7.012 encoded current values.
31currents = map[string]string{
32 "Waschmaschine": "0/0/3",
33 "Trockner": "0/0/3",
34 "Poolpumpe": "0/0/3",
35}
36
37tmpl, err = template.New("foo").Parse(`
38<html>
39<head>
40 <meta name="viewport" content="width=device-width, initial-scale=1">
41 <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
42 <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.5.0/font/bootstrap-icons.css">
43 <script>setInterval(function() { window.location.reload(true); }, 30000);</script>
44</head>
45<body class="bg-light">
46<div class="container py-5">
47 <div class="row">
48 <div class="col-12">
49 <h1>Dashboard</h1>
50 </div>
51 {{ range $key, $value := . }}
52 <div class="col-12 col-md-6 col-lg-4 col-xl-3">
53 <div class="small text-uppercase text-secondary p-1 pt-3">{{ $key }}</div>
54 <ul class="list-group">
55 {{ range $value }}
56 <li class="list-group-item"><i class="{{.Icon}}"></i> {{ .Name }}<span class="float-end {{ if .Highlight }} text-primary {{ else }} text-secondary {{ end }}">{{ .Value }}</span></li>
57 {{ end }}
58 </ul>
59 </div>
60 {{ end }}
61 </div>
62</div>
63</body>
64</html>`)
65
66if err != nil {
67 return err
68}
69
70fetchData = func() {
71 onOffString = func(a) {
72 if a { return "Ein" }
73 return "Aus"
74 }
75 motionString = func(a) {
76 if a { return "Erkannt" }
77 return "Bereit"
78 }
79 data = map[string]interface{}
80 addItem = func(name, value, valueStr, typee, section) {
81 item = make(struct {
82 Name string,
83 Value string,
84 Highlight bool,
85 Icon string
86 })
87 item.Name = name
88 item.Value = valueStr
89 item.Highlight = value != 0
90 item.Icon = "bi-" + typee
91 items = data[section]
92 if items == nil {
93 items = []
94 }
95 data[section] = items + item
96 }
97
98 for key, value in lightbulbs {
99 buf, err = hkknx.GroupRead(value)
100 value = hkknx.ParseDPT1(buf)
101 if err == nil { addItem(key, value, onOffString(value), "lightbulb", "Licht") }
102 }
103
104 for key, value in motionSensors {
105 buf, err = hkknx.GroupRead(value)
106 value = hkknx.ParseDPT1(buf)
107 if err == nil { addItem(key, value, motionString(hkknx.ParseDPT1(buf)), "person", "Melder") }
108 }
109
110 for key, value in lightLevels {
111 buf, err = hkknx.GroupRead(value)
112 value = hkknx.ParseDPT7013(buf)
113 if err == nil { addItem(key, value, hkknx.DPT7013(value).String(), "sun", "Helligkeit") }
114 }
115
116 for key, value in currents {
117 buf, err = hkknx.GroupRead(value)
118 value = hkknx.ParseDPT7012(buf)
119 if err == nil { addItem(key, value, hkknx.DPT7012(value).String(), "lightning", "Strom") }
120 }
121
122 return data
123}
124
125// mux dispatches incoming requests to handlers
126mux = http.NewServeMux()
127
128// register the read handler
129mux.HandleFunc("/", func(w, r) {
130 data = fetchData()
131 err = tmpl.Execute(w, data)
132 if err != nil {
133 println(err)
134 }
135})
136
137// run the server
138http.ListenAndServe(":8081", mux)