12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- // 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 TowerFrameAlarmConfWrite(record *model.TowerAlarmConf) error {
- p := &model.TowerAlarmConf{}
- 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{}{
- "HorizontalDistanceWarning":record.HorizontalDistanceWarning,
- "TiltAlarm":record.TiltAlarm,
- "WindSpeedAlarm":record.WindSpeedAlarm,
- "MomentAlarm":record.MomentAlarm,
- "WeightAlarm":record.WeightAlarm,
- "VerticalDistanceAlarm":record.VerticalDistanceAlarm,
- "HorizontalDistanceAlarm":record.HorizontalDistanceAlarm,
- "MomentWarning":record.MomentWarning,
- "TiltWarning":record.TiltWarning,
- "VerticalDistanceWarning":record.VerticalDistanceWarning,
- "WeightWarning":record.WeightWarning,
- "WindSpeedWarning":record.WindSpeedWarning,
- "UpdatedAt": time.Now(),
- }
- where = map[string]interface{}{
- "ID": p.ID,
- }
- return p.Update(database.DB(), where, values)
- }
- func TowerFrameAlarmConfRequestHandle(sn string, towerNo byte, version byte, data []byte) (res []byte, err error) {
- projectId,_ := checkAuth(sn)
- if projectId <= 0 {
- // 未认证或需重新认证
- return makeResponse(sn, towerNo, TowerFrameAlarmConfResponse, version, []byte{0xfe}), nil
- }
- if len(data) < 24 {
- return makeResponse(sn, towerNo, TowerFrameAlarmConfResponse, version, []byte{0xff}), nil
- }
- record := &model.TowerAlarmConf{}
- // 水平距离报警值
- record.HorizontalDistanceAlarm = (float64(int16(data[0])<<8) + float64(data[1])) * 0.1
- // 垂直距离报警值
- record.VerticalDistanceAlarm = (float64(int16(data[2])<<8) + float64(data[3])) * 0.1
- // 重量报警值
- record.WeightAlarm = int64(data[4])<<8 + int64(data[5])
- // 力矩报警值
- record.MomentAlarm = (float64(int16(data[6])<<8) + float64(data[7])) * 0.01
- // 风速报警值
- record.WindSpeedAlarm = (float64(int16(data[8])<<8) + float64(data[9])) * 0.1
- // 倾斜报警值
- record.TiltAlarm = (float64(int16(data[10])<<8) + float64(data[11]))*0.1
- // 水平距离预警值
- record.HorizontalDistanceWarning = (float64(int16(data[12])<<8) + float64(data[13])) * 0.1
- // 垂直距离预警值
- record.VerticalDistanceWarning = (float64(int16(data[14])<<8) + float64(data[15])) * 0.1
- // 重量预警值
- record.WeightWarning = int64(data[16])<<8 + int64(data[17])
- // 力矩预警值
- record.MomentWarning = (float64(int16(data[18])<<8) + float64(data[19])) * 0.01
- // 风速预警值
- record.WindSpeedWarning = (float64(int16(data[20])<<8) + float64(data[21])) * 0.1
- // 倾斜预警值
- record.TiltWarning = (float64(int16(data[22])<<8) + float64(data[23]))*0.1
- record.ProjectID = projectId
- record.SN = sn
- record.TowerNo = int(towerNo)
- if TowerFrameAlarmConfWrite(record) != nil {
- return makeResponse(sn, towerNo, TowerFrameAlarmConfResponse, version, []byte{0xfd}), nil
- }
- return makeResponse(sn, towerNo, TowerFrameAlarmConfResponse, version, []byte{0x00}), nil
- }
|