lift_alarm_conf.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. "lift-monitor/model"
  9. )
  10. func LiftFrameAlarmConfWrite(record *model.LiftAlarmConf) error {
  11. p := &model.LiftAlarmConf{}
  12. where := map[string]interface{}{
  13. "SN": record.SN,
  14. "LiftNo": record.LiftNo,
  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. "MaxWeightWarning":record.MaxWeightWarning,
  28. "MaxWeightAlarm":record.MaxWeightAlarm,
  29. "MaxHigh":record.MaxHigh,
  30. "MaxSpeedWarning":record.MaxSpeedWarning,
  31. "MaxSpeedAlarm":record.MaxSpeedAlarm,
  32. "MaxPeople":record.MaxPeople,
  33. "TiltWarning":record.TiltWarning ,
  34. "TiltAlarm":record.TiltAlarm,
  35. "WindSpeedWarning":record.WindSpeedWarning,
  36. "WindSpeedAlarm":record.WindSpeedAlarm,
  37. "UpdatedAt": time.Now(),
  38. }
  39. where = map[string]interface{}{
  40. "ID": p.ID,
  41. }
  42. return p.Update(database.DB(), where, values)
  43. }
  44. func LiftFrameAlarmConfRequestHandle(sn string, liftNo byte, version byte, data []byte) (res []byte, err error) {
  45. projectId,_ := checkAuth(sn)
  46. if projectId <= 0 {
  47. // 未认证或需重新认证
  48. return makeResponse(sn, liftNo, LiftFrameAlarmConfResponse, version, []byte{0xfe}), nil
  49. }
  50. if len(data) < 20 {
  51. return makeResponse(sn, liftNo, LiftFrameAlarmConfResponse, version, []byte{0xff}), nil
  52. }
  53. record := &model.LiftAlarmConf{}
  54. // 最大起重量预警
  55. record.MaxWeightWarning = int64(data[0])<<8 + int64(data[1])
  56. // 最大起重量报警
  57. record.MaxWeightAlarm = int64(data[2])<<8 + int64(data[3])
  58. // 最大起升高度
  59. record.MaxHigh = (float64(int16(data[4])<<8) + float64(data[5])) * 0.1
  60. // 最大速度预警
  61. record.MaxSpeedWarning = (float64(int16(data[6])<<8) + float64(data[7])) * 0.1
  62. // 最大速度报警
  63. record.MaxSpeedAlarm = (float64(int16(data[8])<<8) + float64(data[9])) * 0.1
  64. // 最大承载人数
  65. record.MaxPeople = int64(data[10])<<8 + int64(data[11])
  66. // 倾斜预警
  67. record.TiltWarning =(float64(int16(data[12])<<8) + float64(data[13])) * 0.1
  68. // 倾斜报警
  69. record.TiltAlarm = (float64(int16(data[14])<<8) + float64(data[15])) * 0.1
  70. // 风速预警
  71. record.WindSpeedWarning = (float64(int16(data[16])<<8) + float64(data[17])) * 0.1
  72. // 风速报警
  73. record.WindSpeedAlarm = (float64(int16(data[18])<<8) + float64(data[19])) * 0.1
  74. record.ProjectID = projectId
  75. record.SN = sn
  76. record.LiftNo = int(liftNo) >> 1
  77. record.LiftDirect = int(liftNo) & 1
  78. if LiftFrameAlarmConfWrite(record) != nil {
  79. return makeResponse(sn, liftNo, LiftFrameAlarmConfResponse, version, []byte{0xfd}), nil
  80. }
  81. return makeResponse(sn, liftNo, LiftFrameAlarmConfResponse, version, []byte{0x00}), nil
  82. }