Commit 0a666206 authored by Simon Kirsten's avatar Simon Kirsten
Browse files

Changed parameter passing for /tv/state

parent 8950e991
......@@ -2,8 +2,10 @@ package api
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"strconv"
"github.com/JamesStewy/sse"
......@@ -14,8 +16,8 @@ import (
// tvState struct defines the state of the TV.
type tvState struct {
LargeChannel *string `json:"large_channel"`
SmallChannel *string `json:"small_channel"`
LargeChannel string `json:"large_channel"`
SmallChannel string `json:"small_channel"`
Volume float32 `json:"volume"`
SmallScale float32 `json:"small_scale"`
ShowChat bool `json:"show_chat"`
......@@ -24,13 +26,31 @@ type tvState struct {
// state is the actual current state of the TV.
// It is initialized with default values.
var state = tvState{
LargeChannel: nil,
SmallChannel: nil,
LargeChannel: "",
SmallChannel: "",
Volume: 0.5,
SmallScale: 0.3,
ShowChat: false,
}
// getQueryParam is a helper function to get an param from url.Values while making sure the param is present and has only a single value.
func getQueryParam(v url.Values, param string) (value string, ok bool, err error) {
values, ok := v[param]
if !ok {
return
}
valuesCount := len(values)
if valuesCount == 1 {
value = values[0]
return
}
err = fmt.Errorf("Got %d values for parameter '%s' but expected only one: %s", valuesCount, param, values)
return
}
// clients holds the clients that are connected to the event handler. It is used to broadcast state changes to all SSE (Server-Sent Events) clients.
// Note: the only reason we use a sse.Client => bool map is that we can call *delete* with the client as key. The actual bool value that is stored holds no significance whatsoever.
// This is basically a *set* in go.
......@@ -56,53 +76,65 @@ func stateHandleFunc(w http.ResponseWriter, r *http.Request) {
// we save the error messages of the 3 parsing steps here.
var errs error
if q := query.Get("large_channel"); q != "" {
if q == "null" {
newState.LargeChannel = nil
if value, ok, err := getQueryParam(query, "large_channel"); ok {
if err != nil {
errs = multierror.Append(err)
} else {
newState.LargeChannel = &q
newState.LargeChannel = value
}
}
if q := query.Get("small_channel"); q != "" {
if q == "null" {
newState.SmallChannel = nil
if value, ok, err := getQueryParam(query, "small_channel"); ok {
if err != nil {
errs = multierror.Append(err)
} else {
newState.SmallChannel = &q
newState.SmallChannel = value
}
}
if q := query.Get("show_chat"); q != "" {
newShowChat, err := strconv.ParseBool(q)
if value, ok, err := getQueryParam(query, "volume"); ok {
if err != nil {
errs = multierror.Append(errs, err)
errs = multierror.Append(err)
} else {
newState.ShowChat = newShowChat
newVolume, err := strconv.ParseFloat(value, 32)
if err != nil {
errs = multierror.Append(errs, err)
} else {
newState.Volume = float32(newVolume)
}
}
}
if q := query.Get("volume"); q != "" {
newVolume, err := strconv.ParseFloat(q, 32)
if value, ok, err := getQueryParam(query, "small_scale"); ok {
if err != nil {
errs = multierror.Append(errs, err)
errs = multierror.Append(err)
} else {
newState.Volume = float32(newVolume)
newSmallScale, err := strconv.ParseFloat(value, 32)
if err != nil {
errs = multierror.Append(errs, err)
} else {
newState.SmallScale = float32(newSmallScale)
}
}
}
if q := query.Get("small_scale"); q != "" {
newSmallScale, err := strconv.ParseFloat(q, 32)
if value, ok, err := getQueryParam(query, "show_chat"); ok {
if err != nil {
errs = multierror.Append(errs, err)
errs = multierror.Append(err)
} else {
newState.SmallScale = float32(newSmallScale)
newShowChat, err := strconv.ParseBool(value)
if err != nil {
errs = multierror.Append(errs, err)
} else {
newState.ShowChat = newShowChat
}
}
}
if errs != nil { // we had errors
http.Error(w, errs.Error(), http.StatusBadRequest)
log.Printf("Error(s) while parsing update query: %v\n", errs)
log.Printf("Error(s) while parsing /tv/state query: %v\n", errs)
return
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment