system.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package isapi
  2. import (
  3. "errors"
  4. "github.com/gin-gonic/gin"
  5. "github.com/sirupsen/logrus"
  6. "io/ioutil"
  7. "lcfns/gatewayServer"
  8. "lcfns/isapi"
  9. "strings"
  10. )
  11. type SystemApi struct {
  12. }
  13. func (i SystemApi) GetInfo(c *gin.Context) {
  14. host, err := GetHost(c)
  15. if err != nil {
  16. logrus.Errorf("获取host失败:%e\n", err)
  17. return
  18. }
  19. isapi.TouChuan(host, isapi.GetDeviceInfo, nil, c)
  20. }
  21. func (i SystemApi) GetCap(c *gin.Context) {
  22. host, err := GetHost(c)
  23. if err != nil {
  24. logrus.Errorf("获取host失败:%e\n", err)
  25. return
  26. }
  27. isapi.TouChuan(host, isapi.GetSystemCap, nil, c)
  28. }
  29. func (i SystemApi) PutEmail(c *gin.Context) {
  30. defer c.Request.Body.Close()
  31. data, err := ioutil.ReadAll(c.Request.Body)
  32. if err != nil {
  33. logrus.Errorf("读取数据失败:%e\n", err)
  34. }
  35. host, err := GetHost(c)
  36. if err != nil {
  37. logrus.Errorf("获取host失败:%e\n", err)
  38. return
  39. }
  40. isapi.TouChuan(host, isapi.PutEmail, data, c)
  41. }
  42. func GetHost(c *gin.Context) (string, error) {
  43. devId := c.Query("devId")
  44. if devId == "" {
  45. return "", errors.New("设备Id为空")
  46. }
  47. conn, ok := gatewayServer.ConnMap[devId]
  48. if !ok {
  49. return "", errors.New("获取设备Id对应连接失败")
  50. }
  51. return strings.Split(conn.RemoteAddr().String(), ":")[0] + ":8848", nil
  52. }