-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
372 lines (326 loc) · 10.3 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package main
import (
"bytes"
"context"
"database/sql"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"time"
_ "github.com/mattn/go-sqlite3"
)
var (
dbFlag = flag.String("db", "baby.db", "`filename` of SQLite3 database file")
credsFlag = flag.String("creds", filepath.Join(os.Getenv("HOME"), ".glowbabyrc"), "`filename` containing Glow Baby credentials")
)
const domain = "baby.glowing.com"
const usage = `
usage: glowbaby [options] <command>
Commands:
init initialise the database file (specified by -db)
login log in to Glow Baby (using credentials ~/.glowbabyrc)
sync synchronise all data from remote
plot <type> <dst> plot data to PNG (type is "sleep" or "feed")
Options:
`
func main() {
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "%s", usage)
flag.PrintDefaults()
}
flag.Parse()
db, err := sql.Open("sqlite3", *dbFlag)
if err != nil {
log.Fatalf("Opening DB %s: %v", *dbFlag, err)
}
defer db.Close()
db.SetMaxOpenConns(1)
if flag.NArg() == 0 {
flag.Usage()
os.Exit(1)
}
switch cmd := flag.Arg(0); cmd {
default:
log.Fatalf("Unknown command %q", cmd)
case "init":
// TODO: refuse if the DB file already exists?
_, err := db.Exec(initDB)
if err != nil {
log.Fatalf("Initialising DB: %v", err)
}
log.Printf("DB init OK")
case "login":
if err := login(context.Background(), db); err != nil {
log.Fatalf("Logging in: %v", err)
}
log.Printf("Logged in OK")
case "sync":
start := time.Now()
if err := sync(context.Background(), db); err != nil {
log.Fatalf("Syncing data: %v", err)
}
log.Printf("Synced data OK in %v", time.Since(start).Truncate(100*time.Millisecond))
case "plot":
if flag.NArg() != 3 {
flag.Usage()
os.Exit(1)
}
typ, dst := flag.Arg(1), flag.Arg(2)
var data []byte
switch typ {
default:
flag.Usage()
os.Exit(1)
case "sleep", "feed":
b, err := plot(context.Background(), db, typ)
if err != nil {
log.Fatalf("Plotting data: %v", err)
}
data = b
}
if err := ioutil.WriteFile(dst, data, 0644); err != nil {
log.Fatalf("Writing plot to %s: %v", dst, err)
}
log.Printf("OK; wrote %q plot to %s (%d bytes)", typ, dst, len(data))
}
}
const initDB = `
CREATE TABLE Auth (
Domain TEXT NOT NULL PRIMARY KEY, -- always "baby.glowing.com"
Token TEXT NOT NULL
) STRICT;
CREATE TABLE Babies (
BabyID INTEGER NOT NULL PRIMARY KEY,
FirstName TEXT NOT NULL,
LastName TEXT NOT NULL,
Birthday TEXT NOT NULL, -- YYYY-MM-DD
-- Sync status.
SyncTime INTEGER,
SyncToken TEXT
) STRICT;
CREATE TABLE BabyData (
ID INTEGER NOT NULL PRIMARY KEY,
BabyID INTEGER NOT NULL,
StartTimestamp INTEGER NOT NULL,
EndTimestamp INTEGER,
Key TEXT,
ValInt INTEGER,
ValFloat REAL,
ValStr TEXT
) STRICT;
CREATE TABLE BabyFeedData (
ID INTEGER NOT NULL PRIMARY KEY,
BabyID INTEGER NOT NULL,
StartTimestamp INTEGER NOT NULL,
EndTimestamp INTEGER,
FeedType INTEGER,
BreastUsed TEXT,
BreastLeft INTEGER,
BreastRight INTEGER,
BottleML REAL
) STRICT;
`
func login(ctx context.Context, db *sql.DB) error {
// Load credentials.
var creds struct {
Email string `json:"email"`
Password string `json:"password"`
}
rawCreds, err := ioutil.ReadFile(*credsFlag)
if err != nil {
return fmt.Errorf("loading creds from %s: %w", *credsFlag, err)
}
if err := json.Unmarshal(rawCreds, &creds); err != nil {
return fmt.Errorf("parsing creds from %s: %w", *credsFlag, err)
}
// Re-serialise to tidy up, compact, and remove any extraneous keys.
rawCreds, err = json.Marshal(creds)
if err != nil {
return fmt.Errorf("re-marshaling creds: %w", err)
}
req, err := http.NewRequestWithContext(ctx, "POST", "https://"+domain+"/android/user/sign_in", bytes.NewReader(rawCreds))
if err != nil {
return fmt.Errorf("internal error: constructing HTTP request: %w", err)
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("making HTTP login request: %w", err)
}
if resp.StatusCode != 200 {
return fmt.Errorf("HTTP login request gave non-200 status %q", resp.Status)
}
var loginResp LoginResponse
if err := json.NewDecoder(resp.Body).Decode(&loginResp); err != nil {
return fmt.Errorf("decoding JSON login response: %w", err)
}
// Start transaction.
// Any failures after this point should roll back the transaction.
txCtx, cancel := context.WithCancel(ctx)
defer cancel()
tx, err := db.BeginTx(txCtx, nil)
if err != nil {
return fmt.Errorf("starting DB transaction: %w", err)
}
user := loginResp.Data.User
log.Printf("Logging in as %s %s ...", user.FirstName, user.LastName)
_, err = tx.ExecContext(ctx, `INSERT OR REPLACE INTO Auth(Domain, Token) VALUES (?, ?)`, domain, user.AuthToken)
if err != nil {
return fmt.Errorf("recording auth info in DB: %w", err)
}
for _, babyRec := range loginResp.Data.Babies {
baby := babyRec.Baby
log.Printf("Setting up sync info for baby %s %s (baby ID %d) ...", baby.FirstName, baby.LastName, baby.BabyID)
// Transform birthday format into ISO 8601.
t, err := time.Parse("2006/01/02", baby.Birthday)
if err != nil {
return fmt.Errorf("baby has malformed birthday %q: %w", baby.Birthday, err)
}
tStr := t.Format("2006-01-02")
// TODO: automatic conflict resolution?
_, err = tx.ExecContext(ctx, `INSERT INTO Babies(BabyID, FirstName, LastName, Birthday) VALUES (?, ?, ?, ?)`,
baby.BabyID, baby.FirstName, baby.LastName, tStr)
if err != nil {
return fmt.Errorf("recording baby sync info in DB: %w", err)
}
}
// Finalise transaction.
if err := tx.Commit(); err != nil {
return fmt.Errorf("committing DB transaction: %w", err)
}
return nil
}
func sync(ctx context.Context, db *sql.DB) error {
// Load auth token.
var authToken string
row := db.QueryRowContext(ctx, `SELECT Token FROM Auth WHERE Domain = ?`, domain)
if err := row.Scan(&authToken); err == sql.ErrNoRows {
return fmt.Errorf("no auth token; have you logged in?")
} else if err != nil {
return fmt.Errorf("loading auth token from DB: %w", err)
}
// Find all babies to synchronise.
type babyReq struct {
BabyID int64 `json:"baby_id"`
SyncToken string `json:"sync_token,omitempty"`
first, last string
}
var pullReq struct {
Data struct {
Babies []babyReq `json:"babies"`
User struct {
// TODO: anything needed? seems not.
} `json:"user"`
} `json:"data"`
}
rows, err := db.QueryContext(ctx, `SELECT BabyID, FirstName, LastName, SyncToken FROM Babies`)
if err != nil {
return fmt.Errorf("determining list of babies to sync: %w", err)
}
for rows.Next() {
var br babyReq
var st sql.NullString
if err := rows.Scan(&br.BabyID, &br.first, &br.last, &st); err != nil {
return fmt.Errorf("parsing list of babies to sync: %w", err)
}
if st.Valid {
br.SyncToken = st.String
}
pullReq.Data.Babies = append(pullReq.Data.Babies, br)
log.Printf("Going to sync data for baby %s %s (baby ID %d)", br.first, br.last, br.BabyID)
}
if err := rows.Err(); err != nil {
return fmt.Errorf("querying list of babies to sync: %w", err)
}
rawPullReq, err := json.Marshal(pullReq)
if err != nil {
return fmt.Errorf("internal error: marshaling request: %w", err)
}
req, err := http.NewRequestWithContext(ctx, "POST", "https://"+domain+"/android/user/pull", bytes.NewReader(rawPullReq))
if err != nil {
return fmt.Errorf("internal error: constructing HTTP request: %w", err)
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
req.Header.Set("Authorization", authToken)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("making HTTP pull request: %w", err)
}
if resp.StatusCode != 200 {
return fmt.Errorf("HTTP pull request gave non-200 status %q", resp.Status)
}
var pullResp PullResponse
if err := json.NewDecoder(resp.Body).Decode(&pullResp); err != nil {
return fmt.Errorf("decoding JSON pull response: %w", err)
}
// Start big transaction.
// Any failures after this point should roll back the transaction.
txCtx, cancel := context.WithCancel(ctx)
defer cancel()
tx, err := db.BeginTx(txCtx, nil)
if err != nil {
return fmt.Errorf("starting DB transaction: %w", err)
}
// Update sync token and time.
for _, baby := range pullResp.Data.Babies {
_, err = tx.ExecContext(ctx, `UPDATE Babies SET SyncTime = ?, SyncToken = ? WHERE BabyID = ?`,
baby.SyncTime, baby.SyncToken, baby.BabyID)
if err != nil {
return fmt.Errorf("updating baby sync status in DB: %w", err)
}
for _, bd := range baby.BabyData.Remove {
_, err := tx.ExecContext(ctx, `DELETE FROM BabyData WHERE ID = ?`, bd.ID)
if err != nil {
return fmt.Errorf("deleting baby data from DB: %w", err)
}
}
if n := len(baby.BabyData.Remove); n > 0 {
log.Printf("Removed %d old baby data events", n)
}
for _, bd := range baby.BabyData.Update {
_, err := tx.ExecContext(ctx,
`INSERT OR REPLACE INTO BabyData(ID, BabyID, StartTimestamp, EndTimestamp, Key, ValInt, ValFloat, ValStr)
VALUES(?, ?, ?, ?, ?, ?, ?, ?)`,
bd.ID, bd.BabyID, bd.StartTimestamp, sqlNullInt64(bd.EndTimestamp), bd.Key, bd.ValInt, bd.ValFloat, bd.ValStr)
if err != nil {
return fmt.Errorf("applying baby data update in DB: %w", err)
}
}
log.Printf("Applied %d baby data updates", len(baby.BabyData.Update))
for _, bd := range baby.BabyFeedData.Remove {
_, err := tx.ExecContext(ctx, `DELETE FROM BabyFeedData WHERE ID = ?`, bd.ID)
if err != nil {
return fmt.Errorf("deleting baby data from DB: %w", err)
}
}
if n := len(baby.BabyFeedData.Remove); n > 0 {
log.Printf("Removed %d old baby feed data events", n)
}
for _, bfd := range baby.BabyFeedData.Update {
_, err = tx.ExecContext(ctx,
`INSERT OR REPLACE INTO BabyFeedData(ID, BabyID, StartTimestamp, FeedType, BreastUsed, BreastLeft, BreastRight, BottleML)
VALUES(?, ?, ?, ?, ?, ?, ?, ?)`,
bfd.ID, bfd.BabyID, bfd.StartTimestamp, bfd.FeedType, bfd.BreastUsed, bfd.BreastLeft, bfd.BreastRight, bfd.BottleML)
if err != nil {
return fmt.Errorf("applying baby feed data update in DB: %w", err)
}
}
log.Printf("Applied %d baby feed data updates", len(baby.BabyFeedData.Update))
}
// Finalise transaction.
if err := tx.Commit(); err != nil {
return fmt.Errorf("committing DB transaction: %w", err)
}
return nil
}
func sqlNullInt64(x *int64) (ret sql.NullInt64) {
if x != nil {
ret.Int64, ret.Valid = *x, true
}
return
}