wifictld-analyzer/database/main.go

49 lines
798 B
Go
Raw Normal View History

2018-06-03 20:37:52 +02:00
package database
2018-07-10 21:40:38 +02:00
import (
2018-07-16 13:00:37 +02:00
"net"
2018-07-10 21:40:38 +02:00
"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)
2018-07-16 13:00:37 +02:00
for addr, client := range db.Clients {
client.Addr, _ = net.ParseMAC(addr)
if ap, ok := db.APs[client.APAddr]; ok {
client.AP = ap
}
}
2018-07-10 21:40:38 +02:00
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
}