Moved payloads out of their own package because it made the API less nice.

This commit is contained in:
Sam Rose 2019-07-14 21:52:11 +01:00
parent 8ee19c457a
commit b94be0cf1e
5 changed files with 39 additions and 45 deletions

View File

@ -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 {

View File

@ -31,3 +31,11 @@ const (
SetState = "setState"
SwitchToProfile = "switchToProfile"
)
type Target int
const (
HardwareAndSoftware Target = 0
OnlyHardware Target = 1
OnlySoftware Target = 2
)

View File

@ -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)
})
}

View File

@ -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
}

View File

@ -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"`
}