123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- // 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 TowerFrameBaseInfoWrite(record *model.TowerBaseInfo) error {
- p := &model.TowerBaseInfo{}
- 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{}{
- "X": record.X,
- "Y": record.Y,
- "TiltSendsor": record.TiltSendsor,
- "WindSensor": record.WindSensor,
- "WeightSensor": record.WeightSensor,
- "BackturnSensor": record.BackturnSensor,
- "RangeSensor": record.RangeSensor,
- "HighSensor": record.HighSensor,
- "ArticulationLength": record.ArticulationLength,
- "HookWeight": record.HookWeight,
- "MaxMoment": record.MaxMoment,
- "MaxWeight": record.MaxWeight,
- "ArmHigh": record.ArmHigh,
- "HatHigh": record.HatHigh,
- "BackArmLength": record.BackArmLength,
- "FrontArmLength": record.FrontArmLength,
- "UpdatedAt": time.Now(),
- }
- where = map[string]interface{}{
- "ID": p.ID,
- }
- return p.Update(database.DB(), where, values)
- }
- func TowerFrameBaseInfoRequestHandle(sn string, towerNo byte, version byte, data []byte) (res []byte, err error) {
- projectId,_ := checkAuth(sn)
- if projectId <= 0 {
- // 未认证或需重新认证
- return makeResponse(sn, towerNo, TowerFrameBaseInfoResponse, version, []byte{0xfe}), nil
- }
- if len(data) < 21 {
- return makeResponse(sn, towerNo, TowerFrameBaseInfoResponse, version, []byte{0xff}), nil
- }
- record := &model.TowerBaseInfo{}
- record.SN = sn
- // 坐标x
- record.X = (float64(int16(data[0])<<8) + float64(data[1])) * 0.1
- // 坐标y
- record.Y = (float64(int16(data[2])<<8) + float64(data[3])) * 0.1
- // 前臂长
- record.FrontArmLength = (float64(int16(data[4])<<8) + float64(data[5])) * 0.1
- // 后壁长
- record.BackArmLength = (float64(int16(data[6])<<8) + float64(data[7])) * 0.1
- // 塔帽高
- record.HatHigh = (float64(int16(data[8])<<8) + float64(data[9])) * 0.1
- // 塔臂高
- record.ArmHigh = (float64(int16(data[10])<<8) + float64(data[11])) * 0.1
- // 最大吊重kg
- record.MaxWeight = int64(data[12])<<8 + int64(data[13])
- // 最大力矩
- record.MaxMoment = (float64(int16(data[14])<<8) + float64(data[15])) * 0.01
- // 吊钩重量kg
- record.HookWeight = int64(data[16])<<8 + int64(data[17])
- // 铰接长度
- record.ArticulationLength = (float64(int16(data[18])<<8) + float64(data[19])) * 0.1
- // 传感器安装状态
- // 高度传感器
- record.HighSensor = int64(data[20] & 0x01)
- // 幅度传感器
- record.RangeSensor = int64((data[19] >> 1) & 0x1)
- // 回转传感器
- record.BackturnSensor = int64((data[19] >> 2) & 0x1)
- // 重量传感器
- record.WeightSensor = int64((data[19] >> 3) & 0x1)
- // 风速传感器
- record.WindSensor = int64((data[19] >> 4) & 0x1)
- // 倾斜传感器
- record.TiltSendsor = int64((data[19] >> 5) & 0x1)
- record.ProjectID = projectId
- record.TowerNo = int(towerNo)
- if TowerFrameBaseInfoWrite(record) != nil {
- return makeResponse(sn, towerNo, TowerFrameBaseInfoResponse, version, []byte{0xfd}), nil
- }
- return makeResponse(sn, towerNo, TowerFrameBaseInfoResponse, version, []byte{0x00}), nil
- }
|