tower_last.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package tower
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/jaryhe/gopkgs/database"
  6. "github.com/jaryhe/gopkgs/logger"
  7. "go.uber.org/zap"
  8. "smart-site-management/errors"
  9. "smart-site-management/model"
  10. "smart-site-management/pb/v1"
  11. )
  12. type TowerInfo struct {
  13. Time string `json:"time"`
  14. // 回转角度值
  15. BackTurn float64 `json:"back_turn"`
  16. // 幅度值
  17. Scope float64 `json:"scope"`
  18. // 吊钩离地面的距离
  19. Hight float64 `json:"hight"`
  20. // 吊钩所掉重物的重量
  21. Weight float64 `json:"weight"`
  22. // 当前力矩所占最大力矩的百分比
  23. Moment int `json:"moment"`
  24. // 电池电量
  25. Battery int `json:"battery"`
  26. // 风速
  27. WindSpeed float64 `json:"wind_speed"`
  28. // 塔身倾斜度 X 向
  29. AngleX float64 `json:"angle_x"`
  30. // 塔身倾斜度 Y 向
  31. AngleY float64 `json:"angle_y"`
  32. }
  33. type TowerInfoMean struct {
  34. Time string `json:"time"`
  35. // 回转角度值
  36. BackTurn float64 `json:"mean_back_turn"`
  37. // 幅度值
  38. Scope float64 `json:"mean_scope"`
  39. // 吊钩离地面的距离
  40. Hight float64 `json:"mean_hight"`
  41. // 吊钩所掉重物的重量
  42. Weight float64 `json:"mean_weight"`
  43. // 当前力矩所占最大力矩的百分比
  44. Moment int `json:"mean_moment"`
  45. // 电池电量
  46. Battery int `json:"mean_battery"`
  47. // 风速
  48. WindSpeed float64 `json:"mean_wind_speed"`
  49. // 塔身倾斜度 X 向
  50. AngleX float64 `json:"mean_angle_x"`
  51. // 塔身倾斜度 Y 向
  52. AngleY float64 `json:"mean_angle_y"`
  53. }
  54. func getTowerDbName(dtype int32) string {
  55. return "tower_monitor_2"
  56. }
  57. func LatestTowerMonitorData(ctx context.Context, req *v1.LatestTowerMonitorDataRequest)(reply *v1.LatestTowerMonitorDataReply, err error) {
  58. if req.Sn == "" {
  59. // 获取一个扬尘设备
  60. p := model.TDevice{}
  61. where := map[string]interface{}{
  62. "project_id":req.ProjectId,
  63. "device_code":model.DeviceTypeTower,
  64. "verify_status":1,
  65. "status":1,
  66. }
  67. err = p.Find(database.DB(), where)
  68. if err != nil {
  69. return nil, errors.DataBaseError
  70. }
  71. if p.Id == 0 {
  72. return &v1.LatestTowerMonitorDataReply{Data:&v1.TowerMonitorData{}}, nil
  73. }
  74. req.Sn = p.Sn
  75. }
  76. dbname := getTowerDbName(req.Type)
  77. array := []TowerInfo{}
  78. sql := fmt.Sprintf("select * from \"%s\" order by time desc limit 1 tz('Asia/Shanghai')", req.Sn)
  79. _, err = model.QueryInfluxdb(sql, dbname, &array)
  80. if err != nil {
  81. logger.Error("LatestDustMonitorData",
  82. zap.String("err", err.Error()))
  83. return nil, errors.DataBaseError
  84. }
  85. reply = &v1.LatestTowerMonitorDataReply{}
  86. if len(array) == 0 {
  87. return &v1.LatestTowerMonitorDataReply{Data:&v1.TowerMonitorData{}}, nil
  88. }
  89. reply.Data = &v1.TowerMonitorData{}
  90. reply.Data.Date = array[0].Time
  91. reply.Data.AngleY = array[0].AngleY
  92. reply.Data.AngleX = array[0].AngleX
  93. reply.Data.WindSpeed = array[0].WindSpeed
  94. reply.Data.Battery = fmt.Sprintf("%d", array[0].Battery)+"%"
  95. reply.Data.Weight = array[0].Weight
  96. reply.Data.Hight = array[0].Hight
  97. reply.Data.Scope = array[0].Scope
  98. reply.Data.BackTurn = array[0].BackTurn
  99. reply.Data.Moment = fmt.Sprintf("%d", array[0].Moment)+"%"
  100. return reply, nil
  101. }