Using mdns instead of hardcoded IP ...

This commit is contained in:
Philippe Caseiro 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) return fmt.Errorf("Wifi cell with SSID %s is not available", box.SSID)
} }
func (o *OrionService) discoverService() ([]emlid.Service, error) { func (o *OrionService) discoverService(rqContext context.Context) ([]emlid.Service, error) {
return emlid.Discover(20 * time.Second) 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 ! // 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) server.Security = string(updater.SecurityWPAPSK)
} }
var boxCli *updater.Client boxCli, err := o.connectUpdater(rqContext, box)
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
}
if err := boxCli.Connect(); err != nil { if err := boxCli.Connect(); err != nil {
return errors.Wrap(err, "Connecting to Box failed") return errors.Wrap(err, "Connecting to Box failed")
} }
defer boxCli.Close() defer boxCli.Close()
fmt.Println("Running Tests !")
ctx, testResultsCancel := context.WithTimeout(rqContext, 55*time.Second) ctx, testResultsCancel := context.WithTimeout(rqContext, 55*time.Second)
defer testResultsCancel() defer testResultsCancel()
_, err = boxCli.TestResults(ctx) _, err = boxCli.TestResults(ctx)
@ -266,6 +280,7 @@ func (o *OrionService) setupMasterWifi(rqContext context.Context, box *OrionBox,
return errors.Wrap(err, "Minimal test failed") return errors.Wrap(err, "Minimal test failed")
} }
fmt.Println("Add Wifi !")
ctx, addWifiCancel := context.WithTimeout(rqContext, 55*time.Second) ctx, addWifiCancel := context.WithTimeout(rqContext, 55*time.Second)
defer addWifiCancel() defer addWifiCancel()
@ -281,6 +296,7 @@ func (o *OrionService) setupMasterWifi(rqContext context.Context, box *OrionBox,
ctx, joinWifiCancel := context.WithTimeout(ctx, 55*time.Second) ctx, joinWifiCancel := context.WithTimeout(ctx, 55*time.Second)
defer joinWifiCancel() defer joinWifiCancel()
fmt.Println("Join Wifi !")
if err := boxCli.JoinWifiNetwork(ctx, server.SSID, true); err != nil { if err := boxCli.JoinWifiNetwork(ctx, server.SSID, true); err != nil {
return errors.Wrap(err, "Time sync failed") 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 // 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 { func (o *OrionService) updateAndReboot(rqContext context.Context, box *OrionBox, server *OrionServer, reply *UpdateOrionBoxResponse) error {
var boxCli *updater.Client time.Sleep(3 * time.Second)
service, err := o.discoverService() boxCli, err := o.connectUpdater(rqContext, box)
if err != nil { if err != nil {
boxCli = updater.NewClient( return errors.Wrap(err, "Problem connecting to updater")
emlid.WithService(service[0]),
)
} else {
boxCli = updater.NewClient(
emlid.WithEndpoint(box.NewAddress, 80),
)
} }
if err := boxCli.Connect(); err != nil { 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 { if err := boxCli.Connect(); err != nil {
return errors.Wrap(err, "Connecting to Box on master wifi network failed") 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, args *UpdateOrionBoxArgs,
reply *UpdateOrionBoxResponse) error { reply *UpdateOrionBoxResponse) error {
if err := o.setupMasterWifi(r.Context(), args.Box, args.Server); err != nil { // if err := o.setupMasterWifi(r.Context(), args.Box, args.Server); err != nil {
return err // return err
} // }
o.setupMasterWifi(r.Context(), args.Box, args.Server)
return o.updateAndReboot(r.Context(), args.Box, args.Server, reply) return o.updateAndReboot(r.Context(), args.Box, args.Server, reply)
} }

View File

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

3
go.mod
View File

@ -4,8 +4,9 @@ require (
forge.cadoles.com/Pyxis/golang-socketio v0.0.0-20180919100209-bb857ced6b95 forge.cadoles.com/Pyxis/golang-socketio v0.0.0-20180919100209-bb857ced6b95
github.com/caarlos0/env v3.4.0+incompatible github.com/caarlos0/env v3.4.0+incompatible
github.com/cenkalti/backoff v2.0.0+incompatible // indirect github.com/cenkalti/backoff v2.0.0+incompatible // indirect
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1
github.com/go-chi/chi v3.3.3+incompatible github.com/go-chi/chi v3.3.3+incompatible
github.com/gorilla/rpc v1.1.0
github.com/gorilla/websocket v1.4.0 // indirect github.com/gorilla/websocket v1.4.0 // indirect
github.com/grandcat/zeroconf v0.0.0-20180329153754-df75bb3ccae1 github.com/grandcat/zeroconf v0.0.0-20180329153754-df75bb3ccae1
github.com/miekg/dns v1.0.12 // indirect github.com/miekg/dns v1.0.12 // indirect

2
go.sum
View File

@ -8,6 +8,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-chi/chi v3.3.3+incompatible h1:KHkmBEMNkwKuK4FdQL7N2wOeB9jnIx7jR5wsuSBEFI8= github.com/go-chi/chi v3.3.3+incompatible h1:KHkmBEMNkwKuK4FdQL7N2wOeB9jnIx7jR5wsuSBEFI8=
github.com/go-chi/chi v3.3.3+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ= github.com/go-chi/chi v3.3.3+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ=
github.com/gorilla/rpc v1.1.0 h1:marKfvVP0Gpd/jHlVBKCQ8RAoUPdX7K1Nuh6l1BNh7A=
github.com/gorilla/rpc v1.1.0/go.mod h1:V4h9r+4sF5HnzqbwIez0fKSpANP0zlYd3qR7p36jkTQ=
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q= github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/grandcat/zeroconf v0.0.0-20180329153754-df75bb3ccae1 h1:VSELJSxQlpi1bz4ZwT+93hPpzNLRcgytLr77iVRJpcE= github.com/grandcat/zeroconf v0.0.0-20180329153754-df75bb3ccae1 h1:VSELJSxQlpi1bz4ZwT+93hPpzNLRcgytLr77iVRJpcE=