ipCastControlService.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package edge_service
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "io/ioutil"
  7. "iot_manager_service/config"
  8. "iot_manager_service/util/common"
  9. "iot_manager_service/util/logger"
  10. "net/http"
  11. "strings"
  12. )
  13. // IpCastControlService ip音柱连续控制接口
  14. type IpCastControlService struct{}
  15. type IpCastControlReq struct {
  16. Action string `json:"action"`
  17. PostData map[string]interface{} `json:"postData"`
  18. }
  19. type TermInfo struct {
  20. AreaID int `json:"AreaID"`
  21. From string `json:"From"`
  22. ID int `json:"ID"`
  23. IP string `json:"IP"`
  24. Name string `json:"Name"`
  25. Online int `json:"Online"`
  26. State int `json:"State"`
  27. Tasks int `json:"Tasks"`
  28. Vol int `json:"Vol"`
  29. }
  30. // GetTermIds 取设备列表和状态
  31. func (f IpCastControlService) GetTermIds() (terminfos map[string]TermInfo, err error) {
  32. var reqData IpCastControlReq
  33. reqData.Action = "GetTermIds"
  34. m := make(map[string]interface{})
  35. reqData.PostData = m
  36. mapStr, err := f.RequestApi(reqData)
  37. if err != nil {
  38. return terminfos, err
  39. }
  40. terms, ok := mapStr["Terms"]
  41. if ok {
  42. marshal, _ := json.Marshal(terms)
  43. var terminfosList []TermInfo
  44. err := json.Unmarshal(marshal, &terminfosList)
  45. terms := make(map[string]TermInfo)
  46. for _, info := range terminfosList {
  47. info.Vol = int(common.MapRange(float64(info.Vol), 1, 16, 1, 100))
  48. terms[info.Name] = info
  49. }
  50. terminfos = terms
  51. if err != nil {
  52. return nil, err
  53. }
  54. return terminfos, nil
  55. }
  56. return terminfos, err
  57. }
  58. func (f IpCastControlService) RequestApi(reqData IpCastControlReq) (map[string]interface{}, error) {
  59. cfg := config.Instance()
  60. url := fmt.Sprintf("%v/%v", cfg.Foreign.IpCastEdgeUrl, reqData.Action)
  61. method := "POST"
  62. client := &http.Client{}
  63. marshal, _ := json.Marshal(reqData.PostData)
  64. //fmt.Printf("playJson = %v \n", string(marshal))
  65. req, err := http.NewRequest(method, url, strings.NewReader(string(marshal)))
  66. if err != nil {
  67. logger.Logger.Errorf("IpCastControlService NewRequest %v", err.Error())
  68. return nil, err
  69. }
  70. req.Header.Add("Content-Type", "application/json")
  71. res, err := client.Do(req)
  72. if err != nil {
  73. logger.Logger.Errorf("IpCastControlService Do %v", err.Error())
  74. return nil, err
  75. }
  76. defer res.Body.Close()
  77. body, err := ioutil.ReadAll(res.Body)
  78. if err != nil {
  79. logger.Logger.Errorf("IpCastControlService ReadAll %v", err.Error())
  80. return nil, err
  81. }
  82. var IpCastControlRes map[string]interface{}
  83. err = json.Unmarshal(body, &IpCastControlRes)
  84. if err != nil {
  85. logger.Logger.Errorf("IpCastControlService Unmarshal %v", err.Error())
  86. return nil, err
  87. }
  88. //fmt.Printf("IpCastControlRes = %v \n", IpCastControlRes)
  89. if IpCastControlRes["Ret"] == 1000 {
  90. fmt.Printf("RequestApi url = %v \n", url)
  91. fmt.Printf("reqData = %v \n", reqData)
  92. logger.Logger.Errorf("IpCastControlService %v", IpCastControlRes)
  93. return nil, err
  94. }
  95. return IpCastControlRes, nil
  96. }
  97. // SetVol 设置音量
  98. func (f IpCastControlService) SetVol(sn string, vol int) error {
  99. terminfos, err2 := f.GetTermIds()
  100. if err2 != nil {
  101. return err2
  102. }
  103. term, ok := terminfos[sn]
  104. if !ok {
  105. return errors.New("devices not found\tsn=" + sn)
  106. }
  107. var reqData IpCastControlReq
  108. reqData.Action = "TermVolSet"
  109. m := make(map[string]interface{})
  110. m["TermIds"] = []int{term.ID}
  111. m["Volume"] = int(common.MapRange(float64(vol), 1, 100, 1, 16))
  112. reqData.PostData = m
  113. _, err := f.RequestApi(reqData)
  114. if err != nil {
  115. return err
  116. }
  117. return nil
  118. }