tower_position_conf.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 TowerFramePositionConfWrite(record *model.TowerPositionConf) error {
  11. p := &model.TowerPositionConf{}
  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. "BackturnPositionEnd":record.BackturnPositionEnd,
  28. "BackturnPositionStart":record.BackturnPositionStart,
  29. "HighPositionEnd":record.HighPositionEnd,
  30. "HighPositionStart":record.HighPositionStart,
  31. "RangePositionEnd":record.RangePositionEnd,
  32. "RangePositionStart":record.RangePositionStart,
  33. "UpdatedAt": time.Now(),
  34. }
  35. where = map[string]interface{}{
  36. "ID": p.ID,
  37. }
  38. return p.Update(database.DB(), where, values)
  39. }
  40. func TowerFramePositionConfRequestHandle(sn string, towerNo byte, version byte, data []byte) (res []byte, err error) {
  41. projectId,_ := checkAuth(sn)
  42. if projectId <= 0 {
  43. // 未认证或需重新认证
  44. return makeResponse(sn, towerNo, TowerFramePositionConfResponse, version, []byte{0xfe}), nil
  45. }
  46. if len(data) < 12 {
  47. return makeResponse(sn, towerNo, TowerFramePositionConfResponse, version, []byte{0xff}), nil
  48. }
  49. record := &model.TowerPositionConf{}
  50. // 幅度限位起点值
  51. record.RangePositionStart = (float64(int16(data[0])<<8) + float64(data[1])) * 0.1
  52. // 幅度限位终点值
  53. record.RangePositionEnd = (float64(int16(data[2])<<8) + float64(data[3])) * 0.1
  54. // 高度限位起点值
  55. record.HighPositionStart = (float64(int16(data[4])<<8) + float64(data[5])) * 0.1
  56. // 高度限位终点值
  57. record.HighPositionEnd = (float64(int16(data[6])<<8) + float64(data[7])) * 0.1
  58. // 回转限位起点值
  59. record.BackturnPositionStart = (float64(int16(data[8])<<8) + float64(data[9])) * 0.1
  60. // 回转限位终点值
  61. record.BackturnPositionStart = (float64(int16(data[10])<<8) + float64(data[11])) * 0.1
  62. record.SN = sn
  63. record.ProjectID = projectId
  64. record.TowerNo = int(towerNo)
  65. // TODO 入库
  66. return makeResponse(sn, towerNo, TowerFramePositionConfResponse, version, []byte{0x00}), nil
  67. }