Browse Source

Added but didn't enabled Window

Dmitry Yu Okunev 4 years ago
parent
commit
d464e1bea4
3 changed files with 71 additions and 24 deletions
  1. 8 6
      main.go
  2. 8 0
      models/row.go
  3. 55 18
      voltloggerParser/voltloggerParser.go

+ 8 - 6
main.go

@@ -6,6 +6,7 @@ import (
 	"time"
 	"encoding/binary"
 	"code.google.com/p/getopt"
+	"devel.mephi.ru/dyokunev/voltlogger_parser/models"
 	"devel.mephi.ru/dyokunev/voltlogger_parser/voltloggerParser"
 )
 
@@ -30,7 +31,7 @@ func handleHeader(h voltloggerParser.VoltloggerDumpHeader, arg_iface interface{}
 	return nil
 }
 
-func printRow(ts int64, row []int32, h voltloggerParser.VoltloggerDumpHeader, arg_iface interface{}) (err error) {
+func printRow(r models.VoltloggerRow, h voltloggerParser.VoltloggerDumpHeader, arg_iface interface{}) (err error) {
 	arg := arg_iface.(*printRow_arg)
 	var parseTime int64
 
@@ -46,12 +47,12 @@ func printRow(ts int64, row []int32, h voltloggerParser.VoltloggerDumpHeader, ar
 //				return err
 			}
 		}
-		err = binary.Write(arg.outputFile, binary.LittleEndian, ts)
+		err = binary.Write(arg.outputFile, binary.LittleEndian, r.TimestampGlobal)
 		if (err != nil) {
 			panic(err)
 //			return err
 		}
-		err = binary.Write(arg.outputFile, binary.LittleEndian, row)
+		err = binary.Write(arg.outputFile, binary.LittleEndian, r.Values)
 		if (err != nil) {
 			panic(err)
 //			return err
@@ -60,10 +61,11 @@ func printRow(ts int64, row []int32, h voltloggerParser.VoltloggerDumpHeader, ar
 		if (arg.insertParseTime) {
 			fmt.Printf("%v\t", parseTime)
 		}
-		fmt.Printf("%v", ts)
-		rowLen := len(row)
+		fmt.Printf("%v",   r.TimestampGlobal)
+		fmt.Printf("\t%v", r.TimestampLocal)
+		rowLen := len(r.Values)
 		for i:=0; i < rowLen; i++ {
-			fmt.Printf("\t%v", row[i])
+			fmt.Printf("\t%v", r.Values[i])
 		}
 		fmt.Printf("\n")
 	}

+ 8 - 0
models/row.go

@@ -0,0 +1,8 @@
+package models
+
+type VoltloggerRow struct {
+	TimestampGlobal	  int64
+	TimestampLocal	 uint16
+	Values		[]int32
+}
+

+ 55 - 18
voltloggerParser/voltloggerParser.go

@@ -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);
 		}