author | Mikael Berthe <mikael@lilotux.net> |
Sun, 26 Feb 2017 14:46:10 +0100 | |
changeset 12 | fc4f5c69286b |
parent 11 | 01e6addfa1ee |
child 13 | 72f6336c1167 |
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 |
|
10 | 23 |
// ... display more statistics: |
24 |
// % gobm65 --stats |
|
4 | 25 |
// |
26 |
// Display the latest 3 records with the average: |
|
27 |
// % gobm65 -l 3 --average |
|
28 |
// Display all records since a specific date: |
|
29 |
// % gobm65 --since "2016-06-01" |
|
30 |
// Display all records of the last 7 days: |
|
31 |
// % gobm65 --since "$(date "+%F" -d "7 days ago")" |
|
32 |
// |
|
10 | 33 |
// Display statistics for morning records: |
34 |
// % gobm65 --from-time 06:00 --to-time 12:00 --stats |
|
35 |
// One can invert times to get night data: |
|
36 |
// % gobm65 --from-time 21:00 --to-time 09:00 |
|
37 |
// |
|
4 | 38 |
// Display the last/first 10 records in JSON: |
39 |
// % gobm65 -l 10 --format json |
|
40 |
// |
|
41 |
// Save the records to a JSON file: |
|
42 |
// % gobm65 -o data_u2.json |
|
43 |
// |
|
44 |
// Read a JSON file and display average of the last 3 records: |
|
45 |
// % gobm65 -i data_u2.json -l 3 --average |
|
10 | 46 |
// % gobm65 -i data_u2.json -l 3 --stats |
4 | 47 |
// Read a JSON file, merge with device records, and save to another file: |
48 |
// % gobm65 -i data_u2.json --merge -o data_u2-new.json |
|
49 |
||
0 | 50 |
import ( |
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
51 |
"encoding/json" |
1
05b31eb60d27
Switch to github.com/tarm/serial
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
52 |
"fmt" |
0 | 53 |
"io" |
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
54 |
"io/ioutil" |
0 | 55 |
"log" |
8 | 56 |
"math" |
57 |
"sort" |
|
3 | 58 |
"time" |
0 | 59 |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
60 |
flag "github.com/docker/docker/pkg/mflag" |
1
05b31eb60d27
Switch to github.com/tarm/serial
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
61 |
"github.com/tarm/serial" |
0 | 62 |
) |
63 |
||
64 |
type measurement struct { |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
65 |
Header int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
66 |
Systolic int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
67 |
Diastolic int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
68 |
Pulse int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
69 |
Month int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
70 |
Day int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
71 |
Hour int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
72 |
Minute int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
73 |
Year int |
0 | 74 |
} |
75 |
||
9
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
76 |
type simpleTime struct { |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
77 |
hour, minute int |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
78 |
} |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
79 |
|
0 | 80 |
func getData(s io.ReadWriteCloser, buf []byte, size int) (int, error) { |
81 |
t := 0 |
|
82 |
b := buf |
|
83 |
for t < size { |
|
84 |
n, err := s.Read(b[t:]) |
|
85 |
if err != nil { |
|
86 |
log.Fatal(err) // XXX |
|
87 |
return t, err |
|
88 |
} |
|
89 |
//log.Printf("(%d bytes) %q\n", n, b[t:t+1]) |
|
90 |
t = t + n |
|
91 |
} |
|
92 |
return t, nil |
|
93 |
} |
|
94 |
||
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
95 |
func fetchData(dev string) (items []measurement, err error) { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
96 |
c := &serial.Config{Name: dev, Baud: 4800} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
97 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
98 |
var s *serial.Port |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
99 |
s, err = serial.OpenPort(c) |
0 | 100 |
if err != nil { |
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
101 |
return items, err |
0 | 102 |
} |
103 |
||
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
104 |
// =================== Handshake ===================== |
0 | 105 |
q := []byte("\xaa") |
106 |
//log.Printf("Query: %q\n", q) |
|
107 |
log.Println("Starting handshake...") |
|
108 |
n, err := s.Write(q) |
|
109 |
if err != nil { |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
110 |
return items, err |
0 | 111 |
} |
112 |
||
113 |
buf := make([]byte, 128) |
|
114 |
n, err = getData(s, buf, 1) |
|
115 |
if err != nil { |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
116 |
return items, err |
0 | 117 |
} |
118 |
if n == 1 && buf[0] == '\x55' { |
|
119 |
log.Println("Handshake successful.") |
|
120 |
} else { |
|
121 |
log.Printf("(%d bytes) %q\n", n, buf[:n]) |
|
122 |
s.Close() |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
123 |
return items, fmt.Errorf("handshake failed") |
0 | 124 |
} |
125 |
||
126 |
// =================== Desc ===================== |
|
127 |
q = []byte("\xa4") |
|
128 |
//log.Printf("Query: %q\n", q) |
|
129 |
log.Println("Requesting device description...") |
|
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, 32) |
|
136 |
log.Printf("DESC> %q\n", buf[:n]) |
|
137 |
||
138 |
// =================== Count ===================== |
|
139 |
q = []byte("\xa2") |
|
140 |
//log.Printf("Query: %q\n", q) |
|
141 |
log.Println("Requesting data counter...") |
|
142 |
n, err = s.Write(q) |
|
143 |
if err != nil { |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
144 |
return items, err |
0 | 145 |
} |
146 |
||
147 |
n, err = getData(s, buf, 1) |
|
148 |
if err != nil { |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
149 |
return items, err |
0 | 150 |
} |
151 |
var nRecords int |
|
152 |
if n == 1 { |
|
153 |
log.Printf("%d item(s) available.", buf[0]) |
|
154 |
nRecords = int(buf[0]) |
|
155 |
} else { |
|
156 |
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
|
157 |
return items, fmt.Errorf("no measurement found") |
0 | 158 |
} |
159 |
||
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
160 |
// =================== Records ===================== |
0 | 161 |
for i := 0; i < nRecords; i++ { |
162 |
q = []byte{'\xa3', uint8(i + 1)} |
|
163 |
//log.Printf("Query: %q\n", q) |
|
164 |
//log.Printf("Requesting measurement %d...", i+1) |
|
165 |
n, err = s.Write(q) |
|
166 |
if err != nil { |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
167 |
return items, err |
0 | 168 |
} |
169 |
||
170 |
n, err = getData(s, buf, 9) |
|
171 |
//log.Printf("DESC> %q\n", buf[:n]) |
|
172 |
||
173 |
var data measurement |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
174 |
data.Header = int(buf[0]) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
175 |
data.Systolic = int(buf[1]) + 25 |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
176 |
data.Diastolic = int(buf[2]) + 25 |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
177 |
data.Pulse = int(buf[3]) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
178 |
data.Month = int(buf[4]) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
179 |
data.Day = int(buf[5]) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
180 |
data.Hour = int(buf[6]) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
181 |
data.Minute = int(buf[7]) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
182 |
data.Year = int(buf[8]) + 2000 |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
183 |
items = append(items, data) |
0 | 184 |
} |
185 |
||
186 |
s.Close() |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
187 |
return items, nil |
0 | 188 |
} |
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
189 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
190 |
func loadFromJSONFile(filename string) (items []measurement, err error) { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
191 |
data, err := ioutil.ReadFile(filename) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
192 |
if err != nil { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
193 |
return items, err |
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 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
196 |
err = json.Unmarshal(data, &items) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
197 |
return items, err |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
198 |
} |
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 |
func mergeItems(newItems, oldItems []measurement) []measurement { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
201 |
var result []measurement |
11 | 202 |
|
203 |
// Sort method: isLater returns true if mi's date is later or |
|
204 |
// equal to mj's date. |
|
205 |
isLater := func(mi, mj measurement) bool { |
|
206 |
switch { |
|
207 |
case mi.Year < mj.Year: |
|
208 |
return false |
|
209 |
case mi.Year > mj.Year: |
|
210 |
return true |
|
211 |
case mi.Month < mj.Month: |
|
212 |
return false |
|
213 |
case mi.Month > mj.Month: |
|
214 |
return true |
|
215 |
case mi.Day < mj.Day: |
|
216 |
return false |
|
217 |
case mi.Day > mj.Day: |
|
218 |
return true |
|
219 |
case mi.Hour < mj.Hour: |
|
220 |
return false |
|
221 |
case mi.Hour > mj.Hour: |
|
222 |
return true |
|
223 |
case mi.Minute < mj.Minute: |
|
224 |
return false |
|
225 |
default: |
|
226 |
return true |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
227 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
228 |
} |
11 | 229 |
|
12
fc4f5c69286b
Add note about sort.Slice()
Mikael Berthe <mikael@lilotux.net>
parents:
11
diff
changeset
|
230 |
// Note that sort.Slice was introduced in go 1.8 |
11 | 231 |
sort.Slice(oldItems, func(i, j int) bool { |
232 |
return isLater(oldItems[i], oldItems[j]) |
|
233 |
}) |
|
234 |
sort.Slice(newItems, func(i, j int) bool { |
|
235 |
return isLater(newItems[i], newItems[j]) |
|
236 |
}) |
|
237 |
||
238 |
appendIfMissing := func(l []measurement, m measurement) []measurement { |
|
239 |
var i int |
|
240 |
for i = range l { |
|
241 |
if !isLater(l[i], m) { |
|
242 |
break |
|
243 |
} |
|
244 |
if l[i] == m { // Duplicate |
|
245 |
return l |
|
246 |
} |
|
247 |
} |
|
248 |
if i == len(l) { |
|
249 |
return append(l[:i], m) |
|
250 |
} |
|
251 |
r := append(l[:i], append(l[i:], m)...) |
|
252 |
return r |
|
253 |
} |
|
254 |
||
255 |
for _, item := range newItems { |
|
256 |
result = appendIfMissing(result, item) |
|
257 |
} |
|
258 |
for _, item := range oldItems { |
|
259 |
result = appendIfMissing(result, item) |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
260 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
261 |
return result |
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 |
|
3 | 264 |
func parseDate(dateStr string) (date time.Time, err error) { |
265 |
if dateStr == "" { |
|
266 |
return |
|
267 |
} |
|
268 |
||
269 |
var yy, mm, dd, h, m, s int |
|
270 |
n, e := fmt.Sscanf(dateStr, "%d-%d-%d %d:%d:%d", &yy, &mm, &dd, &h, &m, &s) |
|
271 |
if e != nil && n < 3 { |
|
272 |
err = e |
|
273 |
return |
|
274 |
} |
|
275 |
if n < 6 { |
|
276 |
log.Printf("Date parsed with only %d fields\n", n) |
|
277 |
} |
|
278 |
date = time.Date(yy, time.Month(mm), dd, h, m, s, 0, time.Local) |
|
279 |
return |
|
280 |
} |
|
281 |
||
9
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
282 |
func parseTime(timeStr string) (t simpleTime, err error) { |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
283 |
_, err = fmt.Sscanf(timeStr, "%d:%d", &t.hour, &t.minute) |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
284 |
return |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
285 |
} |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
286 |
|
8 | 287 |
func average(items []measurement) (measurement, error) { |
288 |
var avgMeasure measurement |
|
289 |
var avgCount int |
|
290 |
||
291 |
for _, data := range items { |
|
292 |
avgMeasure.Systolic += data.Systolic |
|
293 |
avgMeasure.Diastolic += data.Diastolic |
|
294 |
avgMeasure.Pulse += data.Pulse |
|
295 |
avgCount++ |
|
296 |
} |
|
297 |
||
298 |
roundDivision := func(a, b int) int { |
|
299 |
return int(0.5 + float64(a)/float64(b)) |
|
300 |
} |
|
301 |
||
302 |
if avgCount == 0 { |
|
303 |
return avgMeasure, fmt.Errorf("cannot compute average: empty set") |
|
304 |
} |
|
305 |
||
306 |
avgMeasure.Systolic = roundDivision(avgMeasure.Systolic, avgCount) |
|
307 |
avgMeasure.Diastolic = roundDivision(avgMeasure.Diastolic, avgCount) |
|
308 |
avgMeasure.Pulse = roundDivision(avgMeasure.Pulse, avgCount) |
|
309 |
||
310 |
return avgMeasure, nil |
|
311 |
} |
|
312 |
||
313 |
func intMedian(numbers []int) int { |
|
314 |
middle := len(numbers) / 2 |
|
315 |
med := numbers[middle] |
|
316 |
if len(numbers)%2 == 0 { |
|
317 |
med = (med + numbers[middle-1]) / 2 |
|
318 |
} |
|
319 |
return med |
|
320 |
} |
|
321 |
||
322 |
func median(items []measurement) (measurement, error) { |
|
323 |
var med measurement |
|
324 |
if len(items) == 0 { |
|
325 |
return med, fmt.Errorf("cannot compute average: empty set") |
|
326 |
} |
|
327 |
||
328 |
var sys, dia, pul []int |
|
329 |
for _, data := range items { |
|
330 |
sys = append(sys, data.Systolic) |
|
331 |
dia = append(dia, data.Diastolic) |
|
332 |
pul = append(pul, data.Pulse) |
|
333 |
} |
|
334 |
||
335 |
sort.Ints(sys) |
|
336 |
sort.Ints(dia) |
|
337 |
sort.Ints(pul) |
|
338 |
||
339 |
med.Systolic = intMedian(sys) |
|
340 |
med.Diastolic = intMedian(dia) |
|
341 |
med.Pulse = intMedian(pul) |
|
342 |
||
343 |
return med, nil |
|
344 |
} |
|
345 |
||
346 |
func stdDeviation(items []measurement) (measurement, error) { |
|
347 |
var sDev measurement |
|
348 |
||
349 |
if len(items) <= 1 { |
|
350 |
return sDev, fmt.Errorf("cannot compute deviation: set too small") |
|
351 |
} |
|
352 |
||
353 |
var sumSys, sumDia, sumPul float64 |
|
354 |
avg, err := average(items) |
|
355 |
if err != nil { |
|
356 |
return sDev, err |
|
357 |
} |
|
358 |
||
359 |
for _, data := range items { |
|
360 |
sumSys += math.Pow(float64(data.Systolic-avg.Systolic), 2) |
|
361 |
sumDia += math.Pow(float64(data.Diastolic-avg.Diastolic), 2) |
|
362 |
sumPul += math.Pow(float64(data.Pulse-avg.Pulse), 2) |
|
363 |
} |
|
364 |
||
365 |
sDev.Systolic = int(math.Sqrt(sumSys / float64(len(items)-1))) |
|
366 |
sDev.Diastolic = int(math.Sqrt(sumDia / float64(len(items)-1))) |
|
367 |
sDev.Pulse = int(math.Sqrt(sumPul / float64(len(items)-1))) |
|
368 |
||
369 |
return sDev, nil |
|
370 |
} |
|
371 |
||
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
372 |
func main() { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
373 |
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
|
374 |
outFile := flag.String([]string{"-output-file", "o"}, "", "Output JSON file") |
3 | 375 |
limit := flag.Uint([]string{"-limit", "l"}, 0, "Limit number of items to N first") |
376 |
since := flag.String([]string{"-since"}, "", |
|
377 |
"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
|
378 |
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
|
379 |
avg := flag.Bool([]string{"-average", "a"}, false, "Compute average") |
8 | 380 |
stats := flag.Bool([]string{"-stats"}, false, "Compute statistics") |
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
381 |
merge := flag.Bool([]string{"-merge", "m"}, false, |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
382 |
"Try to merge input JSON file with fetched data") |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
383 |
device := flag.String([]string{"-device", "d"}, "/dev/ttyUSB0", "Serial device") |
9
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
384 |
fromTime := flag.String([]string{"-from-time"}, "", "Select records after time (HH:MM)") |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
385 |
toTime := flag.String([]string{"-to-time"}, "", "Select records bofore time (HH:MM)") |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
386 |
|
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
387 |
var startTime, endTime simpleTime |
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
388 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
389 |
flag.Parse() |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
390 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
391 |
switch *format { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
392 |
case "": |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
393 |
if *outFile == "" { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
394 |
*format = "csv" |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
395 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
396 |
break |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
397 |
case "json", "csv": |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
398 |
break |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
399 |
default: |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
400 |
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
|
401 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
402 |
|
9
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
403 |
if *fromTime != "" { |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
404 |
if t, err := parseTime(*fromTime); err != nil { |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
405 |
log.Fatal("Cannot parse 'from' time: ", err) |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
406 |
} else { |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
407 |
startTime = t |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
408 |
} |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
409 |
} |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
410 |
if *toTime != "" { |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
411 |
if t, err := parseTime(*toTime); err != nil { |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
412 |
log.Fatal("Cannot parse 'to' time: ", err) |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
413 |
} else { |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
414 |
endTime = t |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
415 |
} |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
416 |
} |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
417 |
|
3 | 418 |
startDate, err := parseDate(*since) |
419 |
if err != nil { |
|
420 |
log.Fatal("Could not parse date: ", err) |
|
421 |
} |
|
422 |
||
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
423 |
var items []measurement |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
424 |
|
9
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
425 |
// Read data |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
426 |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
427 |
if *inFile == "" { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
428 |
// Read from device |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
429 |
if items, err = fetchData(*device); err != nil { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
430 |
log.Fatal(err) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
431 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
432 |
} else { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
433 |
// Read from file |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
434 |
var fileItems []measurement |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
435 |
if fileItems, err = loadFromJSONFile(*inFile); err != nil { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
436 |
log.Fatal(err) |
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 |
if *merge { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
439 |
if items, err = fetchData(*device); err != nil { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
440 |
log.Fatal(err) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
441 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
442 |
items = mergeItems(items, fileItems) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
443 |
} else { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
444 |
items = fileItems |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
445 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
446 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
447 |
|
9
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
448 |
// Apply filters |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
449 |
|
3 | 450 |
if !startDate.IsZero() { |
451 |
log.Printf("Filtering out records before %v...\n", startDate) |
|
452 |
for i := range items { |
|
453 |
iDate := time.Date(items[i].Year, time.Month(items[i].Month), |
|
454 |
items[i].Day, items[i].Hour, items[i].Minute, 0, 0, |
|
455 |
time.Local) |
|
456 |
if iDate.Sub(startDate) < 0 { |
|
457 |
items = items[0:i] |
|
458 |
break |
|
459 |
} |
|
460 |
} |
|
461 |
} |
|
462 |
||
9
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
463 |
if *fromTime != "" || *toTime != "" { |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
464 |
log.Println("Filtering hours...") |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
465 |
|
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
466 |
compare := func(m measurement, t simpleTime) int { |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
467 |
if m.Hour*60+m.Minute < t.hour*60+t.minute { |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
468 |
return -1 |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
469 |
} |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
470 |
if m.Hour*60+m.Minute > t.hour*60+t.minute { |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
471 |
return 1 |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
472 |
} |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
473 |
return 0 |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
474 |
} |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
475 |
|
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
476 |
inv := false |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
477 |
if *fromTime != "" && *toTime != "" && |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
478 |
startTime.hour*60+startTime.minute > endTime.hour*60+endTime.minute { |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
479 |
inv = true |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
480 |
} |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
481 |
|
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
482 |
var newItems []measurement |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
483 |
for _, data := range items { |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
484 |
if inv { |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
485 |
if compare(data, startTime) == -1 && compare(data, endTime) == 1 { |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
486 |
continue |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
487 |
} |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
488 |
newItems = append(newItems, data) |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
489 |
continue |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
490 |
} |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
491 |
if *fromTime != "" && compare(data, startTime) == -1 { |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
492 |
continue |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
493 |
} |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
494 |
if *toTime != "" && compare(data, endTime) == 1 { |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
495 |
continue |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
496 |
} |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
497 |
newItems = append(newItems, data) |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
498 |
} |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
499 |
items = newItems |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
500 |
} |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
501 |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
502 |
if *limit > 0 && len(items) > int(*limit) { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
503 |
items = items[0:*limit] |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
504 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
505 |
|
9
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
506 |
// Done with filtering |
588f7779b0b5
Add time filtering (--to-time, --from-time)
Mikael Berthe <mikael@lilotux.net>
parents:
8
diff
changeset
|
507 |
|
8 | 508 |
if *format == "csv" { |
509 |
for i, data := range items { |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
510 |
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
|
511 |
i+1, data.Header, |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
512 |
data.Year, data.Month, data.Day, |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
513 |
data.Hour, data.Minute, |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
514 |
data.Systolic, data.Diastolic, data.Pulse) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
515 |
} |
8 | 516 |
} |
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
517 |
|
8 | 518 |
if *stats { |
519 |
*avg = true |
|
520 |
} |
|
521 |
||
522 |
if *avg && len(items) > 0 { |
|
523 |
avgMeasure, err := average(items) |
|
524 |
if err != nil { |
|
525 |
log.Println("Error:", err) |
|
526 |
} else { |
|
527 |
fmt.Printf("Average: %d;%d;%d\n", avgMeasure.Systolic, |
|
528 |
avgMeasure.Diastolic, avgMeasure.Pulse) |
|
529 |
} |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
530 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
531 |
|
8 | 532 |
if *stats && len(items) > 1 { |
533 |
d, err := stdDeviation(items) |
|
534 |
if err != nil { |
|
535 |
log.Println("Error:", err) |
|
536 |
} else { |
|
537 |
fmt.Printf("Standard deviation: %d;%d;%d\n", |
|
538 |
d.Systolic, d.Diastolic, d.Pulse) |
|
7
17a1a3f4fb86
Round results for average calculation
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
539 |
} |
8 | 540 |
} |
541 |
if *stats && len(items) > 0 { |
|
542 |
m, err := median(items) |
|
543 |
if err != nil { |
|
544 |
log.Println("Error:", err) |
|
545 |
} else { |
|
546 |
fmt.Printf("Median values: %d;%d;%d\n", |
|
547 |
m.Systolic, m.Diastolic, m.Pulse) |
|
548 |
} |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
549 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
550 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
551 |
if *format == "json" || *outFile != "" { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
552 |
rawJSON, err := json.MarshalIndent(items, "", " ") |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
553 |
if err != nil { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
554 |
log.Fatal("Error:", err) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
555 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
556 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
557 |
if *format == "json" { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
558 |
fmt.Println(string(rawJSON)) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
559 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
560 |
if *outFile != "" { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
561 |
err = ioutil.WriteFile(*outFile, rawJSON, 0600) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
562 |
if err != nil { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
563 |
log.Println("Could not write output file:", err) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
564 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
565 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
566 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
567 |
} |