Updated examples, added property inspector example to one

This commit is contained in:
Sam Rose 2019-07-16 20:57:16 +01:00
parent f3f3f4034c
commit 503477a3c3
7 changed files with 1656 additions and 21 deletions

View File

@ -1,7 +1,6 @@
GO = go GO = go
GOFLAGS = GOFLAGS =
INSTALLDIR = "$(APPDATA)\Elgato\StreamDeck\Plugins\dev.samwho.streamdeck.counter.sdPlugin" INSTALLDIR = "$(APPDATA)\Elgato\StreamDeck\Plugins\dev.samwho.streamdeck.counter.sdPlugin"
LOGDIR = "$(APPDATA)\Elgato\StreamDeck\logs"
.PHONY: test install build logs .PHONY: test install build logs
@ -18,4 +17,4 @@ install: build
cp *.exe $(INSTALLDIR) cp *.exe $(INSTALLDIR)
logs: logs:
tail -f $(LOGDIR)/counter.log tail -f "$(TMP)"/streamdeck-counter.log*

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"image" "image"
"image/color" "image/color"
"io/ioutil"
"log" "log"
"os" "os"
"strconv" "strconv"
@ -13,20 +14,17 @@ import (
"github.com/samwho/streamdeck" "github.com/samwho/streamdeck"
) )
const (
logFile = "C:\\Users\\samwh\\AppData\\Roaming\\Elgato\\StreamDeck\\logs\\streamdeck-livesplit.log"
)
type Settings struct { type Settings struct {
Counter int `json:"counter"` Counter int `json:"counter"`
} }
func main() { func main() {
f, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) f, err := ioutil.TempFile("", "streamdeck-counter.log")
if err != nil { if err != nil {
log.Fatalf("error opening file: %v", err) log.Fatalf("error creating temp file: %v", err)
} }
defer f.Close() defer f.Close()
log.SetOutput(f) log.SetOutput(f)
ctx := context.Background() ctx := context.Background()
@ -42,12 +40,12 @@ func run(ctx context.Context) error {
} }
client := streamdeck.NewClient(ctx, params) client := streamdeck.NewClient(ctx, params)
setupCounter(client) setup(client)
return client.Run() return client.Run()
} }
func setupCounter(client *streamdeck.Client) { func setup(client *streamdeck.Client) {
action := client.Action("dev.samwho.streamdeck.counter") action := client.Action("dev.samwho.streamdeck.counter")
settings := make(map[string]*Settings) settings := make(map[string]*Settings)
@ -101,9 +99,9 @@ func setupCounter(client *streamdeck.Client) {
} }
func background() image.Image { func background() image.Image {
img := image.NewRGBA(image.Rect(0, 0, 20, 20)) img := image.NewRGBA(image.Rect(0, 0, 72, 72))
for x := 0; x < 20; x++ { for x := 0; x < 72; x++ {
for y := 0; y < 20; y++ { for y := 0; y < 72; y++ {
img.Set(x, y, color.Black) img.Set(x, y, color.Black)
} }
} }

View File

@ -1,7 +1,6 @@
GO = go GO = go
GOFLAGS = GOFLAGS =
INSTALLDIR = "$(APPDATA)\Elgato\StreamDeck\Plugins\dev.samwho.streamdeck.cpu.sdPlugin" INSTALLDIR = "$(APPDATA)\Elgato\StreamDeck\Plugins\dev.samwho.streamdeck.cpu.sdPlugin"
LOGDIR = "$(APPDATA)\Elgato\StreamDeck\logs"
.PHONY: test install build logs .PHONY: test install build logs
@ -15,7 +14,9 @@ install: build
rm -rf $(INSTALLDIR) rm -rf $(INSTALLDIR)
mkdir $(INSTALLDIR) mkdir $(INSTALLDIR)
cp *.json $(INSTALLDIR) cp *.json $(INSTALLDIR)
cp *.html $(INSTALLDIR)
cp *.css $(INSTALLDIR)
cp *.exe $(INSTALLDIR) cp *.exe $(INSTALLDIR)
logs: logs:
tail -f $(LOGDIR)/cpu.log tail -f "$(TMP)"/streamdeck-cpu.log*

View File

@ -0,0 +1,47 @@
<head>
<meta charset="utf-8" />
<title>CPU graph</title>
<link rel="stylesheet" href="sdpi.css">
</head>
<body>
<div class="sdpi-wrapper">
<div type="checkbox" class="sdpi-item">
<div class="sdpi-item-label">Show text</div>
<div class="sdpi-item-child">
<input id="showText" type="checkbox" value="yes" onchange="sendToPlugin({ showText: document.getElementById('showText').checked })">
<label for="showText" class="sdpi-item-label"><span></span></label>
</div>
</div>
</div>
<script>
var websocket = null,
uuid = null,
actionInfo = {};
function connectElgatoStreamDeckSocket(inPort, inUUID, inRegisterEvent, inInfo, inActionInfo) {
uuid = inUUID;
actionInfo = JSON.parse(inActionInfo); // cache the info
websocket = new WebSocket('ws://localhost:' + inPort);
websocket.onopen = function () {
websocket.send(JSON.stringify({
event: inRegisterEvent,
uuid: inUUID
}));
}
}
function sendToPlugin(obj) {
if (websocket) {
websocket.send(JSON.stringify({
"action": actionInfo['action'],
"event": "sendToPlugin",
"context": uuid,
"payload": obj,
}));
}
}
</script>
</body>

View File

@ -2,9 +2,11 @@ package main
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"image" "image"
"image/color" "image/color"
"io/ioutil"
"log" "log"
"os" "os"
"time" "time"
@ -15,16 +17,18 @@ import (
) )
const ( const (
logFile = "C:\\Users\\samwh\\AppData\\Roaming\\Elgato\\StreamDeck\\logs\\cpu.log"
imgX = 72 imgX = 72
imgY = 72 imgY = 72
) )
type PropertyInspectorSettings struct {
ShowText bool `json:"showText,omitempty"`
}
func main() { func main() {
f, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) f, err := ioutil.TempFile("", "streamdeck-cpu.log")
if err != nil { if err != nil {
log.Fatalf("error opening file: %v", err) log.Fatalf("error creating tempfile: %v", err)
} }
defer f.Close() defer f.Close()
log.SetOutput(f) log.SetOutput(f)
@ -50,8 +54,13 @@ func run(ctx context.Context) error {
func setup(client *streamdeck.Client) { func setup(client *streamdeck.Client) {
action := client.Action("dev.samwho.streamdeck.cpu") action := client.Action("dev.samwho.streamdeck.cpu")
pi := &PropertyInspectorSettings{}
contexts := make(map[string]struct{}) contexts := make(map[string]struct{})
action.RegisterHandler(streamdeck.SendToPlugin, func(ctx context.Context, client *streamdeck.Client, event streamdeck.Event) error {
return json.Unmarshal(event.Payload, pi)
})
action.RegisterHandler(streamdeck.WillAppear, func(ctx context.Context, client *streamdeck.Client, event streamdeck.Event) error { action.RegisterHandler(streamdeck.WillAppear, func(ctx context.Context, client *streamdeck.Client, event streamdeck.Event) error {
contexts[event.Context] = struct{}{} contexts[event.Context] = struct{}{}
return nil return nil
@ -65,7 +74,7 @@ func setup(client *streamdeck.Client) {
readings := make([]float64, imgX, imgX) readings := make([]float64, imgX, imgX)
go func() { go func() {
for range time.Tick(time.Second) { for range time.Tick(time.Second / 4) {
for i := 0; i < imgX-1; i++ { for i := 0; i < imgX-1; i++ {
readings[i] = readings[i+1] readings[i] = readings[i+1]
} }
@ -91,7 +100,12 @@ func setup(client *streamdeck.Client) {
continue continue
} }
if err := client.SetTitle(ctx, fmt.Sprintf("CPU\n%d%%", int(r[0])), streamdeck.HardwareAndSoftware); err != nil { title := ""
if pi.ShowText {
title = fmt.Sprintf("CPU\n%d%%", int(r[0]))
}
if err := client.SetTitle(ctx, title, streamdeck.HardwareAndSoftware); err != nil {
log.Printf("error setting title: %v\n", err) log.Printf("error setting title: %v\n", err)
continue continue
} }

View File

@ -8,6 +8,7 @@
"FontSize": "14" "FontSize": "14"
} }
], ],
"PropertyInspectorPath": "cpu_property_inspector.html",
"SupportedInMultiActions": false, "SupportedInMultiActions": false,
"Tooltip": "Show a pretty little CPU graph", "Tooltip": "Show a pretty little CPU graph",
"UUID": "dev.samwho.streamdeck.cpu" "UUID": "dev.samwho.streamdeck.cpu"

1575
examples/cpu/sdpi.css Normal file

File diff suppressed because it is too large Load Diff