github.com/stampzilla/stampzilla-go@v2.0.0-rc9+incompatible/nodes/stampzilla-server/store/store.go (about) 1 package store 2 3 import ( 4 "sync" 5 6 "github.com/sirupsen/logrus" 7 "github.com/stampzilla/stampzilla-go/nodes/stampzilla-server/logic" 8 "github.com/stampzilla/stampzilla-go/nodes/stampzilla-server/models" 9 "github.com/stampzilla/stampzilla-go/nodes/stampzilla-server/models/devices" 10 ) 11 12 type Nodes map[string]*models.Node 13 type Connections map[string]*models.Connection 14 type UpdateCallback func(string, *Store) error 15 16 type Store struct { 17 Nodes Nodes 18 SavedState *logic.SavedStateStore 19 Logic *logic.Logic 20 Scheduler *logic.Scheduler 21 Devices *devices.List 22 Connections Connections 23 Certificates []Certificate 24 Requests []Request 25 Server map[string]map[string]map[string]interface{} 26 27 onUpdate []UpdateCallback 28 sync.RWMutex 29 } 30 31 func New(l *logic.Logic, s *logic.Scheduler, sss *logic.SavedStateStore) *Store { 32 store := &Store{ 33 Nodes: make(Nodes), 34 SavedState: sss, 35 Devices: devices.NewList(), 36 Connections: make(Connections), 37 Logic: l, 38 Scheduler: s, 39 Certificates: make([]Certificate, 0), 40 Requests: make([]Request, 0), 41 Server: make(map[string]map[string]map[string]interface{}), 42 } 43 44 l.OnReportState(func(uuid string, state map[string]interface{}) { 45 store.AddOrUpdateServer("rules", uuid, state) 46 }) 47 48 return store 49 } 50 51 func (store *Store) runCallbacks(area string) { 52 for _, callback := range store.onUpdate { 53 if err := callback(area, store); err != nil { 54 logrus.Error("store: ", err) 55 } 56 } 57 } 58 59 func (store *Store) OnUpdate(callback UpdateCallback) { 60 store.Lock() 61 store.onUpdate = append(store.onUpdate, callback) 62 store.Unlock() 63 } 64 65 // Load loads all stuff from disk. 66 func (store *Store) Load() error { 67 // Load logic stuff 68 if err := store.SavedState.Load(); err != nil { 69 return err 70 } 71 if err := store.Logic.Load(); err != nil { 72 return err 73 } 74 75 if err := store.Scheduler.Load(); err != nil { 76 return err 77 } 78 79 // load all the nodes 80 return store.LoadNodes() 81 }