123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- package v1
- import (
- "encoding/json"
- "fmt"
- "strconv"
- "strings"
- "time"
- "tower-monitor/model"
- )
- type TowerRealtimeData struct {
- DateTime int64 `json:"date_time"`
- High float64 `json:"high"`
- Range float64 `json:"range"`
- Backturn float64 `json:"backturn"`
- Weight int64 `json:"weight"`
- WindSpeed float64 `json:"wind_speed"`
- Tilt float64 `json:"tilt"`
- WeightPercent int64 `json:"weight_percent"`
- MomentPercent int64 `json:"moment_percent"`
- WindSpeedPercent int64 `json:"wind_speed_percent"`
- TiltPercent int64 `json:"tilt_percent"`
- AlarmReason int64 `json:"alarm_reason"`
- Braking int64 `json:"braking"`
- Lng float64 `json:"lng"`
- Lat float64 `json:"lat"`
- Sn string `json:"sn"`
- ProjectId int64 `json:"project_id"`
- TowerNo int64 `json:"tower_no"`
- }
- func TowerRealtimeDataWrite(req *TowerRealtimeData) error {
- tags := map[string]string{"sn": req.Sn}
- fields := map[string]interface{}{}
- bytes, _ := json.Marshal(req)
- json.Unmarshal(bytes, &fields)
- delete(fields, "sn")
- t := time.Now()
- // 写数据
- err := model.WriteTowerData(req.Sn, tags, fields, t, "realtime_data")
- if err != nil {
- return err
- }
- return nil
- }
- func TowerFrameRealtimeDataRequestHandle(sn string, towerNo byte, version byte, data []byte) (res []byte, err error) {
- hexPrintf(data)
- projectId,devicename := checkAuth(sn)
- if projectId <= 0 {
- // 未认证或需重新认证
- return makeResponse(sn, towerNo, TowerFrameRealtimeDataResponse, version, []byte{0xfe}), nil
- }
- if len(data) < 46 {
- return makeResponse(sn, towerNo, TowerFrameRealtimeDataResponse, version, []byte{0xff}), nil
- }
- record := &TowerRealtimeData{}
- record.Sn = sn
- record.TowerNo = int64(towerNo)
- record.ProjectId = projectId
- // 时间戳
- record.DateTime = int64(data[0]) << 24 + int64(data[1]) << 16 + int64(data[2]) << 8 + int64(data[3])
- // 高度
- record.High = (float64(int16(data[4])<<8) + float64(data[5])) * 0.1
- // 幅度
- record.Range = (float64(int16(data[6])<<8) + float64(data[7])) * 0.1
- // 回转
- record.Backturn = (float64(int16(data[8])<<8) + float64(data[9])) * 0.1
- // 起重量数据
- record.Weight = int64(data[10])<<8 + int64(data[11])
- // 风速
- record.WindSpeed = (float64(int16(data[12])<<8) + float64(data[13])) * 0.1
- // 倾角
- record.Tilt = (float64(int16(data[14])<<8) + float64(data[15]))*0.1
- // 重量百分比
- record.WeightPercent = int64(data[16])
- // 力矩百分比
- record.MomentPercent = int64(data[17])
- // 风速百分比
- record.WindSpeedPercent = int64(data[18])
- // 倾斜百分比
- record.TiltPercent = int64(data[19])
- // 警报原因
- // 211 212 213 214 碰撞预警
- // 111 112 113 114 碰撞报警
- // 12 13 14 15 重量 力矩 倾斜 风速报警
- // 22 23 24 25 重量 力矩 倾斜 风速预警
- record.AlarmReason = int64(data[20])
- // 制动状态:1个字节,8bit 从低到高分别是 上下前后左右
- // Bit 1:表示断开; bit 0:表示闭合
- record.Braking = int64(data[21])
- // 经度
- str := string(data[22:34])
- str = strings.TrimSpace(str)
- record.Lng, _ = strconv.ParseFloat(str, 64)
- fmt.Printf("lng:%s,0x%02x\n", str, data[22])
- // 纬度
- str = string(data[34:46])
- fmt.Printf("lat:%s,0x%02x\n", str, data[34])
- str = strings.TrimSpace(str)
- record.Lat, _ = strconv.ParseFloat(str, 64)
- handleAlarm(record.AlarmReason, sn, int64(towerNo), record.DateTime, projectId, devicename)
- if TowerRealtimeDataWrite(record) != nil {
- return makeResponse(sn, towerNo, TowerFrameRealtimeDataResponse, version, []byte{0xfd}), nil
- }
- if record.AlarmReason > 0 {
- //alarmRecord := &TowerAlarmData{}
- //bytes, _ := json.Marshal(record)
- //json.Unmarshal(bytes, alarmRecord)
- //TowerAlarmDataWrite(alarmRecord)
- }
- return makeResponse(sn, towerNo, TowerFrameRealtimeDataResponse, version, []byte{0x00}), nil
- }
|