2018-06-03 20:37:52 +02:00
|
|
|
package database
|
|
|
|
|
2018-07-10 21:40:38 +02:00
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"dev.sum7.eu/genofire/golang-lib/file"
|
|
|
|
"dev.sum7.eu/genofire/golang-lib/worker"
|
|
|
|
)
|
|
|
|
|
2018-06-03 20:37:52 +02:00
|
|
|
type DB struct {
|
|
|
|
Clients map[string]*Client `json:"client"`
|
|
|
|
APs map[string]*AP `json:"ap"`
|
2018-07-10 21:40:38 +02:00
|
|
|
worker *worker.Worker
|
2018-06-03 20:37:52 +02:00
|
|
|
}
|
|
|
|
|
2018-07-10 21:40:38 +02:00
|
|
|
func NewDB(path string) *DB {
|
|
|
|
db := &DB{
|
2018-06-03 20:37:52 +02:00
|
|
|
Clients: make(map[string]*Client),
|
|
|
|
APs: make(map[string]*AP),
|
|
|
|
}
|
2018-07-10 21:40:38 +02:00
|
|
|
|
|
|
|
file.ReadJSON(path, db)
|
|
|
|
|
|
|
|
db.worker = worker.NewWorker(time.Minute, func() {
|
|
|
|
file.SaveJSON(path, db)
|
|
|
|
log.Debug("save db state")
|
|
|
|
})
|
|
|
|
|
|
|
|
db.worker.Start()
|
|
|
|
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) Close() {
|
|
|
|
if db.worker != nil {
|
|
|
|
db.worker.Close()
|
|
|
|
}
|
2018-06-03 20:37:52 +02:00
|
|
|
}
|