2020-05-16 18:31:25 +00:00
|
|
|
package webserver
|
|
|
|
|
|
|
|
import (
|
2020-05-19 10:48:24 +00:00
|
|
|
queue "git.martyn.berlin/martyn/LEDController/internal/queue"
|
2020-05-16 18:31:25 +00:00
|
|
|
"github.com/gorilla/handlers"
|
|
|
|
"github.com/gorilla/mux"
|
2020-05-17 12:25:46 +00:00
|
|
|
"golang.org/x/image/colornames"
|
2020-05-16 18:31:25 +00:00
|
|
|
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
2020-05-17 15:03:26 +00:00
|
|
|
"strings"
|
2020-05-19 10:48:24 +00:00
|
|
|
"time"
|
2020-05-16 18:31:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func HealthHandler(response http.ResponseWriter, request *http.Request) {
|
|
|
|
response.Header().Add("Content-type", "text/plain")
|
|
|
|
fmt.Fprint(response, "I'm okay jack!")
|
|
|
|
}
|
|
|
|
|
|
|
|
func NotFoundHandler(response http.ResponseWriter, request *http.Request) {
|
|
|
|
response.WriteHeader(404)
|
|
|
|
fmt.Fprint(response, "I'm lost!")
|
|
|
|
}
|
|
|
|
|
|
|
|
func RootHandler(response http.ResponseWriter, request *http.Request) {
|
|
|
|
response.Header().Add("Content-type", "text/plain")
|
|
|
|
fmt.Fprint(response, "Not implemented")
|
|
|
|
}
|
|
|
|
|
2020-05-19 10:48:24 +00:00
|
|
|
var globalQueue chan queue.QueueItem
|
2020-06-28 16:36:33 +00:00
|
|
|
var overrideFlag chan queue.QueueItem
|
2020-05-16 18:31:25 +00:00
|
|
|
|
|
|
|
func PatternHandler(response http.ResponseWriter, request *http.Request) {
|
|
|
|
vars := mux.Vars(request)
|
|
|
|
response.Header().Add("Content-type", "text/plain")
|
|
|
|
var e queue.QueueItem
|
2020-05-17 15:03:26 +00:00
|
|
|
e.Effect = strings.ToLower(vars["pattern"])
|
2020-05-17 12:25:46 +00:00
|
|
|
_, found := vars["duration"]
|
|
|
|
if found {
|
|
|
|
i, _ := strconv.Atoi(vars["duration"])
|
|
|
|
e.Duration = uint16(i)
|
|
|
|
} else {
|
|
|
|
e.Duration = 5000
|
|
|
|
}
|
|
|
|
_, found = vars["speed"]
|
2020-05-16 18:31:25 +00:00
|
|
|
if found {
|
|
|
|
i, _ := strconv.Atoi(vars["speed"])
|
|
|
|
e.Speed = uint16(i)
|
|
|
|
} else {
|
|
|
|
e.Speed = 40
|
|
|
|
}
|
2020-06-28 16:36:33 +00:00
|
|
|
if vars["override"] == "true" {
|
|
|
|
overrideFlag <- e
|
|
|
|
} else {
|
|
|
|
globalQueue <- e
|
|
|
|
}
|
2020-05-16 18:31:25 +00:00
|
|
|
fmt.Fprint(response, "OKAY")
|
|
|
|
}
|
|
|
|
|
2020-05-17 12:25:46 +00:00
|
|
|
func ColourHandler(response http.ResponseWriter, request *http.Request) {
|
|
|
|
vars := mux.Vars(request)
|
2020-06-28 16:36:33 +00:00
|
|
|
fmt.Printf("Vars : %v\n", vars)
|
2020-05-17 12:25:46 +00:00
|
|
|
response.Header().Add("Content-type", "text/plain")
|
|
|
|
var e queue.QueueItem
|
|
|
|
e.Effect = "colour"
|
|
|
|
_, found := vars["duration"]
|
|
|
|
if found {
|
|
|
|
i, _ := strconv.Atoi(vars["duration"])
|
|
|
|
e.Duration = uint16(i)
|
|
|
|
} else {
|
|
|
|
e.Duration = 5000
|
|
|
|
}
|
|
|
|
var c queue.RGBcolor
|
2020-05-19 10:48:24 +00:00
|
|
|
for cn, cv := range colornames.Map {
|
2020-05-17 15:03:26 +00:00
|
|
|
if cn == strings.ToLower(vars["name"]) {
|
2020-05-17 12:25:46 +00:00
|
|
|
c[0] = cv.R
|
|
|
|
c[1] = cv.G
|
|
|
|
c[2] = cv.B
|
|
|
|
}
|
|
|
|
}
|
|
|
|
e.SeedColour = c
|
|
|
|
globalQueue <- e
|
2020-06-28 16:36:33 +00:00
|
|
|
if vars["override"] == "true" {
|
|
|
|
overrideFlag <- e
|
|
|
|
} else {
|
|
|
|
globalQueue <- e
|
|
|
|
}
|
2020-05-17 12:25:46 +00:00
|
|
|
fmt.Fprint(response, "OKAY")
|
|
|
|
}
|
|
|
|
|
|
|
|
func FadeHandler(response http.ResponseWriter, request *http.Request) {
|
|
|
|
vars := mux.Vars(request)
|
|
|
|
response.Header().Add("Content-type", "text/plain")
|
|
|
|
var e queue.QueueItem
|
|
|
|
e.Effect = "fade"
|
|
|
|
_, found := vars["duration"]
|
|
|
|
if found {
|
|
|
|
i, _ := strconv.Atoi(vars["duration"])
|
|
|
|
e.Duration = uint16(i)
|
|
|
|
} else {
|
|
|
|
e.Duration = 5000
|
|
|
|
}
|
|
|
|
var c queue.RGBcolor
|
2020-05-19 10:48:24 +00:00
|
|
|
for cn, cv := range colornames.Map {
|
2020-05-17 15:03:26 +00:00
|
|
|
if cn == strings.ToLower(vars["namefrom"]) {
|
2020-05-17 12:25:46 +00:00
|
|
|
c[0] = cv.R
|
|
|
|
c[1] = cv.G
|
|
|
|
c[2] = cv.B
|
|
|
|
}
|
|
|
|
}
|
|
|
|
e.SeedColour = c
|
2020-05-19 10:48:24 +00:00
|
|
|
for cn, cv := range colornames.Map {
|
2020-05-17 15:03:26 +00:00
|
|
|
if cn == strings.ToLower(vars["nameto"]) {
|
2020-05-17 12:25:46 +00:00
|
|
|
c[0] = cv.R
|
|
|
|
c[1] = cv.G
|
|
|
|
c[2] = cv.B
|
|
|
|
}
|
|
|
|
}
|
|
|
|
e.SecondColour = c
|
2020-06-28 16:36:33 +00:00
|
|
|
if vars["override"] == "true" {
|
|
|
|
overrideFlag <- e
|
|
|
|
} else {
|
|
|
|
globalQueue <- e
|
|
|
|
}
|
|
|
|
fmt.Fprint(response, "OKAY")
|
|
|
|
}
|
|
|
|
|
|
|
|
func ResumeHandler(response http.ResponseWriter, request *http.Request) {
|
|
|
|
response.Header().Add("Content-type", "text/plain")
|
|
|
|
var e queue.QueueItem
|
|
|
|
e.Effect = "queue"
|
|
|
|
overrideFlag <- e
|
2020-05-17 12:25:46 +00:00
|
|
|
fmt.Fprint(response, "OKAY")
|
|
|
|
}
|
|
|
|
|
2020-06-28 16:36:33 +00:00
|
|
|
func HandleHTTP(queueChannel chan queue.QueueItem, Port int, overrideChannel chan queue.QueueItem) {
|
2020-05-16 18:31:25 +00:00
|
|
|
globalQueue = queueChannel
|
2020-06-28 16:36:33 +00:00
|
|
|
overrideFlag = overrideChannel
|
2020-05-16 18:31:25 +00:00
|
|
|
r := mux.NewRouter()
|
|
|
|
loggedRouter := handlers.LoggingHandler(os.Stdout, r)
|
|
|
|
r.NotFoundHandler = http.HandlerFunc(NotFoundHandler)
|
|
|
|
r.HandleFunc("/", RootHandler)
|
|
|
|
r.HandleFunc("/healthz", HealthHandler)
|
2020-05-17 12:25:46 +00:00
|
|
|
r.HandleFunc("/pattern/{pattern}", PatternHandler)
|
2020-05-16 18:31:25 +00:00
|
|
|
r.HandleFunc("/pattern/{pattern}/{duration}", PatternHandler)
|
|
|
|
r.HandleFunc("/pattern/{pattern}/{duration}/{speed}", PatternHandler)
|
2020-05-17 12:25:46 +00:00
|
|
|
r.HandleFunc("/colour/{name}", ColourHandler)
|
2020-06-28 16:36:33 +00:00
|
|
|
r.HandleFunc("/colour/{name}/override={override}", ColourHandler)
|
2020-05-17 12:25:46 +00:00
|
|
|
r.HandleFunc("/colour/{name}/{duration}", ColourHandler)
|
|
|
|
r.HandleFunc("/color/{name}", ColourHandler)
|
2020-06-28 16:36:33 +00:00
|
|
|
r.HandleFunc("/color/{name}/override={override}", ColourHandler)
|
2020-05-17 12:25:46 +00:00
|
|
|
r.HandleFunc("/color/{name}/{duration}", ColourHandler)
|
|
|
|
r.HandleFunc("/fade/{namefrom}/{nameto}", FadeHandler)
|
2020-06-28 16:36:33 +00:00
|
|
|
r.HandleFunc("/fade/{namefrom}/{nameto}/override={override}", FadeHandler)
|
2020-05-17 12:25:46 +00:00
|
|
|
r.HandleFunc("/fade/{namefrom}/{nameto}/{duration}", FadeHandler)
|
2020-06-28 16:36:33 +00:00
|
|
|
r.HandleFunc("/resumequeue", ResumeHandler)
|
2020-05-16 18:31:25 +00:00
|
|
|
http.Handle("/", r)
|
|
|
|
srv := &http.Server{
|
|
|
|
Handler: loggedRouter,
|
2020-05-19 10:48:24 +00:00
|
|
|
Addr: "0.0.0.0:" + strconv.Itoa(Port),
|
2020-05-16 18:31:25 +00:00
|
|
|
WriteTimeout: 15 * time.Second,
|
|
|
|
ReadTimeout: 15 * time.Second,
|
|
|
|
}
|
2020-05-17 15:03:26 +00:00
|
|
|
fmt.Printf("Listening on 0.0.0.0:%d", Port)
|
2020-05-16 18:31:25 +00:00
|
|
|
srv.ListenAndServe()
|
|
|
|
}
|