Adding Tests run and improving reply

This commit is contained in:
2018-10-15 16:42:35 +02:00
committed by William Petit
parent e0a32a2ecb
commit 68e9ba80b3
2 changed files with 27 additions and 22 deletions

View File

@ -188,7 +188,8 @@ type UpdateOrionBoxResponse struct {
Netmask string
Synced bool
Version string
Errors []string
Status *updater.UpdateStatus
Results *updater.TestResults
}
// Connect wifi interface to a Orion box wifi hotspot!
@ -204,7 +205,6 @@ func (o *OrionService) connectBox(box *OrionBox, server *OrionServer) error {
iface := server.ClientIface
cells := iface.Scan()
for _, cell := range cells {
fmt.Printf("Cell : %s\n", cell.Ssid)
if cell.Ssid == box.SSID {
cn := iface.Connect(o.UCI, cell, box.WifiKey)
if cn.ReturnCode != 0 {
@ -226,15 +226,7 @@ func (o *OrionService) connectBox(box *OrionBox, server *OrionServer) error {
}
func (o *OrionService) discoverService() ([]emlid.Service, error) {
service, err := emlid.Discover(20 * time.Second)
if err != nil {
return service, err
}
if len(service) == 0 {
return service, fmt.Errorf("Discovery don't find any Orion Box")
}
return service, err
return emlid.Discover(20 * time.Second)
}
// setupMasterWifi connects to the box, add wifi network and join it !
@ -249,14 +241,16 @@ func (o *OrionService) setupMasterWifi(rqContext context.Context, box *OrionBox,
var boxCli *updater.Client
service, err := o.discoverService()
if err != nil {
if len(service) == 0 {
boxCli = updater.NewClient(
emlid.WithEndpoint(box.Address, 80),
)
} else if err != nil {
boxCli = updater.NewClient(
emlid.WithService(service[0]),
)
} else {
boxCli = updater.NewClient(
emlid.WithEndpoint(box.Address, 80),
)
return err
}
if err := boxCli.Connect(); err != nil {
@ -264,6 +258,13 @@ func (o *OrionService) setupMasterWifi(rqContext context.Context, box *OrionBox,
}
defer boxCli.Close()
ctx, testResultsCancel := context.WithTimeout(rqContext, 55*time.Second)
defer testResultsCancel()
_, err = boxCli.TestResults(ctx)
if err != nil {
return errors.Wrap(err, "Minimal test failed")
}
ctx, addWifiCancel := context.WithTimeout(rqContext, 55*time.Second)
defer addWifiCancel()
@ -300,7 +301,9 @@ func (o *OrionService) updateAndReboot(rqContext context.Context, box *OrionBox,
}
if err := boxCli.Connect(); err != nil {
return errors.Wrap(err, "Connecting to Box on master wifi network failed")
if err := boxCli.Connect(); err != nil {
return errors.Wrap(err, "Connecting to Box on master wifi network failed")
}
}
defer boxCli.Close()
@ -320,6 +323,14 @@ func (o *OrionService) updateAndReboot(rqContext context.Context, box *OrionBox,
}
reply.Version = version
ctx, updateCancel := context.WithTimeout(rqContext, 55*time.Second)
defer updateCancel()
status, err := boxCli.Update(ctx)
if err != nil {
return errors.Wrap(err, "System update failed")
}
reply.Status = status
ctx, rebootCancel := context.WithTimeout(rqContext, 55*time.Second)
defer rebootCancel()
return boxCli.RebootNow(ctx, true)