main.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package main
  2. import (
  3. "os"
  4. "fmt"
  5. "time"
  6. "encoding/binary"
  7. "code.google.com/p/getopt"
  8. "devel.mephi.ru/dyokunev/voltlogger_parser/models"
  9. "devel.mephi.ru/dyokunev/voltlogger_parser/voltloggerParser"
  10. )
  11. type printRow_arg struct {
  12. outputPath string
  13. outputFile *os.File
  14. binaryOutput bool
  15. insertParseTime bool
  16. }
  17. func handleHeader(h voltloggerParser.VoltloggerDumpHeader, arg_iface interface{}) (err error) {
  18. /*
  19. arg := arg_iface.(*printRow_arg)
  20. if (arg.binaryOutput) {
  21. err = binary.Write(arg.outputFile, binary.LittleEndian, h)
  22. if (err != nil) {
  23. panic(err)
  24. // return err
  25. }
  26. }
  27. */
  28. return nil
  29. }
  30. func printRow(r models.VoltloggerRow, h voltloggerParser.VoltloggerDumpHeader, arg_iface interface{}) (err error) {
  31. arg := arg_iface.(*printRow_arg)
  32. var parseTime int64
  33. if (arg.insertParseTime) {
  34. parseTime = time.Now().UnixNano()
  35. }
  36. if (arg.binaryOutput) {
  37. if (arg.insertParseTime) {
  38. err = binary.Write(arg.outputFile, binary.LittleEndian, parseTime)
  39. if (err != nil) {
  40. panic(err)
  41. // return err
  42. }
  43. }
  44. err = binary.Write(arg.outputFile, binary.LittleEndian, r.TimestampGlobal)
  45. if (err != nil) {
  46. panic(err)
  47. // return err
  48. }
  49. err = binary.Write(arg.outputFile, binary.LittleEndian, r.Values)
  50. if (err != nil) {
  51. panic(err)
  52. // return err
  53. }
  54. } else {
  55. if (arg.insertParseTime) {
  56. fmt.Printf("%v\t", parseTime)
  57. }
  58. fmt.Printf("%v", r.TimestampGlobal)
  59. fmt.Printf("\t%v", r.TimestampLocal)
  60. rowLen := len(r.Values)
  61. for i:=0; i < rowLen; i++ {
  62. fmt.Printf("\t%v", r.Values[i])
  63. }
  64. fmt.Printf("\n")
  65. }
  66. return nil
  67. }
  68. func main() {
  69. var err error
  70. var dumpPath string
  71. var noHeaders bool
  72. var printRow_arg printRow_arg
  73. var channelsNumInt int
  74. var channelsNum uint8
  75. var adc8Bit bool
  76. getopt.StringVar(&dumpPath, 'i', "dump-path" )
  77. getopt.StringVar(&printRow_arg.outputPath, 'o', "output-path" ).SetOptional()
  78. getopt.BoolVar (&noHeaders, 'n', "no-headers" ).SetOptional()
  79. getopt.IntVar (&channelsNumInt, 'c', "force-channels-num" ).SetOptional()
  80. getopt.BoolVar (&printRow_arg.binaryOutput, 'b', "binary-output" ).SetOptional()
  81. getopt.BoolVar (&printRow_arg.insertParseTime, 't', "insert-parse-time" ).SetOptional()
  82. getopt.BoolVar (&adc8Bit, '8', "adc-8-bit" ).SetOptional()
  83. getopt.Parse()
  84. if (getopt.NArgs() > 0 || dumpPath == "") {
  85. getopt.Usage()
  86. os.Exit(-2)
  87. }
  88. channelsNum = uint8(channelsNumInt)
  89. switch (printRow_arg.outputPath) {
  90. /* case "":
  91. now := time.Now()
  92. year, month, day := now.Date()
  93. hour, min, sec := now.Clock()
  94. printRow_arg.outputPath = fmt.Sprintf("%v_%v-%02v-%02v_%02v:%02v:%02v.csv", h.DeviceName, year, int(month), day, hour, min, sec)
  95. break*/
  96. case "", "-":
  97. printRow_arg.outputPath = "/dev/stdout"
  98. printRow_arg.outputFile = os.Stdout
  99. break
  100. default:
  101. printRow_arg.outputFile,err = os.Open(printRow_arg.outputPath)
  102. panic(fmt.Errorf("Not supported yet"))
  103. }
  104. if (err != nil) {
  105. fmt.Printf("Cannot open output file: %v", err.Error())
  106. os.Exit(-1)
  107. }
  108. //if (printRow_arg.binaryOutput) {
  109. // err = binary.Write(printRow_arg.outputFile, binary.LittleEndian, printRow_arg)
  110. //}
  111. err = voltloggerParser.ParseVoltloggerDump(dumpPath, noHeaders, channelsNum, adc8Bit, handleHeader, printRow, &printRow_arg)
  112. if (err != nil) {
  113. fmt.Printf("Cannot parse the dump: %v (channels == %v)\n", err.Error(), channelsNum)
  114. os.Exit(-1)
  115. }
  116. printRow_arg.outputFile.Close()
  117. fmt.Printf("%v %v\n", dumpPath, printRow_arg)
  118. }