Using mdns instead of hardcoded IP ...

This commit is contained in:
2018-10-17 15:32:01 +02:00
committed by William Petit
parent b1a4dc162d
commit 94b4b220c6
4 changed files with 51 additions and 32 deletions

View File

@ -226,8 +226,33 @@ func (o *OrionService) connectBox(box *OrionBox, server *OrionServer) error {
return fmt.Errorf("Wifi cell with SSID %s is not available", box.SSID)
}
func (o *OrionService) discoverService() ([]emlid.Service, error) {
return emlid.Discover(20 * time.Second)
func (o *OrionService) discoverService(rqContext context.Context) ([]emlid.Service, error) {
ctx, cancelDiscover := context.WithTimeout(rqContext, 55*time.Second)
defer cancelDiscover()
return emlid.Discover(ctx)
}
func (o *OrionService) connectUpdater(rqContext context.Context, box *OrionBox) (*updater.Client, error) {
var boxCli *updater.Client
service, err := o.discoverService(rqContext)
fmt.Printf("DEBUG len = %d\n", len(service))
if len(service) == 0 {
fmt.Println("DEBUG Connecting to box with EndPoint !")
boxCli = updater.NewClient(
emlid.WithEndpoint(box.Address, 80),
)
} else if err == nil {
fmt.Println("DEBUG Connecting to box with Service !")
boxCli = updater.NewClient(
emlid.WithService(service[0]),
)
} else {
fmt.Printf("Non mais là c'est vraiment étrange !")
return nil, err
}
//fmt.Printf("NAME: %s, IP: %s", service[0].Name, service[0].AddrV4)
return boxCli, err
}
// setupMasterWifi connects to the box, add wifi network and join it !
@ -240,25 +265,14 @@ func (o *OrionService) setupMasterWifi(rqContext context.Context, box *OrionBox,
server.Security = string(updater.SecurityWPAPSK)
}
var boxCli *updater.Client
service, err := o.discoverService()
if len(service) == 0 {
boxCli = updater.NewClient(
emlid.WithEndpoint(box.Address, 80),
)
} else if err != nil {
boxCli = updater.NewClient(
emlid.WithService(service[0]),
)
} else {
return err
}
boxCli, err := o.connectUpdater(rqContext, box)
if err := boxCli.Connect(); err != nil {
return errors.Wrap(err, "Connecting to Box failed")
}
defer boxCli.Close()
fmt.Println("Running Tests !")
ctx, testResultsCancel := context.WithTimeout(rqContext, 55*time.Second)
defer testResultsCancel()
_, err = boxCli.TestResults(ctx)
@ -266,6 +280,7 @@ func (o *OrionService) setupMasterWifi(rqContext context.Context, box *OrionBox,
return errors.Wrap(err, "Minimal test failed")
}
fmt.Println("Add Wifi !")
ctx, addWifiCancel := context.WithTimeout(rqContext, 55*time.Second)
defer addWifiCancel()
@ -281,6 +296,7 @@ func (o *OrionService) setupMasterWifi(rqContext context.Context, box *OrionBox,
ctx, joinWifiCancel := context.WithTimeout(ctx, 55*time.Second)
defer joinWifiCancel()
fmt.Println("Join Wifi !")
if err := boxCli.JoinWifiNetwork(ctx, server.SSID, true); err != nil {
return errors.Wrap(err, "Time sync failed")
}
@ -289,19 +305,19 @@ func (o *OrionService) setupMasterWifi(rqContext context.Context, box *OrionBox,
// updateAndReboot connects to the box with the New adress and run basic tests on version dans updates
func (o *OrionService) updateAndReboot(rqContext context.Context, box *OrionBox, server *OrionServer, reply *UpdateOrionBoxResponse) error {
var boxCli *updater.Client
service, err := o.discoverService()
time.Sleep(3 * time.Second)
boxCli, err := o.connectUpdater(rqContext, box)
if err != nil {
boxCli = updater.NewClient(
emlid.WithService(service[0]),
)
} else {
boxCli = updater.NewClient(
emlid.WithEndpoint(box.NewAddress, 80),
)
return errors.Wrap(err, "Problem connecting to updater")
}
if err := boxCli.Connect(); err != nil {
fmt.Println("Waiting 3 seconds on error")
time.Sleep(3 * time.Second)
boxCli, clerr := o.connectUpdater(rqContext, box)
if clerr != nil {
return errors.Wrap(err, "Problem connecting to updater")
}
if err := boxCli.Connect(); err != nil {
return errors.Wrap(err, "Connecting to Box on master wifi network failed")
}
@ -347,10 +363,10 @@ func (o *OrionService) UpdateOrionBox(r *http.Request,
args *UpdateOrionBoxArgs,
reply *UpdateOrionBoxResponse) error {
if err := o.setupMasterWifi(r.Context(), args.Box, args.Server); err != nil {
return err
}
// if err := o.setupMasterWifi(r.Context(), args.Box, args.Server); err != nil {
// return err
// }
o.setupMasterWifi(r.Context(), args.Box, args.Server)
return o.updateAndReboot(r.Context(), args.Box, args.Server, reply)
}

View File

@ -51,14 +51,14 @@ content-type: application/json
"method": "Orion.UpdateOrionBox",
"params": [ {
"Box": {
"Address": "192.168.42.1",
"NewAddress": "192.168.100.213",
"Address": "",
"NewAddress": "",
"Security": "",
"SSID" : "reach:2a:03",
"WifiKey": "emlidreach"
},
"Server": {
"Address": "192.168.42.1",
"Address": "",
"SSID": "Pyxis",
"Security": "",
"WifiKey": "A2Z3E4R5T6Y7U",