package v1 import ( "time" "tower-monitor/model" "encoding/json" ) type TowerLoopData struct { Sn string `json:"sn"` ProjectId int64 `json:"project_id"` TowerNo int64 `json:"tower_no"` StartTime int64 `json:"start_time"` EndTime int64 `json:"end_time"` MaxWeight int64 `json:"max_weight"` MaxMoment float64 `json:"max_moment"` MaxHigh float64 `json:"max_high"` MinHigh float64 `json:"min_high"` MaxRange float64 `json:"max_range"` MinRange float64 `json:"min_range"` StartAngle float64 `json:"start_angle"` StartRange float64 `json:"start_range"` StartHigh float64 `json:"start_high"` EndAngle float64 `json:"end_angle"` EndHigh float64 `json:"end_high"` EndRange float64 `json:"end_range"` } func TowerLoopDataWrite(req *TowerLoopData) 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, "loop_data") if err != nil { return err } return nil } func TowerFrameLoopDataRequestHandle(sn string, towerNo byte, version byte, data []byte) (res []byte, err error) { projectId,_ := checkAuth(sn) if projectId <= 0 { // 未认证或需重新认证 return makeResponse(sn, towerNo, TowerFrameLoopDataResponse, version, []byte{0xfe}), nil } if len(data) < 32 { return makeResponse(sn, towerNo, TowerFrameLoopDataResponse, version, []byte{0xff}), nil } record := &TowerLoopData{} record.Sn = sn record.TowerNo = int64(towerNo) record.ProjectId = projectId // 时间戳 record.StartTime = int64(data[0]) << 24 + int64(data[1]) << 16 + int64(data[2]) << 8 + int64(data[3]) record.EndTime = int64(data[4]) << 24 + int64(data[5]) << 16 + int64(data[6]) << 8 + int64(data[7]) // 本次循环最大吊重 record.MaxWeight = (int64(data[8]<<8) + int64(data[9])) // 本次循环最大力矩 record.MaxMoment = (float64(int16(data[10]) << 8) + float64(data[11])) * 0.01 // 本次循环最大高度 record.MaxHigh = (float64(int16(data[12]) << 8) + float64(data[13])) * 0.1 // 本次循环最小高度 record.MinHigh = (float64(int16(data[14]) << 8) + float64(data[15])) * 0.1 // 本次循环最大幅度 record.MaxRange = (float64(int16(data[16]) << 8) + float64(data[17])) * 0.1 // 本次循环最小幅度 record.MinRange = (float64(int16(data[18]) << 8) + float64(data[19])) * 0.1 // 起吊点角度 record.StartAngle = (float64(int16(data[20]) << 8) + float64(data[21])) * 0.1 // 起吊点高度 record.StartHigh = (float64(int16(data[22]) << 8) + float64(data[23])) * 0.1 // 起吊点幅度 record.StartRange = (float64(int16(data[24]) << 8) + float64(data[25])) * 0.1 // 卸吊点角度 record.EndAngle = (float64(int16(data[26]) << 8) + float64(data[27])) * 0.1 // 卸吊点高度 record.EndHigh = (float64(int16(data[28]) << 8) + float64(data[29])) * 0.1 // 卸吊点幅度 record.EndRange = (float64(int16(data[30]) << 8) + float64(data[31])) * 0.1 if TowerLoopDataWrite(record) != nil { return makeResponse(sn, towerNo, TowerFrameLoopDataResponse, version, []byte{0xfd}), nil } return makeResponse(sn, towerNo, TowerFrameLoopDataResponse, version, []byte{0x00}), nil }