streamdeck-obs-replay/replay.go

304 lines
5.1 KiB
Go
Raw Normal View History

2020-01-15 04:32:53 +00:00
package main
import (
"github.com/valyala/fastjson"
"log"
"meow.tf/streamdeck/sdk"
"strconv"
"sync"
2020-01-16 05:19:22 +00:00
"time"
2020-01-15 04:32:53 +00:00
)
const (
actionReplayToggle = "tf.meow.obsreplay.replay_toggle"
actionReplaySave = "tf.meow.obsreplay.replay_save"
2020-01-15 04:32:53 +00:00
)
var (
contextMutex sync.RWMutex
clientMutex sync.RWMutex
stateMutex sync.RWMutex
2020-01-15 04:32:53 +00:00
2020-01-16 05:19:22 +00:00
clients = make(map[string]Client)
contexts = make(map[string]string)
contextActions = make(map[string]string)
cachedStates = make(map[string]int)
2020-01-15 04:32:53 +00:00
)
func replayToggle(action, context string, payload *fastjson.Value, deviceId string) {
c := clientForContext(context)
if c == nil {
sdk.ShowAlert(context)
return
}
2020-01-16 05:19:22 +00:00
if err := c.ToggleReplay(); err != nil {
2020-01-15 04:32:53 +00:00
sdk.ShowAlert(context)
return
}
2020-01-16 05:19:22 +00:00
time.AfterFunc(1*time.Second, func() {
contextMutex.RLock()
key, exists := contexts[context]
contextMutex.RUnlock()
if !exists {
return
}
stateMutex.RLock()
state, exists := cachedStates[key]
stateMutex.RUnlock()
if !exists {
return
}
sdk.SetState(context, state)
})
2020-01-15 04:32:53 +00:00
}
func replaySave(action, context string, payload *fastjson.Value, deviceId string) {
c := clientForContext(context)
if c == nil {
sdk.ShowAlert(context)
return
}
2020-01-16 05:19:22 +00:00
contextMutex.RLock()
key, exists := contexts[context]
contextMutex.RUnlock()
2020-01-16 05:19:22 +00:00
if exists {
stateMutex.RLock()
state, exists := cachedStates[key]
stateMutex.RUnlock()
2020-01-16 05:19:22 +00:00
if exists && state == 0 {
sdk.ShowAlert(context)
return
}
}
2020-01-15 04:32:53 +00:00
2020-01-16 05:19:22 +00:00
if err := c.SaveReplay(); err != nil {
2020-01-15 04:32:53 +00:00
sdk.ShowAlert(context)
return
}
sdk.ShowOk(context)
}
2020-01-16 05:19:22 +00:00
func clientForContext(context string) Client {
2020-01-15 04:32:53 +00:00
contextMutex.RLock()
key, exists := contexts[context]
contextMutex.RUnlock()
if !exists {
return nil
}
clientMutex.RLock()
c, exists := clients[key]
clientMutex.RUnlock()
if !exists {
return nil
}
if !c.Connected() {
err := c.Connect()
if err != nil {
return nil
}
}
2020-01-15 04:32:53 +00:00
return c
}
func onWillAppear(e *sdk.WillAppearEvent) {
settings := e.Payload.Get("settings")
if settings != nil {
host := sdk.JsonStringValue(settings, "host")
2020-01-16 05:19:22 +00:00
portStr := sdk.JsonStringValue(settings, "port")
2020-01-15 04:32:53 +00:00
password := sdk.JsonStringValue(settings, "password")
2020-01-16 05:19:22 +00:00
port, err := strconv.Atoi(portStr)
if err != nil {
port = 4444
}
2020-01-15 04:32:53 +00:00
key := checkClient(host, port, password)
if key == "" {
return
}
contextMutex.Lock()
contexts[e.Context] = key
2020-01-16 05:19:22 +00:00
contextActions[e.Context] = e.Action
2020-01-15 04:32:53 +00:00
contextMutex.Unlock()
2020-01-16 05:19:22 +00:00
if e.Action == actionReplayToggle {
stateMutex.RLock()
if state, ok := cachedStates[key]; ok {
sdk.SetState(e.Context, state)
}
stateMutex.RUnlock()
2020-01-15 04:32:53 +00:00
}
}
}
func checkClient(host string, port int, password string) string {
2020-01-16 05:19:22 +00:00
if host == "" {
host = "127.0.0.1"
}
2020-01-15 04:32:53 +00:00
if port == 0 {
port = 4444
}
key := host + ":" + strconv.Itoa(port)
clientMutex.RLock()
client, ok := clients[key]
clientMutex.RUnlock()
if !ok {
2020-01-16 05:19:22 +00:00
client = NewClient(key, host, port, password)
2020-01-15 04:32:53 +00:00
2020-01-16 05:19:22 +00:00
defer client.Connect()
2020-01-15 04:32:53 +00:00
clientMutex.Lock()
clients[key] = client
clientMutex.Unlock()
}
return key
}
2020-01-16 05:19:22 +00:00
func onWillDisappear(e *sdk.WillDisappearEvent) {
contextDiscounnected(e.Context)
2020-01-15 04:32:53 +00:00
}
2020-01-16 05:19:22 +00:00
func contextDiscounnected(context string) {
2020-01-15 04:32:53 +00:00
contextMutex.Lock()
defer contextMutex.Unlock()
// replayToggleContexts
2020-01-16 05:19:22 +00:00
key, ok := contexts[context]
2020-01-15 04:32:53 +00:00
2020-01-16 05:19:22 +00:00
delete(contexts, context)
delete(contextActions, context)
2020-01-15 04:32:53 +00:00
if !ok {
return
}
for _, k := range contexts {
if k == key {
return
}
}
clientMutex.Lock()
clients[key].Disconnect()
delete(clients, key)
clientMutex.Unlock()
}
func onSettingsReceived(e *sdk.ReceiveSettingsEvent) {
2020-01-16 05:19:22 +00:00
host := sdk.JsonStringValue(e.Settings, "host")
portStr := sdk.JsonStringValue(e.Settings, "port")
password := sdk.JsonStringValue(e.Settings, "password")
2020-01-15 04:32:53 +00:00
2020-01-16 05:19:22 +00:00
if host == "" {
host = "127.0.0.1"
}
2020-01-15 04:32:53 +00:00
2020-01-16 05:19:22 +00:00
port, err := strconv.Atoi(portStr)
if err != nil {
2020-01-15 04:32:53 +00:00
port = 4444
}
key := checkClient(host, port, password)
2020-01-16 05:19:22 +00:00
contextMutex.RLock()
previousKey, existing := contexts[e.Context]
contextMutex.RUnlock()
if existing && previousKey != key {
contextDiscounnected(e.Context)
}
2020-01-15 04:32:53 +00:00
if key == "" {
return
}
contextMutex.Lock()
contexts[e.Context] = key
contextMutex.Unlock()
stateMutex.RLock()
if state, ok := cachedStates[key]; ok {
sdk.SetState(e.Context, state)
}
stateMutex.RUnlock()
}
func main() {
sdk.RegisterAction(actionReplayToggle, replayToggle)
sdk.RegisterAction(actionReplaySave, replaySave)
sdk.AddHandler(onWillAppear)
sdk.AddHandler(onWillDisappear)
sdk.AddHandler(onSettingsReceived)
// Open and connect the SDK
err := sdk.Open()
if err != nil {
log.Fatalln(err)
}
defer cleanupSockets()
// Wait until the socket is closed, or SIGTERM/SIGINT is received
sdk.Wait()
}
func cleanupSockets() {
clientMutex.RLock()
defer clientMutex.RUnlock()
for _, client := range clients {
client.Disconnect()
}
}
2020-01-16 05:19:22 +00:00
func loopContextState(key string, state int) {
stateMutex.Lock()
cachedStates[key] = state
stateMutex.Unlock()
contextMutex.RLock()
defer contextMutex.RUnlock()
for ctx, ctxKey := range contexts {
if ctxKey == key {
action := contextActions[ctx]
if action == actionReplayToggle {
sdk.SetState(ctx, state)
}
}
}
}