-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
221 lines (189 loc) · 5.53 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
// Copyright 2018 Andrew E. Bruno
//
// This file is part of myoxi.
//
// myoxi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// myoxi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with myoxi. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"os"
"path/filepath"
"runtime"
"time"
"github.com/aebruno/myoxi/device"
"github.com/aebruno/myoxi/model"
"github.com/aebruno/myoxi/tools"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
var (
MyoxiVersion = "dev"
)
func connectDevice(port string) (device.Device, error) {
log.Infof("Using device port: %s", port)
device := &device.CMS50{}
err := device.Connect(port)
if err != nil {
log.WithFields(log.Fields{
"error": err,
"port": port,
}).Error("Failed to open device port")
return nil, err
}
log.Infof("Successfully connected to device at %s", port)
return device, nil
}
func initDB(dbpath string) (model.Datastore, error) {
if len(dbpath) == 0 {
home := os.Getenv("HOME")
if runtime.GOOS == "windows" {
home = os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if home == "" {
home = os.Getenv("USERPROFILE")
}
}
dbpath = filepath.Join(home, ".myoxi.db")
}
log.Infof("Database path: %s", dbpath)
db, err := model.NewDB("sqlite3", dbpath)
if err != nil {
log.WithFields(log.Fields{
"error": err,
"dbpath": dbpath,
}).Error("Failed to open database file")
return nil, err
}
err = db.Initialize()
if err != nil {
log.WithFields(log.Fields{
"error": err,
"dbpath": dbpath,
}).Error("Failed to initialize database")
return nil, err
}
log.Infof("Successfully opened myoxi database")
return db, nil
}
func setup(dbpath, port string) (model.Datastore, device.Device, error) {
db, err := initDB(dbpath)
if err != nil {
return nil, nil, err
}
device, err := connectDevice(port)
if err != nil {
return nil, nil, err
}
return db, device, nil
}
func main() {
app := cli.NewApp()
app.Name = "myoxi"
app.Authors = []cli.Author{cli.Author{Name: "Andrew E. Bruno", Email: "aeb@qnot.org"}}
app.Usage = "myoxi"
app.Version = MyoxiVersion
app.Flags = []cli.Flag{
&cli.BoolFlag{Name: "debug,d", Usage: "Print debug messages"},
&cli.StringFlag{Name: "port, p", Usage: "Path to device port", Value: "/dev/ttyUSB0"},
&cli.StringFlag{Name: "dbpath, x", Usage: "Path to database file"},
}
app.Before = func(c *cli.Context) error {
if c.GlobalBool("debug") {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
return nil
}
app.Commands = []cli.Command{
{
Name: "import",
Usage: "Import data from device",
Flags: []cli.Flag{
&cli.BoolFlag{Name: "noop, n", Usage: "Dump data only. Don't save to database"},
&cli.BoolFlag{Name: "force, f", Usage: "Force overwrite session if exists"},
},
Action: func(c *cli.Context) error {
db, device, err := setup(c.GlobalString("dbpath"), c.GlobalString("port"))
if err != nil {
return cli.NewExitError(err, 1)
}
err = tools.Import(db, device, c.Bool("noop"), c.Bool("force"))
if err != nil {
return cli.NewExitError(err, 1)
}
return nil
},
},
{
Name: "stats",
Usage: "Display database stats",
Flags: []cli.Flag{
&cli.BoolFlag{Name: "all, a", Usage: "Display stats for all data"},
&cli.BoolFlag{Name: "prev, p", Usage: "Display stats for previous session"},
&cli.BoolFlag{Name: "week, w", Usage: "Display stats for last week"},
&cli.BoolFlag{Name: "month, m", Usage: "Display stats for last month"},
&cli.BoolFlag{Name: "quarter, q", Usage: "Display stats for last quarter"},
&cli.BoolFlag{Name: "year, y", Usage: "Display stats for last year"},
},
Action: func(c *cli.Context) error {
db, err := initDB(c.GlobalString("dbpath"))
if err != nil {
return cli.NewExitError(err, 1)
}
now := time.Now()
var records []*model.OxiRecord
if c.Bool("all") {
records, err = db.FetchRecords(time.Time{}, time.Time{})
} else if c.Bool("prev") {
session, err := db.FetchPreviousSession()
if err != nil {
return cli.NewExitError(err, 1)
}
records, err = db.FetchRecordsBySessionID(session.ID)
} else if c.Bool("week") {
records, err = db.FetchRecords(now.Add(-24*7*time.Hour), now)
} else if c.Bool("month") {
records, err = db.FetchRecords(now.Add(-24*30*time.Hour), now)
} else if c.Bool("year") {
records, err = db.FetchRecords(now.Add(-24*365*time.Hour), now)
} else {
session, err := db.FetchLatestSession()
if err != nil {
return cli.NewExitError(err, 1)
}
records, err = db.FetchRecordsBySessionID(session.ID)
}
if err != nil {
return cli.NewExitError(err, 1)
}
tools.ComputeAndPrintStats(records)
return nil
},
},
{
Name: "device",
Usage: "Display information about device",
Action: func(c *cli.Context) error {
device, err := connectDevice(c.GlobalString("port"))
if err != nil {
return cli.NewExitError(err, 1)
}
err = tools.DeviceInfo(device)
if err != nil {
return cli.NewExitError(err, 1)
}
return nil
},
}}
app.RunAndExitOnError()
}