Use context.Context to provide timeout detection to websocket calls

This commit is contained in:
2018-09-26 12:05:55 +02:00
parent 1dcd03e455
commit 9367e5e79e
18 changed files with 170 additions and 47 deletions

View File

@ -1,10 +1,12 @@
package main
import (
"context"
"flag"
"fmt"
"log"
"strings"
"time"
"forge.cadoles.com/Pyxis/orion/emlid"
"forge.cadoles.com/Pyxis/orion/emlid/updater"
@ -78,8 +80,13 @@ func configureWifi() {
c := connect()
defer c.Close()
ctx := context.Background()
log.Println("checking module status")
results, err := c.TestResults()
ctx, testResultsCancel := context.WithTimeout(ctx, 5*time.Second)
defer testResultsCancel()
results, err := c.TestResults(ctx)
if err != nil {
log.Fatal(err)
}
@ -92,7 +99,9 @@ func configureWifi() {
log.Printf("adding wifi network '%s'", ssid)
done, err := c.AddWifiNetwork(ssid, updater.WifiSecurity(security), password)
ctx, addWifiCancel := context.WithTimeout(ctx, 5*time.Second)
defer addWifiCancel()
done, err := c.AddWifiNetwork(ctx, ssid, updater.WifiSecurity(security), password)
if err != nil {
log.Fatal(err)
}
@ -102,7 +111,9 @@ func configureWifi() {
}
log.Println("connecting module to wifi network")
if err := c.JoinWifiNetwork(ssid, true); err != nil {
ctx, joinWifiCancel := context.WithTimeout(ctx, 5*time.Second)
defer joinWifiCancel()
if err := c.JoinWifiNetwork(ctx, ssid, true); err != nil {
log.Fatal(err)
}
log.Printf("you can now switch to the wifi network and start phase '%s'", phaseUpdateThenReboot)
@ -114,22 +125,30 @@ func updateThenReboot() {
c := connect()
defer c.Close()
ctx := context.Background()
log.Println("checking time sync")
synced, err := c.TimeSynced()
ctx, timeSyncedCancel := context.WithTimeout(ctx, 5*time.Second)
defer timeSyncedCancel()
synced, err := c.TimeSynced(ctx)
if err != nil {
log.Fatal(err)
}
log.Printf("time synced ? %v", synced)
log.Println("checking reachview version")
version, err := c.ReachViewVersion()
ctx, reachviewVersionCancel := context.WithTimeout(ctx, 5*time.Second)
defer reachviewVersionCancel()
version, err := c.ReachViewVersion(ctx)
if err != nil {
log.Fatal(err)
}
log.Printf("reachview version ? '%s'", version)
log.Println("checking for update")
status, err := c.Update()
ctx, updateCancel := context.WithTimeout(ctx, 5*time.Second)
defer updateCancel()
status, err := c.Update(ctx)
if err != nil {
log.Fatal(err)
}
@ -143,7 +162,9 @@ func updateThenReboot() {
}
log.Println("rebooting device")
if err := c.RebootNow(true); err != nil {
ctx, rebootCancel := context.WithTimeout(ctx, 5*time.Second)
defer rebootCancel()
if err := c.RebootNow(ctx, true); err != nil {
log.Fatal(err)
}