tower_loop_data.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package v1
  2. import (
  3. "time"
  4. "tower-monitor/model"
  5. "encoding/json"
  6. )
  7. type TowerLoopData struct {
  8. Sn string `json:"sn"`
  9. ProjectId int64 `json:"project_id"`
  10. TowerNo int64 `json:"tower_no"`
  11. StartTime int64 `json:"start_time"`
  12. EndTime int64 `json:"end_time"`
  13. MaxWeight int64 `json:"max_weight"`
  14. MaxMoment float64 `json:"max_moment"`
  15. MaxHigh float64 `json:"max_high"`
  16. MinHigh float64 `json:"min_high"`
  17. MaxRange float64 `json:"max_range"`
  18. MinRange float64 `json:"min_range"`
  19. StartAngle float64 `json:"start_angle"`
  20. StartRange float64 `json:"start_range"`
  21. StartHigh float64 `json:"start_high"`
  22. EndAngle float64 `json:"end_angle"`
  23. EndHigh float64 `json:"end_high"`
  24. EndRange float64 `json:"end_range"`
  25. }
  26. func TowerLoopDataWrite(req *TowerLoopData) error {
  27. tags := map[string]string{"sn": req.Sn}
  28. fields := map[string]interface{}{}
  29. bytes, _ := json.Marshal(req)
  30. json.Unmarshal(bytes, &fields)
  31. delete(fields, "sn")
  32. t := time.Now()
  33. // 写数据
  34. err := model.WriteTowerData(req.Sn, tags, fields, t, "loop_data")
  35. if err != nil {
  36. return err
  37. }
  38. return nil
  39. }
  40. func TowerFrameLoopDataRequestHandle(sn string, towerNo byte, version byte, data []byte) (res []byte, err error) {
  41. projectId,_ := checkAuth(sn)
  42. if projectId <= 0 {
  43. // 未认证或需重新认证
  44. return makeResponse(sn, towerNo, TowerFrameLoopDataResponse, version, []byte{0xfe}), nil
  45. }
  46. if len(data) < 32 {
  47. return makeResponse(sn, towerNo, TowerFrameLoopDataResponse, version, []byte{0xff}), nil
  48. }
  49. record := &TowerLoopData{}
  50. record.Sn = sn
  51. record.TowerNo = int64(towerNo)
  52. record.ProjectId = projectId
  53. // 时间戳
  54. record.StartTime = int64(data[0]) << 24 + int64(data[1]) << 16 + int64(data[2]) << 8 + int64(data[3])
  55. record.EndTime = int64(data[4]) << 24 + int64(data[5]) << 16 + int64(data[6]) << 8 + int64(data[7])
  56. // 本次循环最大吊重
  57. record.MaxWeight = (int64(data[8]<<8) + int64(data[9]))
  58. // 本次循环最大力矩
  59. record.MaxMoment = (float64(int16(data[10]) << 8) + float64(data[11])) * 0.01
  60. // 本次循环最大高度
  61. record.MaxHigh = (float64(int16(data[12]) << 8) + float64(data[13])) * 0.1
  62. // 本次循环最小高度
  63. record.MinHigh = (float64(int16(data[14]) << 8) + float64(data[15])) * 0.1
  64. // 本次循环最大幅度
  65. record.MaxRange = (float64(int16(data[16]) << 8) + float64(data[17])) * 0.1
  66. // 本次循环最小幅度
  67. record.MinRange = (float64(int16(data[18]) << 8) + float64(data[19])) * 0.1
  68. // 起吊点角度
  69. record.StartAngle = (float64(int16(data[20]) << 8) + float64(data[21])) * 0.1
  70. // 起吊点高度
  71. record.StartHigh = (float64(int16(data[22]) << 8) + float64(data[23])) * 0.1
  72. // 起吊点幅度
  73. record.StartRange = (float64(int16(data[24]) << 8) + float64(data[25])) * 0.1
  74. // 卸吊点角度
  75. record.EndAngle = (float64(int16(data[26]) << 8) + float64(data[27])) * 0.1
  76. // 卸吊点高度
  77. record.EndHigh = (float64(int16(data[28]) << 8) + float64(data[29])) * 0.1
  78. // 卸吊点幅度
  79. record.EndRange = (float64(int16(data[30]) << 8) + float64(data[31])) * 0.1
  80. if TowerLoopDataWrite(record) != nil {
  81. return makeResponse(sn, towerNo, TowerFrameLoopDataResponse, version, []byte{0xfd}), nil
  82. }
  83. return makeResponse(sn, towerNo, TowerFrameLoopDataResponse, version, []byte{0x00}), nil
  84. }