// 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 TowerFramePositionConfWrite(record *model.TowerPositionConf) error { p := &model.TowerPositionConf{} 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{}{ "BackturnPositionEnd":record.BackturnPositionEnd, "BackturnPositionStart":record.BackturnPositionStart, "HighPositionEnd":record.HighPositionEnd, "HighPositionStart":record.HighPositionStart, "RangePositionEnd":record.RangePositionEnd, "RangePositionStart":record.RangePositionStart, "UpdatedAt": time.Now(), } where = map[string]interface{}{ "ID": p.ID, } return p.Update(database.DB(), where, values) } func TowerFramePositionConfRequestHandle(sn string, towerNo byte, version byte, data []byte) (res []byte, err error) { projectId,_ := checkAuth(sn) if projectId <= 0 { // 未认证或需重新认证 return makeResponse(sn, towerNo, TowerFramePositionConfResponse, version, []byte{0xfe}), nil } if len(data) < 12 { return makeResponse(sn, towerNo, TowerFramePositionConfResponse, version, []byte{0xff}), nil } record := &model.TowerPositionConf{} // 幅度限位起点值 record.RangePositionStart = (float64(int16(data[0])<<8) + float64(data[1])) * 0.1 // 幅度限位终点值 record.RangePositionEnd = (float64(int16(data[2])<<8) + float64(data[3])) * 0.1 // 高度限位起点值 record.HighPositionStart = (float64(int16(data[4])<<8) + float64(data[5])) * 0.1 // 高度限位终点值 record.HighPositionEnd = (float64(int16(data[6])<<8) + float64(data[7])) * 0.1 // 回转限位起点值 record.BackturnPositionStart = (float64(int16(data[8])<<8) + float64(data[9])) * 0.1 // 回转限位终点值 record.BackturnPositionStart = (float64(int16(data[10])<<8) + float64(data[11])) * 0.1 record.SN = sn record.ProjectID = projectId record.TowerNo = int(towerNo) // TODO 入库 return makeResponse(sn, towerNo, TowerFramePositionConfResponse, version, []byte{0x00}), nil }