Dmitry Yu Okunev лет назад: 8
Сommit
da7d1e80c0
5 измененных файлов с 101 добавлено и 0 удалено
  1. 1 0
      .gitignore
  2. 7 0
      Makefile
  3. BIN
      doc/testinput.bin
  4. 38 0
      main.go
  5. 55 0
      voltloggerParser/voltloggerReader.go

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+voltlogger_parser

+ 7 - 0
Makefile

@@ -0,0 +1,7 @@
+
+all:
+	go build -o voltlogger_parser main.go
+
+clean:
+	rm -f voltlogger_parser
+

BIN
doc/testinput.bin


+ 38 - 0
main.go

@@ -0,0 +1,38 @@
+package main
+
+import (
+	"os"
+	"fmt"
+	"time"
+	"code.google.com/p/getopt"
+	"devel.mephi.ru/dyokunev/voltlogger_parser/voltloggerParser"
+)
+
+func main() {
+	var dumpPath	string
+	var outputPath	string
+
+	getopt.StringVar(&dumpPath,	'i',	"dump-path")
+	getopt.StringVar(&outputPath,	'o',	"output-path").SetOptional()
+
+	getopt.Parse()
+	if (getopt.NArgs() > 0 || dumpPath == "") {
+		getopt.Usage()
+		os.Exit(-2)
+	}
+
+	dump, err := voltloggerReader.ParseVoltloggerDump(dumpPath)
+	if (err != nil) {
+		fmt.Printf("Cannot parse the dump: %v\n", err.Error())
+		os.Exit(-1)
+	}
+
+	if (outputPath == "") {
+		now              := time.Now()
+		year, month, day := now.Date()
+		hour, min,   sec := now.Clock()
+		outputPath        = fmt.Sprintf("%v_%v-%02v-%02v_%02v:%02v:%02v.csv", dump.DeviceName, year, int(month), day, hour, min, sec)
+	}
+
+	fmt.Printf("%v %v %v\n", dumpPath, outputPath, dump)
+}

+ 55 - 0
voltloggerParser/voltloggerReader.go

@@ -0,0 +1,55 @@
+package voltloggerReader
+
+import (
+	"os"
+	"fmt"
+	"encoding/binary"
+)
+
+type VoltloggerDumpRaw struct {
+	Version		byte
+	Magic		[11]byte
+	Modificators	byte
+	ChannelsNum	byte
+	Reserved0	[2]byte
+	DeviceName	[16]byte
+	Reserved1	[480]byte
+}
+
+type VoltloggerDump struct {
+	DeviceName	string
+	Data		map[int]map[int]int
+}
+
+func ParseVoltloggerDump(dumpPath string) (r VoltloggerDump, err error) {
+	// Openning the "dumpPath" as a file
+	dumpFile, err := os.Open(dumpPath)
+	if (err != nil) {
+		return r, err
+	}
+	defer dumpFile.Close()
+
+	// Reading binary data to a VoltloggerDumpRaw
+	var raw VoltloggerDumpRaw
+	err = binary.Read(dumpFile, binary.LittleEndian, &raw)
+	if (err != nil) {
+		return r, fmt.Errorf("Cannot read dump: %v", err.Error())
+	}
+
+	// Checking if the data of a known type
+	magicString := string(raw.Magic[:])
+	if (magicString != "voltlogger\000") {
+		return r, fmt.Errorf("The source is not a voltlogger dump (magic doesn't match: \"%v\" != \"voltlogger\")", magicString)
+	}
+
+	if (raw.Version != 0) {
+		return r, fmt.Errorf("Unsupported dump version: %v", raw.Version)
+	}
+
+
+
+	// Filling the VoltloggerDump struct
+	r.DeviceName = string(raw.DeviceName[:])
+
+	return r, nil
+}