122 lines
2.3 KiB
Go
122 lines
2.3 KiB
Go
|
package mpvipc
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"encoding/json"
|
||
|
"errors"
|
||
|
"net"
|
||
|
)
|
||
|
|
||
|
type Command struct {
|
||
|
Command []interface{} `json:"command"`
|
||
|
}
|
||
|
|
||
|
type Response[T any] struct {
|
||
|
Data T `json:"data"`
|
||
|
RequestID int `json:"request_id"`
|
||
|
Error string `json:"error"`
|
||
|
}
|
||
|
|
||
|
type Event[T any] struct {
|
||
|
Data T `json:"data"`
|
||
|
ID int `json:"id"`
|
||
|
Name string `json:"name"`
|
||
|
Error string `json:"error"`
|
||
|
}
|
||
|
|
||
|
func SendCommand[T any](socket string, cmd *Command) (*Response[T], error) {
|
||
|
conn, err := net.Dial("unix", socket)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
defer conn.Close()
|
||
|
|
||
|
if err := json.NewEncoder(conn).Encode(cmd); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
resp := &Response[T]{}
|
||
|
if err := json.NewDecoder(conn).Decode(resp); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return resp, nil
|
||
|
}
|
||
|
|
||
|
func GetProperty[T any](socket string, propertyName string) (T, error) {
|
||
|
cmd := &Command{[]interface{}{"get_property", propertyName}}
|
||
|
resp, err := SendCommand[T](socket, cmd)
|
||
|
if err != nil {
|
||
|
return *new(T), err
|
||
|
}
|
||
|
|
||
|
if resp.Error != "success" {
|
||
|
return *new(T), errors.New(resp.Error)
|
||
|
}
|
||
|
|
||
|
return resp.Data, nil
|
||
|
}
|
||
|
|
||
|
func SetProperty[T any](socket string, propertyName string, propertyValue T) error {
|
||
|
cmd := &Command{[]interface{}{"set_property", propertyName, propertyValue}}
|
||
|
resp, err := SendCommand[T](socket, cmd)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if resp.Error != "success" {
|
||
|
return errors.New(resp.Error)
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func ObserveProperty[T any](ctx context.Context, socket string, propertyName string) (<-chan T, error) {
|
||
|
out := make(chan T, 10)
|
||
|
|
||
|
conn, err := net.Dial("unix", socket)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
enc := json.NewEncoder(conn)
|
||
|
dec := json.NewDecoder(conn)
|
||
|
|
||
|
cmd := &Command{[]interface{}{"observe_property", 1, propertyName}}
|
||
|
if err := enc.Encode(cmd); err != nil {
|
||
|
conn.Close()
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
resp := &Response[T]{}
|
||
|
if err := json.NewDecoder(conn).Decode(resp); err != nil {
|
||
|
conn.Close()
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
if resp.Error != "success" {
|
||
|
conn.Close()
|
||
|
return nil, errors.New(resp.Error)
|
||
|
}
|
||
|
|
||
|
go func() {
|
||
|
defer conn.Close()
|
||
|
defer enc.Encode(&Command{[]interface{}{"unobserve_property", 0}})
|
||
|
<-ctx.Done()
|
||
|
}()
|
||
|
|
||
|
go func() {
|
||
|
defer close(out)
|
||
|
|
||
|
for ctx.Err() == nil {
|
||
|
event := Event[T]{}
|
||
|
if err := dec.Decode(&event); err != nil {
|
||
|
break
|
||
|
}
|
||
|
out <- event.Data
|
||
|
}
|
||
|
}()
|
||
|
|
||
|
return out, nil
|
||
|
}
|