1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- 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
- logrus.Debugf("IpCast Speak txt : %s", txt)
- 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")
- logrus.Debugf("IpCast Speak req : %+v", req)
- 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() {}
|