Add Updater CLI example
This commit is contained in:
parent
964e1d713e
commit
5008dfbd56
|
@ -0,0 +1,27 @@
|
|||
# Example: Updater
|
||||
|
||||
A simple example of an updater client wich can:
|
||||
|
||||
- Configure a ReachRS module to use the given WiFi network
|
||||
- Check for updates then reboot to "ReachView" mode
|
||||
|
||||
## Usage
|
||||
|
||||
1. Boot your ReachRS module in "Updater" mode. You can see a documentation on how to reset your device [here](https://forge.cadoles.com/Pyxis/orion/wiki/FlashIt).
|
||||
|
||||
2. Launch the example in `configure-wifi` phase and provides WiFi network informations.
|
||||
```shell
|
||||
go run example/updater/main.go \
|
||||
-phase 'configure-wifi'\
|
||||
-host '<DEVICE_IP_ADDRESS'\
|
||||
-ssid '<WIFI_SSID>'\
|
||||
-password '<WIFI_PASSWORD>'\
|
||||
-security '<WIFI_SECURITY>'
|
||||
```
|
||||
3. The device will switch to the provided WiFi network, as you should do.
|
||||
4. Launch the example in `update-then-reboot` phase.
|
||||
```shell
|
||||
go run example/updater/main.go \
|
||||
-phase 'update-and-reboot'\
|
||||
-host '<DEVICE_IP_ADDRESS'
|
||||
```
|
|
@ -0,0 +1,148 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"forge.cadoles.com/Pyxis/orion/reach"
|
||||
)
|
||||
|
||||
const (
|
||||
phaseConfigureWifi = "configure-wifi"
|
||||
phaseUpdateThenReboot = "update-then-reboot"
|
||||
)
|
||||
|
||||
var (
|
||||
phase = phaseConfigureWifi
|
||||
host = "192.168.42.1"
|
||||
ssid = ""
|
||||
security = string(reach.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() *reach.Updater {
|
||||
|
||||
updater := reach.NewUpdaterClient(
|
||||
reach.WithEndpoint(host, 80),
|
||||
)
|
||||
|
||||
log.Printf("connecting to module '%s'", host)
|
||||
if err := updater.Connect(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
log.Println("connected")
|
||||
|
||||
return updater
|
||||
|
||||
}
|
||||
|
||||
func configureWifi() {
|
||||
|
||||
if ssid == "" {
|
||||
log.Fatal("you must provide a WiFi SSID with the -ssid flag")
|
||||
}
|
||||
|
||||
updater := connect()
|
||||
defer updater.Close()
|
||||
|
||||
log.Println("checking module status")
|
||||
results, err := updater.TestResults()
|
||||
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)
|
||||
|
||||
done, err := updater.AddWifiNetwork(ssid, reach.WifiSecurity(security), password)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if !done {
|
||||
log.Fatal("couldnt add wifi network")
|
||||
}
|
||||
|
||||
log.Println("connecting module to wifi network")
|
||||
if err := updater.ConnectToWifiNetwork(ssid, true); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Printf("you can now switch to the wifi network and start phase '%s'", phaseUpdateThenReboot)
|
||||
|
||||
}
|
||||
|
||||
func updateThenReboot() {
|
||||
updater := connect()
|
||||
defer updater.Close()
|
||||
|
||||
log.Println("checking time sync")
|
||||
synced, err := updater.TimeSyncStatus()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Printf("time synced ? %v", synced)
|
||||
|
||||
log.Println("checking reachview version")
|
||||
version, err := updater.ReachViewVersion()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Printf("reachview version ? '%s'", version)
|
||||
|
||||
log.Println("checking for update")
|
||||
status, err := updater.Update()
|
||||
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")
|
||||
if err := updater.RebootNow(true); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue