From 348cb6f42636d4fa1349e017052ed1fee179b362 Mon Sep 17 00:00:00 2001 From: Maximilian Richt Date: Thu, 24 Aug 2023 23:35:51 +0200 Subject: [PATCH] initial commit --- .gitignore | 1 + README.md | 6 +++ go.mod | 3 ++ main.go | 95 +++++++++++++++++++++++++++++++++++++++ static/style.css | 6 +++ templates/index.html.tmpl | 15 +++++++ 6 files changed, 126 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 go.mod create mode 100644 main.go create mode 100644 static/style.css create mode 100644 templates/index.html.tmpl diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f2d43de --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +matelight-publiccontrol diff --git a/README.md b/README.md new file mode 100644 index 0000000..b3d1af9 --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +matelight public control +======================== + +This is a simple web interface to control the [matelight](https://wiki.temporaerhaus.de/matelight) installation at the [temporaerhaus](https://temporaerhaus.de/) through the outside window. + +It requires the WLED host and the PORT it should run on. You can provide them as environment variables `WLED_HOST` and `PORT` or as command line arguments `--wled-host` and `--port`. \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..73e34f1 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/temporaerhaus/matelight-publiccontrol + +go 1.21.0 diff --git a/main.go b/main.go new file mode 100644 index 0000000..bf0d0ba --- /dev/null +++ b/main.go @@ -0,0 +1,95 @@ +package main + +import ( + "bytes" + "embed" + "encoding/json" + "flag" + "html/template" + "log" + "math/rand" + "net/http" + "os" +) + +//go:embed templates +var indexHTML embed.FS + +//go:embed static +var staticFiles embed.FS + +func main() { + port := flag.String("port", os.Getenv("PORT"), "port to serve on") + wledHost := flag.String("wled-host", os.Getenv("WLED_HOST"), "WLED host") + flag.Parse() + if *port == "" { + *port = "3000" + } + if *wledHost == "" { + log.Fatal("WLED_HOST is required") + } + + // Note the call to ParseFS instead of Parse + t, err := template.ParseFS(indexHTML, "templates/index.html.tmpl") + if err != nil { + log.Fatal(err) + } + + // http.FS can be used to create a http Filesystem + var staticFS = http.FS(staticFiles) + fs := http.FileServer(staticFS) + + // Serve static files + http.Handle("/static/", fs) + + // Handle only the root path + http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { + var path = req.URL.Path + if path != "/" { + w.WriteHeader(http.StatusNotFound) + return + } + + w.Header().Add("Content-Type", "text/html") + + // respond with the output of template execution + t.Execute(w, struct { + Title string + Response string + }{Title: "hello", Response: path}) + }) + http.HandleFunc("/random", func(w http.ResponseWriter, req *http.Request) { + effect := rand.Intn(5) + 1 + + obj := map[string]int{"ps": effect} + json, err := json.Marshal(obj) + if err != nil { + return + } + + // log the request + log.Println(string(json)) + + // post to wled + client := &http.Client{} + response, err := client.Post(*wledHost+"/json/state", "application/json", bytes.NewBuffer(json)) + if err != nil { + log.Println(err) + + w.Header().Add("Location", "/") + w.WriteHeader(http.StatusFound) + return + } + defer response.Body.Close() + + // redirect to home + w.Header().Add("Location", "/") + w.WriteHeader(http.StatusFound) + }) + + log.Println("Listening on port", *port) + err = http.ListenAndServe(":"+*port, nil) + if err != nil { + log.Fatal(err) + } +} diff --git a/static/style.css b/static/style.css new file mode 100644 index 0000000..cccb7e6 --- /dev/null +++ b/static/style.css @@ -0,0 +1,6 @@ +body { + font-family: sans-serif; +} +button { + font-size: 200%; +} \ No newline at end of file diff --git a/templates/index.html.tmpl b/templates/index.html.tmpl new file mode 100644 index 0000000..d8acf62 --- /dev/null +++ b/templates/index.html.tmpl @@ -0,0 +1,15 @@ + + + + +matelight + + + +

matelight

+

das matelight des temporärhaus besteht aus Kästen von Club-Mate, einer in Hack- und Makespaces verbreitete Mate-Tee-Limonade und darin verbauter LEDs, die über WLED angesteuert werden.

+
+ +
+ + \ No newline at end of file