tower_sensor_conf.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 TowerFrameSensorConfWrite(record *model.TowerSensorConf) error {
  11. p := &model.TowerSensorConf{}
  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. "TiltCoefficient":record.TiltCoefficient,
  28. "WindSpeedCoefficient":record.WindSpeedCoefficient,
  29. "BackturnEnd":record.BackturnEnd,
  30. "BackturnEndAD":record.BackturnEndAD,
  31. "BackturnStart":record.BackturnStart,
  32. "LoadWeight":record.LoadWeight,
  33. "LoadWeightAD":record.LoadWeightAD,
  34. "EmptyWeight":record.EmptyWeight,
  35. "EmptyWeightAD":record.EmptyWeightAD,
  36. "FarHigh":record.FarHigh,
  37. "FarHighAD":record.FarHighAD,
  38. "NearHigh":record.NearHigh,
  39. "NearHighAD":record.NearHighAD,
  40. "FarRange":record.FarRange,
  41. "FarRangeAD":record.FarRangeAD,
  42. "NearRange":record.NearRange,
  43. "NearRangeAD":record.NearRangeAD,
  44. "BackturnStartAD":record.BackturnStartAD,
  45. "UpdatedAt": time.Now(),
  46. }
  47. where = map[string]interface{}{
  48. "ID": p.ID,
  49. }
  50. return p.Update(database.DB(), where, values)
  51. }
  52. func TowerFrameSensorConfRequestHandle(sn string, towerNo byte, version byte, data []byte) (res []byte, err error) {
  53. projectId,_ := checkAuth(sn)
  54. if projectId <= 0 {
  55. // 未认证或需重新认证
  56. return makeResponse(sn, towerNo, TowerFrameSensorConfResponse, version, []byte{0xfe}), nil
  57. }
  58. if len(data) < 36 {
  59. return makeResponse(sn, towerNo, TowerFrameSensorConfResponse, version, []byte{0xff}), nil
  60. }
  61. record := &model.TowerSensorConf{}
  62. // 近端幅度标AD定值
  63. record.NearRangeAD = (float64(int16(data[0])<<8) + float64(data[1]))*0.1
  64. // 近端幅度标定实际值
  65. record.NearRange= (float64(int16(data[2])<<8) + float64(data[3])) * 0.1
  66. // 远端幅度标AD定值
  67. record.FarRangeAD= (float64(int16(data[4])<<8) + float64(data[5]))*0.1
  68. // 远端幅度标定实际值
  69. record.FarRange = (float64(int16(data[6])<<8) + float64(data[7])) * 0.1
  70. // 高度近端标AD定值
  71. record.NearHighAD = (float64(int16(data[8])<<8) + float64(data[9]))*0.1
  72. // 高度近端标定实际值
  73. record.NearHigh = (float64(int16(data[10])<<8) + float64(data[11])) * 0.1
  74. // 高度远端标AD定值
  75. record.FarHighAD= (float64(int16(data[12])<<8) + float64(data[13]))*0.1
  76. // 高度远端标定实际值
  77. record.FarHigh = (float64(int16(data[14])<<8) + float64(data[15])) * 0.1
  78. // 空载重量AD值
  79. record.EmptyWeightAD = int64(data[16])<<8 + int64(data[17])
  80. // 空载重量实际值
  81. record.EmptyWeight = (int64(data[18])<<8 + int64(data[19]))
  82. // 负载重量AD值
  83. record.LoadWeightAD = int64(data[20])<<8 + int64(data[21])
  84. // 负载重量实际值
  85. record.LoadWeight = (int64(data[22])<<8 + int64(data[23]))
  86. // 回转起点AD值
  87. record.BackturnStartAD= (float64(int16(data[24])<<8) + float64(data[25]))*0.1
  88. // 回转起点实际角度
  89. record.BackturnStart = (float64(int16(data[26])<<8) + float64(data[27])) * 0.1
  90. // 回转终点AD值
  91. record.BackturnEndAD = (float64(int16(data[28])<<8) + float64(data[29]))*0.1
  92. // 回转终点实际角度
  93. record.BackturnEnd = (float64(int16(data[30])<<8) + float64(data[31])) * 0.1
  94. // 风速校准系数
  95. record.WindSpeedCoefficient = float64(int16(data[32])<<8) + float64(data[33])
  96. // 倾斜校准系数
  97. record.TiltCoefficient = (float64(int16(data[34])<<8) + float64(data[35]))
  98. record.SN = sn
  99. record.ProjectID = projectId
  100. record.TowerNo = int(towerNo)
  101. if TowerFrameSensorConfWrite(record) != nil {
  102. return makeResponse(sn, towerNo, TowerFrameSensorConfResponse, version, []byte{0xfd}), nil
  103. }
  104. return makeResponse(sn, towerNo, TowerFrameSensorConfResponse, version, []byte{0x00}), nil
  105. }