Blame view

go/src/ubees-conv/conv.go 2.2 KB
05acf2c25   김태훈   Added Converter f...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
  // Converter for Ubees-Tracker
  
  package main
  
  import (
  	"bufio"
  	"bytes"
  	"encoding/binary"
  	"flag"
  	"fmt"
  	"io"
  	"log"
  	"os"
  	"regexp"
  	"strconv"
  	"time"
  )
  
  type data struct {
  	Time      uint32
  	Msec      uint16
  	Latitude  int32
  	Longitude int32
  	Checksum  uint16
  }
  
  const bytesPerLine = 128
  
  var inputFileName string
  var outputFileName string
  
  func init() {
  	flag.Usage = func() {
  		fmt.Fprintf(os.Stderr, "Usage: %s [OPTION] FILE
  ", os.Args[0])
  		flag.PrintDefaults()
  	}
  
  	flag.StringVar(&outputFileName, "o", "", "Specify output file path. Default is FILE.out")
  	flag.Parse()
  
  	if flag.NArg() == 0 {
  		flag.Usage()
  		os.Exit(2)
  	}
  
  	inputFileName = flag.Arg(0)
  
  	if outputFileName == "" {
  		outputFileName = inputFileName + ".out"
  	}
  }
  
  func main() {
  	fileIn, err := os.Open(inputFileName)
  	check(err)
  
  	defer fileIn.Close()
  
  	fileOut, err := os.OpenFile(outputFileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
  	check(err)
  
  	defer fileOut.Close()
  
  	b := parse(fileIn)
  	print(b, fileOut)
  }
  
  func check(err error) {
  	if err != nil {
  		log.Fatal(err)
  	}
  }
  
  func parse(r io.Reader) *bytes.Buffer {
  	b := bytes.NewBuffer(nil)
  
  	rLine := regexp.MustCompile(`:.*\|`)
  	rByte := regexp.MustCompile(`[0-9A-Fa-f]{2}`)
  
  	br := bufio.NewReader(r)
  	for {
  		l, _, err := br.ReadLine()
  		if err != nil {
  			if err == io.EOF {
  				return b
  			}
  
  			log.Fatal(err)
  		}
  
  		mLine := rLine.Find(l)
  		if mLine == nil {
  			log.Printf("Line not matched %#v", string(l))
  			continue
  		}
  
  		if bytes.HasPrefix(mLine, []byte(":FF")) {
  			continue
  		}
  
  		mBytes := rByte.FindAll(mLine, -1)
  		if len(mBytes) != bytesPerLine {
  			log.Printf("Length not matched %v %#v
  ", len(mBytes), string(l))
  		}
  
  		for _, mByte := range mBytes {
  			i, err := strconv.ParseUint(string(mByte), 16, 8)
  			check(err)
  
  			b.WriteByte(byte(i))
  		}
  	}
  }
  
  func print(r io.Reader, w io.Writer) {
  	bw := bufio.NewWriter(w)
  	defer bw.Flush()
  
  	var d data
  	for {
  		err := binary.Read(r, binary.LittleEndian, &d)
  		if err != nil {
  			if err == io.EOF {
  				return
  			}
  
  			log.Fatal(err)
  		}
  
  		t := time.Unix(int64(d.Time), int64(d.Msec)*1e6)
  		bw.WriteString(fmt.Sprintf("%v, %.4f, %.4f
  ",
  			t.Format("2006-01-02 15:04:05.000"),
  			float64(d.Latitude)/10000,
  			float64(d.Longitude)/10000))
  	}
  }