package faultRecordFile import ( "fmt" proto "fwh-toolbox-server/function/FaultRecordProtocol" ) // Overview code=1 故障总体信息 type Overview struct { MaxRecords uint8 `json:"maxRecords"` CurrentRecords uint8 `json:"currentRecords"` EarliestTime string `json:"earliestTime"` // 设备时间 LatestTime string `json:"latestTime"` ChannelGroups } // DirectoryEntry code=3 目录项(14 字节) type DirectoryEntry struct { Index uint8 `json:"index"` Time string `json:"time"` // 设备时间 ParamListVersion string `json:"paramListVersion"` TriggerSource uint16 `json:"triggerSource"` TriggerSourceName string `json:"triggerSourceName,omitempty"` } const directoryEntrySize = 14 // ParseOverview 解析 code=1 func ParseOverview(b []byte) (Overview, error) { r := &byteReader{b: b} o := Overview{ MaxRecords: r.u8("maxRecords"), CurrentRecords: r.u8("currentRecords"), EarliestTime: r.time("earliestTime").String(), LatestTime: r.time("latestTime").String(), } o.ChannelGroups = readChannelGroups(r, false) return o, r.err } // ParseBasicInfo 解析 code=2 func ParseBasicInfo(b []byte) (BasicInfo, error) { if len(b) != 16 { return BasicInfo{}, fmt.Errorf("basic info must be 16 bytes, got %d", len(b)) } r := &byteReader{b: b} return readBasicInfo(r), r.err } // ParseDirectory 解析 code=3:1 字节条数 + N × 14 字节 func ParseDirectory(b []byte) ([]DirectoryEntry, error) { if len(b) < 1 { return nil, fmt.Errorf("directory is empty") } n := int(b[0]) if len(b) != 1+n*directoryEntrySize { return nil, fmt.Errorf("directory length %d does not match %d entries", len(b), n) } r := &byteReader{b: b[1:]} list := make([]DirectoryEntry, 0, n) for i := 0; i < n; i++ { e := DirectoryEntry{ Index: r.u8("index"), Time: r.time("time").String(), ParamListVersion: fmt.Sprintf("%08X", r.u32("paramListVersion")), TriggerSource: r.u16("triggerSource"), } e.TriggerSourceName = proto.TriggerSourceName(e.TriggerSource) list = append(list, e) } return list, r.err } // ParseRealtime 按 code 解析 hexData,返回对应结构 func ParseRealtime(code int, hexData string) (any, error) { b, err := proto.DecodeHex(hexData) if err != nil { return nil, err } switch code { case 1: return ParseOverview(b) case 2: return ParseBasicInfo(b) case 3: return ParseDirectory(b) default: return nil, fmt.Errorf("unsupported code %d", code) } }