From e3bfd27b0a94e970bfc9a776be221ba829589c77 Mon Sep 17 00:00:00 2001 From: Philippe Caseiro Date: Fri, 19 Oct 2018 14:46:55 +0200 Subject: [PATCH] Make roverConfiguration with JSON-RPC possible --- cmd/server/rpc/server.go | 154 ++++++++++++++++++++++++- cmd/server/rpc/testdata/test_rest.rest | 18 +++ 2 files changed, 167 insertions(+), 5 deletions(-) diff --git a/cmd/server/rpc/server.go b/cmd/server/rpc/server.go index 97497b8..944745f 100644 --- a/cmd/server/rpc/server.go +++ b/cmd/server/rpc/server.go @@ -8,6 +8,7 @@ import ( "time" "forge.cadoles.com/Pyxis/orion/emlid" + "forge.cadoles.com/Pyxis/orion/emlid/reachview" "forge.cadoles.com/Pyxis/orion/emlid/updater" "forge.cadoles.com/Pyxis/orion/openwrt" "github.com/gorilla/rpc" @@ -193,7 +194,7 @@ type UpdateOrionBoxResponse struct { Results *updater.TestResults } -// Connect wifi interface to a Orion box wifi hotspot! +// connect wifi interface to a Orion box wifi hotspot! func (o *OrionService) connectBox(box *OrionBox, server *OrionServer) error { o.UCI.LoadWirelessConf() if server == nil { @@ -265,7 +266,10 @@ func (o *OrionService) setupMasterWifi(rqContext context.Context, box *OrionBox, server.Security = string(updater.SecurityWPAPSK) } - boxCli, err := o.connectUpdater(rqContext, box) + boxCli, cliErr := o.connectUpdater(rqContext, box) + if cliErr != nil { + return errors.Wrap(cliErr, "Creation of updater client failed") + } if err := boxCli.Connect(); err != nil { return errors.Wrap(err, "Connecting to Box failed") @@ -275,7 +279,7 @@ func (o *OrionService) setupMasterWifi(rqContext context.Context, box *OrionBox, fmt.Println("Running Tests !") ctx, testResultsCancel := context.WithTimeout(rqContext, 55*time.Second) defer testResultsCancel() - _, err = boxCli.TestResults(ctx) + _, err := boxCli.TestResults(ctx) if err != nil { return errors.Wrap(err, "Minimal test failed") } @@ -304,7 +308,7 @@ func (o *OrionService) setupMasterWifi(rqContext context.Context, box *OrionBox, } // updateAndReboot connects to the box with the New address 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, reply *UpdateOrionBoxResponse) error { time.Sleep(3 * time.Second) boxCli, err := o.connectUpdater(rqContext, box) if err != nil { @@ -367,7 +371,147 @@ func (o *OrionService) UpdateOrionBox(r *http.Request, // 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, reply) +} + +// connectReachview creates the client to a reachview module and returns it ! +func (o *OrionService) connectReachView(rqContext context.Context, box *OrionBox) (*reachview.Client, error) { + var boxCli *reachview.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 = reachview.NewClient( + emlid.WithEndpoint(box.Address, 80), + ) + } else if err == nil { + fmt.Println("DEBUG Connecting to box with Service !") + boxCli = reachview.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 +} + +// ConfigOrionBoxArgs argument structure for exported method OwrtCreateWifiInterface +type ConfigOrionBoxArgs struct { + Box *OrionBox +} + +// ConfigOrionBoxResponse argument structure for exported method OwrtCreateWifiInterface +type ConfigOrionBoxResponse struct { +} + +func (o *OrionService) resetConfiguration(rqContext context.Context, c *reachview.Client) { + ctx, resetCancel := context.WithTimeout(rqContext, 55*time.Second) + defer resetCancel() + result, _, err := c.ResetConfiguration(ctx) + if err != nil { + log.Fatal(err) + } + + if result != reachview.ConfigurationApplySuccess { + log.Fatal("configuration reset failed !") + } +} + +// FIXME have to came from parameters ! +func (o *OrionService) getCommonConfiguration() *reachview.Configuration { + return &reachview.Configuration{ + RTKSettings: &reachview.RTKSettings{ + PositioningSystems: &reachview.PositionningSystems{ + GPS: reachview.True, + GLONASS: reachview.True, + Galileo: reachview.True, + SBAS: reachview.True, + QZSS: reachview.True, + }, + UpdateRate: reachview.String("5"), + }, + LoRa: &reachview.LoRa{ + AirRate: reachview.String("9.11"), + Frequency: reachview.Float(868000), + OutputPower: reachview.String("20"), + }, + PositionOutput: &reachview.PositionOutput{ + Output1: &reachview.Output{ + Enabled: reachview.False, + }, + Output2: &reachview.Output{ + Enabled: reachview.False, + }, + }, + } +} + +func (o *OrionService) applyConfiguration(rqContext context.Context, c *reachview.Client, config *reachview.Configuration) error { + + ctx, applyConfCancel := context.WithTimeout(rqContext, 55*time.Second) + defer applyConfCancel() + + result, _, err := c.ApplyConfiguration(ctx, config) + if err != nil { + return err + } + + if result != reachview.ConfigurationApplySuccess { + return fmt.Errorf("Configuration update failed") + } + + return c.RestartRTKLib() +} + +func (o *OrionService) configureRover(rqContext context.Context, c *reachview.Client) error { + + _ = c.Connect() + defer c.Close() + + o.resetConfiguration(rqContext, c) + + config := o.getCommonConfiguration() + config.RTKSettings.GPSARMode = reachview.GPSARModeFixAndHold + config.RTKSettings.GLONASSARMode = reachview.On + config.RTKSettings.PositionningMode = reachview.PositionningModeKinematic + config.RTKSettings.UpdateRate = reachview.String("5") + config.CorrectionInput = &reachview.CorrectionInput{ + Input2: &reachview.Input2{ + Input: reachview.Input{ + Enabled: reachview.True, + Format: reachview.IOFormatRTCM3, + Type: reachview.IOTypeLoRa, + Path: reachview.String("lora"), + }, + SendPositionToBase: reachview.Off, + }, + } + config.BaseMode = &reachview.BaseMode{ + Output: &reachview.Output{ + Enabled: reachview.False, + }, + } + log.Println("configuring module as rover") + + return o.applyConfiguration(rqContext, c, config) + +} + +// ConfigureOrionBox starts provisionning process for an Orion box (base or rover) +func (o *OrionService) ConfigureOrionBox(r *http.Request, + args *ConfigOrionBoxArgs, + reply *ConfigOrionBoxResponse) error { + + reach, err := o.connectReachView(r.Context(), args.Box) + if err != nil { + return errors.Wrap(err, "Impossible to create ReachView client") + } + + fmt.Println("Start Rover Configuraiton !") + return o.configureRover(r.Context(), reach) } // NewServer returns a new configured JSON-RPC server diff --git a/cmd/server/rpc/testdata/test_rest.rest b/cmd/server/rpc/testdata/test_rest.rest index 9cc3def..cfac96f 100644 --- a/cmd/server/rpc/testdata/test_rest.rest +++ b/cmd/server/rpc/testdata/test_rest.rest @@ -72,4 +72,22 @@ content-type: application/json } } } ] +} + +### +POST http://192.168.100.1:8888/rpc HTTP/1.1 +content-type: application/json + +{ + "id": 1, + "method": "Orion.ConfigureOrionBox", + "params": [ { + "Box": { + "Address": "", + "NewAddress": "", + "Security": "", + "SSID" : "reach:2a:03", + "WifiKey": "emlidreach" + } + } ] } \ No newline at end of file