tower_alarm_conf.go 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 TowerFrameAlarmConfWrite(record *model.TowerAlarmConf) error {
  11. p := &model.TowerAlarmConf{}
  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. "HorizontalDistanceWarning":record.HorizontalDistanceWarning,
  28. "TiltAlarm":record.TiltAlarm,
  29. "WindSpeedAlarm":record.WindSpeedAlarm,
  30. "MomentAlarm":record.MomentAlarm,
  31. "WeightAlarm":record.WeightAlarm,
  32. "VerticalDistanceAlarm":record.VerticalDistanceAlarm,
  33. "HorizontalDistanceAlarm":record.HorizontalDistanceAlarm,
  34. "MomentWarning":record.MomentWarning,
  35. "TiltWarning":record.TiltWarning,
  36. "VerticalDistanceWarning":record.VerticalDistanceWarning,
  37. "WeightWarning":record.WeightWarning,
  38. "WindSpeedWarning":record.WindSpeedWarning,
  39. "UpdatedAt": time.Now(),
  40. }
  41. where = map[string]interface{}{
  42. "ID": p.ID,
  43. }
  44. return p.Update(database.DB(), where, values)
  45. }
  46. func TowerFrameAlarmConfRequestHandle(sn string, towerNo byte, version byte, data []byte) (res []byte, err error) {
  47. projectId,_ := checkAuth(sn)
  48. if projectId <= 0 {
  49. // 未认证或需重新认证
  50. return makeResponse(sn, towerNo, TowerFrameAlarmConfResponse, version, []byte{0xfe}), nil
  51. }
  52. if len(data) < 24 {
  53. return makeResponse(sn, towerNo, TowerFrameAlarmConfResponse, version, []byte{0xff}), nil
  54. }
  55. record := &model.TowerAlarmConf{}
  56. // 水平距离报警值
  57. record.HorizontalDistanceAlarm = (float64(int16(data[0])<<8) + float64(data[1])) * 0.1
  58. // 垂直距离报警值
  59. record.VerticalDistanceAlarm = (float64(int16(data[2])<<8) + float64(data[3])) * 0.1
  60. // 重量报警值
  61. record.WeightAlarm = int64(data[4])<<8 + int64(data[5])
  62. // 力矩报警值
  63. record.MomentAlarm = (float64(int16(data[6])<<8) + float64(data[7])) * 0.01
  64. // 风速报警值
  65. record.WindSpeedAlarm = (float64(int16(data[8])<<8) + float64(data[9])) * 0.1
  66. // 倾斜报警值
  67. record.TiltAlarm = (float64(int16(data[10])<<8) + float64(data[11]))*0.1
  68. // 水平距离预警值
  69. record.HorizontalDistanceWarning = (float64(int16(data[12])<<8) + float64(data[13])) * 0.1
  70. // 垂直距离预警值
  71. record.VerticalDistanceWarning = (float64(int16(data[14])<<8) + float64(data[15])) * 0.1
  72. // 重量预警值
  73. record.WeightWarning = int64(data[16])<<8 + int64(data[17])
  74. // 力矩预警值
  75. record.MomentWarning = (float64(int16(data[18])<<8) + float64(data[19])) * 0.01
  76. // 风速预警值
  77. record.WindSpeedWarning = (float64(int16(data[20])<<8) + float64(data[21])) * 0.1
  78. // 倾斜预警值
  79. record.TiltWarning = (float64(int16(data[22])<<8) + float64(data[23]))*0.1
  80. record.ProjectID = projectId
  81. record.SN = sn
  82. record.TowerNo = int(towerNo)
  83. if TowerFrameAlarmConfWrite(record) != nil {
  84. return makeResponse(sn, towerNo, TowerFrameAlarmConfResponse, version, []byte{0xfd}), nil
  85. }
  86. return makeResponse(sn, towerNo, TowerFrameAlarmConfResponse, version, []byte{0x00}), nil
  87. }