package lc import ( "bytes" "encoding/json" "fmt" "github.com/sirupsen/logrus" "io" "lc-smartX/lc/model" "net/http" ) // Loudspeaker 扬声器接口 type Loudspeaker interface { Speak(txt string) } type IpCast struct { Name string Ip string Speed byte Volume byte Audio string liveState bool } func NewIpCast(name, ip, audio string, speed, volume byte) *IpCast { s := &IpCast{ Name: name, Ip: ip, Speed: speed, Volume: volume, Audio: audio, liveState: false, } s.Reconnect() return s } func (ip *IpCast) Speak(txt string) { data := &model.PlayReq{ Text: txt, Vcn: "xiaoyan", Speed: ip.Speed, Volume: ip.Volume, 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 } req, _ := http.NewRequest("POST", fmt.Sprintf("http://%s/v1/speech", ip.Ip), bytes.NewReader(body)) req.Header.Set("Content-Type", "application/json") rsp, err := http.DefaultClient.Do(req) if err != nil { logrus.Errorf("IpCast Speak Do err : %s", err.Error()) return } rspData, err := io.ReadAll(rsp.Body) logrus.Debugf("IpCast Speak rsp : %+v", string(rspData)) } 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() {}