diff --git a/src/device/input/button/parser_test.go b/src/device/input/button/parser_test.go index 55adf25..af5db09 100644 --- a/src/device/input/button/parser_test.go +++ b/src/device/input/button/parser_test.go @@ -7,5 +7,5 @@ import ( func TestParser(t *testing.T) { var _ button.Parser = button.Plaintext{} - var _ button.Parser = button.V01{} + var _ button.Parser = &button.V01{} } diff --git a/src/device/input/button/v01.go b/src/device/input/button/v01.go index 4732ae0..8f8c059 100644 --- a/src/device/input/button/v01.go +++ b/src/device/input/button/v01.go @@ -3,6 +3,7 @@ package button import ( "context" "encoding/json" + "io" "io/ioutil" "log" "mayhem-party/src/device/input/raw" @@ -46,12 +47,12 @@ type ( v01Transformation map[string]string ) -func NewV01(ctx context.Context, src raw.Raw) V01 { +func NewV01(ctx context.Context, src raw.Raw) *V01 { var cfg v01Cfg b, _ := ioutil.ReadFile(FlagButtonV01Config) yaml.Unmarshal(b, &cfg) ctx, can := context.WithCancel(ctx) - result := V01{ + result := &V01{ ctx: ctx, can: can, src: src, @@ -61,19 +62,26 @@ func NewV01(ctx context.Context, src raw.Raw) V01 { return result } -func (v01 V01) listen() { +func (v01 *V01) listen() { if v01.cfg.Feedback.Addr == "" { return } s := &http.Server{ Addr: v01.cfg.Feedback.Addr, Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - r = r.WithContext(v01.ctx) - user, ok := v01.cfg.Users[r.URL.Query().Get("user")] - if !ok { - user = v01.cfg.Users["broadcast"] + if r.Method == http.MethodGet { + r = r.WithContext(v01.ctx) + user, ok := v01.cfg.Users[r.URL.Query().Get("user")] + if !ok { + user = v01.cfg.Users["broadcast"] + } + w.Write([]byte(user.Message)) + } else if r.URL.Path == "/broadcast" { + b, _ := io.ReadAll(r.Body) + v := v01.cfg.Users["broadcast"] + v.Message = string(b) + v01.cfg.Users["broadcast"] = v } - w.Write([]byte(user.Message)) }), } go func() { @@ -88,17 +96,17 @@ func (v01 V01) listen() { } } -func (v01 V01) CloseWrap() raw.Raw { +func (v01 *V01) CloseWrap() raw.Raw { v01.can() return v01.src } -func (v01 V01) Close() { +func (v01 *V01) Close() { v01.can() v01.src.Close() } -func (v01 V01) Read() []Button { +func (v01 *V01) Read() []Button { line := v01.src.Read() var msg v01Msg if err := json.Unmarshal(line, &msg); err != nil { @@ -134,7 +142,7 @@ func (t v01Transformation) pipe(s string) string { return s } -func (v01 V01) telemetry(msg v01Msg) { +func (v01 *V01) telemetry(msg v01Msg) { if FlagDebug { log.Printf("%s|%dms", msg.U, time.Now().UnixNano()/int64(time.Millisecond)-msg.T) } diff --git a/src/device/input/button/v01_exported_test.go b/src/device/input/button/v01_exported_test.go index 73e5a99..e8b4501 100644 --- a/src/device/input/button/v01_exported_test.go +++ b/src/device/input/button/v01_exported_test.go @@ -8,6 +8,7 @@ import ( "net/http" "os" "path" + "strings" "testing" "time" ) @@ -134,4 +135,24 @@ func TestV01Feedback(t *testing.T) { t.Error(b) } }) + + t.Run("change broadcast", func(t *testing.T) { + want := `my new broadcast` + r, _ := http.NewRequest(http.MethodPut, "http://localhost:27071/broadcast", strings.NewReader(want)) + resp, err := http.DefaultClient.Do(r) + if err != nil { + t.Fatal(err) + } + resp.Body.Close() + + resp, err = http.Get("http://localhost:27071") + if err != nil { + t.Fatal(err) + } + defer resp.Body.Close() + b, _ := io.ReadAll(resp.Body) + if string(b) != want { + t.Error(string(b)) + } + }) }