-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
db.go
323 lines (280 loc) · 5.81 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
319
320
321
322
323
package main
import (
"fmt"
_ "github.com/mattn/go-sqlite3"
uj "github.com/nanoscopic/ujsonin/v2/mod"
log "github.com/sirupsen/logrus"
"os"
"time"
"xorm.io/xorm"
)
var gDb *xorm.Engine
func openDbConnection() {
gDb = openDb()
}
type DbDevice struct {
Udid string `xorm:"pk"`
Name string
CustomName string
ProviderId int64
JsonInfo string
Width int
Height int
ClickWidth int
ClickHeight int
Ready string `xorm:"-"`
WdaPort int
}
func (DbDevice) TableName() string {
return "device"
}
type DbReservation struct {
Udid string `xorm:"pk"`
User string
Rid string
Start time.Time
}
func (DbReservation) TableName() string {
return "reservation"
}
type DbProvider struct {
Id int64
Username string
Password string
}
func (DbProvider) TableName() string {
return "provider"
}
type DbConf struct {
Id int64
RegPass string
}
func (DbConf) TableName() string {
return "conf"
}
func openDb() *xorm.Engine {
doesNotExist := false
if _, err := os.Stat("db.sqlite3"); os.IsNotExist(err) {
doesNotExist = true
}
engine, err := xorm.NewEngine("sqlite3", "./db.sqlite3")
if err != nil {
panic(err)
}
if !doesNotExist {
return engine
}
err = engine.Sync2(new(DbDevice), new(DbProvider), new(DbConf), new(DbReservation))
if err != nil {
}
//addDummyDevice( engine, "4f5d", "Test" )
addConf(engine, "doreg")
return engine
}
func getProvider(username string) *DbProvider {
var provider DbProvider
has, err := gDb.Table(&provider).Where("username = ?", username).Get(&provider)
if err != nil {
return nil
}
if !has {
return nil
}
return &provider
}
func getReservation(udid string) *DbReservation {
rv := DbReservation{
Udid: udid,
}
has, err := gDb.Get(&rv)
if err != nil || !has {
return nil
}
return &rv
}
func deleteReservation(udid string) {
log.WithFields(log.Fields{
"type": "reserve_delete",
"udid": censorUuid(udid),
}).Info("Deleting device reservation")
rv := DbReservation{
Udid: udid,
}
_, err := gDb.Delete(&rv)
if err != nil {
fmt.Printf("Error: %s\n", err)
panic("Delete reservation error")
}
}
func deleteReservationWithRid(udid string, rid string) {
log.WithFields(log.Fields{
"type": "reserve_delete",
"udid": censorUuid(udid),
"rid": rid,
}).Info("Deleting device reservation")
rv := DbReservation{
Udid: udid,
}
affected, err := gDb.Where("Udid=? and Rid=?", udid, rid).Delete(&rv)
if err != nil {
fmt.Printf("Error: %s\n", err)
panic("Delete reservation error")
}
if affected == 0 {
fmt.Printf("Delete reservation with rid %s; no rows deleted\n", rid)
}
}
func addReservation(udid string, user string, rid string) bool {
rv := DbReservation{
Udid: udid,
User: user,
Rid: rid,
}
_, err := gDb.Insert(&rv)
if err != nil {
fmt.Printf("Error adding reservation: %s\n", err)
return false
}
return true
}
func getReservations() (map[string]DbReservation, error) {
var rs []DbReservation
err := gDb.Find(&rs)
if err != nil {
return nil, err
}
rmap := make(map[string]DbReservation)
for _, r := range rs {
rmap[r.Udid] = r
}
return rmap, nil
}
func getDevice(udid string) *DbDevice {
dev := DbDevice{
Udid: udid,
}
has, err := gDb.Get(&dev)
if err != nil {
return nil
}
if !has {
return nil
}
return &dev
}
func getDevices() ([]DbDevice, error) {
var devices []DbDevice
err := gDb.Find(&devices)
if err != nil {
return []DbDevice{}, err
}
return devices, nil
}
func addProvider(username string, password string) bool {
cur := getProvider(username)
if cur != nil {
fmt.Printf("Provider with username %s already existed\n", username)
cur.Password = password
fmt.Printf(" Updating password to %s\n", password)
_, err := gDb.ID(cur.Id).Update(cur)
after := getProvider(username)
fmt.Printf(" After update: %s\n", after.Password)
if err != nil {
panic(err)
}
return true
}
provider := DbProvider{
Username: username,
Password: password,
}
_, err := gDb.Insert(&provider)
if err != nil {
panic(err)
}
return false
}
func updateDeviceInfo(udid string, info string, pId int64) {
root, _ := uj.Parse([]byte(info))
devNameNode := root.Get("DeviceName")
if devNameNode == nil {
}
devName := devNameNode.String()
dev := DbDevice{
Udid: udid,
Name: devName,
JsonInfo: info,
ProviderId: pId,
}
_, err := gDb.ID(udid).Update(&dev) // Cols("JsonInfo", "Name", "ProviderId" )
if err != nil {
panic(err)
}
}
func updateDeviceWdaPort(udid string, port int) {
dev := DbDevice{
Udid: udid,
WdaPort: port,
}
_, err := gDb.ID(udid).Update(&dev) // Cols("JsonInfo", "Name", "ProviderId" )
if err != nil {
panic(err)
}
}
func addConf(db *xorm.Engine, regPass string) {
conf := DbConf{
RegPass: regPass,
}
_, err := db.Insert(&conf)
if err != nil {
panic(err)
}
}
func addDevice(udid string, name string, pId int64, width int, height int, clickWidth int, clickHeight int) {
fmt.Printf("Adding device:\n"+
" udid:%s\n"+
" name:%s\n"+
" clickWidth:%d\n"+
" clickHegiht:%d\n",
udid, name, clickWidth, clickHeight)
dev := DbDevice{
Udid: udid,
Name: name,
ProviderId: pId,
Width: width,
Height: height,
ClickWidth: clickWidth,
ClickHeight: clickHeight,
}
cur := getDevice(udid)
if cur != nil {
fmt.Printf("Device with udid %s already existed\n", dev.Udid)
_, err := gDb.ID(udid).Update(&dev) // Cols("Name","ProviderId","ClickWidth","ClickHeight").
if err != nil {
panic(err)
}
return
}
_, err := gDb.Insert(&dev)
if err != nil {
panic(err)
}
}
func addDummyDevice(db *xorm.Engine, udid string, name string) {
device := DbDevice{
Udid: udid,
Name: name,
}
_, err := db.Insert(&device)
if err != nil {
panic(err)
}
}
func getConf() *DbConf {
var confs []DbConf
err := gDb.Find(&confs)
if err != nil {
panic(err)
}
return &confs[0]
}