last.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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-government-management/errors"
  9. "smart-government-management/model"
  10. "smart-government-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. "device_code":model.DeviceTypeTower,
  63. "verify_status":1,
  64. "status":1,
  65. }
  66. err = p.Find(database.DB(), where)
  67. if err != nil {
  68. return nil, errors.DataBaseError
  69. }
  70. if p.Id == 0 {
  71. return &v1.LatestTowerMonitorDataReply{Data:&v1.TowerMonitorData{}}, nil
  72. }
  73. req.Sn = p.Sn
  74. }
  75. dbname := getTowerDbName(req.Type)
  76. array := []TowerInfo{}
  77. sql := fmt.Sprintf("select * from \"%s\" order by time desc limit 1 tz('Asia/Shanghai')", req.Sn)
  78. _, err = model.QueryInfluxdb(sql, dbname, &array)
  79. if err != nil {
  80. logger.Error("LatestDustMonitorData",
  81. zap.String("err", err.Error()))
  82. return nil, errors.DataBaseError
  83. }
  84. reply = &v1.LatestTowerMonitorDataReply{}
  85. if len(array) == 0 {
  86. return &v1.LatestTowerMonitorDataReply{Data:&v1.TowerMonitorData{}}, nil
  87. }
  88. reply.Data = &v1.TowerMonitorData{}
  89. reply.Data.Date = array[0].Time
  90. reply.Data.AngleY = array[0].AngleY
  91. reply.Data.AngleX = array[0].AngleX
  92. reply.Data.WindSpeed = array[0].WindSpeed
  93. reply.Data.Battery = fmt.Sprintf("%d", array[0].Battery)+"%"
  94. reply.Data.Weight = array[0].Weight
  95. reply.Data.Hight = array[0].Hight
  96. reply.Data.Scope = array[0].Scope
  97. reply.Data.BackTurn = array[0].BackTurn
  98. reply.Data.Moment = fmt.Sprintf("%d", array[0].Moment)+"%"
  99. return reply, nil
  100. }