123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- // Copyright 2019 github.com. All rights reserved.
- // Use of this source code is governed by github.com.
- package v1
- import (
- "github.com/jaryhe/gopkgs/database"
- "github.com/jinzhu/gorm"
- "time"
- "tower-monitor/model"
- )
- func TowerFrameSensorConfWrite(record *model.TowerSensorConf) error {
- p := &model.TowerSensorConf{}
- where := map[string]interface{}{
- "SN": record.SN,
- "TowerNo": record.TowerNo,
- "ProjectID": record.ProjectID,
- }
- err := p.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound {
- return err
- }
- if p.ID == 0 {
- record.CreatedAt = time.Now()
- record.UpdatedAt = record.CreatedAt
- return record.Insert(database.DB())
- }
- values := map[string]interface{}{
- "TiltCoefficient":record.TiltCoefficient,
- "WindSpeedCoefficient":record.WindSpeedCoefficient,
- "BackturnEnd":record.BackturnEnd,
- "BackturnEndAD":record.BackturnEndAD,
- "BackturnStart":record.BackturnStart,
- "LoadWeight":record.LoadWeight,
- "LoadWeightAD":record.LoadWeightAD,
- "EmptyWeight":record.EmptyWeight,
- "EmptyWeightAD":record.EmptyWeightAD,
- "FarHigh":record.FarHigh,
- "FarHighAD":record.FarHighAD,
- "NearHigh":record.NearHigh,
- "NearHighAD":record.NearHighAD,
- "FarRange":record.FarRange,
- "FarRangeAD":record.FarRangeAD,
- "NearRange":record.NearRange,
- "NearRangeAD":record.NearRangeAD,
- "BackturnStartAD":record.BackturnStartAD,
- "UpdatedAt": time.Now(),
- }
- where = map[string]interface{}{
- "ID": p.ID,
- }
- return p.Update(database.DB(), where, values)
- }
- func TowerFrameSensorConfRequestHandle(sn string, towerNo byte, version byte, data []byte) (res []byte, err error) {
- projectId,_ := checkAuth(sn)
- if projectId <= 0 {
- // 未认证或需重新认证
- return makeResponse(sn, towerNo, TowerFrameSensorConfResponse, version, []byte{0xfe}), nil
- }
- if len(data) < 36 {
- return makeResponse(sn, towerNo, TowerFrameSensorConfResponse, version, []byte{0xff}), nil
- }
- record := &model.TowerSensorConf{}
- // 近端幅度标AD定值
- record.NearRangeAD = (float64(int16(data[0])<<8) + float64(data[1]))*0.1
- // 近端幅度标定实际值
- record.NearRange= (float64(int16(data[2])<<8) + float64(data[3])) * 0.1
- // 远端幅度标AD定值
- record.FarRangeAD= (float64(int16(data[4])<<8) + float64(data[5]))*0.1
- // 远端幅度标定实际值
- record.FarRange = (float64(int16(data[6])<<8) + float64(data[7])) * 0.1
- // 高度近端标AD定值
- record.NearHighAD = (float64(int16(data[8])<<8) + float64(data[9]))*0.1
- // 高度近端标定实际值
- record.NearHigh = (float64(int16(data[10])<<8) + float64(data[11])) * 0.1
- // 高度远端标AD定值
- record.FarHighAD= (float64(int16(data[12])<<8) + float64(data[13]))*0.1
- // 高度远端标定实际值
- record.FarHigh = (float64(int16(data[14])<<8) + float64(data[15])) * 0.1
- // 空载重量AD值
- record.EmptyWeightAD = int64(data[16])<<8 + int64(data[17])
- // 空载重量实际值
- record.EmptyWeight = (int64(data[18])<<8 + int64(data[19]))
- // 负载重量AD值
- record.LoadWeightAD = int64(data[20])<<8 + int64(data[21])
- // 负载重量实际值
- record.LoadWeight = (int64(data[22])<<8 + int64(data[23]))
- // 回转起点AD值
- record.BackturnStartAD= (float64(int16(data[24])<<8) + float64(data[25]))*0.1
- // 回转起点实际角度
- record.BackturnStart = (float64(int16(data[26])<<8) + float64(data[27])) * 0.1
- // 回转终点AD值
- record.BackturnEndAD = (float64(int16(data[28])<<8) + float64(data[29]))*0.1
- // 回转终点实际角度
- record.BackturnEnd = (float64(int16(data[30])<<8) + float64(data[31])) * 0.1
- // 风速校准系数
- record.WindSpeedCoefficient = float64(int16(data[32])<<8) + float64(data[33])
- // 倾斜校准系数
- record.TiltCoefficient = (float64(int16(data[34])<<8) + float64(data[35]))
- record.SN = sn
- record.ProjectID = projectId
- record.TowerNo = int(towerNo)
- if TowerFrameSensorConfWrite(record) != nil {
- return makeResponse(sn, towerNo, TowerFrameSensorConfResponse, version, []byte{0xfd}), nil
- }
- return makeResponse(sn, towerNo, TowerFrameSensorConfResponse, version, []byte{0x00}), nil
- }
|