diff --git a/cmd/server/rpc/server.go b/cmd/server/rpc/server.go index 8b3a2a9..e76bb20 100644 --- a/cmd/server/rpc/server.go +++ b/cmd/server/rpc/server.go @@ -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) } diff --git a/cmd/server/rpc/testdata/test_rest.rest b/cmd/server/rpc/testdata/test_rest.rest index 1ff82ce..9cc3def 100644 --- a/cmd/server/rpc/testdata/test_rest.rest +++ b/cmd/server/rpc/testdata/test_rest.rest @@ -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", diff --git a/go.mod b/go.mod index 4989193..124a239 100644 --- a/go.mod +++ b/go.mod @@ -4,8 +4,9 @@ require ( forge.cadoles.com/Pyxis/golang-socketio v0.0.0-20180919100209-bb857ced6b95 github.com/caarlos0/env v3.4.0+incompatible 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/gorilla/rpc v1.1.0 github.com/gorilla/websocket v1.4.0 // indirect github.com/grandcat/zeroconf v0.0.0-20180329153754-df75bb3ccae1 github.com/miekg/dns v1.0.12 // indirect diff --git a/go.sum b/go.sum index 63a97cb..c0132ef 100644 --- a/go.sum +++ b/go.sum @@ -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/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/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/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/grandcat/zeroconf v0.0.0-20180329153754-df75bb3ccae1 h1:VSELJSxQlpi1bz4ZwT+93hPpzNLRcgytLr77iVRJpcE=