author | Mikael Berthe <mikael@lilotux.net> |
Sun, 19 Feb 2017 13:40:13 +0100 | |
changeset 3 | 429d7e612cfd |
parent 2 | 2452d9b23ec1 |
child 4 | 1ec268839876 |
permissions | -rw-r--r-- |
0 | 1 |
package main |
2 |
||
3 |
import ( |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
4 |
"encoding/json" |
1
05b31eb60d27
Switch to github.com/tarm/serial
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
5 |
"fmt" |
0 | 6 |
"io" |
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
7 |
"io/ioutil" |
0 | 8 |
"log" |
3 | 9 |
"time" |
0 | 10 |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
11 |
flag "github.com/docker/docker/pkg/mflag" |
1
05b31eb60d27
Switch to github.com/tarm/serial
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
12 |
"github.com/tarm/serial" |
0 | 13 |
) |
14 |
||
15 |
type measurement struct { |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
16 |
Header int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
17 |
Systolic int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
18 |
Diastolic int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
19 |
Pulse int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
20 |
Month int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
21 |
Day int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
22 |
Hour int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
23 |
Minute int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
24 |
Year int |
0 | 25 |
} |
26 |
||
27 |
func getData(s io.ReadWriteCloser, buf []byte, size int) (int, error) { |
|
28 |
t := 0 |
|
29 |
b := buf |
|
30 |
for t < size { |
|
31 |
n, err := s.Read(b[t:]) |
|
32 |
if err != nil { |
|
33 |
log.Fatal(err) // XXX |
|
34 |
return t, err |
|
35 |
} |
|
36 |
//log.Printf("(%d bytes) %q\n", n, b[t:t+1]) |
|
37 |
t = t + n |
|
38 |
} |
|
39 |
return t, nil |
|
40 |
} |
|
41 |
||
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
42 |
func fetchData(dev string) (items []measurement, err error) { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
43 |
c := &serial.Config{Name: dev, Baud: 4800} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
44 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
45 |
var s *serial.Port |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
46 |
s, err = serial.OpenPort(c) |
0 | 47 |
if err != nil { |
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
48 |
return items, err |
0 | 49 |
} |
50 |
||
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
51 |
// =================== Handshake ===================== |
0 | 52 |
q := []byte("\xaa") |
53 |
//log.Printf("Query: %q\n", q) |
|
54 |
log.Println("Starting handshake...") |
|
55 |
n, err := s.Write(q) |
|
56 |
if err != nil { |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
57 |
return items, err |
0 | 58 |
} |
59 |
||
60 |
buf := make([]byte, 128) |
|
61 |
n, err = getData(s, buf, 1) |
|
62 |
if err != nil { |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
63 |
return items, err |
0 | 64 |
} |
65 |
if n == 1 && buf[0] == '\x55' { |
|
66 |
log.Println("Handshake successful.") |
|
67 |
} else { |
|
68 |
log.Printf("(%d bytes) %q\n", n, buf[:n]) |
|
69 |
s.Close() |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
70 |
return items, fmt.Errorf("handshake failed") |
0 | 71 |
} |
72 |
||
73 |
// =================== Desc ===================== |
|
74 |
q = []byte("\xa4") |
|
75 |
//log.Printf("Query: %q\n", q) |
|
76 |
log.Println("Requesting device description...") |
|
77 |
n, err = s.Write(q) |
|
78 |
if err != nil { |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
79 |
return items, err |
0 | 80 |
} |
81 |
||
82 |
n, err = getData(s, buf, 32) |
|
83 |
log.Printf("DESC> %q\n", buf[:n]) |
|
84 |
||
85 |
// =================== Count ===================== |
|
86 |
q = []byte("\xa2") |
|
87 |
//log.Printf("Query: %q\n", q) |
|
88 |
log.Println("Requesting data counter...") |
|
89 |
n, err = s.Write(q) |
|
90 |
if err != nil { |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
91 |
return items, err |
0 | 92 |
} |
93 |
||
94 |
n, err = getData(s, buf, 1) |
|
95 |
if err != nil { |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
96 |
return items, err |
0 | 97 |
} |
98 |
var nRecords int |
|
99 |
if n == 1 { |
|
100 |
log.Printf("%d item(s) available.", buf[0]) |
|
101 |
nRecords = int(buf[0]) |
|
102 |
} else { |
|
103 |
log.Printf("(%d bytes) %q\n", n, buf[:n]) |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
104 |
return items, fmt.Errorf("no measurement found") |
0 | 105 |
} |
106 |
||
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
107 |
// =================== Records ===================== |
0 | 108 |
for i := 0; i < nRecords; i++ { |
109 |
q = []byte{'\xa3', uint8(i + 1)} |
|
110 |
//log.Printf("Query: %q\n", q) |
|
111 |
//log.Printf("Requesting measurement %d...", i+1) |
|
112 |
n, err = s.Write(q) |
|
113 |
if err != nil { |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
114 |
return items, err |
0 | 115 |
} |
116 |
||
117 |
n, err = getData(s, buf, 9) |
|
118 |
//log.Printf("DESC> %q\n", buf[:n]) |
|
119 |
||
120 |
var data measurement |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
121 |
data.Header = int(buf[0]) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
122 |
data.Systolic = int(buf[1]) + 25 |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
123 |
data.Diastolic = int(buf[2]) + 25 |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
124 |
data.Pulse = int(buf[3]) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
125 |
data.Month = int(buf[4]) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
126 |
data.Day = int(buf[5]) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
127 |
data.Hour = int(buf[6]) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
128 |
data.Minute = int(buf[7]) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
129 |
data.Year = int(buf[8]) + 2000 |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
130 |
items = append(items, data) |
0 | 131 |
} |
132 |
||
133 |
s.Close() |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
134 |
return items, nil |
0 | 135 |
} |
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
136 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
137 |
func loadFromJSONFile(filename string) (items []measurement, err error) { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
138 |
data, err := ioutil.ReadFile(filename) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
139 |
if err != nil { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
140 |
return items, err |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
141 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
142 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
143 |
err = json.Unmarshal(data, &items) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
144 |
return items, err |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
145 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
146 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
147 |
func mergeItems(newItems, oldItems []measurement) []measurement { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
148 |
var result []measurement |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
149 |
var j int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
150 |
// TODO: Would be better to compare dates and merge chronologically... |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
151 |
for _, nItem := range newItems { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
152 |
result = append(result, nItem) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
153 |
if j+1 <= len(oldItems) && nItem == oldItems[j] { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
154 |
j++ |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
155 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
156 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
157 |
if j+1 <= len(oldItems) { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
158 |
result = append(result, oldItems[j:]...) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
159 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
160 |
return result |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
161 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
162 |
|
3 | 163 |
func parseDate(dateStr string) (date time.Time, err error) { |
164 |
if dateStr == "" { |
|
165 |
return |
|
166 |
} |
|
167 |
||
168 |
var yy, mm, dd, h, m, s int |
|
169 |
n, e := fmt.Sscanf(dateStr, "%d-%d-%d %d:%d:%d", &yy, &mm, &dd, &h, &m, &s) |
|
170 |
if e != nil && n < 3 { |
|
171 |
err = e |
|
172 |
return |
|
173 |
} |
|
174 |
if n < 6 { |
|
175 |
log.Printf("Date parsed with only %d fields\n", n) |
|
176 |
} |
|
177 |
date = time.Date(yy, time.Month(mm), dd, h, m, s, 0, time.Local) |
|
178 |
return |
|
179 |
} |
|
180 |
||
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
181 |
func main() { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
182 |
inFile := flag.String([]string{"-input-file", "i"}, "", "Input JSON file") |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
183 |
outFile := flag.String([]string{"-output-file", "o"}, "", "Output JSON file") |
3 | 184 |
limit := flag.Uint([]string{"-limit", "l"}, 0, "Limit number of items to N first") |
185 |
since := flag.String([]string{"-since"}, "", |
|
186 |
"Filter records from date (YYYY-mm-dd HH:MM:SS)") |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
187 |
format := flag.String([]string{"-format", "f"}, "", "Output format (csv, json)") |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
188 |
avg := flag.Bool([]string{"-average", "a"}, false, "Compute average") |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
189 |
merge := flag.Bool([]string{"-merge", "m"}, false, |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
190 |
"Try to merge input JSON file with fetched data") |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
191 |
device := flag.String([]string{"-device", "d"}, "/dev/ttyUSB0", "Serial device") |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
192 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
193 |
flag.Parse() |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
194 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
195 |
switch *format { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
196 |
case "": |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
197 |
if *outFile == "" { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
198 |
*format = "csv" |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
199 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
200 |
break |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
201 |
case "json", "csv": |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
202 |
break |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
203 |
default: |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
204 |
log.Fatal("Unknown output format. Possible choices are csv, json.") |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
205 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
206 |
|
3 | 207 |
startDate, err := parseDate(*since) |
208 |
if err != nil { |
|
209 |
log.Fatal("Could not parse date: ", err) |
|
210 |
} |
|
211 |
||
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
212 |
var items []measurement |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
213 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
214 |
if *inFile == "" { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
215 |
// Read from device |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
216 |
if items, err = fetchData(*device); err != nil { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
217 |
log.Fatal(err) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
218 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
219 |
} else { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
220 |
// Read from file |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
221 |
var fileItems []measurement |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
222 |
if fileItems, err = loadFromJSONFile(*inFile); err != nil { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
223 |
log.Fatal(err) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
224 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
225 |
if *merge { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
226 |
if items, err = fetchData(*device); err != nil { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
227 |
log.Fatal(err) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
228 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
229 |
items = mergeItems(items, fileItems) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
230 |
} else { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
231 |
items = fileItems |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
232 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
233 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
234 |
|
3 | 235 |
if !startDate.IsZero() { |
236 |
log.Printf("Filtering out records before %v...\n", startDate) |
|
237 |
for i := range items { |
|
238 |
iDate := time.Date(items[i].Year, time.Month(items[i].Month), |
|
239 |
items[i].Day, items[i].Hour, items[i].Minute, 0, 0, |
|
240 |
time.Local) |
|
241 |
if iDate.Sub(startDate) < 0 { |
|
242 |
items = items[0:i] |
|
243 |
break |
|
244 |
} |
|
245 |
} |
|
246 |
} |
|
247 |
||
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
248 |
if *limit > 0 && len(items) > int(*limit) { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
249 |
items = items[0:*limit] |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
250 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
251 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
252 |
var avgMeasure measurement |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
253 |
var avgCount int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
254 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
255 |
for i, data := range items { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
256 |
if *format == "csv" { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
257 |
fmt.Printf("%d;%x;%d-%02d-%02d %02d:%02d;%d;%d;%d\n", |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
258 |
i+1, data.Header, |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
259 |
data.Year, data.Month, data.Day, |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
260 |
data.Hour, data.Minute, |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
261 |
data.Systolic, data.Diastolic, data.Pulse) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
262 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
263 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
264 |
avgMeasure.Systolic += data.Systolic |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
265 |
avgMeasure.Diastolic += data.Diastolic |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
266 |
avgMeasure.Pulse += data.Pulse |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
267 |
avgCount++ |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
268 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
269 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
270 |
if *avg && avgCount > 0 { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
271 |
avgMeasure.Systolic /= avgCount |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
272 |
avgMeasure.Diastolic /= avgCount |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
273 |
avgMeasure.Pulse /= avgCount |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
274 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
275 |
fmt.Printf("Average: %d;%d;%d\n", avgMeasure.Systolic, |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
276 |
avgMeasure.Diastolic, avgMeasure.Pulse) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
277 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
278 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
279 |
if *format == "json" || *outFile != "" { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
280 |
rawJSON, err := json.MarshalIndent(items, "", " ") |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
281 |
if err != nil { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
282 |
log.Fatal("Error:", err) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
283 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
284 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
285 |
if *format == "json" { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
286 |
fmt.Println(string(rawJSON)) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
287 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
288 |
if *outFile != "" { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
289 |
err = ioutil.WriteFile(*outFile, rawJSON, 0600) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
290 |
if err != nil { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
291 |
log.Println("Could not write output file:", err) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
292 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
293 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
294 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
295 |
} |