system.go 1.4 KB

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