speaker.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package lc
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/sirupsen/logrus"
  7. "lc-smartX/lc/model"
  8. "net/http"
  9. )
  10. // Loudspeaker 扬声器接口
  11. type Loudspeaker interface {
  12. Speak(txt string)
  13. }
  14. type IpCast struct {
  15. Name string
  16. Ip string
  17. liveState bool
  18. }
  19. func NewIpCast(name, ip string) *IpCast {
  20. s := &IpCast{
  21. Name: name,
  22. Ip: ip,
  23. liveState: false,
  24. }
  25. s.Reconnect()
  26. return s
  27. }
  28. func (ip *IpCast) Speak(txt string) {
  29. data := &model.PlayReq{
  30. Text: txt,
  31. Vcn: "xiaoyan",
  32. Speed: 50, // todo 配置文件
  33. Volume: 10, // todo 配置文件
  34. Rdn: "2",
  35. Rcn: "1",
  36. Reg: 0,
  37. Sync: false,
  38. Queue: false,
  39. }
  40. data.Loop.Times = 1
  41. body, err := json.Marshal(data)
  42. if err != nil {
  43. logrus.Errorf("IpCast Marshal err : %s", err.Error())
  44. return
  45. }
  46. c := http.DefaultClient
  47. req, _ := http.NewRequest("POST", fmt.Sprintf("http://%s/v1/speech", ip.Ip), bytes.NewReader(body))
  48. rsp, err := c.Do(req)
  49. if err != nil {
  50. logrus.Errorf("IpCast Speak err : %s", err.Error())
  51. return
  52. }
  53. }
  54. func (ip *IpCast) Reconnect() {
  55. c := http.DefaultClient
  56. req, _ := http.NewRequest("GET", fmt.Sprintf("http://%s/v1/check_alive", ip.Ip), nil)
  57. rsp, err := c.Do(req)
  58. if err != nil {
  59. logrus.Errorf("IpCast Reconnect err : %s", err.Error())
  60. return
  61. }
  62. if rsp.StatusCode == http.StatusOK {
  63. ip.liveState = true
  64. }
  65. }
  66. func (ip IpCast) CorrectTime() {}