From b94be0cf1ee5a8019a1247868f1769ff4268c405 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Sun, 14 Jul 2019 21:52:11 +0100 Subject: [PATCH] Moved payloads out of their own package because it made the API less nice. --- client.go | 17 ++++++------ constants.go | 8 ++++++ examples/counter/main.go | 9 +++---- examples/cpu/main.go | 6 ++--- payload/payload.go => payloads.go | 44 ++++++++++++------------------- 5 files changed, 39 insertions(+), 45 deletions(-) rename payload/payload.go => payloads.go (81%) diff --git a/client.go b/client.go index de4da73..e3de3e7 100644 --- a/client.go +++ b/client.go @@ -12,7 +12,6 @@ import ( "github.com/gorilla/websocket" sdcontext "github.com/samwho/streamdeck/context" - "github.com/samwho/streamdeck/payload" ) type EventHandler func(ctx context.Context, client *Client, event Event) error @@ -153,19 +152,19 @@ func (client *Client) GetGlobalSettings(ctx context.Context) error { } func (client *Client) OpenURL(ctx context.Context, u url.URL) error { - return client.send(NewEvent(ctx, OpenURL, payload.OpenURL{URL: u.String()})) + return client.send(NewEvent(ctx, OpenURL, OpenURLPayload{URL: u.String()})) } func (client *Client) LogMessage(message string) error { - return client.send(NewEvent(nil, LogMessage, payload.LogMessage{Message: message})) + return client.send(NewEvent(nil, LogMessage, LogMessagePayload{Message: message})) } -func (client *Client) SetTitle(ctx context.Context, title string, target payload.Target) error { - return client.send(NewEvent(ctx, SetTitle, payload.SetTitle{Title: title, Target: target})) +func (client *Client) SetTitle(ctx context.Context, title string, target Target) error { + return client.send(NewEvent(ctx, SetTitle, SetTitlePayload{Title: title, Target: target})) } -func (client *Client) SetImage(ctx context.Context, base64image string, target payload.Target) error { - return client.send(NewEvent(ctx, SetImage, payload.SetImage{Base64Image: base64image, Target: target})) +func (client *Client) SetImage(ctx context.Context, base64image string, target Target) error { + return client.send(NewEvent(ctx, SetImage, SetImagePayload{Base64Image: base64image, Target: target})) } func (client *Client) ShowAlert(ctx context.Context) error { @@ -177,11 +176,11 @@ func (client *Client) ShowOk(ctx context.Context) error { } func (client *Client) SetState(ctx context.Context, state int) error { - return client.send(NewEvent(ctx, SetState, payload.SetState{State: state})) + return client.send(NewEvent(ctx, SetState, SetStatePayload{State: state})) } func (client *Client) SwitchToProfile(ctx context.Context, profile string) error { - return client.send(NewEvent(ctx, SwitchToProfile, payload.SwitchProfile{Profile: profile})) + return client.send(NewEvent(ctx, SwitchToProfile, SwitchProfilePayload{Profile: profile})) } func (client *Client) SendToPropertyInspector(ctx context.Context, payload interface{}) error { diff --git a/constants.go b/constants.go index c2018b2..d70e044 100644 --- a/constants.go +++ b/constants.go @@ -31,3 +31,11 @@ const ( SetState = "setState" SwitchToProfile = "switchToProfile" ) + +type Target int + +const ( + HardwareAndSoftware Target = 0 + OnlyHardware Target = 1 + OnlySoftware Target = 2 +) diff --git a/examples/counter/main.go b/examples/counter/main.go index 9e187d7..0a52a2a 100644 --- a/examples/counter/main.go +++ b/examples/counter/main.go @@ -11,7 +11,6 @@ import ( "strconv" "github.com/samwho/streamdeck" - "github.com/samwho/streamdeck/payload" ) const ( @@ -53,7 +52,7 @@ func setupCounter(client *streamdeck.Client) { settings := make(map[string]*Settings) action.RegisterHandler(streamdeck.WillAppear, func(ctx context.Context, client *streamdeck.Client, event streamdeck.Event) error { - p := payload.WillAppear{} + p := streamdeck.WillAppearPayload{} if err := json.Unmarshal(event.Payload, &p); err != nil { return err } @@ -73,11 +72,11 @@ func setupCounter(client *streamdeck.Client) { return err } - if err := client.SetImage(ctx, bg, payload.HardwareAndSoftware); err != nil { + if err := client.SetImage(ctx, bg, streamdeck.HardwareAndSoftware); err != nil { return err } - return client.SetTitle(ctx, strconv.Itoa(s.Counter), payload.HardwareAndSoftware) + return client.SetTitle(ctx, strconv.Itoa(s.Counter), streamdeck.HardwareAndSoftware) }) action.RegisterHandler(streamdeck.WillDisappear, func(ctx context.Context, client *streamdeck.Client, event streamdeck.Event) error { @@ -97,7 +96,7 @@ func setupCounter(client *streamdeck.Client) { return err } - return client.SetTitle(ctx, strconv.Itoa(s.Counter), payload.HardwareAndSoftware) + return client.SetTitle(ctx, strconv.Itoa(s.Counter), streamdeck.HardwareAndSoftware) }) } diff --git a/examples/cpu/main.go b/examples/cpu/main.go index 37d7e10..454dfa1 100644 --- a/examples/cpu/main.go +++ b/examples/cpu/main.go @@ -9,8 +9,6 @@ import ( "os" "time" - "github.com/samwho/streamdeck/payload" - "github.com/samwho/streamdeck" sdcontext "github.com/samwho/streamdeck/context" "github.com/shirou/gopsutil/cpu" @@ -88,12 +86,12 @@ func setup(client *streamdeck.Client) { continue } - if err := client.SetImage(ctx, img, payload.HardwareAndSoftware); err != nil { + if err := client.SetImage(ctx, img, streamdeck.HardwareAndSoftware); err != nil { log.Printf("error setting image: %v\n", err) continue } - if err := client.SetTitle(ctx, fmt.Sprintf("CPU\n%d%%", int(r[0])), payload.HardwareAndSoftware); err != nil { + if err := client.SetTitle(ctx, fmt.Sprintf("CPU\n%d%%", int(r[0])), streamdeck.HardwareAndSoftware); err != nil { log.Printf("error setting title: %v\n", err) continue } diff --git a/payload/payload.go b/payloads.go similarity index 81% rename from payload/payload.go rename to payloads.go index df3daf0..91033e8 100644 --- a/payload/payload.go +++ b/payloads.go @@ -1,44 +1,34 @@ -package payload +package streamdeck -import ( - "encoding/json" -) +import "encoding/json" -type Target int - -const ( - HardwareAndSoftware Target = 0 - OnlyHardware Target = 1 - OnlySoftware Target = 2 -) - -type LogMessage struct { +type LogMessagePayload struct { Message string `json:"message"` } -type OpenURL struct { +type OpenURLPayload struct { URL string `json:"url"` } -type SetTitle struct { +type SetTitlePayload struct { Title string `json:"title"` Target Target `json:"target"` } -type SetImage struct { +type SetImagePayload struct { Base64Image string `json:"image"` Target Target `json:"target"` } -type SetState struct { +type SetStatePayload struct { State int `json:"state"` } -type SwitchProfile struct { +type SwitchProfilePayload struct { Profile string `json:"profile"` } -type DidReceiveSettings struct { +type DidReceiveSettingsPayload struct { Settings json.RawMessage `json:"settings,omitempty"` Coordinates Coordinates `json:"coordinates,omitempty"` IsInMultiAction bool `json:"isInMultiAction,omitempty"` @@ -49,11 +39,11 @@ type Coordinates struct { Row int `json:"row,omitempty"` } -type DidReceiveGlobalSettings struct { +type DidReceiveGlobalSettingsPayload struct { Settings json.RawMessage `json:"settings,omitempty"` } -type KeyDown struct { +type KeyDownPayload struct { Settings json.RawMessage `json:"settings,omitempty"` Coordinates Coordinates `json:"coordinates,omitempty"` State int `json:"state,omitempty"` @@ -61,7 +51,7 @@ type KeyDown struct { IsInMultiAction bool `json:"isInMultiAction,omitempty"` } -type KeyUp struct { +type KeyUpPayload struct { Settings json.RawMessage `json:"settings,omitempty"` Coordinates Coordinates `json:"coordinates,omitempty"` State int `json:"state,omitempty"` @@ -69,21 +59,21 @@ type KeyUp struct { IsInMultiAction bool `json:"isInMultiAction,omitempty"` } -type WillAppear struct { +type WillAppearPayload struct { Settings json.RawMessage `json:"settings,omitempty"` Coordinates Coordinates `json:"coordinates,omitempty"` State int `json:"state,omitempty"` IsInMultiAction bool `json:"isInMultiAction,omitempty"` } -type WillDisappear struct { +type WillDisappearPayload struct { Settings json.RawMessage `json:"settings,omitempty"` Coordinates Coordinates `json:"coordinates,omitempty"` State int `json:"state,omitempty"` IsInMultiAction bool `json:"isInMultiAction,omitempty"` } -type TitleParametersDidChange struct { +type TitleParametersDidChangePayload struct { Settings json.RawMessage `json:"settings,omitempty"` Coordinates Coordinates `json:"coordinates,omitempty"` State int `json:"state,omitempty"` @@ -101,10 +91,10 @@ type TitleParameters struct { TitleColor string `json:"titleColor,omitempty"` } -type ApplicationDidLaunch struct { +type ApplicationDidLaunchPayload struct { Application string `json:"application,omitempty"` } -type ApplicationDidTerminate struct { +type ApplicationDidTerminatePayload struct { Application string `json:"application,omitempty"` }