author | Mikael Berthe <mikael@lilotux.net> |
Wed, 22 Feb 2017 19:47:43 +0100 | |
changeset 8 | 366f991716a9 |
parent 7 | 17a1a3f4fb86 |
child 9 | 588f7779b0b5 |
permissions | -rw-r--r-- |
4 | 1 |
// Copyright (C) 2015-2017 Mikael Berthe <mikael@lilotux.net>. All rights reserved. |
2 |
// Use of this source code is governed by the MIT license, |
|
3 |
// which can be found in the LICENSE file. |
|
5 | 4 |
// |
5 |
// Thanks to atbrask's blog post <http://www.atbrask.dk/?p=98> for the |
|
6 |
// protocol details. |
|
4 | 7 |
|
8 |
// gobm65 is a Beurer BM65 Blood Pressure Monitor CLI reader. |
|
9 |
||
0 | 10 |
package main |
11 |
||
6
9b16361e7950
Add installation instructions
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
12 |
// Installation: |
9b16361e7950
Add installation instructions
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
13 |
// |
9b16361e7950
Add installation instructions
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
14 |
// % go get hg.lilotux.net/golang/mikael/gobm65 |
9b16361e7950
Add installation instructions
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
15 |
// |
4 | 16 |
// Examples: |
17 |
// |
|
18 |
// Get help: |
|
19 |
// % gobm65 --help |
|
20 |
// |
|
21 |
// Get records and display the average: |
|
22 |
// % gobm65 --average |
|
23 |
// |
|
24 |
// Display the latest 3 records with the average: |
|
25 |
// % gobm65 -l 3 --average |
|
26 |
// Display all records since a specific date: |
|
27 |
// % gobm65 --since "2016-06-01" |
|
28 |
// Display all records of the last 7 days: |
|
29 |
// % gobm65 --since "$(date "+%F" -d "7 days ago")" |
|
30 |
// |
|
31 |
// Display the last/first 10 records in JSON: |
|
32 |
// % gobm65 -l 10 --format json |
|
33 |
// |
|
34 |
// Save the records to a JSON file: |
|
35 |
// % gobm65 -o data_u2.json |
|
36 |
// |
|
37 |
// Read a JSON file and display average of the last 3 records: |
|
38 |
// % gobm65 -i data_u2.json -l 3 --average |
|
39 |
// Read a JSON file, merge with device records, and save to another file: |
|
40 |
// % gobm65 -i data_u2.json --merge -o data_u2-new.json |
|
41 |
||
0 | 42 |
import ( |
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
43 |
"encoding/json" |
1
05b31eb60d27
Switch to github.com/tarm/serial
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
44 |
"fmt" |
0 | 45 |
"io" |
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
46 |
"io/ioutil" |
0 | 47 |
"log" |
8 | 48 |
"math" |
49 |
"sort" |
|
3 | 50 |
"time" |
0 | 51 |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
52 |
flag "github.com/docker/docker/pkg/mflag" |
1
05b31eb60d27
Switch to github.com/tarm/serial
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
53 |
"github.com/tarm/serial" |
0 | 54 |
) |
55 |
||
56 |
type measurement struct { |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
57 |
Header int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
58 |
Systolic int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
59 |
Diastolic int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
60 |
Pulse int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
61 |
Month int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
62 |
Day int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
63 |
Hour int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
64 |
Minute int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
65 |
Year int |
0 | 66 |
} |
67 |
||
68 |
func getData(s io.ReadWriteCloser, buf []byte, size int) (int, error) { |
|
69 |
t := 0 |
|
70 |
b := buf |
|
71 |
for t < size { |
|
72 |
n, err := s.Read(b[t:]) |
|
73 |
if err != nil { |
|
74 |
log.Fatal(err) // XXX |
|
75 |
return t, err |
|
76 |
} |
|
77 |
//log.Printf("(%d bytes) %q\n", n, b[t:t+1]) |
|
78 |
t = t + n |
|
79 |
} |
|
80 |
return t, nil |
|
81 |
} |
|
82 |
||
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
83 |
func fetchData(dev string) (items []measurement, err error) { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
84 |
c := &serial.Config{Name: dev, Baud: 4800} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
85 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
86 |
var s *serial.Port |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
87 |
s, err = serial.OpenPort(c) |
0 | 88 |
if err != nil { |
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
89 |
return items, err |
0 | 90 |
} |
91 |
||
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
92 |
// =================== Handshake ===================== |
0 | 93 |
q := []byte("\xaa") |
94 |
//log.Printf("Query: %q\n", q) |
|
95 |
log.Println("Starting handshake...") |
|
96 |
n, err := s.Write(q) |
|
97 |
if err != nil { |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
98 |
return items, err |
0 | 99 |
} |
100 |
||
101 |
buf := make([]byte, 128) |
|
102 |
n, err = getData(s, buf, 1) |
|
103 |
if err != nil { |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
104 |
return items, err |
0 | 105 |
} |
106 |
if n == 1 && buf[0] == '\x55' { |
|
107 |
log.Println("Handshake successful.") |
|
108 |
} else { |
|
109 |
log.Printf("(%d bytes) %q\n", n, buf[:n]) |
|
110 |
s.Close() |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
111 |
return items, fmt.Errorf("handshake failed") |
0 | 112 |
} |
113 |
||
114 |
// =================== Desc ===================== |
|
115 |
q = []byte("\xa4") |
|
116 |
//log.Printf("Query: %q\n", q) |
|
117 |
log.Println("Requesting device description...") |
|
118 |
n, err = s.Write(q) |
|
119 |
if err != nil { |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
120 |
return items, err |
0 | 121 |
} |
122 |
||
123 |
n, err = getData(s, buf, 32) |
|
124 |
log.Printf("DESC> %q\n", buf[:n]) |
|
125 |
||
126 |
// =================== Count ===================== |
|
127 |
q = []byte("\xa2") |
|
128 |
//log.Printf("Query: %q\n", q) |
|
129 |
log.Println("Requesting data counter...") |
|
130 |
n, err = s.Write(q) |
|
131 |
if err != nil { |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
132 |
return items, err |
0 | 133 |
} |
134 |
||
135 |
n, err = getData(s, buf, 1) |
|
136 |
if err != nil { |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
137 |
return items, err |
0 | 138 |
} |
139 |
var nRecords int |
|
140 |
if n == 1 { |
|
141 |
log.Printf("%d item(s) available.", buf[0]) |
|
142 |
nRecords = int(buf[0]) |
|
143 |
} else { |
|
144 |
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
|
145 |
return items, fmt.Errorf("no measurement found") |
0 | 146 |
} |
147 |
||
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
148 |
// =================== Records ===================== |
0 | 149 |
for i := 0; i < nRecords; i++ { |
150 |
q = []byte{'\xa3', uint8(i + 1)} |
|
151 |
//log.Printf("Query: %q\n", q) |
|
152 |
//log.Printf("Requesting measurement %d...", i+1) |
|
153 |
n, err = s.Write(q) |
|
154 |
if err != nil { |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
155 |
return items, err |
0 | 156 |
} |
157 |
||
158 |
n, err = getData(s, buf, 9) |
|
159 |
//log.Printf("DESC> %q\n", buf[:n]) |
|
160 |
||
161 |
var data measurement |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
162 |
data.Header = int(buf[0]) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
163 |
data.Systolic = int(buf[1]) + 25 |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
164 |
data.Diastolic = int(buf[2]) + 25 |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
165 |
data.Pulse = int(buf[3]) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
166 |
data.Month = int(buf[4]) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
167 |
data.Day = int(buf[5]) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
168 |
data.Hour = int(buf[6]) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
169 |
data.Minute = int(buf[7]) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
170 |
data.Year = int(buf[8]) + 2000 |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
171 |
items = append(items, data) |
0 | 172 |
} |
173 |
||
174 |
s.Close() |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
175 |
return items, nil |
0 | 176 |
} |
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
177 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
178 |
func loadFromJSONFile(filename string) (items []measurement, err error) { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
179 |
data, err := ioutil.ReadFile(filename) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
180 |
if err != nil { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
181 |
return items, err |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
182 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
183 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
184 |
err = json.Unmarshal(data, &items) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
185 |
return items, err |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
186 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
187 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
188 |
func mergeItems(newItems, oldItems []measurement) []measurement { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
189 |
var result []measurement |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
190 |
var j int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
191 |
// 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
|
192 |
for _, nItem := range newItems { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
193 |
result = append(result, nItem) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
194 |
if j+1 <= len(oldItems) && nItem == oldItems[j] { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
195 |
j++ |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
196 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
197 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
198 |
if j+1 <= len(oldItems) { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
199 |
result = append(result, oldItems[j:]...) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
200 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
201 |
return result |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
202 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
203 |
|
3 | 204 |
func parseDate(dateStr string) (date time.Time, err error) { |
205 |
if dateStr == "" { |
|
206 |
return |
|
207 |
} |
|
208 |
||
209 |
var yy, mm, dd, h, m, s int |
|
210 |
n, e := fmt.Sscanf(dateStr, "%d-%d-%d %d:%d:%d", &yy, &mm, &dd, &h, &m, &s) |
|
211 |
if e != nil && n < 3 { |
|
212 |
err = e |
|
213 |
return |
|
214 |
} |
|
215 |
if n < 6 { |
|
216 |
log.Printf("Date parsed with only %d fields\n", n) |
|
217 |
} |
|
218 |
date = time.Date(yy, time.Month(mm), dd, h, m, s, 0, time.Local) |
|
219 |
return |
|
220 |
} |
|
221 |
||
8 | 222 |
func average(items []measurement) (measurement, error) { |
223 |
var avgMeasure measurement |
|
224 |
var avgCount int |
|
225 |
||
226 |
for _, data := range items { |
|
227 |
avgMeasure.Systolic += data.Systolic |
|
228 |
avgMeasure.Diastolic += data.Diastolic |
|
229 |
avgMeasure.Pulse += data.Pulse |
|
230 |
avgCount++ |
|
231 |
} |
|
232 |
||
233 |
roundDivision := func(a, b int) int { |
|
234 |
return int(0.5 + float64(a)/float64(b)) |
|
235 |
} |
|
236 |
||
237 |
if avgCount == 0 { |
|
238 |
return avgMeasure, fmt.Errorf("cannot compute average: empty set") |
|
239 |
} |
|
240 |
||
241 |
avgMeasure.Systolic = roundDivision(avgMeasure.Systolic, avgCount) |
|
242 |
avgMeasure.Diastolic = roundDivision(avgMeasure.Diastolic, avgCount) |
|
243 |
avgMeasure.Pulse = roundDivision(avgMeasure.Pulse, avgCount) |
|
244 |
||
245 |
return avgMeasure, nil |
|
246 |
} |
|
247 |
||
248 |
func intMedian(numbers []int) int { |
|
249 |
middle := len(numbers) / 2 |
|
250 |
med := numbers[middle] |
|
251 |
if len(numbers)%2 == 0 { |
|
252 |
med = (med + numbers[middle-1]) / 2 |
|
253 |
} |
|
254 |
return med |
|
255 |
} |
|
256 |
||
257 |
func median(items []measurement) (measurement, error) { |
|
258 |
var med measurement |
|
259 |
if len(items) == 0 { |
|
260 |
return med, fmt.Errorf("cannot compute average: empty set") |
|
261 |
} |
|
262 |
||
263 |
var sys, dia, pul []int |
|
264 |
for _, data := range items { |
|
265 |
sys = append(sys, data.Systolic) |
|
266 |
dia = append(dia, data.Diastolic) |
|
267 |
pul = append(pul, data.Pulse) |
|
268 |
} |
|
269 |
||
270 |
sort.Ints(sys) |
|
271 |
sort.Ints(dia) |
|
272 |
sort.Ints(pul) |
|
273 |
||
274 |
med.Systolic = intMedian(sys) |
|
275 |
med.Diastolic = intMedian(dia) |
|
276 |
med.Pulse = intMedian(pul) |
|
277 |
||
278 |
return med, nil |
|
279 |
} |
|
280 |
||
281 |
func stdDeviation(items []measurement) (measurement, error) { |
|
282 |
var sDev measurement |
|
283 |
||
284 |
if len(items) <= 1 { |
|
285 |
return sDev, fmt.Errorf("cannot compute deviation: set too small") |
|
286 |
} |
|
287 |
||
288 |
var sumSys, sumDia, sumPul float64 |
|
289 |
avg, err := average(items) |
|
290 |
if err != nil { |
|
291 |
return sDev, err |
|
292 |
} |
|
293 |
||
294 |
for _, data := range items { |
|
295 |
sumSys += math.Pow(float64(data.Systolic-avg.Systolic), 2) |
|
296 |
sumDia += math.Pow(float64(data.Diastolic-avg.Diastolic), 2) |
|
297 |
sumPul += math.Pow(float64(data.Pulse-avg.Pulse), 2) |
|
298 |
} |
|
299 |
||
300 |
sDev.Systolic = int(math.Sqrt(sumSys / float64(len(items)-1))) |
|
301 |
sDev.Diastolic = int(math.Sqrt(sumDia / float64(len(items)-1))) |
|
302 |
sDev.Pulse = int(math.Sqrt(sumPul / float64(len(items)-1))) |
|
303 |
||
304 |
return sDev, nil |
|
305 |
} |
|
306 |
||
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
307 |
func main() { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
308 |
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
|
309 |
outFile := flag.String([]string{"-output-file", "o"}, "", "Output JSON file") |
3 | 310 |
limit := flag.Uint([]string{"-limit", "l"}, 0, "Limit number of items to N first") |
311 |
since := flag.String([]string{"-since"}, "", |
|
312 |
"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
|
313 |
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
|
314 |
avg := flag.Bool([]string{"-average", "a"}, false, "Compute average") |
8 | 315 |
stats := flag.Bool([]string{"-stats"}, false, "Compute statistics") |
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
316 |
merge := flag.Bool([]string{"-merge", "m"}, false, |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
317 |
"Try to merge input JSON file with fetched data") |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
318 |
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
|
319 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
320 |
flag.Parse() |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
321 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
322 |
switch *format { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
323 |
case "": |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
324 |
if *outFile == "" { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
325 |
*format = "csv" |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
326 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
327 |
break |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
328 |
case "json", "csv": |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
329 |
break |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
330 |
default: |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
331 |
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
|
332 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
333 |
|
3 | 334 |
startDate, err := parseDate(*since) |
335 |
if err != nil { |
|
336 |
log.Fatal("Could not parse date: ", err) |
|
337 |
} |
|
338 |
||
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
339 |
var items []measurement |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
340 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
341 |
if *inFile == "" { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
342 |
// Read from device |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
343 |
if items, err = fetchData(*device); err != nil { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
344 |
log.Fatal(err) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
345 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
346 |
} else { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
347 |
// Read from file |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
348 |
var fileItems []measurement |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
349 |
if fileItems, err = loadFromJSONFile(*inFile); err != nil { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
350 |
log.Fatal(err) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
351 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
352 |
if *merge { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
353 |
if items, err = fetchData(*device); err != nil { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
354 |
log.Fatal(err) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
355 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
356 |
items = mergeItems(items, fileItems) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
357 |
} else { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
358 |
items = fileItems |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
359 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
360 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
361 |
|
3 | 362 |
if !startDate.IsZero() { |
363 |
log.Printf("Filtering out records before %v...\n", startDate) |
|
364 |
for i := range items { |
|
365 |
iDate := time.Date(items[i].Year, time.Month(items[i].Month), |
|
366 |
items[i].Day, items[i].Hour, items[i].Minute, 0, 0, |
|
367 |
time.Local) |
|
368 |
if iDate.Sub(startDate) < 0 { |
|
369 |
items = items[0:i] |
|
370 |
break |
|
371 |
} |
|
372 |
} |
|
373 |
} |
|
374 |
||
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
375 |
if *limit > 0 && len(items) > int(*limit) { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
376 |
items = items[0:*limit] |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
377 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
378 |
|
8 | 379 |
if *format == "csv" { |
380 |
for i, data := range items { |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
381 |
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
|
382 |
i+1, data.Header, |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
383 |
data.Year, data.Month, data.Day, |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
384 |
data.Hour, data.Minute, |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
385 |
data.Systolic, data.Diastolic, data.Pulse) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
386 |
} |
8 | 387 |
} |
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
388 |
|
8 | 389 |
if *stats { |
390 |
*avg = true |
|
391 |
} |
|
392 |
||
393 |
if *avg && len(items) > 0 { |
|
394 |
avgMeasure, err := average(items) |
|
395 |
if err != nil { |
|
396 |
log.Println("Error:", err) |
|
397 |
} else { |
|
398 |
fmt.Printf("Average: %d;%d;%d\n", avgMeasure.Systolic, |
|
399 |
avgMeasure.Diastolic, avgMeasure.Pulse) |
|
400 |
} |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
401 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
402 |
|
8 | 403 |
if *stats && len(items) > 1 { |
404 |
d, err := stdDeviation(items) |
|
405 |
if err != nil { |
|
406 |
log.Println("Error:", err) |
|
407 |
} else { |
|
408 |
fmt.Printf("Standard deviation: %d;%d;%d\n", |
|
409 |
d.Systolic, d.Diastolic, d.Pulse) |
|
7
17a1a3f4fb86
Round results for average calculation
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
410 |
} |
8 | 411 |
} |
412 |
if *stats && len(items) > 0 { |
|
413 |
m, err := median(items) |
|
414 |
if err != nil { |
|
415 |
log.Println("Error:", err) |
|
416 |
} else { |
|
417 |
fmt.Printf("Median values: %d;%d;%d\n", |
|
418 |
m.Systolic, m.Diastolic, m.Pulse) |
|
419 |
} |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
420 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
421 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
422 |
if *format == "json" || *outFile != "" { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
423 |
rawJSON, err := json.MarshalIndent(items, "", " ") |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
424 |
if err != nil { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
425 |
log.Fatal("Error:", err) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
426 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
427 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
428 |
if *format == "json" { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
429 |
fmt.Println(string(rawJSON)) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
430 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
431 |
if *outFile != "" { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
432 |
err = ioutil.WriteFile(*outFile, rawJSON, 0600) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
433 |
if err != nil { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
434 |
log.Println("Could not write output file:", err) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
435 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
436 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
437 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
438 |
} |