91 lines
1.6 KiB
Go
91 lines
1.6 KiB
Go
package ipc
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"log"
|
|
"net"
|
|
"net/http"
|
|
)
|
|
|
|
var ErrPingFail = errors.New("ping failed")
|
|
|
|
type Client struct {
|
|
httpC http.Client
|
|
}
|
|
|
|
// Connect attempts to connect to the IPC socket as client.
|
|
func Connect() (*Client, error) {
|
|
client := &Client{httpC: http.Client{
|
|
Transport: &http.Transport{
|
|
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
|
|
return Dial()
|
|
},
|
|
},
|
|
}}
|
|
if err := client.Ping(); err != nil {
|
|
return nil, err
|
|
}
|
|
return client, nil
|
|
}
|
|
|
|
func (c *Client) Ping() error {
|
|
if c.sendRequest(PingPath) != nil {
|
|
return ErrPingFail
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c *Client) Play() error {
|
|
return c.sendRequest(PlayPath)
|
|
}
|
|
|
|
func (c *Client) Pause() error {
|
|
return c.sendRequest(PausePath)
|
|
}
|
|
|
|
func (c *Client) PlayPause() error {
|
|
return c.sendRequest(PlayPausePath)
|
|
}
|
|
|
|
func (c *Client) SeekNext() error {
|
|
return c.sendRequest(NextPath)
|
|
}
|
|
|
|
func (c *Client) SeekBackOrPrevious() error {
|
|
return c.sendRequest(PreviousPath)
|
|
}
|
|
|
|
func (c *Client) SeekSeconds(secs float64) error {
|
|
return c.sendRequest(SeekToSecondsPath(secs))
|
|
}
|
|
|
|
func (c *Client) SetVolume(vol int) error {
|
|
return c.sendRequest(SetVolumePath(vol))
|
|
}
|
|
|
|
func (c *Client) Show() error {
|
|
return c.sendRequest(ShowPath)
|
|
}
|
|
|
|
func (c *Client) Quit() error {
|
|
return c.sendRequest(QuitPath)
|
|
}
|
|
|
|
func (c *Client) sendRequest(path string) error {
|
|
resp, err := c.httpC.Get("http://supersonic/" + path)
|
|
|
|
if err != nil {
|
|
log.Printf("http err: %v\n", err)
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != http.StatusOK {
|
|
var r Response
|
|
json.NewDecoder(resp.Body).Decode(&r)
|
|
return errors.New(r.Error)
|
|
}
|
|
return nil
|
|
}
|