Added support for remote0

This commit is contained in:
mikx
2023-10-31 20:16:41 -04:00
commit 5c6e496688
88 changed files with 7833 additions and 0 deletions

327
pkg/graph/graph.go Normal file
View File

@@ -0,0 +1,327 @@
package graph
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"math"
"regexp"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
"image"
"image/color"
"image/png"
"sync"
)
// Label struct contains text, position and color information
type Label struct {
text string
y uint
fontSize float64
clr *color.RGBA
}
// Graph is used to display a histogram of data passed to Update
type Graph struct {
img *image.RGBA
lvay int
width int
height int
min int
max int
yvals []uint8
fgColor *color.RGBA
bgColor *color.RGBA
hlColor *color.RGBA
labels map[int]*Label
drawn bool
redraw bool
}
// FontFaceManager builds and caches fonts based on size
type FontFaceManager struct {
mux sync.Mutex
fontCache map[float64]font.Face
}
// NewFontFaceManager constructs new manager
func NewFontFaceManager() *FontFaceManager {
return &FontFaceManager{fontCache: make(map[float64]font.Face)}
}
func (f *FontFaceManager) newFace(size float64) font.Face {
b, err := ioutil.ReadFile("DejaVuSans-Bold.ttf")
if err != nil {
log.Fatal(err)
}
tt, err := truetype.Parse(b)
if err != nil {
log.Fatal("failed to parse font")
}
face := truetype.NewFace(tt, &truetype.Options{Size: size, DPI: 72})
return face
}
// GetFaceOfSize returns font face for given size
func (f *FontFaceManager) GetFaceOfSize(size float64) font.Face {
f.mux.Lock()
defer f.mux.Unlock()
if f, ok := f.fontCache[size]; ok {
return f
}
nf := f.newFace(size)
f.fontCache[size] = nf
return nf
}
type singleshared struct {
fontFaceManager *FontFaceManager
pngEnc *png.Encoder
pngBuf *bytes.Buffer
}
var sharedinstance *singleshared
var once sync.Once
func shared() *singleshared {
once.Do(func() {
sharedinstance = &singleshared{
pngEnc: &png.Encoder{
CompressionLevel: png.NoCompression,
},
pngBuf: bytes.NewBuffer(make([]byte, 0, 15697)),
}
sharedinstance.fontFaceManager = NewFontFaceManager()
})
return sharedinstance
}
// NewGraph initializes a new Graph for rendering
func NewGraph(width, height, min, max int, fgColor, bgColor, hlColor *color.RGBA) *Graph {
img := image.NewRGBA(image.Rect(0, 0, int(width), int(height)))
labels := make(map[int]*Label)
return &Graph{
img: img,
lvay: -1,
width: width,
height: height,
min: min,
max: max,
labels: labels,
yvals: make([]uint8, 0, width),
fgColor: fgColor,
bgColor: bgColor,
hlColor: hlColor,
}
}
// SetForegroundColor sets the foreground color of the graph
func (g *Graph) SetForegroundColor(clr *color.RGBA) {
g.fgColor = clr
g.redraw = true
}
// SetBackgroundColor sets the background color of the graph
func (g *Graph) SetBackgroundColor(clr *color.RGBA) {
g.bgColor = clr
g.redraw = true
}
// SetHighlightColor sets the highlight color of the graph
func (g *Graph) SetHighlightColor(clr *color.RGBA) {
g.hlColor = clr
g.redraw = true
}
// SetMin sets the min value for the graph scale
func (g *Graph) SetMin(min int) {
g.min = min
}
// SetMax sets the max value for the graph scale
func (g *Graph) SetMax(max int) {
g.max = max
}
// SetLabel given a key, set the initial text, position and color
func (g *Graph) SetLabel(key int, text string, y uint, clr *color.RGBA) {
l := &Label{text: text, y: y, clr: clr}
g.labels[key] = l
}
// SetLabelText given a key, update the text for a pre-set label
func (g *Graph) SetLabelText(key int, text string) error {
l, ok := g.labels[key]
if !ok {
return fmt.Errorf("Label with key (%d) does not exist", key)
}
l.text = text
return nil
}
// SetLabelFontSize given a key, update the text for a pre-set label
func (g *Graph) SetLabelFontSize(key int, size float64) error {
l, ok := g.labels[key]
if !ok {
return fmt.Errorf("Label with key (%d) does not exist", key)
}
l.fontSize = size
return nil
}
// SetLabelColor given a key and color, sets the color of the text
func (g *Graph) SetLabelColor(key int, clr *color.RGBA) error {
l, ok := g.labels[key]
if !ok {
return fmt.Errorf("Label with key (%d) does not exist", key)
}
l.clr = clr
return nil
}
func (g *Graph) drawGraph(x, vay, maxx int) {
var clr *color.RGBA
for ; x <= maxx; x++ {
for y := 0; y < g.height; y++ {
if y == vay {
clr = g.hlColor
} else if g.lvay != -1 && vay > g.lvay && vay >= y && y >= g.lvay {
clr = g.hlColor
} else if g.lvay != -1 && vay < g.lvay && vay <= y && y <= g.lvay {
clr = g.hlColor
} else if vay > y {
clr = g.fgColor
} else {
clr = g.bgColor
}
i := g.img.PixOffset(x, g.width-1-y)
g.img.Pix[i+0] = clr.R
g.img.Pix[i+1] = clr.G
g.img.Pix[i+2] = clr.B
g.img.Pix[i+3] = clr.A
}
g.lvay = vay
}
}
// Update given a value draws the graph, shifting contents left. Call EncodePNG to get a rendered PNG
func (g *Graph) Update(value float64) {
vay := vAsY(g.height-1, value, g.min, g.max)
if len(g.yvals) >= g.width {
_, a := g.yvals[0], g.yvals[1:]
g.yvals = a
}
g.yvals = append(g.yvals, uint8(vay))
if g.redraw {
g.lvay = -1
lyvals := len(g.yvals)
for idx := lyvals - 1; idx >= 0; idx-- {
x := g.width - lyvals + idx
maxx := x
if idx == 0 {
x = 0
}
v := int(g.yvals[idx])
g.drawGraph(x, v, maxx)
}
g.lvay = int(g.yvals[lyvals-1])
g.redraw = false
} else if g.drawn {
// shift the graph left 1px
for y := 0; y < g.height; y++ {
idx := g.img.PixOffset(0, y)
p1 := g.img.Pix[:idx]
p2 := g.img.Pix[idx+4 : idx+(g.width*4)]
p3 := g.img.Pix[idx+(g.width*4):]
g.img.Pix = append(p1, append(append(p2, []uint8{0, 0, 0, 0}...), p3...)...)
}
g.drawGraph(int(g.width)-1, int(vay), g.width-1)
} else {
g.drawGraph(0, vay, g.width-1)
g.drawn = true
}
}
// EncodePNG renders the current state of the graph
func (g *Graph) EncodePNG() ([]byte, error) {
bak := append(g.img.Pix[:0:0], g.img.Pix...)
for _, l := range g.labels {
g.drawLabel(l)
}
shared := shared()
err := shared.pngEnc.Encode(shared.pngBuf, g.img)
if err != nil {
return nil, err
}
g.img.Pix = bak
bts := shared.pngBuf.Bytes()
shared.pngBuf.Reset()
return bts, nil
}
func vAsY(maxY int, v float64, minV, maxV int) int {
r := maxV - minV
v1 := v - float64(minV)
yf := v1 / float64(r) * float64(maxY)
yi := int(math.Round(yf))
return yi
}
func unfix(x fixed.Int26_6) float64 {
const shift, mask = 6, 1<<6 - 1
if x >= 0 {
return float64(x>>shift) + float64(x&mask)/64
}
x = -x
if x >= 0 {
return -(float64(x>>shift) + float64(x&mask)/64)
}
return 0
}
var newlineRegex = regexp.MustCompile("(\n|\\\\n)+")
func (g *Graph) drawLabel(l *Label) {
shared := shared()
lines := newlineRegex.Split(l.text, -1)
face := shared.fontFaceManager.GetFaceOfSize(l.fontSize)
curY := l.y - uint(10.5-float64(face.Metrics().Height.Round()))
for _, line := range lines {
var lwidth float64
for _, x := range line {
awidth, ok := face.GlyphAdvance(rune(x))
if ok != true {
log.Println("drawLabel: Failed to GlyphAdvance")
return
}
lwidth += unfix(awidth)
}
lx := (float64(g.width) / 2.) - (lwidth / 2.)
point := fixed.Point26_6{X: fixed.Int26_6(lx * 64), Y: fixed.Int26_6(curY * 64)}
d := &font.Drawer{
Dst: g.img,
Src: image.NewUniform(l.clr),
Face: face,
Dot: point,
}
d.DrawString(line)
curY += 12
}
}

126
pkg/service/grpc.go Normal file
View File

@@ -0,0 +1,126 @@
package hwsensorsservice
import (
"context"
"errors"
"io"
"github.com/golang/protobuf/ptypes/empty"
"github.com/shayne/hwinfo-streamdeck/pkg/service/proto"
)
// GRPCClient is an implementation of KV that talks over RPC.
type GRPCClient struct {
Client proto.HWServiceClient
}
// PollTime rpc call
func (c *GRPCClient) PollTime() (uint64, error) {
resp, err := c.Client.PollTime(context.Background(), &empty.Empty{})
if err != nil {
return 0, err
}
return resp.GetPollTime(), nil
}
// Sensors implementation
func (c *GRPCClient) Sensors() ([]Sensor, error) {
stream, err := c.Client.Sensors(context.Background(), &empty.Empty{})
if err != nil {
return nil, err
}
var sensors []Sensor
for {
s, err := stream.Recv()
if errors.Is(err, io.EOF) {
break
}
if err != nil {
return nil, err
}
sensors = append(sensors, &sensor{s})
}
return sensors, nil
}
// ReadingsForSensorID implementation
func (c *GRPCClient) ReadingsForSensorID(id string) ([]Reading, error) {
stream, err := c.Client.ReadingsForSensorID(context.Background(), &proto.SensorIDRequest{Id: id})
if err != nil {
return nil, err
}
var readings []Reading
for {
r, err := stream.Recv()
if errors.Is(err, io.EOF) {
break
}
if err != nil {
return nil, err
}
readings = append(readings, &reading{r})
}
return readings, nil
}
// GRPCServer is the gRPC server that GRPCClient talks to.
type GRPCServer struct {
// This is the real implementation
Impl HardwareService
proto.UnimplementedHWServiceServer
}
// PollTime gRPC wrapper
func (s *GRPCServer) PollTime(ctx context.Context, _ *empty.Empty) (*proto.PollTimeReply, error) {
v, err := s.Impl.PollTime()
return &proto.PollTimeReply{PollTime: v}, err
}
// Sensors gRPC wrapper
func (s *GRPCServer) Sensors(_ *empty.Empty, stream proto.HWService_SensorsServer) error {
sensors, err := s.Impl.Sensors()
if err != nil {
return err
}
for _, sensor := range sensors {
if err := stream.Send(&proto.Sensor{
ID: sensor.ID(),
Name: sensor.Name(),
}); err != nil {
return err
}
}
return nil
}
// ReadingsForSensorID gRPC wrapper
func (s *GRPCServer) ReadingsForSensorID(req *proto.SensorIDRequest, stream proto.HWService_ReadingsForSensorIDServer) error {
readings, err := s.Impl.ReadingsForSensorID(req.GetId())
if err != nil {
return err
}
for _, reading := range readings {
if err := stream.Send(&proto.Reading{
ID: reading.ID(),
TypeI: reading.TypeI(),
Type: reading.Type(),
Label: reading.Label(),
Unit: reading.Unit(),
Value: reading.Value(),
ValueMin: reading.ValueMin(),
ValueMax: reading.ValueMax(),
ValueAvg: reading.ValueAvg(),
}); err != nil {
return err
}
}
return nil
}

149
pkg/service/interface.go Normal file
View File

@@ -0,0 +1,149 @@
package hwsensorsservice
import (
"context"
"google.golang.org/grpc"
"github.com/hashicorp/go-plugin"
"github.com/shayne/hwinfo-streamdeck/pkg/service/proto"
)
// Handshake is a common handshake that is shared by plugin and host.
var Handshake = plugin.HandshakeConfig{
// This isn't required when using VersionedPlugins
ProtocolVersion: 1,
MagicCookieKey: "BASIC_PLUGIN",
MagicCookieValue: "hello",
}
// PluginMap is the map of plugins we can dispense.
var PluginMap = map[string]plugin.Plugin{
"hwinfoplugin": &HardwareServicePlugin{},
}
// HardwareService is the interface that we're exposing as a plugin.
type HardwareService interface {
PollTime() (uint64, error)
Sensors() ([]Sensor, error)
ReadingsForSensorID(id string) ([]Reading, error)
}
// HardwareServicePlugin is the implementation of plugin.GRPCPlugin so we can serve/consume this.
type HardwareServicePlugin struct {
// GRPCPlugin must still implement the Plugin interface
plugin.Plugin
// Concrete implementation, written in Go. This is only used for plugins
// that are written in Go.
Impl HardwareService
}
// GRPCServer constructor
func (p *HardwareServicePlugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
proto.RegisterHWServiceServer(s, &GRPCServer{Impl: p.Impl})
return nil
}
// GRPCClient constructor
func (p *HardwareServicePlugin) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
return &GRPCClient{Client: proto.NewHWServiceClient(c)}, nil
}
// Sensor is the common hardware interface for a sensor
type Sensor interface {
ID() string
Name() string
}
// ReadingType enum of value/unit type for reading
type ReadingType int
const (
// ReadingTypeNone no type
ReadingTypeNone ReadingType = iota
// ReadingTypeTemp temperature in celsius
ReadingTypeTemp
// ReadingTypeVolt voltage
ReadingTypeVolt
// ReadingTypeFan RPM
ReadingTypeFan
// ReadingTypeCurrent amps
ReadingTypeCurrent
// ReadingTypePower watts
ReadingTypePower
// ReadingTypeClock Mhz
ReadingTypeClock
// ReadingTypeUsage e.g. MBs
ReadingTypeUsage
// ReadingTypeOther other
ReadingTypeOther
)
func (t ReadingType) String() string {
return [...]string{"None", "Temp", "Volt", "Fan", "Current", "Power", "Clock", "Usage", "Other"}[t]
}
// Reading is the common hardware interface for a sensor's reading
type Reading interface {
ID() int32
TypeI() int32
Type() string
Label() string
Unit() string
Value() float64
ValueMin() float64
ValueMax() float64
ValueAvg() float64
}
type sensor struct {
*proto.Sensor
}
func (s sensor) ID() string {
return s.Sensor.GetID()
}
func (s sensor) Name() string {
return s.Sensor.GetName()
}
type reading struct {
*proto.Reading
}
func (r reading) ID() int32 {
return r.Reading.GetID()
}
func (r reading) Label() string {
return r.Reading.GetLabel()
}
func (r reading) Type() string {
return r.Reading.GetType()
}
func (r reading) TypeI() int32 {
return r.Reading.GetTypeI()
}
func (r reading) Unit() string {
return r.Reading.GetUnit()
}
func (r reading) Value() float64 {
return r.Reading.GetValue()
}
func (r reading) ValueMin() float64 {
return r.Reading.GetValueMin()
}
func (r reading) ValueMax() float64 {
return r.Reading.GetValueMax()
}
func (r reading) ValueAvg() float64 {
return r.Reading.GetValueAvg()
}

View File

@@ -0,0 +1,439 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.28.1
// protoc v3.21.12
// source: pkg/service/proto/hwservice.proto
package proto
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
emptypb "google.golang.org/protobuf/types/known/emptypb"
reflect "reflect"
sync "sync"
)
const (
// Verify that this generated code is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
// Verify that runtime/protoimpl is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
type PollTimeReply struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
PollTime uint64 `protobuf:"varint,1,opt,name=pollTime,proto3" json:"pollTime,omitempty"`
}
func (x *PollTimeReply) Reset() {
*x = PollTimeReply{}
if protoimpl.UnsafeEnabled {
mi := &file_pkg_service_proto_hwservice_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *PollTimeReply) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*PollTimeReply) ProtoMessage() {}
func (x *PollTimeReply) ProtoReflect() protoreflect.Message {
mi := &file_pkg_service_proto_hwservice_proto_msgTypes[0]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use PollTimeReply.ProtoReflect.Descriptor instead.
func (*PollTimeReply) Descriptor() ([]byte, []int) {
return file_pkg_service_proto_hwservice_proto_rawDescGZIP(), []int{0}
}
func (x *PollTimeReply) GetPollTime() uint64 {
if x != nil {
return x.PollTime
}
return 0
}
type Sensor struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
ID string `protobuf:"bytes,1,opt,name=ID,proto3" json:"ID,omitempty"`
Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`
}
func (x *Sensor) Reset() {
*x = Sensor{}
if protoimpl.UnsafeEnabled {
mi := &file_pkg_service_proto_hwservice_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *Sensor) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*Sensor) ProtoMessage() {}
func (x *Sensor) ProtoReflect() protoreflect.Message {
mi := &file_pkg_service_proto_hwservice_proto_msgTypes[1]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use Sensor.ProtoReflect.Descriptor instead.
func (*Sensor) Descriptor() ([]byte, []int) {
return file_pkg_service_proto_hwservice_proto_rawDescGZIP(), []int{1}
}
func (x *Sensor) GetID() string {
if x != nil {
return x.ID
}
return ""
}
func (x *Sensor) GetName() string {
if x != nil {
return x.Name
}
return ""
}
type SensorIDRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
}
func (x *SensorIDRequest) Reset() {
*x = SensorIDRequest{}
if protoimpl.UnsafeEnabled {
mi := &file_pkg_service_proto_hwservice_proto_msgTypes[2]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *SensorIDRequest) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*SensorIDRequest) ProtoMessage() {}
func (x *SensorIDRequest) ProtoReflect() protoreflect.Message {
mi := &file_pkg_service_proto_hwservice_proto_msgTypes[2]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use SensorIDRequest.ProtoReflect.Descriptor instead.
func (*SensorIDRequest) Descriptor() ([]byte, []int) {
return file_pkg_service_proto_hwservice_proto_rawDescGZIP(), []int{2}
}
func (x *SensorIDRequest) GetId() string {
if x != nil {
return x.Id
}
return ""
}
type Reading struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
ID int32 `protobuf:"varint,1,opt,name=ID,proto3" json:"ID,omitempty"`
TypeI int32 `protobuf:"varint,2,opt,name=typeI,proto3" json:"typeI,omitempty"`
Type string `protobuf:"bytes,3,opt,name=type,proto3" json:"type,omitempty"`
Label string `protobuf:"bytes,4,opt,name=label,proto3" json:"label,omitempty"`
Unit string `protobuf:"bytes,5,opt,name=unit,proto3" json:"unit,omitempty"`
Value float64 `protobuf:"fixed64,6,opt,name=value,proto3" json:"value,omitempty"`
ValueMin float64 `protobuf:"fixed64,7,opt,name=valueMin,proto3" json:"valueMin,omitempty"`
ValueMax float64 `protobuf:"fixed64,8,opt,name=valueMax,proto3" json:"valueMax,omitempty"`
ValueAvg float64 `protobuf:"fixed64,9,opt,name=valueAvg,proto3" json:"valueAvg,omitempty"`
}
func (x *Reading) Reset() {
*x = Reading{}
if protoimpl.UnsafeEnabled {
mi := &file_pkg_service_proto_hwservice_proto_msgTypes[3]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *Reading) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*Reading) ProtoMessage() {}
func (x *Reading) ProtoReflect() protoreflect.Message {
mi := &file_pkg_service_proto_hwservice_proto_msgTypes[3]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use Reading.ProtoReflect.Descriptor instead.
func (*Reading) Descriptor() ([]byte, []int) {
return file_pkg_service_proto_hwservice_proto_rawDescGZIP(), []int{3}
}
func (x *Reading) GetID() int32 {
if x != nil {
return x.ID
}
return 0
}
func (x *Reading) GetTypeI() int32 {
if x != nil {
return x.TypeI
}
return 0
}
func (x *Reading) GetType() string {
if x != nil {
return x.Type
}
return ""
}
func (x *Reading) GetLabel() string {
if x != nil {
return x.Label
}
return ""
}
func (x *Reading) GetUnit() string {
if x != nil {
return x.Unit
}
return ""
}
func (x *Reading) GetValue() float64 {
if x != nil {
return x.Value
}
return 0
}
func (x *Reading) GetValueMin() float64 {
if x != nil {
return x.ValueMin
}
return 0
}
func (x *Reading) GetValueMax() float64 {
if x != nil {
return x.ValueMax
}
return 0
}
func (x *Reading) GetValueAvg() float64 {
if x != nil {
return x.ValueAvg
}
return 0
}
var File_pkg_service_proto_hwservice_proto protoreflect.FileDescriptor
var file_pkg_service_proto_hwservice_proto_rawDesc = []byte{
0x0a, 0x21, 0x70, 0x6b, 0x67, 0x2f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2f, 0x70, 0x72,
0x6f, 0x74, 0x6f, 0x2f, 0x68, 0x77, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x70, 0x72,
0x6f, 0x74, 0x6f, 0x12, 0x05, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x1b, 0x67, 0x6f, 0x6f, 0x67,
0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x65, 0x6d, 0x70, 0x74,
0x79, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x2b, 0x0a, 0x0d, 0x50, 0x6f, 0x6c, 0x6c, 0x54,
0x69, 0x6d, 0x65, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, 0x1a, 0x0a, 0x08, 0x70, 0x6f, 0x6c, 0x6c,
0x54, 0x69, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x04, 0x52, 0x08, 0x70, 0x6f, 0x6c, 0x6c,
0x54, 0x69, 0x6d, 0x65, 0x22, 0x2c, 0x0a, 0x06, 0x53, 0x65, 0x6e, 0x73, 0x6f, 0x72, 0x12, 0x0e,
0x0a, 0x02, 0x49, 0x44, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x49, 0x44, 0x12, 0x12,
0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61,
0x6d, 0x65, 0x22, 0x21, 0x0a, 0x0f, 0x53, 0x65, 0x6e, 0x73, 0x6f, 0x72, 0x49, 0x44, 0x52, 0x65,
0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28,
0x09, 0x52, 0x02, 0x69, 0x64, 0x22, 0xd7, 0x01, 0x0a, 0x07, 0x52, 0x65, 0x61, 0x64, 0x69, 0x6e,
0x67, 0x12, 0x0e, 0x0a, 0x02, 0x49, 0x44, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x02, 0x49,
0x44, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x79, 0x70, 0x65, 0x49, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05,
0x52, 0x05, 0x74, 0x79, 0x70, 0x65, 0x49, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x18,
0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x74, 0x79, 0x70, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x6c,
0x61, 0x62, 0x65, 0x6c, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x6c, 0x61, 0x62, 0x65,
0x6c, 0x12, 0x12, 0x0a, 0x04, 0x75, 0x6e, 0x69, 0x74, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52,
0x04, 0x75, 0x6e, 0x69, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x06,
0x20, 0x01, 0x28, 0x01, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x76,
0x61, 0x6c, 0x75, 0x65, 0x4d, 0x69, 0x6e, 0x18, 0x07, 0x20, 0x01, 0x28, 0x01, 0x52, 0x08, 0x76,
0x61, 0x6c, 0x75, 0x65, 0x4d, 0x69, 0x6e, 0x12, 0x1a, 0x0a, 0x08, 0x76, 0x61, 0x6c, 0x75, 0x65,
0x4d, 0x61, 0x78, 0x18, 0x08, 0x20, 0x01, 0x28, 0x01, 0x52, 0x08, 0x76, 0x61, 0x6c, 0x75, 0x65,
0x4d, 0x61, 0x78, 0x12, 0x1a, 0x0a, 0x08, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x41, 0x76, 0x67, 0x18,
0x09, 0x20, 0x01, 0x28, 0x01, 0x52, 0x08, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x41, 0x76, 0x67, 0x32,
0xc0, 0x01, 0x0a, 0x09, 0x48, 0x57, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x3a, 0x0a,
0x08, 0x50, 0x6f, 0x6c, 0x6c, 0x54, 0x69, 0x6d, 0x65, 0x12, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67,
0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74,
0x79, 0x1a, 0x14, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x50, 0x6f, 0x6c, 0x6c, 0x54, 0x69,
0x6d, 0x65, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x22, 0x00, 0x12, 0x34, 0x0a, 0x07, 0x53, 0x65, 0x6e,
0x73, 0x6f, 0x72, 0x73, 0x12, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72,
0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x1a, 0x0d, 0x2e, 0x70,
0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x53, 0x65, 0x6e, 0x73, 0x6f, 0x72, 0x22, 0x00, 0x30, 0x01, 0x12,
0x41, 0x0a, 0x13, 0x52, 0x65, 0x61, 0x64, 0x69, 0x6e, 0x67, 0x73, 0x46, 0x6f, 0x72, 0x53, 0x65,
0x6e, 0x73, 0x6f, 0x72, 0x49, 0x44, 0x12, 0x16, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x53,
0x65, 0x6e, 0x73, 0x6f, 0x72, 0x49, 0x44, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x0e,
0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x52, 0x65, 0x61, 0x64, 0x69, 0x6e, 0x67, 0x22, 0x00,
0x30, 0x01, 0x42, 0x37, 0x5a, 0x35, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d,
0x2f, 0x73, 0x68, 0x61, 0x79, 0x6e, 0x65, 0x2f, 0x68, 0x77, 0x69, 0x6e, 0x66, 0x6f, 0x2d, 0x73,
0x74, 0x72, 0x65, 0x61, 0x6d, 0x64, 0x65, 0x63, 0x6b, 0x2f, 0x70, 0x6b, 0x67, 0x2f, 0x73, 0x65,
0x72, 0x76, 0x69, 0x63, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f,
0x74, 0x6f, 0x33,
}
var (
file_pkg_service_proto_hwservice_proto_rawDescOnce sync.Once
file_pkg_service_proto_hwservice_proto_rawDescData = file_pkg_service_proto_hwservice_proto_rawDesc
)
func file_pkg_service_proto_hwservice_proto_rawDescGZIP() []byte {
file_pkg_service_proto_hwservice_proto_rawDescOnce.Do(func() {
file_pkg_service_proto_hwservice_proto_rawDescData = protoimpl.X.CompressGZIP(file_pkg_service_proto_hwservice_proto_rawDescData)
})
return file_pkg_service_proto_hwservice_proto_rawDescData
}
var file_pkg_service_proto_hwservice_proto_msgTypes = make([]protoimpl.MessageInfo, 4)
var file_pkg_service_proto_hwservice_proto_goTypes = []interface{}{
(*PollTimeReply)(nil), // 0: proto.PollTimeReply
(*Sensor)(nil), // 1: proto.Sensor
(*SensorIDRequest)(nil), // 2: proto.SensorIDRequest
(*Reading)(nil), // 3: proto.Reading
(*emptypb.Empty)(nil), // 4: google.protobuf.Empty
}
var file_pkg_service_proto_hwservice_proto_depIdxs = []int32{
4, // 0: proto.HWService.PollTime:input_type -> google.protobuf.Empty
4, // 1: proto.HWService.Sensors:input_type -> google.protobuf.Empty
2, // 2: proto.HWService.ReadingsForSensorID:input_type -> proto.SensorIDRequest
0, // 3: proto.HWService.PollTime:output_type -> proto.PollTimeReply
1, // 4: proto.HWService.Sensors:output_type -> proto.Sensor
3, // 5: proto.HWService.ReadingsForSensorID:output_type -> proto.Reading
3, // [3:6] is the sub-list for method output_type
0, // [0:3] is the sub-list for method input_type
0, // [0:0] is the sub-list for extension type_name
0, // [0:0] is the sub-list for extension extendee
0, // [0:0] is the sub-list for field type_name
}
func init() { file_pkg_service_proto_hwservice_proto_init() }
func file_pkg_service_proto_hwservice_proto_init() {
if File_pkg_service_proto_hwservice_proto != nil {
return
}
if !protoimpl.UnsafeEnabled {
file_pkg_service_proto_hwservice_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*PollTimeReply); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_pkg_service_proto_hwservice_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*Sensor); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_pkg_service_proto_hwservice_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*SensorIDRequest); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_pkg_service_proto_hwservice_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*Reading); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_pkg_service_proto_hwservice_proto_rawDesc,
NumEnums: 0,
NumMessages: 4,
NumExtensions: 0,
NumServices: 1,
},
GoTypes: file_pkg_service_proto_hwservice_proto_goTypes,
DependencyIndexes: file_pkg_service_proto_hwservice_proto_depIdxs,
MessageInfos: file_pkg_service_proto_hwservice_proto_msgTypes,
}.Build()
File_pkg_service_proto_hwservice_proto = out.File
file_pkg_service_proto_hwservice_proto_rawDesc = nil
file_pkg_service_proto_hwservice_proto_goTypes = nil
file_pkg_service_proto_hwservice_proto_depIdxs = nil
}

View File

@@ -0,0 +1,34 @@
syntax = "proto3";
option go_package = "github.com/shayne/hwinfo-streamdeck/pkg/service/proto";
import "google/protobuf/empty.proto";
package proto;
service HWService {
rpc PollTime(google.protobuf.Empty) returns (PollTimeReply) {}
rpc Sensors(google.protobuf.Empty) returns (stream Sensor) {}
rpc ReadingsForSensorID(SensorIDRequest) returns (stream Reading) {}
}
message PollTimeReply { uint64 pollTime = 1; }
message Sensor {
string ID = 1;
string name = 2;
}
message SensorIDRequest { string id = 1; }
message Reading {
int32 ID = 1;
int32 typeI = 2;
string type = 3;
string label = 4;
string unit = 5;
double value = 6;
double valueMin = 7;
double valueMax = 8;
double valueAvg = 9;
}

View File

@@ -0,0 +1,233 @@
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
// versions:
// - protoc-gen-go-grpc v1.2.0
// - protoc v3.21.12
// source: pkg/service/proto/hwservice.proto
package proto
import (
context "context"
grpc "google.golang.org/grpc"
codes "google.golang.org/grpc/codes"
status "google.golang.org/grpc/status"
emptypb "google.golang.org/protobuf/types/known/emptypb"
)
// This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against.
// Requires gRPC-Go v1.32.0 or later.
const _ = grpc.SupportPackageIsVersion7
// HWServiceClient is the client API for HWService service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
type HWServiceClient interface {
PollTime(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*PollTimeReply, error)
Sensors(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (HWService_SensorsClient, error)
ReadingsForSensorID(ctx context.Context, in *SensorIDRequest, opts ...grpc.CallOption) (HWService_ReadingsForSensorIDClient, error)
}
type hWServiceClient struct {
cc grpc.ClientConnInterface
}
func NewHWServiceClient(cc grpc.ClientConnInterface) HWServiceClient {
return &hWServiceClient{cc}
}
func (c *hWServiceClient) PollTime(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*PollTimeReply, error) {
out := new(PollTimeReply)
err := c.cc.Invoke(ctx, "/proto.HWService/PollTime", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *hWServiceClient) Sensors(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (HWService_SensorsClient, error) {
stream, err := c.cc.NewStream(ctx, &HWService_ServiceDesc.Streams[0], "/proto.HWService/Sensors", opts...)
if err != nil {
return nil, err
}
x := &hWServiceSensorsClient{stream}
if err := x.ClientStream.SendMsg(in); err != nil {
return nil, err
}
if err := x.ClientStream.CloseSend(); err != nil {
return nil, err
}
return x, nil
}
type HWService_SensorsClient interface {
Recv() (*Sensor, error)
grpc.ClientStream
}
type hWServiceSensorsClient struct {
grpc.ClientStream
}
func (x *hWServiceSensorsClient) Recv() (*Sensor, error) {
m := new(Sensor)
if err := x.ClientStream.RecvMsg(m); err != nil {
return nil, err
}
return m, nil
}
func (c *hWServiceClient) ReadingsForSensorID(ctx context.Context, in *SensorIDRequest, opts ...grpc.CallOption) (HWService_ReadingsForSensorIDClient, error) {
stream, err := c.cc.NewStream(ctx, &HWService_ServiceDesc.Streams[1], "/proto.HWService/ReadingsForSensorID", opts...)
if err != nil {
return nil, err
}
x := &hWServiceReadingsForSensorIDClient{stream}
if err := x.ClientStream.SendMsg(in); err != nil {
return nil, err
}
if err := x.ClientStream.CloseSend(); err != nil {
return nil, err
}
return x, nil
}
type HWService_ReadingsForSensorIDClient interface {
Recv() (*Reading, error)
grpc.ClientStream
}
type hWServiceReadingsForSensorIDClient struct {
grpc.ClientStream
}
func (x *hWServiceReadingsForSensorIDClient) Recv() (*Reading, error) {
m := new(Reading)
if err := x.ClientStream.RecvMsg(m); err != nil {
return nil, err
}
return m, nil
}
// HWServiceServer is the server API for HWService service.
// All implementations must embed UnimplementedHWServiceServer
// for forward compatibility
type HWServiceServer interface {
PollTime(context.Context, *emptypb.Empty) (*PollTimeReply, error)
Sensors(*emptypb.Empty, HWService_SensorsServer) error
ReadingsForSensorID(*SensorIDRequest, HWService_ReadingsForSensorIDServer) error
mustEmbedUnimplementedHWServiceServer()
}
// UnimplementedHWServiceServer must be embedded to have forward compatible implementations.
type UnimplementedHWServiceServer struct {
}
func (UnimplementedHWServiceServer) PollTime(context.Context, *emptypb.Empty) (*PollTimeReply, error) {
return nil, status.Errorf(codes.Unimplemented, "method PollTime not implemented")
}
func (UnimplementedHWServiceServer) Sensors(*emptypb.Empty, HWService_SensorsServer) error {
return status.Errorf(codes.Unimplemented, "method Sensors not implemented")
}
func (UnimplementedHWServiceServer) ReadingsForSensorID(*SensorIDRequest, HWService_ReadingsForSensorIDServer) error {
return status.Errorf(codes.Unimplemented, "method ReadingsForSensorID not implemented")
}
func (UnimplementedHWServiceServer) mustEmbedUnimplementedHWServiceServer() {}
// UnsafeHWServiceServer may be embedded to opt out of forward compatibility for this service.
// Use of this interface is not recommended, as added methods to HWServiceServer will
// result in compilation errors.
type UnsafeHWServiceServer interface {
mustEmbedUnimplementedHWServiceServer()
}
func RegisterHWServiceServer(s grpc.ServiceRegistrar, srv HWServiceServer) {
s.RegisterService(&HWService_ServiceDesc, srv)
}
func _HWService_PollTime_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(emptypb.Empty)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(HWServiceServer).PollTime(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/proto.HWService/PollTime",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(HWServiceServer).PollTime(ctx, req.(*emptypb.Empty))
}
return interceptor(ctx, in, info, handler)
}
func _HWService_Sensors_Handler(srv interface{}, stream grpc.ServerStream) error {
m := new(emptypb.Empty)
if err := stream.RecvMsg(m); err != nil {
return err
}
return srv.(HWServiceServer).Sensors(m, &hWServiceSensorsServer{stream})
}
type HWService_SensorsServer interface {
Send(*Sensor) error
grpc.ServerStream
}
type hWServiceSensorsServer struct {
grpc.ServerStream
}
func (x *hWServiceSensorsServer) Send(m *Sensor) error {
return x.ServerStream.SendMsg(m)
}
func _HWService_ReadingsForSensorID_Handler(srv interface{}, stream grpc.ServerStream) error {
m := new(SensorIDRequest)
if err := stream.RecvMsg(m); err != nil {
return err
}
return srv.(HWServiceServer).ReadingsForSensorID(m, &hWServiceReadingsForSensorIDServer{stream})
}
type HWService_ReadingsForSensorIDServer interface {
Send(*Reading) error
grpc.ServerStream
}
type hWServiceReadingsForSensorIDServer struct {
grpc.ServerStream
}
func (x *hWServiceReadingsForSensorIDServer) Send(m *Reading) error {
return x.ServerStream.SendMsg(m)
}
// HWService_ServiceDesc is the grpc.ServiceDesc for HWService service.
// It's only intended for direct use with grpc.RegisterService,
// and not to be introspected or modified (even as a copy)
var HWService_ServiceDesc = grpc.ServiceDesc{
ServiceName: "proto.HWService",
HandlerType: (*HWServiceServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "PollTime",
Handler: _HWService_PollTime_Handler,
},
},
Streams: []grpc.StreamDesc{
{
StreamName: "Sensors",
Handler: _HWService_Sensors_Handler,
ServerStreams: true,
},
{
StreamName: "ReadingsForSensorID",
Handler: _HWService_ReadingsForSensorID_Handler,
ServerStreams: true,
},
},
Metadata: "pkg/service/proto/hwservice.proto",
}

View File

@@ -0,0 +1,295 @@
package streamdeck
import (
"encoding/base64"
"encoding/json"
"fmt"
"log"
"net/url"
"os"
"os/signal"
"time"
"github.com/gorilla/websocket"
)
// EventDelegate receives callbacks for Stream Deck SDK events
type EventDelegate interface {
OnConnected(*websocket.Conn)
OnWillAppear(*EvWillAppear)
OnTitleParametersDidChange(*EvTitleParametersDidChange)
OnPropertyInspectorConnected(*EvSendToPlugin)
OnSendToPlugin(*EvSendToPlugin)
OnApplicationDidLaunch(*EvApplication)
OnApplicationDidTerminate(*EvApplication)
}
// StreamDeck SDK APIs
type StreamDeck struct {
Port string
PluginUUID string
RegisterEvent string
Info string
delegate EventDelegate
conn *websocket.Conn
done chan struct{}
}
// NewStreamDeck prepares StreamDeck struct
func NewStreamDeck(port, pluginUUID, registerEvent, info string) *StreamDeck {
return &StreamDeck{
Port: port,
PluginUUID: pluginUUID,
RegisterEvent: registerEvent,
Info: info,
done: make(chan struct{}),
}
}
// SetDelegate sets the delegate for receiving Stream Deck SDK event callbacks
func (sd *StreamDeck) SetDelegate(ed EventDelegate) {
sd.delegate = ed
}
func (sd *StreamDeck) register() error {
reg := evRegister{Event: sd.RegisterEvent, UUID: sd.PluginUUID}
data, err := json.Marshal(reg)
log.Println(string(data))
if err != nil {
return err
}
err = sd.conn.WriteMessage(websocket.TextMessage, data)
if err != nil {
return err
}
return nil
}
// Connect establishes WebSocket connection to StreamDeck software
func (sd *StreamDeck) Connect() error {
u := url.URL{Scheme: "ws", Host: fmt.Sprintf("127.0.0.1:%s", sd.Port)}
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
return err
}
sd.conn = c
err = sd.register()
if err != nil {
return fmt.Errorf("failed register: %v", err)
}
if sd.delegate != nil {
sd.delegate.OnConnected(sd.conn)
}
return nil
}
// Close closes the websocket connection, defer after Connect
func (sd *StreamDeck) Close() {
sd.conn.Close()
}
func (sd *StreamDeck) onPropertyInspectorMessage(value string, ev *EvSendToPlugin) error {
switch value {
case "propertyInspectorConnected":
if sd.delegate != nil {
sd.delegate.OnPropertyInspectorConnected(ev)
}
default:
log.Printf("Unknown property_inspector value: %s\n", value)
}
return nil
}
func (sd *StreamDeck) onSendToPlugin(ev *EvSendToPlugin) error {
payload := make(map[string]*json.RawMessage)
err := json.Unmarshal(*ev.Payload, &payload)
if err != nil {
return fmt.Errorf("onSendToPlugin payload unmarshal: %v", err)
}
if raw, ok := payload["property_inspector"]; ok {
var value string
err := json.Unmarshal(*raw, &value)
if err != nil {
return fmt.Errorf("onSendToPlugin unmarshal property_inspector value: %v", err)
}
err = sd.onPropertyInspectorMessage(value, ev)
if err != nil {
return fmt.Errorf("onPropertyInspectorMessage: %v", err)
}
return nil
}
if sd.delegate != nil {
sd.delegate.OnSendToPlugin(ev)
}
return nil
}
func (sd *StreamDeck) spawnMessageReader() {
defer close(sd.done)
for {
_, message, err := sd.conn.ReadMessage()
if err != nil {
log.Println("read:", err)
return
}
log.Printf("recv: %s", message)
var objmap map[string]*json.RawMessage
err = json.Unmarshal(message, &objmap)
if err != nil {
log.Fatal("message unmarshal", err)
}
var event string
err = json.Unmarshal(*objmap["event"], &event)
if err != nil {
log.Fatal("event unmarshal", err)
}
switch event {
case "willAppear":
var ev EvWillAppear
err := json.Unmarshal(message, &ev)
if err != nil {
log.Fatal("willAppear unmarshal", err)
}
if sd.delegate != nil {
sd.delegate.OnWillAppear(&ev)
}
case "titleParametersDidChange":
var ev EvTitleParametersDidChange
err := json.Unmarshal(message, &ev)
if err != nil {
log.Fatal("titleParametersDidChange unmarshal", err)
}
if sd.delegate != nil {
sd.delegate.OnTitleParametersDidChange(&ev)
}
case "sendToPlugin":
var ev EvSendToPlugin
err := json.Unmarshal(message, &ev)
if err != nil {
log.Fatal("onSendToPlugin event unmarshal", err)
}
err = sd.onSendToPlugin(&ev)
if err != nil {
log.Fatal("onSendToPlugin", err)
}
case "applicationDidLaunch":
var ev EvApplication
err := json.Unmarshal(message, &ev)
if err != nil {
log.Fatal("applicationDidLaunch unmarshal", err)
}
if sd.delegate != nil {
sd.delegate.OnApplicationDidLaunch(&ev)
}
case "applicationDidTerminate":
var ev EvApplication
err := json.Unmarshal(message, &ev)
if err != nil {
log.Fatal("applicationDidTerminate unmarshal", err)
}
if sd.delegate != nil {
sd.delegate.OnApplicationDidTerminate(&ev)
}
default:
log.Printf("Unknown event: %s\n", event)
}
}
}
// ListenAndWait processes messages and waits until closed
func (sd *StreamDeck) ListenAndWait() {
go sd.spawnMessageReader()
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
for {
select {
case <-sd.done:
return
case <-interrupt:
log.Println("interrupt")
// Cleanly close the connection by sending a close message and then
// waiting (with timeout) for the server to close the connection.
err := sd.conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
if err != nil {
log.Println("write close:", err)
return
}
select {
case <-sd.done:
case <-time.After(time.Second):
}
}
}
}
// SendToPropertyInspector sends a payload to the Property Inspector
func (sd *StreamDeck) SendToPropertyInspector(action, context string, payload interface{}) error {
event := evSendToPropertyInspector{Action: action, Event: "sendToPropertyInspector",
Context: context, Payload: payload}
data, err := json.Marshal(event)
if err != nil {
return fmt.Errorf("sendToPropertyInspector: %v", err)
}
err = sd.conn.WriteMessage(websocket.TextMessage, data)
if err != nil {
return fmt.Errorf("setTitle write: %v", err)
}
return nil
}
// SetTitle dynamically changes the title displayed by an instance of an action
func (sd *StreamDeck) SetTitle(context, title string) error {
event := evSetTitle{Event: "setTitle", Context: context, Payload: evSetTitlePayload{
Title: title,
Target: 0,
}}
data, err := json.Marshal(event)
if err != nil {
return fmt.Errorf("setTitle: %v", err)
}
err = sd.conn.WriteMessage(websocket.TextMessage, data)
if err != nil {
return fmt.Errorf("setTitle write: %v", err)
}
return nil
}
// SetSettings saves persistent data for the action's instance
func (sd *StreamDeck) SetSettings(context string, payload interface{}) error {
event := evSetSettings{Event: "setSettings", Context: context, Payload: payload}
data, err := json.Marshal(event)
if err != nil {
return fmt.Errorf("setSettings: %v", err)
}
err = sd.conn.WriteMessage(websocket.TextMessage, data)
if err != nil {
return fmt.Errorf("setSettings write: %v", err)
}
return nil
}
// SetImage dynamically changes the image displayed by an instance of an action
func (sd *StreamDeck) SetImage(context string, bts []byte) error {
b64 := base64.StdEncoding.EncodeToString(bts)
event := evSetImage{Event: "setImage", Context: context, Payload: evSetImagePayload{
Image: fmt.Sprintf("data:image/png;base64, %s", b64),
Target: 0,
}}
data, err := json.Marshal(event)
if err != nil {
return fmt.Errorf("setImage: %v", err)
}
err = sd.conn.WriteMessage(websocket.TextMessage, data)
if err != nil {
return fmt.Errorf("setImage write: %v", err)
}
return nil
}

124
pkg/streamdeck/types.go Normal file
View File

@@ -0,0 +1,124 @@
package streamdeck
import "encoding/json"
type evRegister struct {
Event string `json:"event"`
UUID string `json:"uuid"`
}
// EvCoordinates is the coordinates structure from events
type EvCoordinates struct {
Column int `json:"column"`
Row int `json:"row"`
}
// EvWillAppearPayload is the Payload structure from the willAppear event
type EvWillAppearPayload struct {
Settings *json.RawMessage `json:"settings"`
Coordinates EvCoordinates `json:"coordinates"`
Device string `json:"device"`
State int `json:"state"`
IsInMultiAction bool `json:"isInMultiAction"`
}
// EvWillAppear is the payload from the willAppear event
type EvWillAppear struct {
Action string `json:"action"`
Event string `json:"event"`
Context string `json:"context"`
Device string `json:"device"`
Payload EvWillAppearPayload `json:"payload"`
}
// EvWillDisappearPayload is the Payload structure from willDisappear event
type EvWillDisappearPayload struct {
EvWillAppearPayload
}
// EvWillDisappear is the payload from the willDisappear event
type EvWillDisappear struct {
EvWillAppear
}
// EvApplicationPayload is the sub-strcture from the EvApplication struct
type EvApplicationPayload struct {
Application string `json:"application"`
}
// EvApplication is the payload from the applicatioDidLaunch/Terminate events
type EvApplication struct {
Payload EvApplicationPayload `json:"payload"`
}
// EvTitleParameters is sub-structure from EvTitleParametersDidChangePayload
type EvTitleParameters struct {
FontFamily string `json:"fontFamily"`
FontSize int `json:"fontSize"`
FontStyle string `json:"fontStyle"`
FontUnderline bool `json:"fontUnderline"`
ShowTitle bool `json:"showTitle"`
TitleAlignment string `json:"titleAlignment"`
TitleColor string `json:"titleColor"`
}
// EvTitleParametersDidChangePayload is the payload structure of EvTitleParametersDidChange
type EvTitleParametersDidChangePayload struct {
Coordinates EvCoordinates `json:"coordinates"`
Settings *json.RawMessage `json:"settings"`
TitleParameters EvTitleParameters `json:"titleParameters"`
Title string `json:"title"`
State int `json:"state"`
}
// EvTitleParametersDidChange is the payload from the titleParametersDidChange event
type EvTitleParametersDidChange struct {
Action string `json:"action"`
Event string `json:"event"`
Context string `json:"context"`
Device string `json:"device"`
Payload EvTitleParametersDidChangePayload `json:"payload"`
}
// EvSendToPlugin is received from the Property Inspector
type EvSendToPlugin struct {
Action string `json:"action"`
Event string `json:"event"`
Context string `json:"context"`
Payload *json.RawMessage `json:"payload"`
}
type evSendToPropertyInspector struct {
Action string `json:"action"`
Event string `json:"event"`
Context string `json:"context"`
Payload interface{} `json:"payload"`
}
type evSetTitlePayload struct {
Title string `json:"title"`
Target int `json:"target"`
}
type evSetTitle struct {
Event string `json:"event"`
Context string `json:"context"`
Payload evSetTitlePayload `json:"payload"`
}
type evSetSettings struct {
Event string `json:"event"`
Context string `json:"context"`
Payload interface{} `json:"payload"`
}
type evSetImagePayload struct {
Image string `json:"image"`
Target int `json:"target"`
}
type evSetImage struct {
Event string `json:"event"`
Context string `json:"context"`
Payload evSetImagePayload `json:"payload"`
}