-
Notifications
You must be signed in to change notification settings - Fork 0
/
yamaha.go
66 lines (54 loc) · 2.08 KB
/
yamaha.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package yamaha
import (
"net/http"
"strconv"
"strings"
)
// struct used to hold our Yamaha connection
type Yamaha struct {
address string
client *http.Client
}
// open a connection to the Yamaha device
func Connect(address string) (Yamaha, error) {
return Yamaha{address: address, client: &http.Client{}}, nil
}
// common http post method
func (r Yamaha) Post(data string) error {
req, err := http.NewRequest("POST", "http://"+r.address+"/YamahaRemoteControl/ctrl", strings.NewReader(data))
if err != nil {
return err
}
_, err = r.client.Do(req)
if err != nil {
return err
}
return nil
}
// power on device
func (y Yamaha) PowerOn() error {
return y.Post("<YAMAHA_AV cmd=\"PUT\"><Main_Zone><Power_Control><Power>On</Power></Power_Control></Main_Zone></YAMAHA_AV>")
}
// power off device, which really just sets it to standby where network functions are still available
func (y Yamaha) PowerOff() error {
return y.Post("<YAMAHA_AV cmd=\"PUT\"><Main_Zone><Power_Control><Power>Standby</Power></Power_Control></Main_Zone></YAMAHA_AV>")
}
// set input to HDMI1
func (y Yamaha) SetInputHDMI1() error {
return y.Post("<YAMAHA_AV cmd=\"PUT\"><Main_Zone><Input><Input_Sel>HDMI1</Input_Sel></Input></Main_Zone></YAMAHA_AV>")
}
// set input to HDMI2
func (y Yamaha) SetInputHDMI2() error {
return y.Post("<YAMAHA_AV cmd=\"PUT\"><Main_Zone><Input><Input_Sel>HDMI2</Input_Sel></Input></Main_Zone></YAMAHA_AV>")
}
// set input to AirPlay
func (y Yamaha) SetInputAirPlay() error {
return y.Post("<YAMAHA_AV cmd=\"PUT\"><Main_Zone><Input><Input_Sel>AirPlay</Input_Sel></Input></Main_Zone></YAMAHA_AV>")
}
// Set volume from a range of 160 (full volume) to -800 (lowest volume before muting)
// which is displayed as +16.0dB to -80.0dB on the receiver. I never set this higher
// than -200 (-20.0dB) which is loud enough for my place. Be careful and don't blow out
// your speaker or ear drums!
func (y Yamaha) SetVolume(volume int) error {
return y.Post("<YAMAHA_AV cmd=\"PUT\"><Main_Zone><Volume><Lvl><Val>" + strconv.Itoa(volume) + "</Val><Exp>1</Exp><Unit>dB</Unit></Lvl></Volume></Main_Zone></YAMAHA_AV>")
}