Major restructure and added a few example plugins.

This commit is contained in:
Sam Rose
2019-07-14 21:44:00 +01:00
parent cad454a4bf
commit 8ee19c457a
20 changed files with 810 additions and 310 deletions

21
examples/cpu/Makefile Normal file
View File

@ -0,0 +1,21 @@
GO = go
GOFLAGS =
INSTALLDIR = "$(APPDATA)\Elgato\StreamDeck\Plugins\dev.samwho.streamdeck.cpu.sdPlugin"
LOGDIR = "$(APPDATA)\Elgato\StreamDeck\logs"
.PHONY: test install build logs
build:
$(GO) build $(GOFLAGS)
test:
$(GO) run $(GOFLAGS) main.go -port 12345 -pluginUUID 213 -registerEvent test -info "{\"application\":{\"language\":\"en\",\"platform\":\"mac\",\"version\":\"4.1.0\"},\"plugin\":{\"version\":\"1.1\"},\"devicePixelRatio\":2,\"devices\":[{\"id\":\"55F16B35884A859CCE4FFA1FC8D3DE5B\",\"name\":\"Device Name\",\"size\":{\"columns\":5,\"rows\":3},\"type\":0},{\"id\":\"B8F04425B95855CF417199BCB97CD2BB\",\"name\":\"Another Device\",\"size\":{\"columns\":3,\"rows\":2},\"type\":1}]}"
install: build
rm -rf $(INSTALLDIR)
mkdir $(INSTALLDIR)
cp *.json $(INSTALLDIR)
cp *.exe $(INSTALLDIR)
logs:
tail -f $(LOGDIR)/cpu.log

118
examples/cpu/main.go Normal file
View File

@ -0,0 +1,118 @@
package main
import (
"context"
"fmt"
"image"
"image/color"
"log"
"os"
"time"
"github.com/samwho/streamdeck/payload"
"github.com/samwho/streamdeck"
sdcontext "github.com/samwho/streamdeck/context"
"github.com/shirou/gopsutil/cpu"
)
const (
logFile = "C:\\Users\\samwh\\AppData\\Roaming\\Elgato\\StreamDeck\\logs\\cpu.log"
imgX = 72
imgY = 72
)
func main() {
f, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
defer f.Close()
log.SetOutput(f)
ctx := context.Background()
if err := run(ctx); err != nil {
log.Fatalf("%v\n", err)
}
}
func run(ctx context.Context) error {
params, err := streamdeck.ParseRegistrationParams(os.Args)
if err != nil {
return err
}
client := streamdeck.NewClient(ctx, params)
setup(client)
return client.Run()
}
func setup(client *streamdeck.Client) {
action := client.Action("dev.samwho.streamdeck.cpu")
contexts := make(map[string]struct{})
action.RegisterHandler(streamdeck.WillAppear, func(ctx context.Context, client *streamdeck.Client, event streamdeck.Event) error {
contexts[event.Context] = struct{}{}
return nil
})
action.RegisterHandler(streamdeck.WillDisappear, func(ctx context.Context, client *streamdeck.Client, event streamdeck.Event) error {
delete(contexts, event.Context)
return nil
})
readings := make([]float64, imgX, imgX)
go func() {
for range time.Tick(time.Second) {
for i := 0; i < imgX-1; i++ {
readings[i] = readings[i+1]
}
r, err := cpu.Percent(0, false)
if err != nil {
log.Printf("error getting CPU reading: %v\n", err)
}
readings[imgX-1] = r[0]
for ctxStr := range contexts {
ctx := context.Background()
ctx = sdcontext.WithContext(ctx, ctxStr)
img, err := streamdeck.Image(graph(readings))
if err != nil {
log.Printf("error creating image: %v\n", err)
continue
}
if err := client.SetImage(ctx, img, payload.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 {
log.Printf("error setting title: %v\n", err)
continue
}
}
}
}()
}
func graph(readings []float64) image.Image {
img := image.NewRGBA(image.Rect(0, 0, imgX, imgY))
for x := 0; x < imgX; x++ {
reading := readings[x] / 100
upto := int(float64(imgY) * reading)
for y := 0; y < upto; y++ {
img.Set(x, imgY-y, color.RGBA{R: 255, A: 255})
}
for y := upto; y < imgY; y++ {
img.Set(x, imgY-y, color.Black)
}
}
return img
}

View File

@ -0,0 +1,37 @@
{
"Actions": [
{
"Name": "CPU graph",
"States": [
{
"TitleAlignment": "middle",
"FontSize": "14"
}
],
"SupportedInMultiActions": false,
"Tooltip": "Show a pretty little CPU graph",
"UUID": "dev.samwho.streamdeck.cpu"
}
],
"SDKVersion": 2,
"Author": "Sam Rose",
"CodePath": "cpu.exe",
"Description": "Show a pretty little CPU graph",
"Name": "CPU graph",
"URL": "https://samwho.dev",
"Version": "0.1",
"OS": [
{
"Platform": "mac",
"MinimumVersion" : "10.11"
},
{
"Platform": "windows",
"MinimumVersion" : "10"
}
],
"Software":
{
"MinimumVersion" : "4.1"
}
}