-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 348cb6f
Showing
6 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
matelight-publiccontrol |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/temporaerhaus/matelight-publiccontrol | ||
|
||
go 1.21.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
body { | ||
font-family: sans-serif; | ||
} | ||
button { | ||
font-size: 200%; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |