tower_base_info.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package v1
  4. import (
  5. "github.com/jaryhe/gopkgs/database"
  6. "github.com/jinzhu/gorm"
  7. "time"
  8. "tower-monitor/model"
  9. )
  10. func TowerFrameBaseInfoWrite(record *model.TowerBaseInfo) error {
  11. p := &model.TowerBaseInfo{}
  12. where := map[string]interface{}{
  13. "SN": record.SN,
  14. "TowerNo": record.TowerNo,
  15. "ProjectID": record.ProjectID,
  16. }
  17. err := p.Find(database.DB(), where)
  18. if err != nil && err != gorm.ErrRecordNotFound {
  19. return err
  20. }
  21. if p.ID == 0 {
  22. record.CreatedAt = time.Now()
  23. record.UpdatedAt = record.CreatedAt
  24. return record.Insert(database.DB())
  25. }
  26. values := map[string]interface{}{
  27. "X": record.X,
  28. "Y": record.Y,
  29. "TiltSendsor": record.TiltSendsor,
  30. "WindSensor": record.WindSensor,
  31. "WeightSensor": record.WeightSensor,
  32. "BackturnSensor": record.BackturnSensor,
  33. "RangeSensor": record.RangeSensor,
  34. "HighSensor": record.HighSensor,
  35. "ArticulationLength": record.ArticulationLength,
  36. "HookWeight": record.HookWeight,
  37. "MaxMoment": record.MaxMoment,
  38. "MaxWeight": record.MaxWeight,
  39. "ArmHigh": record.ArmHigh,
  40. "HatHigh": record.HatHigh,
  41. "BackArmLength": record.BackArmLength,
  42. "FrontArmLength": record.FrontArmLength,
  43. "UpdatedAt": time.Now(),
  44. }
  45. where = map[string]interface{}{
  46. "ID": p.ID,
  47. }
  48. return p.Update(database.DB(), where, values)
  49. }
  50. func TowerFrameBaseInfoRequestHandle(sn string, towerNo byte, version byte, data []byte) (res []byte, err error) {
  51. projectId,_ := checkAuth(sn)
  52. if projectId <= 0 {
  53. // 未认证或需重新认证
  54. return makeResponse(sn, towerNo, TowerFrameBaseInfoResponse, version, []byte{0xfe}), nil
  55. }
  56. if len(data) < 21 {
  57. return makeResponse(sn, towerNo, TowerFrameBaseInfoResponse, version, []byte{0xff}), nil
  58. }
  59. record := &model.TowerBaseInfo{}
  60. record.SN = sn
  61. // 坐标x
  62. record.X = (float64(int16(data[0])<<8) + float64(data[1])) * 0.1
  63. // 坐标y
  64. record.Y = (float64(int16(data[2])<<8) + float64(data[3])) * 0.1
  65. // 前臂长
  66. record.FrontArmLength = (float64(int16(data[4])<<8) + float64(data[5])) * 0.1
  67. // 后壁长
  68. record.BackArmLength = (float64(int16(data[6])<<8) + float64(data[7])) * 0.1
  69. // 塔帽高
  70. record.HatHigh = (float64(int16(data[8])<<8) + float64(data[9])) * 0.1
  71. // 塔臂高
  72. record.ArmHigh = (float64(int16(data[10])<<8) + float64(data[11])) * 0.1
  73. // 最大吊重kg
  74. record.MaxWeight = int64(data[12])<<8 + int64(data[13])
  75. // 最大力矩
  76. record.MaxMoment = (float64(int16(data[14])<<8) + float64(data[15])) * 0.01
  77. // 吊钩重量kg
  78. record.HookWeight = int64(data[16])<<8 + int64(data[17])
  79. // 铰接长度
  80. record.ArticulationLength = (float64(int16(data[18])<<8) + float64(data[19])) * 0.1
  81. // 传感器安装状态
  82. // 高度传感器
  83. record.HighSensor = int64(data[20] & 0x01)
  84. // 幅度传感器
  85. record.RangeSensor = int64((data[19] >> 1) & 0x1)
  86. // 回转传感器
  87. record.BackturnSensor = int64((data[19] >> 2) & 0x1)
  88. // 重量传感器
  89. record.WeightSensor = int64((data[19] >> 3) & 0x1)
  90. // 风速传感器
  91. record.WindSensor = int64((data[19] >> 4) & 0x1)
  92. // 倾斜传感器
  93. record.TiltSendsor = int64((data[19] >> 5) & 0x1)
  94. record.ProjectID = projectId
  95. record.TowerNo = int(towerNo)
  96. if TowerFrameBaseInfoWrite(record) != nil {
  97. return makeResponse(sn, towerNo, TowerFrameBaseInfoResponse, version, []byte{0xfd}), nil
  98. }
  99. return makeResponse(sn, towerNo, TowerFrameBaseInfoResponse, version, []byte{0x00}), nil
  100. }