package main import ( "context" "flag" "fmt" "log" "strings" "time" "forge.cadoles.com/Pyxis/orion/emlid" "forge.cadoles.com/Pyxis/orion/emlid/updater" ) const ( phaseConfigureWifi = "configure-wifi" phaseUpdateThenReboot = "update-then-reboot" ) var ( phase = phaseConfigureWifi host = "192.168.42.1" ssid = "" security = string(updater.SecurityWPAPSK) password = "" ) func init() { phases := []string{phaseConfigureWifi, phaseUpdateThenReboot} flag.StringVar( &phase, "phase", phase, fmt.Sprintf("The configuration phase. Available: %v", strings.Join(phases, ", ")), ) flag.StringVar(&host, "host", host, "ReachRS module host") flag.StringVar(&ssid, "ssid", ssid, "The WiFi SSID to connect the module to") flag.StringVar(&security, "security", security, "The WiFi security algorithm") flag.StringVar(&password, "password", password, "The WiFi password") } func main() { flag.Parse() switch phase { case phaseConfigureWifi: configureWifi() case phaseUpdateThenReboot: updateThenReboot() default: log.Fatalf("unknown phase '%s'", phase) } log.Println("done") } func connect() *updater.Client { c := updater.NewClient( emlid.WithEndpoint(host, 80), ) log.Printf("connecting to module '%s'", host) if err := c.Connect(); err != nil { log.Fatal(err) } log.Println("connected") return c } func configureWifi() { if ssid == "" { log.Fatal("you must provide a WiFi SSID with the -ssid flag") } c := connect() defer c.Close() ctx := context.Background() log.Println("checking module status") ctx, testResultsCancel := context.WithTimeout(ctx, 5*time.Second) defer testResultsCancel() results, err := c.TestResults(ctx) if err != nil { log.Fatal(err) } log.Printf("device: '%s'", results.Device) log.Printf("lora activated ? %v", results.Lora) log.Printf("mpu activated ? %v", results.MPU) log.Printf("stc activated ? %v", results.STC) log.Printf("ublox activated ? %v", results.UBlox) log.Printf("adding wifi network '%s'", ssid) 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) } if !done { log.Fatal("couldnt add wifi network") } log.Println("connecting module to wifi network") 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) } func updateThenReboot() { c := connect() defer c.Close() ctx := context.Background() log.Println("checking time sync") 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") 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") ctx, updateCancel := context.WithTimeout(ctx, 5*time.Second) defer updateCancel() status, err := c.Update(ctx) if err != nil { log.Fatal(err) } log.Printf("is update running ? %v", status.Active) log.Printf("is update locked ? %v", status.Locked) log.Printf("last update state ? '%s'", status.State) if status.Active { log.Fatal("cannot reboot while an update is active") } log.Println("rebooting device") ctx, rebootCancel := context.WithTimeout(ctx, 5*time.Second) defer rebootCancel() if err := c.RebootNow(ctx, true); err != nil { log.Fatal(err) } }