Skip to content

Commit

Permalink
add bravia
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Bibby committed Oct 18, 2023
1 parent 938a383 commit 64f6ea7
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 11 deletions.
14 changes: 14 additions & 0 deletions bravia/avContent.go
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)
}
82 changes: 82 additions & 0 deletions bravia/bravia.go
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)
}
15 changes: 15 additions & 0 deletions bravia/system.go
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)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.19

require (
github.com/abibby/fileserver v0.0.0-20221208191705-087686654550
github.com/davecgh/go-spew v1.1.1
github.com/gorilla/websocket v1.5.0
github.com/joho/godotenv v1.5.1
github.com/pkg/errors v0.9.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/abibby/fileserver v0.0.0-20221208191705-087686654550 h1:UsWK0321m+VxaYzoRMbpCXO4RBgM8lwkX9V1tTojN1w=
github.com/abibby/fileserver v0.0.0-20221208191705-087686654550/go.mod h1:xHBGw2P5B6OigP/ehSFN0L0SVNJSuGnllCrkfZBEX4s=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
Expand Down
19 changes: 8 additions & 11 deletions ui/src/bravia/avContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,16 @@ export interface Source {
source: string
}

export const getSourceList = braviaAPIFactory<[options:GetSourceListOptions], Source[]>(
'avContent',
'getSourceList',
'1.0',
)
export const getSourceList = braviaAPIFactory<
[options: GetSourceListOptions],
Source[]
>('avContent', 'getSourceList', '1.0')

export interface SetPlayContentOptions {
uri: string
}


export const setPlayContent = braviaAPIFactory<[options:SetPlayContentOptions], void>(
'avContent',
'setPlayContent',
'1.0',
)
export const setPlayContent = braviaAPIFactory<
[options: SetPlayContentOptions],
void
>('avContent', 'setPlayContent', '1.0')

0 comments on commit 64f6ea7

Please sign in to comment.