ipCastControlService.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. "strconv"
  12. "strings"
  13. )
  14. // IpCastControlService ip音柱连续控制接口
  15. type IpCastControlService struct{}
  16. type IpCastControlReq struct {
  17. Action string `json:"action"`
  18. PostData map[string]interface{} `json:"postData"`
  19. }
  20. type TermInfo struct {
  21. AreaID int `json:"AreaID"`
  22. From string `json:"From"`
  23. ID int `json:"ID"`
  24. IP string `json:"IP"`
  25. Name string `json:"Name"`
  26. Online int `json:"Online"`
  27. State int `json:"State"`
  28. Tasks int `json:"Tasks"`
  29. Vol int `json:"Vol"`
  30. }
  31. // GetTermIds 取设备列表和状态
  32. func (f IpCastControlService) GetTermIds() (terminfos map[string]TermInfo, err error) {
  33. var reqData IpCastControlReq
  34. reqData.Action = "GetTermIds"
  35. m := make(map[string]interface{})
  36. reqData.PostData = m
  37. mapStr, err := f.RequestApi(reqData)
  38. if err != nil {
  39. return terminfos, err
  40. }
  41. terms, ok := mapStr["Terms"]
  42. if ok {
  43. marshal, _ := json.Marshal(terms)
  44. var terminfosList []TermInfo
  45. err := json.Unmarshal(marshal, &terminfosList)
  46. terms := make(map[string]TermInfo)
  47. for _, info := range terminfosList {
  48. info.Vol = int(common.MapRange(float64(info.Vol), 1, 16, 1, 100))
  49. terms[info.Name] = info
  50. }
  51. terminfos = terms
  52. if err != nil {
  53. return nil, err
  54. }
  55. return terminfos, nil
  56. }
  57. return terminfos, err
  58. }
  59. // SetVol 设置音量
  60. func (f IpCastControlService) SetVol(sn string, vol int) error {
  61. terminfos, err2 := f.GetTermIds()
  62. if err2 != nil {
  63. return err2
  64. }
  65. term, ok := terminfos[sn]
  66. if !ok {
  67. return errors.New("devices not found\tsn=" + sn)
  68. }
  69. var reqData IpCastControlReq
  70. reqData.Action = "TermVolSet"
  71. m := make(map[string]interface{})
  72. m["TermIds"] = []int{term.ID}
  73. m["Volume"] = int(common.MapRange(float64(vol), 1, 100, 1, 16))
  74. reqData.PostData = m
  75. _, err := f.RequestApi(reqData)
  76. if err != nil {
  77. return err
  78. }
  79. return nil
  80. }
  81. // RequestApi 公共请求
  82. func (f IpCastControlService) RequestApi(reqData IpCastControlReq) (map[string]interface{}, error) {
  83. cfg := config.Instance()
  84. url := fmt.Sprintf("%v/%v", cfg.Foreign.IpCastEdgeUrl, reqData.Action)
  85. method := "POST"
  86. client := &http.Client{}
  87. marshal, _ := json.Marshal(reqData.PostData)
  88. req, err := http.NewRequest(method, url, strings.NewReader(string(marshal)))
  89. if err != nil {
  90. logger.Logger.Errorf("IpCastControlService NewRequest %v", err.Error())
  91. return nil, err
  92. }
  93. req.Header.Add("Content-Type", "application/json")
  94. res, err := client.Do(req)
  95. if err != nil {
  96. logger.Logger.Errorf("IpCastControlService Do %v", err.Error())
  97. return nil, err
  98. }
  99. defer res.Body.Close()
  100. body, err := ioutil.ReadAll(res.Body)
  101. if err != nil {
  102. logger.Logger.Errorf("IpCastControlService ReadAll %v", err.Error())
  103. return nil, err
  104. }
  105. var IpCastControlRes map[string]interface{}
  106. err = json.Unmarshal(body, &IpCastControlRes)
  107. if err != nil {
  108. logger.Logger.Errorf("IpCastControlService Unmarshal %v", err.Error())
  109. return nil, err
  110. }
  111. if IpCastControlRes["Ret"] == 1000 {
  112. logger.Logger.Errorf("IpCastControlService %v", IpCastControlRes)
  113. return nil, err
  114. }
  115. return IpCastControlRes, nil
  116. }
  117. type uploadMp3Data struct {
  118. Remark string `json:"Remark"`
  119. Ret int `json:"Ret"`
  120. NSeconds int `json:"nSeconds"`
  121. }
  122. // UploadMp3GetFileId 上传素材 到ip音柱服务端
  123. func (f IpCastControlService) UploadMp3GetFileId(imgUrl string, fileName string) (string, error) {
  124. cfg := config.Instance()
  125. url := fmt.Sprintf("%v/UploadFile:%v", cfg.Foreign.IpCastEdgeUrl, fileName)
  126. method := "POST"
  127. response, err := http.Get(imgUrl)
  128. if err != nil {
  129. panic(err)
  130. }
  131. defer response.Body.Close()
  132. client := &http.Client{}
  133. req, err := http.NewRequest(method, url, response.Body)
  134. if err != nil {
  135. logger.Logger.Errorf("IpCastControlService UploadMp3GetFileId 1 %v", err.Error())
  136. return "", err
  137. }
  138. req.Header.Add("Content-Type", "audio/mpeg")
  139. res, err := client.Do(req)
  140. if err != nil {
  141. logger.Logger.Errorf("IpCastControlService UploadMp3GetFileId 2 %v", err.Error())
  142. return "", err
  143. }
  144. defer res.Body.Close()
  145. body, err := ioutil.ReadAll(res.Body)
  146. if err != nil {
  147. logger.Logger.Errorf("IpCastControlService UploadMp3GetFileId 3 %v", err.Error())
  148. return "", err
  149. }
  150. var resData uploadMp3Data
  151. err = json.Unmarshal(body, &resData)
  152. if err != nil {
  153. return "", err
  154. }
  155. if resData.Ret > 0 {
  156. return strconv.Itoa(resData.Ret), nil
  157. }
  158. return "", errors.New("上传失败啦")
  159. }
  160. func (f IpCastControlService) SetStop() error {
  161. var reqData IpCastControlReq
  162. reqData.Action = "StopMp3ByID"
  163. m := make(map[string]interface{})
  164. m["ID"] = 0
  165. reqData.PostData = m
  166. _, err := f.RequestApi(reqData)
  167. return err
  168. }
  169. // 发布节目
  170. func (f IpCastControlService) PushPlay(playJson string, ptype int) error {
  171. var reqData IpCastControlReq
  172. if ptype == 1 {
  173. reqData.Action = "PlayMp3ByID"
  174. } else {
  175. reqData.Action = "PlayText"
  176. }
  177. m := make(map[string]interface{})
  178. err := json.Unmarshal([]byte(playJson), &m)
  179. reqData.PostData = m
  180. if err != nil {
  181. return err
  182. }
  183. _, err = f.RequestApi(reqData)
  184. return err
  185. }