// 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" "lift-monitor/model" ) func LiftFrameSensorConfWrite(record *model.LiftSensorConf) error { p := &model.LiftSensorConf{} where := map[string]interface{}{ "SN": record.SN, "LiftNo": record.LiftNo, "ProjectID": record.ProjectID, "LiftDirect":record.LiftDirect, } 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{}{ "EmptyWeightAD":record.EmptyWeightAD, "EmptyWeight":record.EmptyWeight, "LoadWeightAD":record.LoadWeightAD, "LoadWeight":record.LoadWeight, "LowHighAD":record.LowHighAD, "LowHigh":record.LowHigh, "TopHighAD":record.TopHighAD, "TopHigh":record.TopHigh, "UpdatedAt": time.Now(), } where = map[string]interface{}{ "ID": p.ID, } return p.Update(database.DB(), where, values) } func LiftFrameSensorConfRequestHandle(sn string, liftNo byte, version byte, data []byte) (res []byte, err error) { projectId,_ := checkAuth(sn) if projectId <= 0 { // 未认证或需重新认证 return makeResponse(sn, liftNo, LiftFrameSensorConfResponse, version, []byte{0xfe}), nil } if len(data) < 16 { return makeResponse(sn, liftNo, LiftFrameSensorConfResponse, version, []byte{0xff}), nil } record := &model.LiftSensorConf{} // 重量空载AD值 record.EmptyWeightAD = int64(data[0])<<8 + int64(data[1]) // 重量空载实际值 record.EmptyWeight = int64(data[2])<<8 + int64(data[3]) // 重量负载AD值 record.LoadWeightAD = int64(data[4])<<8 + int64(data[5]) // 重量负载实际值 record.LoadWeight = int64(data[6])<<8 + int64(data[7]) // 高度底端AD值 record.LowHighAD = (float64(int16(data[8])<<8) + float64(data[9])) * 0.1 // 高度底端实际值 record.LowHigh = (float64(int16(data[10])<<8) + float64(data[11])) * 0.1 // 高度顶端AD值 record.TopHighAD = (float64(int16(data[12])<<8) + float64(data[13])) * 0.1 // 高度顶端实际值 record.TopHigh = (float64(int16(data[14])<<8) + float64(data[15])) * 0.1 record.ProjectID = projectId record.LiftNo = int(liftNo) >> 1 record.LiftDirect = int(liftNo) & 1 record.SN = sn if LiftFrameSensorConfWrite(record) != nil { return makeResponse(sn, liftNo, LiftFrameSensorConfResponse, version, []byte{0xfd}), nil } return makeResponse(sn, liftNo, LiftFrameSensorConfResponse, version, []byte{0x00}), nil }