12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- 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
- Speed byte
- Volume byte
- liveState bool
- }
- func NewIpCast(name, ip string, speed, volume byte) *IpCast {
- s := &IpCast{
- Name: name,
- Ip: ip,
- Speed: speed,
- Volume: volume,
- 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
- }
- c := http.DefaultClient
- req, _ := http.NewRequest("POST", fmt.Sprintf("http://%s/v1/speech", ip.Ip), bytes.NewReader(body))
- _, err = c.Do(req)
- if err != nil {
- logrus.Errorf("IpCast Speak Do 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() {}
|