v1 complete
parent
7182ab387f
commit
a9ca58f154
|
|
@ -3,6 +3,7 @@ package button
|
|||
import (
|
||||
"context"
|
||||
"mayhem-party/src/device/input/raw"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Parser interface {
|
||||
|
|
@ -11,5 +12,8 @@ type Parser interface {
|
|||
}
|
||||
|
||||
func New(ctx context.Context, src raw.Raw) Parser {
|
||||
if os.Getenv("BUTTON_PARSER_V1") == "true" {
|
||||
return NewV1(src)
|
||||
}
|
||||
return NewPlaintext(src)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,13 @@
|
|||
package button
|
||||
|
||||
import "mayhem-party/src/device/input/raw"
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"mayhem-party/src/device/input/raw"
|
||||
"os"
|
||||
)
|
||||
|
||||
var debugging = os.Getenv("DEBUG") == "true"
|
||||
|
||||
type V1 struct {
|
||||
src raw.Raw
|
||||
|
|
@ -15,5 +22,25 @@ func NewV1(src raw.Raw) V1 {
|
|||
func (v1 V1) Close() { v1.src.Close() }
|
||||
|
||||
func (v1 V1) Read() []Button {
|
||||
panic(nil)
|
||||
line := v1.src.Read()
|
||||
var msg struct {
|
||||
T int64
|
||||
U string
|
||||
Y string
|
||||
N string
|
||||
}
|
||||
if err := json.Unmarshal(line, &msg); err != nil {
|
||||
log.Printf("%v: %s", err, line)
|
||||
}
|
||||
buttons := make([]Button, len(msg.Y)+len(msg.N))
|
||||
for i := range msg.Y {
|
||||
buttons[i] = Button{Char: msg.Y[i], Down: true}
|
||||
}
|
||||
for i := range msg.N {
|
||||
buttons[len(msg.Y)+i] = Button{Char: msg.N[i], Down: false}
|
||||
}
|
||||
if debugging {
|
||||
log.Printf("%+v", msg)
|
||||
}
|
||||
return buttons
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,29 @@
|
|||
package button_test
|
||||
|
||||
import (
|
||||
"mayhem-party/src/device/input/button"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestV1(t *testing.T) {
|
||||
src := constSrc(`{"T":1,"U":"bel","Y":"abc","N":"cde"}`)
|
||||
t.Logf("(%v) %s", len(src), src.Read())
|
||||
v1 := button.NewV1(src)
|
||||
got := v1.Read()
|
||||
want := []button.Button{
|
||||
{Down: true, Char: 'a'},
|
||||
{Down: true, Char: 'b'},
|
||||
{Down: true, Char: 'c'},
|
||||
{Down: false, Char: 'c'},
|
||||
{Down: false, Char: 'd'},
|
||||
{Down: false, Char: 'e'},
|
||||
}
|
||||
if len(got) != len(want) {
|
||||
t.Fatal(len(want), len(got))
|
||||
}
|
||||
for i := range got {
|
||||
if got[i] != want[i] {
|
||||
t.Errorf("[%d] want %+v got %+v", i, want[i], got[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue