Skip to content

Commit

Permalink
Sync button statuses
Browse files Browse the repository at this point in the history
  • Loading branch information
john-scalingo committed Apr 29, 2020
1 parent 3121796 commit 4f8f067
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 17 deletions.
4 changes: 3 additions & 1 deletion gma2ws/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Client struct {
writeLock *sync.Mutex
ws *websocket.Conn
playbackChan chan []ServerPlaybacks
getDataChan chan []map[string]string
stopResponseHandler chan bool
}

Expand All @@ -37,6 +38,7 @@ func NewClient(host, user, password string) (*Client, error) {
user: user,
hashedPassword: hashedPassword,
playbackChan: make(chan []ServerPlaybacks),
getDataChan: make(chan []map[string]string),
stopResponseHandler: make(chan bool),
writeLock: &sync.Mutex{},
}, nil
Expand Down Expand Up @@ -170,7 +172,7 @@ func (c *Client) serverResponseHandler(ctx context.Context) {
case RequestTypeLogin:
log.Info("Receive request type login. Ignoring...")
case RequestTypeGetData:
log.Info("Receive request type get data. Ignoring...")
go c.serverResponseHandleGetData(ctx, bufferCopy(buffer))
case RequestTypePlaybacks:
go c.serverResponseHandlePlaybacks(ctx, bufferCopy(buffer))
default:
Expand Down
73 changes: 68 additions & 5 deletions gma2ws/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ package gma2ws

import (
"context"
"encoding/json"
"strings"
"time"

"github.com/Scalingo/go-utils/logger"
"github.com/pkg/errors"
)

type KeyName string
type KeyStatus int
type KeyStatus string

const (
KeyStatusPressed KeyStatus = 1
KeyStatusReleased KeyStatus = 0

KeyName0 KeyName = "0"
KeyName1 KeyName = "1"
KeyName2 KeyName = "2"
Expand All @@ -29,14 +30,22 @@ const (
KeyNameHighlight KeyName = "HIGH"
KeyNameSolo KeyName = "SOLO"
KeyNameSelect KeyName = "SELECT"

KeyStatusOff KeyStatus = "off"
KeyStatusOn KeyStatus = "on"
KeyStatusBlink KeyStatus = "blink"
)

var autoSubmitKeys map[KeyName]bool = map[KeyName]bool{
KeyNameHighlight: true,
KeyNameSolo: true,
}

func (c *Client) SendKey(ctx context.Context, key KeyName, status KeyStatus) error {
func (c *Client) SendKey(ctx context.Context, key KeyName, pressed bool) error {
status := 0
if pressed {
status = 1
}
autoSubmit := autoSubmitKeys[key]
err := c.WriteJSON(ClientRequestKeyName{
ClientRequestGeneric: ClientRequestGeneric{
Expand All @@ -53,3 +62,57 @@ func (c *Client) SendKey(ctx context.Context, key KeyName, status KeyStatus) err
}
return nil
}

func (c *Client) serverResponseHandleGetData(ctx context.Context, buffer []byte) {
log := logger.Get(ctx)
var getData ServerResponseGetData
err := json.Unmarshal(buffer, &getData)
if err != nil {
log.WithError(err).Error("fail to unmarshal getdata")
return
}

select {
case c.getDataChan <- getData.Data:
default:
}
}

func (c *Client) KeyStatuses(keys ...string) (map[string]KeyStatus, error) {
request := ClientRequestGetData{
ClientRequestGeneric: ClientRequestGeneric{
RequestType: RequestTypeGetData,
Session: c.session,
MaxRequests: 1,
},
Data: strings.Join(keys, ","),
}

err := c.WriteJSON(request)
if err != nil {
return nil, errors.Wrap(err, "fail to send getdata request")
}

timer := time.NewTimer(2 * time.Second)
var data []map[string]string
select {
case data = <-c.getDataChan:
case <-timer.C:
return nil, errors.New("timeout")
}

res := make(map[string]KeyStatus)
for _, d := range data {
for k, v := range d {
switch v {
case "0":
res[k] = KeyStatusOff
case "1":
res[k] = KeyStatusOn
case "b":
res[k] = KeyStatusBlink
}
}
}
return res, nil
}
14 changes: 11 additions & 3 deletions gma2ws/playbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@ import (
"github.com/pkg/errors"
)

type PlaybacksItemType int

const (
PlaybacksItemTypeFader PlaybacksItemType = 2
PlaybacksItemTypeButton PlaybacksItemType = 3
)

type PlaybacksRange struct {
Index int
Count int
Index int
Count int
ItemType PlaybacksItemType
}

type PlaybackResonse struct {
Expand All @@ -34,7 +42,7 @@ func (c *Client) Playbacks(page int, ranges []PlaybacksRange) ([]ServerPlaybacks
}
startIndex[i] = r.Index
itemCount[i] = r.Count
itemType[i] = 2
itemType[i] = int(r.ItemType)
}
request := ClientRequestPlaybacks{
ClientRequestGeneric: ClientRequestGeneric{
Expand Down
13 changes: 9 additions & 4 deletions gma2ws/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ type ServerResponseGeneric struct {
WorldIndex int `json:"worldIndex"`
}

type ServerResponseGetData struct {
ServerResponseGeneric
Data []map[string]string `json:"data"`
}

type ServerResponsePlayback struct {
ServerResponseGeneric
ResponseSubType int `json:"responseSubType"` // ???
Expand Down Expand Up @@ -151,10 +156,10 @@ type ClientRequestPlaybacksUserInput struct {

type ClientRequestKeyName struct {
ClientRequestGeneric
KeyName KeyName `json:"keyname"`
Value KeyStatus `json:"value"`
CommandLineText string `json:"cmdlinetext"`
AutoSubmit bool `json:"autoSubmit"`
KeyName KeyName `json:"keyname"`
Value int `json:"value"`
CommandLineText string `json:"cmdlinetext"`
AutoSubmit bool `json:"autoSubmit"`
}

type ClientRequestCommand struct {
Expand Down
44 changes: 44 additions & 0 deletions link/buttons.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package link
import (
"context"

"github.com/johnsudaar/xtouchgma2/gma2ws"
"github.com/johnsudaar/xtouchgma2/xtouch"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -173,7 +174,50 @@ func (l *Link) updateButtons(ctx context.Context) error {
if err != nil {
return errors.Wrap(err, "fail to set channel next status")
}
}

res, err := l.GMA.KeyStatuses("set", "edit", "clear", "solo", "high", "align")
if err != nil {
return errors.Wrap(err, "fail to get gma key statuses")
}

for key, status := range res {
var button xtouch.Button
switch key {
case "set":
button = xtouch.ButtonZoom
case "edit":
button = xtouch.ButtonF3
case "clear":
button = xtouch.ButtonF5
case "solo":
button = xtouch.ButtonPan
case "high":
button = xtouch.ButtonTrack
case "align":
button = xtouch.ButtonInst
}
err := l.setKeyStatus(ctx, button, status)
if err != nil {
return errors.Wrap(err, "fail to update button")
}
}

return nil
}

func (l *Link) setKeyStatus(ctx context.Context, key xtouch.Button, status gma2ws.KeyStatus) error {
var bStatus xtouch.ButtonStatus = xtouch.ButtonStatusOff
if status == gma2ws.KeyStatusOn {
bStatus = xtouch.ButtonStatusOn
}
if status == gma2ws.KeyStatusBlink {
bStatus = xtouch.ButtonStatusBlink
}

err := l.XTouch.SetButtonStatus(ctx, key, bStatus)
if err != nil {
return errors.Wrap(err, "fail to send button status")
}
return nil
}
37 changes: 33 additions & 4 deletions link/fader.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,19 @@ func (l *Link) faderGmaToXtouch(ctx context.Context) error {

playbacks, err := l.GMA.Playbacks(page, []gma2ws.PlaybacksRange{
gma2ws.PlaybacksRange{
Index: 0,
Count: 10,
Index: 0,
Count: 10,
ItemType: gma2ws.PlaybacksItemTypeFader,
},
gma2ws.PlaybacksRange{
Index: 15,
Count: 10,
Index: 15,
Count: 10,
ItemType: gma2ws.PlaybacksItemTypeFader,
},
gma2ws.PlaybacksRange{
Index: 100,
Count: 10,
ItemType: gma2ws.PlaybacksItemTypeButton,
},
})
if err != nil {
Expand Down Expand Up @@ -65,6 +72,16 @@ func (l *Link) faderGmaToXtouch(ctx context.Context) error {
if err != nil {
log.WithError(err).Error("fail to send scribble data")
}

var buttonStatus xtouch.ButtonStatus = xtouch.ButtonStatusOff
if executor.IsRun != 0 {
buttonStatus = xtouch.ButtonStatusOn
}
err = l.XTouch.SetFaderButtonStatus(ctx, i, xtouch.FaderButtonPositionSelect, buttonStatus)
if err != nil {
return errors.Wrap(err, "fail to change button status")
}

}

l.encoderLock.Lock()
Expand All @@ -83,6 +100,18 @@ func (l *Link) faderGmaToXtouch(ctx context.Context) error {
l.XTouch.SetRingPosition(ctx, i, value)
}

for i := 0; i < 8; i++ {
executor := playbacks[2].Items[i/5][i%5]
var value xtouch.ButtonStatus = xtouch.ButtonStatusOff
if executor.IsRun != 0 {
value = xtouch.ButtonStatusOn
}
err := l.XTouch.SetFaderButtonStatus(ctx, i, xtouch.FaderButtonPositionRec, value)
if err != nil {
return errors.Wrap(err, "fail to change button status")
}
}

l.XTouch.SetAssignement(ctx, page+1)
return nil
}
Expand Down

0 comments on commit 4f8f067

Please sign in to comment.