-
Notifications
You must be signed in to change notification settings - Fork 2
/
db.go
318 lines (296 loc) · 6.13 KB
/
db.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
package db
import (
"database/sql"
"encoding/json"
"fmt"
"sort"
"time"
// load ql drier
"github.com/FarmRadioHangar/fdevices/log"
_ "github.com/cznic/ql/driver"
)
//CtxKey is the key which is used to store the *sql.DB instance inside
//context.Context.
const CtxKey = "_db"
const migrationSQL = `
BEGIN TRANSACTION ;
CREATE TABLE IF NOT EXISTS dongles(
imei string,
imsi string,
path string,
symlink bool,
tty int,
ati string,
properties blob,
created_on time,
updated_on time);
CREATE UNIQUE INDEX UQE_dongels on dongles(path);
COMMIT;
`
//Dongle holds information about device dongles. This relies on combination from
//the information provided by udev and information that is gathered by talking
//to the device serial port directly.
type Dongle struct {
IMEI string `json:"imei"`
IMSI string `json:"imsi"`
Path string `json:"path"`
IsSymlinked bool `json:"symlink"`
TTY int `json:"-"`
ATI string `json:"ati"`
Properties map[string]string `json:"properties"`
CreatedOn time.Time `json:"-"`
UpdatedOn time.Time `json:"-"`
}
type Dongles []*Dongle
func (a Dongles) Len() int { return len(a) }
func (a Dongles) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a Dongles) Less(i, j int) bool { return a[i].TTY < a[j].TTY }
//Migration creates necessary database tables if they aint created yet.
func Migration(db *sql.DB) error {
tx, err := db.Begin()
if err != nil {
return err
}
_, err = tx.Exec(migrationSQL)
if err != nil {
_ = tx.Rollback()
return err
}
return tx.Commit()
}
//DB returns a ql backed database, with migrations already performed.
func DB() (*sql.DB, error) {
return dbWIthName("devices.db")
}
func dbWIthName(name string) (*sql.DB, error) {
db, err := sql.Open("ql-mem", name)
if err != nil {
return nil, err
}
err = Migration(db)
if err != nil {
return nil, err
}
return db, nil
}
func GetAllDongles(db *sql.DB) ([]*Dongle, error) {
query := "select * from dongles"
var rst []*Dongle
rows, err := db.Query(query)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
d := &Dongle{}
var prop []byte
err := rows.Scan(
&d.IMEI,
&d.IMSI,
&d.Path,
&d.IsSymlinked,
&d.TTY,
&d.ATI,
&prop,
&d.CreatedOn,
&d.UpdatedOn,
)
if err != nil {
return nil, err
}
if prop != nil {
err = json.Unmarshal(prop, &d.Properties)
if err != nil {
return nil, err
}
}
rst = append(rst, d)
}
if err = rows.Err(); err != nil {
return nil, err
}
return rst, nil
}
func GetDistinct(db *sql.DB) ([]*Dongle, error) {
s := make(map[string]Dongles)
a, err := GetAllDongles(db)
if err != nil {
return nil, err
}
if len(a) == 0 {
return a, nil
}
for k := range a {
if v, ok := s[a[k].IMEI]; ok {
v = append(v, a[k])
s[a[k].IMEI] = v
}
s[a[k].IMEI] = Dongles{a[k]}
}
for k, v := range s {
sort.Sort(v)
s[k] = v
}
var out []*Dongle
for _, v := range s {
out = append(out, v[0])
}
return out, nil
}
func CreateDongle(db *sql.DB, d *Dongle) error {
query := `
BEGIN TRANSACTION;
INSERT INTO dongles (imei,imsi,path,symlink,tty,ati,properties,created_on,updated_on)
VALUES ($1,$2,$3,$4,$5,$6,$7,now(),now());
COMMIT;
`
var prop []byte
var err error
if d.Properties != nil {
prop, err = json.Marshal(d.Properties)
if err != nil {
return err
}
}
tx, err := db.Begin()
if err != nil {
return err
}
_, err = tx.Exec(query, d.IMEI, d.IMSI,
d.Path, d.IsSymlinked, d.TTY, d.ATI, prop)
if err != nil {
tx.Rollback()
return err
}
return tx.Commit()
}
func UpdateDongle(db *sql.DB, d *Dongle) error {
query := `
BEGIN TRANSACTION;
UPDATE dongles
imei=$1,imsi=$2 ,path=$3,symlink=$4,
tty=$5,properties=$6,
created_on=$7 ,updated_on=now(),
WHERE path=$3&&imei=$1;
COMMIT;
`
var prop []byte
var err error
if d.Properties != nil {
prop, err = json.Marshal(d.Properties)
if err != nil {
return err
}
}
tx, err := db.Begin()
if err != nil {
return err
}
_, err = tx.Exec(query, d.IMEI, d.IMSI, d.Path, d.IsSymlinked, d.TTY, prop, d.CreatedOn)
if err != nil {
tx.Rollback()
return err
}
return tx.Commit()
}
func RemoveDongle(db *sql.DB, d *Dongle) error {
var query = `
BEGIN TRANSACTION;
DELETE FROM dongles
WHERE imei=$1;
COMMIT;
`
tx, err := db.Begin()
if err != nil {
return err
}
_, err = tx.Exec(query, d.IMEI)
if err != nil {
tx.Rollback()
return err
}
return tx.Commit()
return nil
}
func GetDongle(db *sql.DB, path string) (*Dongle, error) {
var query = `
SELECT * from dongles WHERE path=$1 LIMIT 1;
`
d := &Dongle{}
var prop []byte
err := db.QueryRow(query, path).Scan(
&d.IMEI,
&d.IMSI,
&d.Path,
&d.IsSymlinked,
&d.TTY,
&d.ATI,
&prop,
&d.CreatedOn,
&d.UpdatedOn,
)
if err != nil {
return nil, err
}
if prop != nil {
err = json.Unmarshal(prop, &d.Properties)
if err != nil {
return nil, err
}
}
return d, nil
}
func GetDongleByIMEI(db *sql.DB, imei string) (*Dongle, error) {
var query = `
SELECT * from dongles WHERE imei=$1 LIMIT 1;
`
d := &Dongle{}
var prop []byte
err := db.QueryRow(query, imei).Scan(
&d.IMEI,
&d.IMSI,
&d.Path,
&d.IsSymlinked,
&d.TTY,
&d.ATI,
&prop,
&d.CreatedOn,
&d.UpdatedOn,
)
if err != nil {
return nil, err
}
if prop != nil {
err = json.Unmarshal(prop, &d.Properties)
if err != nil {
return nil, err
}
}
return d, nil
}
// GetSymlinkCandidate returns the dongle with the lowest tty number
func GetSymlinkCandidate(db *sql.DB, imei string) (*Dongle, error) {
query := `select min(tty) from dongles where imei=$1 `
var tty int
err := db.QueryRow(query, imei).Scan(&tty)
if err != nil {
return nil, err
}
path := fmt.Sprintf("/dev/ttyUSB%d", tty)
return GetDongle(db, path)
}
// DongleExists return true when the dongle DongleExists
func DongleExists(db *sql.DB, modem *Dongle) bool {
query := `select count(*) from dongles where imei=$1&&imsi=$2&&path=$3 `
var count int
err := db.QueryRow(query,
modem.IMEI,
modem.IMSI,
modem.Path,
).Scan(&count)
if err != nil {
log.Error(err.Error())
return false
}
return count > 0
}