tower_realtime_data.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package v1
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "tower-monitor/model"
  9. )
  10. type TowerRealtimeData struct {
  11. DateTime int64 `json:"date_time"`
  12. High float64 `json:"high"`
  13. Range float64 `json:"range"`
  14. Backturn float64 `json:"backturn"`
  15. Weight int64 `json:"weight"`
  16. WindSpeed float64 `json:"wind_speed"`
  17. Tilt float64 `json:"tilt"`
  18. WeightPercent int64 `json:"weight_percent"`
  19. MomentPercent int64 `json:"moment_percent"`
  20. WindSpeedPercent int64 `json:"wind_speed_percent"`
  21. TiltPercent int64 `json:"tilt_percent"`
  22. AlarmReason int64 `json:"alarm_reason"`
  23. Braking int64 `json:"braking"`
  24. Lng float64 `json:"lng"`
  25. Lat float64 `json:"lat"`
  26. Sn string `json:"sn"`
  27. ProjectId int64 `json:"project_id"`
  28. TowerNo int64 `json:"tower_no"`
  29. }
  30. func TowerRealtimeDataWrite(req *TowerRealtimeData) error {
  31. tags := map[string]string{"sn": req.Sn}
  32. fields := map[string]interface{}{}
  33. bytes, _ := json.Marshal(req)
  34. json.Unmarshal(bytes, &fields)
  35. delete(fields, "sn")
  36. t := time.Now()
  37. // 写数据
  38. err := model.WriteTowerData(req.Sn, tags, fields, t, "realtime_data")
  39. if err != nil {
  40. return err
  41. }
  42. return nil
  43. }
  44. func TowerFrameRealtimeDataRequestHandle(sn string, towerNo byte, version byte, data []byte) (res []byte, err error) {
  45. hexPrintf(data)
  46. projectId,devicename := checkAuth(sn)
  47. if projectId <= 0 {
  48. // 未认证或需重新认证
  49. return makeResponse(sn, towerNo, TowerFrameRealtimeDataResponse, version, []byte{0xfe}), nil
  50. }
  51. if len(data) < 46 {
  52. return makeResponse(sn, towerNo, TowerFrameRealtimeDataResponse, version, []byte{0xff}), nil
  53. }
  54. record := &TowerRealtimeData{}
  55. record.Sn = sn
  56. record.TowerNo = int64(towerNo)
  57. record.ProjectId = projectId
  58. // 时间戳
  59. record.DateTime = int64(data[0]) << 24 + int64(data[1]) << 16 + int64(data[2]) << 8 + int64(data[3])
  60. // 高度
  61. record.High = (float64(int16(data[4])<<8) + float64(data[5])) * 0.1
  62. // 幅度
  63. record.Range = (float64(int16(data[6])<<8) + float64(data[7])) * 0.1
  64. // 回转
  65. record.Backturn = (float64(int16(data[8])<<8) + float64(data[9])) * 0.1
  66. // 起重量数据
  67. record.Weight = int64(data[10])<<8 + int64(data[11])
  68. // 风速
  69. record.WindSpeed = (float64(int16(data[12])<<8) + float64(data[13])) * 0.1
  70. // 倾角
  71. record.Tilt = (float64(int16(data[14])<<8) + float64(data[15]))*0.1
  72. // 重量百分比
  73. record.WeightPercent = int64(data[16])
  74. // 力矩百分比
  75. record.MomentPercent = int64(data[17])
  76. // 风速百分比
  77. record.WindSpeedPercent = int64(data[18])
  78. // 倾斜百分比
  79. record.TiltPercent = int64(data[19])
  80. // 警报原因
  81. // 211 212 213 214 碰撞预警
  82. // 111 112 113 114 碰撞报警
  83. // 12 13 14 15 重量 力矩 倾斜 风速报警
  84. // 22 23 24 25 重量 力矩 倾斜 风速预警
  85. record.AlarmReason = int64(data[20])
  86. // 制动状态:1个字节,8bit 从低到高分别是 上下前后左右
  87. // Bit 1:表示断开; bit 0:表示闭合
  88. record.Braking = int64(data[21])
  89. // 经度
  90. str := string(data[22:34])
  91. str = strings.TrimSpace(str)
  92. record.Lng, _ = strconv.ParseFloat(str, 64)
  93. fmt.Printf("lng:%s,0x%02x\n", str, data[22])
  94. // 纬度
  95. str = string(data[34:46])
  96. fmt.Printf("lat:%s,0x%02x\n", str, data[34])
  97. str = strings.TrimSpace(str)
  98. record.Lat, _ = strconv.ParseFloat(str, 64)
  99. handleAlarm(record.AlarmReason, sn, int64(towerNo), record.DateTime, projectId, devicename)
  100. if TowerRealtimeDataWrite(record) != nil {
  101. return makeResponse(sn, towerNo, TowerFrameRealtimeDataResponse, version, []byte{0xfd}), nil
  102. }
  103. if record.AlarmReason > 0 {
  104. //alarmRecord := &TowerAlarmData{}
  105. //bytes, _ := json.Marshal(record)
  106. //json.Unmarshal(bytes, alarmRecord)
  107. //TowerAlarmDataWrite(alarmRecord)
  108. }
  109. return makeResponse(sn, towerNo, TowerFrameRealtimeDataResponse, version, []byte{0x00}), nil
  110. }