Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
robbi5 committed Aug 24, 2023
0 parents commit 348cb6f
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
matelight-publiccontrol
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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`.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/temporaerhaus/matelight-publiccontrol

go 1.21.0
95 changes: 95 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
6 changes: 6 additions & 0 deletions static/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
body {
font-family: sans-serif;
}
button {
font-size: 200%;
}
15 changes: 15 additions & 0 deletions templates/index.html.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>matelight</title>
<link rel="stylesheet" href="/static/style.css" />
</head>
<body>
<h1>matelight</h1>
<p>das <em>matelight</em> des <a href="https://temporaerhaus.de">temporärhaus</a> besteht aus Kästen von <a href="https://de.wikipedia.org/wiki/Club-Mate">Club-Mate</a>, einer in <a href="https://de.wikipedia.org/wiki/Hackerspace">Hack- und Makespaces</a> verbreitete <a href="https://de.wikipedia.org/wiki/Mate-Tee">Mate-Tee</a>-Limonade und darin verbauter LEDs, die über <a href="https://wled.me">WLED</a> angesteuert werden.</p>
<form action="/random" method="post">
<button type="submit">Anderer Effekt!</button>
</form>
</body>
</html>

0 comments on commit 348cb6f

Please sign in to comment.