author | Mikael Berthe <mikael@lilotux.net> |
Sun, 19 Feb 2017 17:06:48 +0100 | |
changeset 6 | 9b16361e7950 |
parent 5 | 9243a5739983 |
child 7 | 17a1a3f4fb86 |
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" |
3 | 48 |
"time" |
0 | 49 |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
50 |
flag "github.com/docker/docker/pkg/mflag" |
1
05b31eb60d27
Switch to github.com/tarm/serial
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
51 |
"github.com/tarm/serial" |
0 | 52 |
) |
53 |
||
54 |
type measurement struct { |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
55 |
Header int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
56 |
Systolic int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
57 |
Diastolic int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
58 |
Pulse int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
59 |
Month int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
60 |
Day int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
61 |
Hour int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
62 |
Minute int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
63 |
Year int |
0 | 64 |
} |
65 |
||
66 |
func getData(s io.ReadWriteCloser, buf []byte, size int) (int, error) { |
|
67 |
t := 0 |
|
68 |
b := buf |
|
69 |
for t < size { |
|
70 |
n, err := s.Read(b[t:]) |
|
71 |
if err != nil { |
|
72 |
log.Fatal(err) // XXX |
|
73 |
return t, err |
|
74 |
} |
|
75 |
//log.Printf("(%d bytes) %q\n", n, b[t:t+1]) |
|
76 |
t = t + n |
|
77 |
} |
|
78 |
return t, nil |
|
79 |
} |
|
80 |
||
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
81 |
func fetchData(dev string) (items []measurement, err error) { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
82 |
c := &serial.Config{Name: dev, Baud: 4800} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
83 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
84 |
var s *serial.Port |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
85 |
s, err = serial.OpenPort(c) |
0 | 86 |
if err != nil { |
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
87 |
return items, err |
0 | 88 |
} |
89 |
||
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
90 |
// =================== Handshake ===================== |
0 | 91 |
q := []byte("\xaa") |
92 |
//log.Printf("Query: %q\n", q) |
|
93 |
log.Println("Starting handshake...") |
|
94 |
n, err := s.Write(q) |
|
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 |
||
99 |
buf := make([]byte, 128) |
|
100 |
n, err = getData(s, buf, 1) |
|
101 |
if err != nil { |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
102 |
return items, err |
0 | 103 |
} |
104 |
if n == 1 && buf[0] == '\x55' { |
|
105 |
log.Println("Handshake successful.") |
|
106 |
} else { |
|
107 |
log.Printf("(%d bytes) %q\n", n, buf[:n]) |
|
108 |
s.Close() |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
109 |
return items, fmt.Errorf("handshake failed") |
0 | 110 |
} |
111 |
||
112 |
// =================== Desc ===================== |
|
113 |
q = []byte("\xa4") |
|
114 |
//log.Printf("Query: %q\n", q) |
|
115 |
log.Println("Requesting device description...") |
|
116 |
n, err = s.Write(q) |
|
117 |
if err != nil { |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
118 |
return items, err |
0 | 119 |
} |
120 |
||
121 |
n, err = getData(s, buf, 32) |
|
122 |
log.Printf("DESC> %q\n", buf[:n]) |
|
123 |
||
124 |
// =================== Count ===================== |
|
125 |
q = []byte("\xa2") |
|
126 |
//log.Printf("Query: %q\n", q) |
|
127 |
log.Println("Requesting data counter...") |
|
128 |
n, err = s.Write(q) |
|
129 |
if err != nil { |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
130 |
return items, err |
0 | 131 |
} |
132 |
||
133 |
n, err = getData(s, buf, 1) |
|
134 |
if err != nil { |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
135 |
return items, err |
0 | 136 |
} |
137 |
var nRecords int |
|
138 |
if n == 1 { |
|
139 |
log.Printf("%d item(s) available.", buf[0]) |
|
140 |
nRecords = int(buf[0]) |
|
141 |
} else { |
|
142 |
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
|
143 |
return items, fmt.Errorf("no measurement found") |
0 | 144 |
} |
145 |
||
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
146 |
// =================== Records ===================== |
0 | 147 |
for i := 0; i < nRecords; i++ { |
148 |
q = []byte{'\xa3', uint8(i + 1)} |
|
149 |
//log.Printf("Query: %q\n", q) |
|
150 |
//log.Printf("Requesting measurement %d...", i+1) |
|
151 |
n, err = s.Write(q) |
|
152 |
if err != nil { |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
153 |
return items, err |
0 | 154 |
} |
155 |
||
156 |
n, err = getData(s, buf, 9) |
|
157 |
//log.Printf("DESC> %q\n", buf[:n]) |
|
158 |
||
159 |
var data measurement |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
160 |
data.Header = int(buf[0]) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
161 |
data.Systolic = int(buf[1]) + 25 |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
162 |
data.Diastolic = int(buf[2]) + 25 |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
163 |
data.Pulse = int(buf[3]) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
164 |
data.Month = int(buf[4]) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
165 |
data.Day = int(buf[5]) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
166 |
data.Hour = int(buf[6]) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
167 |
data.Minute = int(buf[7]) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
168 |
data.Year = int(buf[8]) + 2000 |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
169 |
items = append(items, data) |
0 | 170 |
} |
171 |
||
172 |
s.Close() |
|
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
173 |
return items, nil |
0 | 174 |
} |
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
175 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
176 |
func loadFromJSONFile(filename string) (items []measurement, err error) { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
177 |
data, err := ioutil.ReadFile(filename) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
178 |
if err != nil { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
179 |
return items, err |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
180 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
181 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
182 |
err = json.Unmarshal(data, &items) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
183 |
return items, err |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
184 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
185 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
186 |
func mergeItems(newItems, oldItems []measurement) []measurement { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
187 |
var result []measurement |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
188 |
var j int |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
189 |
// 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
|
190 |
for _, nItem := range newItems { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
191 |
result = append(result, nItem) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
192 |
if j+1 <= len(oldItems) && nItem == oldItems[j] { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
193 |
j++ |
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 |
if j+1 <= len(oldItems) { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
197 |
result = append(result, oldItems[j:]...) |
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 |
return result |
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 |
|
3 | 202 |
func parseDate(dateStr string) (date time.Time, err error) { |
203 |
if dateStr == "" { |
|
204 |
return |
|
205 |
} |
|
206 |
||
207 |
var yy, mm, dd, h, m, s int |
|
208 |
n, e := fmt.Sscanf(dateStr, "%d-%d-%d %d:%d:%d", &yy, &mm, &dd, &h, &m, &s) |
|
209 |
if e != nil && n < 3 { |
|
210 |
err = e |
|
211 |
return |
|
212 |
} |
|
213 |
if n < 6 { |
|
214 |
log.Printf("Date parsed with only %d fields\n", n) |
|
215 |
} |
|
216 |
date = time.Date(yy, time.Month(mm), dd, h, m, s, 0, time.Local) |
|
217 |
return |
|
218 |
} |
|
219 |
||
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
220 |
func main() { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
221 |
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
|
222 |
outFile := flag.String([]string{"-output-file", "o"}, "", "Output JSON file") |
3 | 223 |
limit := flag.Uint([]string{"-limit", "l"}, 0, "Limit number of items to N first") |
224 |
since := flag.String([]string{"-since"}, "", |
|
225 |
"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
|
226 |
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
|
227 |
avg := flag.Bool([]string{"-average", "a"}, false, "Compute average") |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
228 |
merge := flag.Bool([]string{"-merge", "m"}, false, |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
229 |
"Try to merge input JSON file with fetched data") |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
230 |
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
|
231 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
232 |
flag.Parse() |
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 |
switch *format { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
235 |
case "": |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
236 |
if *outFile == "" { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
237 |
*format = "csv" |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
238 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
239 |
break |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
240 |
case "json", "csv": |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
241 |
break |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
242 |
default: |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
243 |
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
|
244 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
245 |
|
3 | 246 |
startDate, err := parseDate(*since) |
247 |
if err != nil { |
|
248 |
log.Fatal("Could not parse date: ", err) |
|
249 |
} |
|
250 |
||
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
251 |
var items []measurement |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
252 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
253 |
if *inFile == "" { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
254 |
// Read from device |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
255 |
if items, err = fetchData(*device); err != nil { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
256 |
log.Fatal(err) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
257 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
258 |
} else { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
259 |
// Read from file |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
260 |
var fileItems []measurement |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
261 |
if fileItems, err = loadFromJSONFile(*inFile); err != nil { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
262 |
log.Fatal(err) |
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 |
if *merge { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
265 |
if items, err = fetchData(*device); err != nil { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
266 |
log.Fatal(err) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
267 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
268 |
items = mergeItems(items, fileItems) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
269 |
} else { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
270 |
items = fileItems |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
271 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
272 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
273 |
|
3 | 274 |
if !startDate.IsZero() { |
275 |
log.Printf("Filtering out records before %v...\n", startDate) |
|
276 |
for i := range items { |
|
277 |
iDate := time.Date(items[i].Year, time.Month(items[i].Month), |
|
278 |
items[i].Day, items[i].Hour, items[i].Minute, 0, 0, |
|
279 |
time.Local) |
|
280 |
if iDate.Sub(startDate) < 0 { |
|
281 |
items = items[0:i] |
|
282 |
break |
|
283 |
} |
|
284 |
} |
|
285 |
} |
|
286 |
||
2
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
287 |
if *limit > 0 && len(items) > int(*limit) { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
288 |
items = items[0:*limit] |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
289 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
290 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
291 |
var avgMeasure measurement |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
292 |
var avgCount int |
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 |
for i, data := range items { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
295 |
if *format == "csv" { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
296 |
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
|
297 |
i+1, data.Header, |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
298 |
data.Year, data.Month, data.Day, |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
299 |
data.Hour, data.Minute, |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
300 |
data.Systolic, data.Diastolic, data.Pulse) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
301 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
302 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
303 |
avgMeasure.Systolic += data.Systolic |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
304 |
avgMeasure.Diastolic += data.Diastolic |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
305 |
avgMeasure.Pulse += data.Pulse |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
306 |
avgCount++ |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
307 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
308 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
309 |
if *avg && avgCount > 0 { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
310 |
avgMeasure.Systolic /= avgCount |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
311 |
avgMeasure.Diastolic /= avgCount |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
312 |
avgMeasure.Pulse /= avgCount |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
313 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
314 |
fmt.Printf("Average: %d;%d;%d\n", avgMeasure.Systolic, |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
315 |
avgMeasure.Diastolic, avgMeasure.Pulse) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
316 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
317 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
318 |
if *format == "json" || *outFile != "" { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
319 |
rawJSON, err := json.MarshalIndent(items, "", " ") |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
320 |
if err != nil { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
321 |
log.Fatal("Error:", err) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
322 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
323 |
|
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
324 |
if *format == "json" { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
325 |
fmt.Println(string(rawJSON)) |
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 |
if *outFile != "" { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
328 |
err = ioutil.WriteFile(*outFile, rawJSON, 0600) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
329 |
if err != nil { |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
330 |
log.Println("Could not write output file:", err) |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
331 |
} |
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 |
} |
2452d9b23ec1
Add options and JSON support
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
334 |
} |