-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Adam Bibby
committed
Oct 18, 2023
1 parent
938a383
commit 64f6ea7
Showing
6 changed files
with
122 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package bravia | ||
|
||
type setPlayContentOptions struct { | ||
URI string `json:"uri"` | ||
} | ||
|
||
type AVContent struct { | ||
*Client | ||
} | ||
|
||
func (c *AVContent) SetPlayContent(uri string) error { | ||
options := &setPlayContentOptions{URI: uri} | ||
return c.rpc("avContent", "setPowerStatus", "1.0", options, nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package bravia | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
) | ||
|
||
type Client struct { | ||
client *http.Client | ||
ip string | ||
psk string | ||
id int | ||
|
||
System *System | ||
AVContent *AVContent | ||
} | ||
|
||
func NewClient(ip, psk string) *Client { | ||
c := &Client{ | ||
client: http.DefaultClient, | ||
ip: ip, | ||
psk: psk, | ||
} | ||
c.System = &System{Client: c} | ||
c.AVContent = &AVContent{Client: c} | ||
return c | ||
} | ||
|
||
func (c *Client) nextID() int { | ||
c.id++ | ||
return c.id | ||
} | ||
|
||
type rpcRequest struct { | ||
Method string `json:"method"` | ||
ID int `json:"id"` | ||
Params []any `json:"params"` | ||
Version string `json:"version"` | ||
} | ||
|
||
func (c *Client) rpc(service, method, version string, params, result any) error { | ||
|
||
rpcBody := &rpcRequest{ | ||
Method: method, | ||
ID: c.nextID(), | ||
Params: []any{params}, | ||
Version: version, | ||
} | ||
|
||
body := &bytes.Buffer{} | ||
err := json.NewEncoder(body).Encode(rpcBody) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
req, err := http.NewRequest("POST", fmt.Sprintf("http://%s/sony/%s", c.ip, service), body) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
req.Header.Add("X-Auth-PSK", c.psk) | ||
|
||
resp, err := c.client.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode < 200 || resp.StatusCode > 299 { | ||
return fmt.Errorf("request failed with %s", resp.Status) | ||
} | ||
|
||
if result == nil { | ||
io.Copy(os.Stdout, resp.Body) | ||
return nil | ||
} | ||
return json.NewDecoder(req.Body).Decode(result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package bravia | ||
|
||
type System struct { | ||
*Client | ||
} | ||
|
||
type setPowerStatusOptions struct { | ||
Status bool `json:"status"` | ||
} | ||
|
||
// https://pro-bravia.sony.net/develop/integrate/rest-api/spec/service/system/v1_0/setPowerStatus/index.html | ||
func (c *System) SetPowerStatus(status bool) error { | ||
options := &setPowerStatusOptions{Status: status} | ||
return c.rpc("system", "setPowerStatus", "1.0", options, nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters