mirror of
https://github.com/RoganDawes/P4wnP1_aloa.git
synced 2025-11-15 08:32:06 +01:00
Init state from DB instead of script
This commit is contained in:
BIN
dist/db/init.db
vendored
Normal file
BIN
dist/db/init.db
vendored
Normal file
Binary file not shown.
@@ -30,6 +30,8 @@ type BtService struct {
|
|||||||
BrName string
|
BrName string
|
||||||
//bridgeIfDeployed bool
|
//bridgeIfDeployed bool
|
||||||
|
|
||||||
|
defaultSettings *pb.BluetoothSettings //This settings are changed if the BluetoothService isn't up, but settings are deployed (Master Template on startup)
|
||||||
|
|
||||||
Agent *bluetooth.DefaultAgent
|
Agent *bluetooth.DefaultAgent
|
||||||
|
|
||||||
serviceAvailableLock *sync.Mutex
|
serviceAvailableLock *sync.Mutex
|
||||||
@@ -70,6 +72,7 @@ func NewBtService(rootService *Service, retryTimeout time.Duration) (res *BtServ
|
|||||||
Agent: bluetooth.NewDefaultAgent("1337"),
|
Agent: bluetooth.NewDefaultAgent("1337"),
|
||||||
BrName: BT_ETHERNET_BRIDGE_NAME,
|
BrName: BT_ETHERNET_BRIDGE_NAME,
|
||||||
serviceAvailableLock: &sync.Mutex{},
|
serviceAvailableLock: &sync.Mutex{},
|
||||||
|
defaultSettings:GetDefaultBluetoothSettings(),
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("Starting Bluetooth sub system...")
|
log.Println("Starting Bluetooth sub system...")
|
||||||
@@ -101,12 +104,12 @@ func NewBtService(rootService *Service, retryTimeout time.Duration) (res *BtServ
|
|||||||
res.Agent.Start(toolz.AGENT_CAP_NO_INPUT_NO_OUTPUT)
|
res.Agent.Start(toolz.AGENT_CAP_NO_INPUT_NO_OUTPUT)
|
||||||
|
|
||||||
// Deploy default settings
|
// Deploy default settings
|
||||||
defaultSettings := GetDefaultBluetoothSettings()
|
|
||||||
_,err := res.DeployBluetoothControllerInformation(defaultSettings.Ci)
|
_,err := res.DeployBluetoothControllerInformation(res.defaultSettings.Ci)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Not able to deploy default bluetooth settings: ", err.Error())
|
log.Println("Not able to deploy default bluetooth settings: ", err.Error())
|
||||||
} else {
|
} else {
|
||||||
_,err = res.DeployBluetoothAgentSettings(defaultSettings.As)
|
_,err = res.DeployBluetoothAgentSettings(res.defaultSettings.As)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Not able to deploy default bluetooth agent settings: ", err.Error())
|
log.Println("Not able to deploy default bluetooth agent settings: ", err.Error())
|
||||||
}
|
}
|
||||||
@@ -119,6 +122,10 @@ func NewBtService(rootService *Service, retryTimeout time.Duration) (res *BtServ
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (bt *BtService) ReplaceDefaultSettings(s *pb.BluetoothSettings) {
|
||||||
|
bt.defaultSettings = s
|
||||||
|
}
|
||||||
|
|
||||||
func (bt *BtService) Stop() {
|
func (bt *BtService) Stop() {
|
||||||
bt.Agent.Stop() // unregister the agent again
|
bt.Agent.Stop() // unregister the agent again
|
||||||
if ci,err := bt.Controller.ReadControllerInformation(); err == nil {
|
if ci,err := bt.Controller.ReadControllerInformation(); err == nil {
|
||||||
|
|||||||
@@ -32,17 +32,39 @@ type Store struct {
|
|||||||
serializer Serializer
|
serializer Serializer
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) Open() (err error) {
|
func exists(path string) (bool, error) {
|
||||||
|
_, err := os.Stat(path)
|
||||||
|
if err == nil {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
return true, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Store) Open(initDbBackupPath string) (err error) {
|
||||||
badgerOpts := badger.DefaultOptions
|
badgerOpts := badger.DefaultOptions
|
||||||
badgerOpts.Dir = s.Path
|
badgerOpts.Dir = s.Path
|
||||||
badgerOpts.ValueDir = s.Path
|
badgerOpts.ValueDir = s.Path
|
||||||
badgerOpts.SyncWrites = true
|
badgerOpts.SyncWrites = true
|
||||||
badgerOpts.TableLoadingMode = options.FileIO
|
badgerOpts.TableLoadingMode = options.FileIO
|
||||||
badgerOpts.ValueLogLoadingMode = options.FileIO
|
badgerOpts.ValueLogLoadingMode = options.FileIO
|
||||||
|
|
||||||
|
// check if DB dir exists
|
||||||
|
exists,err := exists(s.Path)
|
||||||
|
if err != nil { return err }
|
||||||
|
|
||||||
s.Db, err = badger.Open(badgerOpts)
|
s.Db, err = badger.Open(badgerOpts)
|
||||||
if s.serializer == nil {
|
if s.serializer == nil {
|
||||||
s.serializer = NewSerializerProtobuf(false)
|
s.serializer = NewSerializerProtobuf(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//If the s.Path didn't exist, we have a clean and empty DB at this point and thus restore a initial db
|
||||||
|
if !exists {
|
||||||
|
err = s.Restore(initDbBackupPath, true)
|
||||||
|
}
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -314,11 +336,11 @@ func (s *Store) DeleteMulti(keys []string) (err error) {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func Open(path string) (store *Store, err error) {
|
func Open(workPath string, initDbBackupPath string) (store *Store, err error) {
|
||||||
store = &Store{
|
store = &Store{
|
||||||
Path: path,
|
Path: workPath,
|
||||||
}
|
}
|
||||||
if err = store.Open(); err != nil {
|
if err = store.Open(initDbBackupPath); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"github.com/mame82/P4wnP1_go/common"
|
"github.com/mame82/P4wnP1_go/common"
|
||||||
"github.com/mame82/P4wnP1_go/common_web"
|
"github.com/mame82/P4wnP1_go/common_web"
|
||||||
pb "github.com/mame82/P4wnP1_go/proto"
|
pb "github.com/mame82/P4wnP1_go/proto"
|
||||||
|
"github.com/mame82/P4wnP1_go/service/bluetooth"
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
"io"
|
"io"
|
||||||
@@ -77,30 +78,64 @@ func (s *server) GetAvailableGpios(context.Context, *pb.Empty) (res *pb.StringMe
|
|||||||
func (s *server) DeployMasterTemplate(ctx context.Context, mt *pb.MasterTemplate) (e *pb.Empty, err error) {
|
func (s *server) DeployMasterTemplate(ctx context.Context, mt *pb.MasterTemplate) (e *pb.Empty, err error) {
|
||||||
e = &pb.Empty{}
|
e = &pb.Empty{}
|
||||||
|
|
||||||
|
fmt.Println("Deploying master template ...")
|
||||||
|
|
||||||
//ignore templates with name of length 0
|
//ignore templates with name of length 0
|
||||||
if len(mt.TemplateNameTriggerActions) > 0 {
|
if len(mt.TemplateNameTriggerActions) > 0 {
|
||||||
|
fmt.Printf("... deploying TriggerActions '%s' ...\n", mt.TemplateNameTriggerActions)
|
||||||
_,err = s.DeployStoredTriggerActionSetReplace(ctx, &pb.StringMessage{Msg: mt.TemplateNameTriggerActions})
|
_,err = s.DeployStoredTriggerActionSetReplace(ctx, &pb.StringMessage{Msg: mt.TemplateNameTriggerActions})
|
||||||
if err != nil { return }
|
if err != nil {
|
||||||
|
fmt.Printf("... error deploying TriggerActions '%s'\n", mt.TemplateNameTriggerActions)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Printf("... succeeded deploying TriggerActions '%s'\n", mt.TemplateNameTriggerActions)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _,nnw := range mt.TemplateNamesNetwork {
|
for _,nnw := range mt.TemplateNamesNetwork {
|
||||||
|
fmt.Printf("... deploying Network Interface Settings '%s' ...\n", nnw)
|
||||||
_,err = s.DeployStoredEthernetInterfaceSettings(ctx, &pb.StringMessage{Msg: nnw})
|
_,err = s.DeployStoredEthernetInterfaceSettings(ctx, &pb.StringMessage{Msg: nnw})
|
||||||
if err != nil { return }
|
if err != nil {
|
||||||
|
fmt.Printf("... error deploying Network Interface Settings '%s'\n", nnw)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Printf("... succeeded deploying Network Interface Settings '%s'\n", nnw)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(mt.TemplateNameBluetooth) > 0 {
|
if len(mt.TemplateNameBluetooth) > 0 {
|
||||||
_, err = s.DeployStoredBluetoothSettings(ctx, &pb.StringMessage{Msg: mt.TemplateNameBluetooth})
|
fmt.Printf("... deploying Bluetooth settings '%s' ...\n", mt.TemplateNameBluetooth)
|
||||||
if err != nil { return }
|
_, btErr := s.DeployStoredBluetoothSettings(ctx, &pb.StringMessage{Msg: mt.TemplateNameBluetooth})
|
||||||
|
if btErr != nil {
|
||||||
|
if btErr == bluetooth.ErrBtSvcNotAvailable {
|
||||||
|
fmt.Printf("... ignoring Bluetooth error '%s'\n", mt.TemplateNameBluetooth)
|
||||||
|
|
||||||
|
} else {
|
||||||
|
fmt.Printf("... error deploying Bluetooth settings '%s'\n", mt.TemplateNameBluetooth)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Printf("... error deploying Bluetooth settings '%s'\n", mt.TemplateNameBluetooth)
|
||||||
|
}
|
||||||
|
fmt.Printf("... succeeded deploying Bluetooth settings '%s'\n", mt.TemplateNameBluetooth)
|
||||||
}
|
}
|
||||||
if len(mt.TemplateNameUsb) > 0 {
|
if len(mt.TemplateNameUsb) > 0 {
|
||||||
|
fmt.Printf("... deploying USB settings '%s' ...\n", mt.TemplateNameUsb)
|
||||||
_, err = s.DeployStoredUSBSettings(ctx, &pb.StringMessage{Msg: mt.TemplateNameUsb})
|
_, err = s.DeployStoredUSBSettings(ctx, &pb.StringMessage{Msg: mt.TemplateNameUsb})
|
||||||
if err != nil { return }
|
if err != nil {
|
||||||
|
fmt.Printf("... error deploying USB settings '%s'\n", mt.TemplateNameUsb)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Printf("... succeeded deploying USB settings '%s'\n", mt.TemplateNameUsb)
|
||||||
}
|
}
|
||||||
if len(mt.TemplateNameWifi) > 0 {
|
if len(mt.TemplateNameWifi) > 0 {
|
||||||
|
fmt.Printf("... deploying WiFi settings '%s' ...\n", mt.TemplateNameWifi)
|
||||||
_, err = s.DeployStoredWifiSettings(ctx, &pb.StringMessage{Msg: mt.TemplateNameWifi})
|
_, err = s.DeployStoredWifiSettings(ctx, &pb.StringMessage{Msg: mt.TemplateNameWifi})
|
||||||
if err != nil { return }
|
if err != nil {
|
||||||
|
fmt.Printf("... error deploying WiFi settings '%s'\n", mt.TemplateNameWifi)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Printf("... succeeded deploying WiFi settings '%s'\n", mt.TemplateNameWifi)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Println("... master template deployed successfully")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -206,6 +241,9 @@ func (s *server) FireActionGroupSend(ctx context.Context, gs *pb.ActionGroupSend
|
|||||||
|
|
||||||
func (s *server) DeployBluetoothSettings(ctx context.Context, settings *pb.BluetoothSettings) (resultSettings *pb.BluetoothSettings, err error) {
|
func (s *server) DeployBluetoothSettings(ctx context.Context, settings *pb.BluetoothSettings) (resultSettings *pb.BluetoothSettings, err error) {
|
||||||
defer s.rootSvc.SubSysEvent.Emit(ConstructEventNotifyStateChange(common_web.STATE_CHANGE_EVT_TYPE_BLUETOOTH))
|
defer s.rootSvc.SubSysEvent.Emit(ConstructEventNotifyStateChange(common_web.STATE_CHANGE_EVT_TYPE_BLUETOOTH))
|
||||||
|
//Overwrite default settings, in case the bluetooth sub system comes up later
|
||||||
|
s.rootSvc.SubSysBluetooth.ReplaceDefaultSettings(settings)
|
||||||
|
|
||||||
as := settings.As
|
as := settings.As
|
||||||
ci := settings.Ci
|
ci := settings.Ci
|
||||||
resultSettings = &pb.BluetoothSettings{}
|
resultSettings = &pb.BluetoothSettings{}
|
||||||
@@ -805,7 +843,7 @@ func (s *server) GetAllDeployedEthernetInterfaceSettings(ctx context.Context, em
|
|||||||
|
|
||||||
func (s *server) DeployEthernetInterfaceSettings(ctx context.Context, es *pb.EthernetInterfaceSettings) (empty *pb.Empty, err error) {
|
func (s *server) DeployEthernetInterfaceSettings(ctx context.Context, es *pb.EthernetInterfaceSettings) (empty *pb.Empty, err error) {
|
||||||
defer s.rootSvc.SubSysEvent.Emit(ConstructEventNotifyStateChange(common_web.STATE_CHANGE_EVT_TYPE_NETWORK))
|
defer s.rootSvc.SubSysEvent.Emit(ConstructEventNotifyStateChange(common_web.STATE_CHANGE_EVT_TYPE_NETWORK))
|
||||||
log.Printf("Trying to deploy ethernet interface settings %v", es)
|
log.Printf("Trying to deploy ethernet interface settings %v\n", es)
|
||||||
|
|
||||||
empty = &pb.Empty{}
|
empty = &pb.Empty{}
|
||||||
iname := es.Name
|
iname := es.Name
|
||||||
@@ -814,7 +852,7 @@ func (s *server) DeployEthernetInterfaceSettings(ctx context.Context, es *pb.Eth
|
|||||||
|
|
||||||
err = nim.DeploySettings(es)
|
err = nim.DeploySettings(es)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error deploying ethernet interface settings %v", err)
|
log.Printf("Error deploying ethernet interface settings %v\n", err)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,8 @@
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
"github.com/mame82/P4wnP1_go/common_web"
|
"github.com/mame82/P4wnP1_go/common_web"
|
||||||
pb "github.com/mame82/P4wnP1_go/proto"
|
pb "github.com/mame82/P4wnP1_go/proto"
|
||||||
"github.com/mame82/P4wnP1_go/service/datastore"
|
"github.com/mame82/P4wnP1_go/service/datastore"
|
||||||
@@ -129,7 +131,7 @@ type Service struct {
|
|||||||
func NewService() (svc *Service, err error) {
|
func NewService() (svc *Service, err error) {
|
||||||
svc = &Service{}
|
svc = &Service{}
|
||||||
|
|
||||||
svc.SubSysDataStore, err = datastore.Open(PATH_DATA_STORE)
|
svc.SubSysDataStore, err = datastore.Open(PATH_DATA_STORE, PATH_DATA_STORE_BACKUP + "/init.db")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -175,20 +177,13 @@ func (s *Service) Start() {
|
|||||||
s.SubSysTriggerActions.Start()
|
s.SubSysTriggerActions.Start()
|
||||||
|
|
||||||
// Register TriggerActions
|
// Register TriggerActions
|
||||||
|
/*
|
||||||
log.Println("Register default TriggerActions ...")
|
log.Println("Register default TriggerActions ...")
|
||||||
RegisterDefaultTriggerActions(s.SubSysTriggerActions)
|
RegisterDefaultTriggerActions(s.SubSysTriggerActions)
|
||||||
|
|
||||||
/*
|
|
||||||
// ToDo: 1) Manual start of BT NAP, has to be replaced by settings based approach (same as other subsystems)
|
|
||||||
// ToDo: 2) create a signal based method s.SubSysBluetooth.WaitTillServiceUp(timeout duration)
|
|
||||||
go func() {
|
|
||||||
timeStart := time.Now()
|
|
||||||
for timeSinceStart := time.Since(timeStart); !s.SubSysBluetooth.IsServiceAvailable() && timeSinceStart < time.Second*120 ;timeSinceStart = time.Since(timeStart) {
|
|
||||||
time.Sleep(time.Second)
|
|
||||||
}
|
|
||||||
s.SubSysBluetooth.StartNAP()
|
|
||||||
}()
|
|
||||||
*/
|
*/
|
||||||
|
fmt.Println("Trying to deploy master template...")
|
||||||
|
_,err := s.SubSysRPC.DeployStoredMasterTemplate(context.Background(), &pb.StringMessage{Msg:"startup"})
|
||||||
|
fmt.Println("...!!!!! MASTER TEMPLATE:", err)
|
||||||
|
|
||||||
// fire service started Event
|
// fire service started Event
|
||||||
log.Println("Fire service started event ...")
|
log.Println("Fire service started event ...")
|
||||||
|
|||||||
Reference in New Issue
Block a user