|
@@ -2,67 +2,132 @@ package rgb
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
"encoding/json"
|
|
"encoding/json"
|
|
|
|
|
+ "errors"
|
|
|
"fmt"
|
|
"fmt"
|
|
|
- "os"
|
|
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/cobra"
|
|
|
- "github.com/wobkey/rgb/internal/rgb"
|
|
|
|
|
|
|
+ intrgb "github.com/wobkey/rgb/internal/rgb"
|
|
|
"github.com/wobkey/rgb/internal/via"
|
|
"github.com/wobkey/rgb/internal/via"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
+type infoGetter interface {
|
|
|
|
|
+ GetValue(via.LEDType, uint8) ([]byte, error)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+type infoColor struct {
|
|
|
|
|
+ Hue uint8 `json:"hue"`
|
|
|
|
|
+ Saturation uint8 `json:"saturation"`
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+type zoneInfo struct {
|
|
|
|
|
+ Zone intrgb.Zone `json:"zone"`
|
|
|
|
|
+ Channel uint8 `json:"channel"`
|
|
|
|
|
+ Enabled bool `json:"enabled"`
|
|
|
|
|
+ Effect string `json:"effect"`
|
|
|
|
|
+ EffectID uint8 `json:"effectId"`
|
|
|
|
|
+ Brightness uint8 `json:"brightness"`
|
|
|
|
|
+ Speed uint8 `json:"speed"`
|
|
|
|
|
+ Color infoColor `json:"color"`
|
|
|
|
|
+ Error string `json:"error,omitempty"`
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+type infoOutput struct {
|
|
|
|
|
+ Enabled bool `json:"enabled"`
|
|
|
|
|
+ Mode string `json:"mode"`
|
|
|
|
|
+ Brightness uint8 `json:"brightness"`
|
|
|
|
|
+ Speed uint8 `json:"speed"`
|
|
|
|
|
+ Zones []zoneInfo `json:"zones"`
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func readInfo(proto infoGetter, zones []intrgb.Zone) (infoOutput, error) {
|
|
|
|
|
+ out := infoOutput{Zones: make([]zoneInfo, 0, len(zones))}
|
|
|
|
|
+ var queryErrors []error
|
|
|
|
|
+ for _, zone := range zones {
|
|
|
|
|
+ record, err := readZoneInfo(proto, zone)
|
|
|
|
|
+ out.Zones = append(out.Zones, record)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ queryErrors = append(queryErrors, err)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if len(out.Zones) > 0 {
|
|
|
|
|
+ first := out.Zones[0]
|
|
|
|
|
+ out.Enabled = first.Enabled
|
|
|
|
|
+ out.Mode = first.Effect
|
|
|
|
|
+ out.Brightness = first.Brightness
|
|
|
|
|
+ out.Speed = first.Speed
|
|
|
|
|
+ }
|
|
|
|
|
+ return out, errors.Join(queryErrors...)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func readZoneInfo(proto infoGetter, zone intrgb.Zone) (zoneInfo, error) {
|
|
|
|
|
+ record := zoneInfo{Zone: zone, Channel: zone.Channel()}
|
|
|
|
|
+ channel := via.LEDType(zone.Channel())
|
|
|
|
|
+
|
|
|
|
|
+ brightness, err := getInfoValue(proto, channel, uint8(intrgb.Brightness), 1)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ record.Error = err.Error()
|
|
|
|
|
+ return record, err
|
|
|
|
|
+ }
|
|
|
|
|
+ record.Brightness = brightness[0]
|
|
|
|
|
+
|
|
|
|
|
+ effect, err := getInfoValue(proto, channel, uint8(intrgb.EffectID), 1)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ record.Error = err.Error()
|
|
|
|
|
+ return record, err
|
|
|
|
|
+ }
|
|
|
|
|
+ record.EffectID = effect[0]
|
|
|
|
|
+ record.Effect = intrgb.EffectName(zone, record.EffectID)
|
|
|
|
|
+ record.Enabled = lightingEnabled(record.EffectID, record.Brightness)
|
|
|
|
|
+
|
|
|
|
|
+ speed, err := getInfoValue(proto, channel, uint8(intrgb.Speed), 1)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ record.Error = err.Error()
|
|
|
|
|
+ return record, err
|
|
|
|
|
+ }
|
|
|
|
|
+ record.Speed = speed[0]
|
|
|
|
|
+
|
|
|
|
|
+ color, err := getInfoValue(proto, channel, uint8(intrgb.ColorValue), 2)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ record.Error = err.Error()
|
|
|
|
|
+ return record, err
|
|
|
|
|
+ }
|
|
|
|
|
+ record.Color = infoColor{Hue: color[0], Saturation: color[1]}
|
|
|
|
|
+ return record, nil
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func getInfoValue(proto infoGetter, channel via.LEDType, param uint8, size int) ([]byte, error) {
|
|
|
|
|
+ value, err := proto.GetValue(channel, param)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return nil, err
|
|
|
|
|
+ }
|
|
|
|
|
+ if len(value) < size {
|
|
|
|
|
+ return nil, fmt.Errorf("short value response: got %d, want %d", len(value), size)
|
|
|
|
|
+ }
|
|
|
|
|
+ return value, nil
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
func NewInfoCmd() *cobra.Command {
|
|
func NewInfoCmd() *cobra.Command {
|
|
|
return &cobra.Command{
|
|
return &cobra.Command{
|
|
|
Use: "info",
|
|
Use: "info",
|
|
|
Short: "Show current RGB state",
|
|
Short: "Show current RGB state",
|
|
|
- Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
|
|
|
+ RunE: func(_ *cobra.Command, args []string) error {
|
|
|
|
|
+ zones, err := selectedZones()
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
proto, _, err := OpenDevice()
|
|
proto, _, err := OpenDevice()
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
|
|
|
|
- os.Exit(1)
|
|
|
|
|
|
|
+ return err
|
|
|
}
|
|
}
|
|
|
defer proto.Close()
|
|
defer proto.Close()
|
|
|
|
|
|
|
|
- getValue := func(param uint8) (uint8, error) {
|
|
|
|
|
- value, err := proto.GetValue(via.RGBLight, param)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return 0, err
|
|
|
|
|
- }
|
|
|
|
|
- if len(value) == 0 {
|
|
|
|
|
- return 0, fmt.Errorf("empty value response")
|
|
|
|
|
- }
|
|
|
|
|
- return value[0], nil
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- brightness, err := getValue(uint8(rgb.Brightness))
|
|
|
|
|
|
|
+ out, queryErr := readInfo(proto, zones)
|
|
|
|
|
+ data, err := json.MarshalIndent(out, "", " ")
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- fmt.Fprintf(os.Stderr, "Error getting brightness: %v\n", err)
|
|
|
|
|
- os.Exit(1)
|
|
|
|
|
|
|
+ return err
|
|
|
}
|
|
}
|
|
|
- speed, err := getValue(uint8(rgb.Speed))
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- fmt.Fprintf(os.Stderr, "Error getting speed: %v\n", err)
|
|
|
|
|
- os.Exit(1)
|
|
|
|
|
- }
|
|
|
|
|
- mode, err := getValue(uint8(rgb.EffectID))
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- fmt.Fprintf(os.Stderr, "Error getting mode: %v\n", err)
|
|
|
|
|
- os.Exit(1)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- out := struct {
|
|
|
|
|
- Enabled bool `json:"enabled"`
|
|
|
|
|
- Mode string `json:"mode"`
|
|
|
|
|
- Brightness uint8 `json:"brightness"`
|
|
|
|
|
- Speed uint8 `json:"speed"`
|
|
|
|
|
- }{
|
|
|
|
|
- Enabled: lightingEnabled(mode, brightness),
|
|
|
|
|
- Mode: rgb.Impact80EffectName(mode),
|
|
|
|
|
- Brightness: brightness,
|
|
|
|
|
- Speed: speed,
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- data, _ := json.MarshalIndent(out, "", " ")
|
|
|
|
|
fmt.Println(string(data))
|
|
fmt.Println(string(data))
|
|
|
|
|
+ return queryErr
|
|
|
},
|
|
},
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|