From eb6e0bc95a9494b7fc0a825234d70e645fc37014 Mon Sep 17 00:00:00 2001 From: nuknal Date: Wed, 22 May 2024 11:31:08 +0800 Subject: [PATCH] xml report --- cmd/extract.go | 5 +++++ cmd/parse.go | 2 +- extract/aux.go | 18 +++++++++++++----- extract/process.go | 6 +++++- extract/report.go | 46 +++++++++++++++++++++++++++++++++++++++++++++ extract/seperate.go | 24 ++++++++++++++++++++++- 6 files changed, 93 insertions(+), 8 deletions(-) create mode 100644 extract/report.go diff --git a/cmd/extract.go b/cmd/extract.go index 93bc31e..bc12546 100644 --- a/cmd/extract.go +++ b/cmd/extract.go @@ -34,6 +34,11 @@ var extractCmd = &cobra.Command{ for i, d := range dats { e.SeprateAuxAndImgData(d, i) } + + if err := e.Report(); err != nil { + panic(err) + } + e.Cleanup() return diff --git a/cmd/parse.go b/cmd/parse.go index ada72e4..c3d7411 100644 --- a/cmd/parse.go +++ b/cmd/parse.go @@ -21,7 +21,7 @@ var parseCmd = &cobra.Command{ } e := extract.NewExtractor(¶ms) // p.ParseAuxPlatformWithHead("demo/ref/辅助数据.dat") - err := e.ExtractAux("demo/output/051622/SJY01_PMS_20240516_101236_051622_096.AUX", + _, _, _, err := e.ExtractAux("demo/output/051622/SJY01_PMS_20240516_101236_051622_096.AUX", "demo/temp/1.xlsx") if err != nil { diff --git a/extract/aux.go b/extract/aux.go index f6f8a86..90734e1 100644 --- a/extract/aux.go +++ b/extract/aux.go @@ -12,15 +12,15 @@ var ( ReferenceTime2000 = 946728000 ) -func (e Extractor) ExtractAux(auxfile, xlsxfile string) error { +func (e *Extractor) ExtractAux(auxfile, xlsxfile string) ([]*AuxFrameHead, []*AuxFocalBox, []*AuxPlatform, error) { os.Remove(xlsxfile) if err := createAuxXlsx(xlsxfile); err != nil { - return err + return nil, nil, nil, err } f, err := excelize.OpenFile(xlsxfile) if err != nil { - return err + return nil, nil, nil, err } defer f.Close() @@ -28,9 +28,13 @@ func (e Extractor) ExtractAux(auxfile, xlsxfile string) error { data, err := os.ReadFile(auxfile) if err != nil { log.Println("read aux data from", auxfile, "error:", err.Error()) - return err + return nil, nil, nil, err } + var afh []*AuxFrameHead + var afb []*AuxFocalBox + var aps []*AuxPlatform + row := 2 col := 1 for i := 0; i < len(data); i += 24 + 128 + 512 { @@ -40,17 +44,21 @@ func (e Extractor) ExtractAux(auxfile, xlsxfile string) error { var head AuxFrameHead head.Decode(data[i : i+24]) l0, _ := head.SaveXlsx(f, col, row) + afh = append(afh, &head) var box AuxFocalBox box.Decode(data[i+24 : i+24+128]) l1, _ := box.SaveXlsx(f, col+l0, row) + afb = append(afb, &box) var plat AuxPlatform plat.Decode(data[i+24+128 : i+24+128+512]) plat.SaveXlsx(f, col+l0+l1, row) + aps = append(aps, &plat) row++ } - return f.Save() + err = f.Save() + return afh, afb, aps, err } diff --git a/extract/process.go b/extract/process.go index 7686962..e0ea98e 100644 --- a/extract/process.go +++ b/extract/process.go @@ -3,11 +3,15 @@ package extract import ( "os" "path/filepath" + "sync" ) type Extractor struct { params *Params Clean bool + + mutex sync.RWMutex + report *Report } func NewExtractor(params *Params) *Extractor { @@ -20,7 +24,7 @@ func NewExtractor(params *Params) *Extractor { if err := os.MkdirAll(filepath.Base(params.LogFile), 0755); err != nil { panic(err) } - return &Extractor{params: params, Clean: true} + return &Extractor{params: params, Clean: true, report: &Report{SegmentDirRoot: params.OutputPath}} } func (e *Extractor) Cleanup() error { diff --git a/extract/report.go b/extract/report.go new file mode 100644 index 0000000..a315a0a --- /dev/null +++ b/extract/report.go @@ -0,0 +1,46 @@ +package extract + +import ( + "encoding/xml" + "os" + "path/filepath" +) + +type Report struct { + XMLName xml.Name `xml:"report"` + SegmentDirRoot string `xml:"segmentDirRoot"` + Segments []Segment `xml:"segments>segment"` +} + +type XMLTime struct { + TimeZone string `xml:"timeZone,attr"` + Value string `xml:",chardata"` +} + +type Segment struct { + Id string `xml:"segmentId"` + Aux string `xml:"aux"` + Pan string `xml:"pan"` + Mss string `xml:"mss"` + StartTime XMLTime `xml:"startTime"` + EndTime XMLTime `xml:"endTime"` + Meta string `xml:"meta"` +} + +func (e *Extractor) Report() error { + os.MkdirAll(filepath.Dir(e.params.Report), 0755) + f, err := os.Create(e.params.Report) + if err != nil { + return err + } + defer f.Close() + + data, err := xml.MarshalIndent(e.report, " ", " ") + if err != nil { + return err + } + + _, err = f.Write(data) + + return err +} diff --git a/extract/seperate.go b/extract/seperate.go index 49d8cb5..b4103c4 100644 --- a/extract/seperate.go +++ b/extract/seperate.go @@ -6,6 +6,7 @@ import ( "fmt" "math" "os" + "path/filepath" "strings" "time" @@ -198,7 +199,28 @@ func (e *Extractor) SeprateAuxAndImgData(dataFile string, segmentIndex int) erro lw.ws[AUX].w.Write(platAux[i*512 : (i+1)*512]) } - e.ExtractAux(lw.ws[AUX].name, lw.ws[AUX].name+".xlsx") + _, _, aps, err := e.ExtractAux(lw.ws[AUX].name, lw.ws[AUX].name+".xlsx") + if err != nil { + return err + } + + e.mutex.Lock() + defer e.mutex.Unlock() + seg := Segment{ + Pan: lw.ws[PAN_RAW].name, + Mss: lw.ws[MSS_RAW].name, + Aux: lw.ws[AUX].name, + Id: strings.Split(filepath.Base(lw.ws[AUX].name), ".")[0], + StartTime: XMLTime{ + TimeZone: "UTC", + Value: time.Unix(int64(aps[0].UTCTimeSec)+int64(ReferenceTime2000), int64(aps[0].Microsecond)*1000).UTC().Format(time.RFC3339), + }, + EndTime: XMLTime{ + TimeZone: "UTC", + Value: time.Unix(int64(aps[len(aps)-1].UTCTimeSec)+int64(ReferenceTime2000), int64(aps[len(aps)-1].Microsecond)*1000).UTC().Format(time.RFC3339), + }, + } + e.report.Segments = append(e.report.Segments, seg) return nil }