package lc import ( "bytes" "encoding/json" "fmt" "github.com/sirupsen/logrus" "lc-smartX/lc/model" "net/http" ) // Loudspeaker 扬声器接口 type Loudspeaker interface { Speak(txt string) } type IpCast struct { Name string Ip string liveState bool } func NewIpCast(name, ip string) *IpCast { s := &IpCast{ Name: name, Ip: ip, liveState: false, } s.Reconnect() return s } func (ip *IpCast) Speak(txt string) { data := &model.PlayReq{ Text: txt, Vcn: "xiaoyan", Speed: 50, // todo 配置文件 Volume: 10, // todo 配置文件 Rdn: "2", Rcn: "1", Reg: 0, Sync: false, Queue: false, } data.Loop.Times = 1 body, err := json.Marshal(data) if err != nil { logrus.Errorf("IpCast Marshal err : %s", err.Error()) return } c := http.DefaultClient req, _ := http.NewRequest("POST", fmt.Sprintf("http://%s/v1/speech", ip.Ip), bytes.NewReader(body)) rsp, err := c.Do(req) if err != nil { logrus.Errorf("IpCast Speak err : %s", err.Error()) return } } func (ip *IpCast) Reconnect() { c := http.DefaultClient req, _ := http.NewRequest("GET", fmt.Sprintf("http://%s/v1/check_alive", ip.Ip), nil) rsp, err := c.Do(req) if err != nil { logrus.Errorf("IpCast Reconnect err : %s", err.Error()) return } if rsp.StatusCode == http.StatusOK { ip.liveState = true } } func (ip IpCast) CorrectTime() {}