lift_sensor_conf.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 LiftFrameSensorConfWrite(record *model.LiftSensorConf) error {
  11. p := &model.LiftSensorConf{}
  12. where := map[string]interface{}{
  13. "SN": record.SN,
  14. "LiftNo": record.LiftNo,
  15. "ProjectID": record.ProjectID,
  16. "LiftDirect":record.LiftDirect,
  17. }
  18. err := p.Find(database.DB(), where)
  19. if err != nil && err != gorm.ErrRecordNotFound {
  20. return err
  21. }
  22. if p.ID == 0 {
  23. record.CreatedAt = time.Now()
  24. record.UpdatedAt = record.CreatedAt
  25. return record.Insert(database.DB())
  26. }
  27. values := map[string]interface{}{
  28. "EmptyWeightAD":record.EmptyWeightAD,
  29. "EmptyWeight":record.EmptyWeight,
  30. "LoadWeightAD":record.LoadWeightAD,
  31. "LoadWeight":record.LoadWeight,
  32. "LowHighAD":record.LowHighAD,
  33. "LowHigh":record.LowHigh,
  34. "TopHighAD":record.TopHighAD,
  35. "TopHigh":record.TopHigh,
  36. "UpdatedAt": time.Now(),
  37. }
  38. where = map[string]interface{}{
  39. "ID": p.ID,
  40. }
  41. return p.Update(database.DB(), where, values)
  42. }
  43. func LiftFrameSensorConfRequestHandle(sn string, liftNo byte, version byte, data []byte) (res []byte, err error) {
  44. projectId,_ := checkAuth(sn)
  45. if projectId <= 0 {
  46. // 未认证或需重新认证
  47. return makeResponse(sn, liftNo, LiftFrameSensorConfResponse, version, []byte{0xfe}), nil
  48. }
  49. if len(data) < 16 {
  50. return makeResponse(sn, liftNo, LiftFrameSensorConfResponse, version, []byte{0xff}), nil
  51. }
  52. record := &model.LiftSensorConf{}
  53. // 重量空载AD值
  54. record.EmptyWeightAD = int64(data[0])<<8 + int64(data[1])
  55. // 重量空载实际值
  56. record.EmptyWeight = int64(data[2])<<8 + int64(data[3])
  57. // 重量负载AD值
  58. record.LoadWeightAD = int64(data[4])<<8 + int64(data[5])
  59. // 重量负载实际值
  60. record.LoadWeight = int64(data[6])<<8 + int64(data[7])
  61. // 高度底端AD值
  62. record.LowHighAD = (float64(int16(data[8])<<8) + float64(data[9])) * 0.1
  63. // 高度底端实际值
  64. record.LowHigh = (float64(int16(data[10])<<8) + float64(data[11])) * 0.1
  65. // 高度顶端AD值
  66. record.TopHighAD = (float64(int16(data[12])<<8) + float64(data[13])) * 0.1
  67. // 高度顶端实际值
  68. record.TopHigh = (float64(int16(data[14])<<8) + float64(data[15])) * 0.1
  69. record.ProjectID = projectId
  70. record.LiftNo = int(liftNo) >> 1
  71. record.LiftDirect = int(liftNo) & 1
  72. record.SN = sn
  73. if LiftFrameSensorConfWrite(record) != nil {
  74. return makeResponse(sn, liftNo, LiftFrameSensorConfResponse, version, []byte{0xfd}), nil
  75. }
  76. return makeResponse(sn, liftNo, LiftFrameSensorConfResponse, version, []byte{0x00}), nil
  77. }