|
@@ -6,14 +6,12 @@ import (
|
|
|
"fmt"
|
|
|
"strings"
|
|
|
"encoding/binary"
|
|
|
+ "devel.mephi.ru/dyokunev/voltlogger_parser/models"
|
|
|
)
|
|
|
const (
|
|
|
WRITEBLOCK_SIZE int64 = 512
|
|
|
+ WINDOW_SIZE int = 1 // Should be essentially less than 32768 (localTimestamp size/2)
|
|
|
)
|
|
|
-/*
|
|
|
-const (
|
|
|
- TIMESTAMP_WINDOW int = 4096
|
|
|
-)*/
|
|
|
|
|
|
type VoltloggerDumpRawHeader struct {
|
|
|
Version byte
|
|
@@ -35,6 +33,35 @@ type VoltloggerDumpHeader struct {
|
|
|
ChannelsNum uint8
|
|
|
}
|
|
|
|
|
|
+func makeWindow(r VoltloggerDumpHeader) (ret []models.VoltloggerRow) {
|
|
|
+ ret = make([]models.VoltloggerRow, WINDOW_SIZE)
|
|
|
+/*
|
|
|
+ for i:=0; i < WINDOW_SIZE; i++ {
|
|
|
+ ret[i].Values = make([]int32, r.ChannelsNum)
|
|
|
+ }
|
|
|
+*/
|
|
|
+ return ret
|
|
|
+}
|
|
|
+
|
|
|
+func pushPopWindow(window []models.VoltloggerRow, newRow models.VoltloggerRow) (oldRow models.VoltloggerRow) {
|
|
|
+ oldestNum := 0
|
|
|
+
|
|
|
+ for i:=0; i < WINDOW_SIZE; i++ {
|
|
|
+ if (window[i].TimestampGlobal == 0) {
|
|
|
+ window[i] = newRow
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if (window[i].TimestampGlobal < window[oldestNum].TimestampGlobal) {
|
|
|
+ oldestNum = i
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ oldRow = window[oldestNum]
|
|
|
+ window[oldestNum].TimestampGlobal = 0
|
|
|
+
|
|
|
+ return oldRow
|
|
|
+}
|
|
|
+
|
|
|
func get16(dumpFile *os.File) (r uint16, err error) {
|
|
|
err = binary.Read(dumpFile, binary.LittleEndian, &r)
|
|
|
return r, err
|
|
@@ -45,7 +72,7 @@ func get8(dumpFile *os.File) (r uint8, err error) {
|
|
|
return r, err
|
|
|
}
|
|
|
|
|
|
-func ParseVoltloggerDump(dumpPath string, noHeaders bool, channelsNum uint8, adc8Bit bool, headerHandler func(VoltloggerDumpHeader, interface{})(error), rowHandler func(int64, []int32, VoltloggerDumpHeader, interface{})(error), arg interface{}) (err error) {
|
|
|
+func ParseVoltloggerDump(dumpPath string, noHeaders bool, channelsNum uint8, adc8Bit bool, headerHandler func(VoltloggerDumpHeader, interface{})(error), rowHandler func(models.VoltloggerRow, VoltloggerDumpHeader, interface{})(error), arg interface{}) (err error) {
|
|
|
var r VoltloggerDumpHeader
|
|
|
|
|
|
// Openning the "dumpPath" as a file
|
|
@@ -113,44 +140,47 @@ func ParseVoltloggerDump(dumpPath string, noHeaders bool, channelsNum uint8, adc
|
|
|
// Parsing the Data
|
|
|
|
|
|
var pos int64
|
|
|
- var timestampGlobal int64
|
|
|
var timestampLocalOld uint16
|
|
|
- timestampGlobal = -1
|
|
|
+ var row models.VoltloggerRow
|
|
|
+ row.TimestampGlobal = -1
|
|
|
+
|
|
|
+ window := makeWindow(r)
|
|
|
|
|
|
for err = nil; err == nil; pos++ {
|
|
|
+ row.TimestampLocal = 0;
|
|
|
|
|
|
if (r.NoClock != 0) {
|
|
|
- timestampGlobal++
|
|
|
+ row.TimestampGlobal++
|
|
|
} else {
|
|
|
- timestampLocal, err := get16(dumpFile)
|
|
|
+ row.TimestampLocal, err = get16(dumpFile)
|
|
|
if (err != nil) {
|
|
|
break
|
|
|
}
|
|
|
|
|
|
- if (timestampGlobal < 0) {
|
|
|
- timestampGlobal = int64(timestampLocal)
|
|
|
+ if (row.TimestampGlobal < 0) {
|
|
|
+ row.TimestampGlobal = int64(row.TimestampLocal)
|
|
|
} else {
|
|
|
var timestampLocalDiff int
|
|
|
- timestampLocalDiff = int(timestampLocal) - int(timestampLocalOld)
|
|
|
+ timestampLocalDiff = int(row.TimestampLocal) - int(timestampLocalOld)
|
|
|
/*if (timestampLocalDiff*timestampLocalDiff > TIMESTAMP_WINDOW*TIMESTAMP_WINDOW) {
|
|
|
break
|
|
|
}*/
|
|
|
|
|
|
- timestampLocalOld = uint16(timestampLocal)
|
|
|
+ timestampLocalOld = uint16(row.TimestampLocal)
|
|
|
if (timestampLocalDiff < 0) {
|
|
|
if (r.ZeroClockOnNewBlock != 0) {
|
|
|
timestampLocalDiff = 0
|
|
|
} else {
|
|
|
timestampLocalDiff += (1 << 16)
|
|
|
}
|
|
|
- timestampGlobal += r.BlockWriteClockDelay
|
|
|
+ row.TimestampGlobal += r.BlockWriteClockDelay
|
|
|
}
|
|
|
|
|
|
- timestampGlobal += int64(timestampLocalDiff)
|
|
|
+ row.TimestampGlobal += int64(timestampLocalDiff)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- row := make([]int32, r.ChannelsNum)
|
|
|
+ row.Values = make([]int32, r.ChannelsNum)
|
|
|
for i:=uint8(0); i < r.ChannelsNum; i++ {
|
|
|
var value uint16
|
|
|
if (r.Bitness8ADC != 0) {
|
|
@@ -163,10 +193,17 @@ func ParseVoltloggerDump(dumpPath string, noHeaders bool, channelsNum uint8, adc
|
|
|
if (err != nil) {
|
|
|
break
|
|
|
}
|
|
|
- row[i] = int32(value)
|
|
|
+ row.Values[i] = int32(value)
|
|
|
+ }
|
|
|
+
|
|
|
+ row = pushPopWindow(window, row)
|
|
|
+
|
|
|
+ if (row.TimestampGlobal == 0) { // If the window is not filled, yet
|
|
|
+ continue
|
|
|
}
|
|
|
+ timestampLocalOld = row.TimestampLocal
|
|
|
|
|
|
- err = rowHandler(timestampGlobal, row, r, arg);
|
|
|
+ err = rowHandler(row, r, arg);
|
|
|
if (err != nil) {
|
|
|
return fmt.Errorf("Got an error from rowHandler: %v", err);
|
|
|
}
|