Skip to content

Commit

Permalink
Wait for update completion for gen2 devices
Browse files Browse the repository at this point in the history
  • Loading branch information
IngmarStein committed Nov 6, 2023
1 parent ac58fa9 commit acc2f29
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,9 @@ func updateShellyGen1(name, address string) {
updateStatusCheck, err := checkShellyUpdateStatus(address)
if err != nil {
fmt.Printf("%s failed to query update status: %s, retrying...\n", prefix, err)
} else {
updateStatus = updateStatusCheck
continue
}
updateStatus = updateStatusCheck
}

fmt.Printf("%s device updated to %s!\n", prefix, updateStatus.OldVersion)
Expand All @@ -234,13 +234,37 @@ func updateShellyGen2(name, address string) {
fmt.Printf("%s already up to date\n", prefix)
return
}
newVersion := updateVersion

fmt.Printf("%s updating to version %s...\n", prefix, updateVersion)
err = makeGen2UpdateRequest(address, updateStage)
if err != nil {
fmt.Printf("%s failed to update: %s, aborting...\n", prefix, err)
return
}

// wait for update to complete
tries := 0
for updateVersion != "" {
tries++
if tries > 12 {
fmt.Printf("%s failed to check if update completed successfully", prefix)
return
}
time.Sleep(time.Second * 5)
updates, err := makeGen2CheckForUpdateRequest(address)
if err != nil {
fmt.Printf("%s failed to query update status: %s, retrying...\n", prefix, err)
continue
}

if updateStage == "beta" {
updateVersion = updates.Beta.Version
} else {
updateVersion = updates.Stable.Version
}
}
fmt.Printf("%s device updated to %s!\n", prefix, newVersion)
}

func updateShelly(name, address string, txtRecords []string, genToUpdate int) {
Expand Down Expand Up @@ -273,7 +297,11 @@ func main() {
os.Exit(2)
}

resolver, err := zeroconf.NewResolver(nil)
// Listen only for IPv4 addresses. Otherwise, it may happen that ServiceEntry has an empty
// AddrIPv4 slice. It happens when the IPv6 arrives first and ServiceEntries are not updated
// when more data arrives.
// See https://github.com/grandcat/zeroconf/issues/27
resolver, err := zeroconf.NewResolver(zeroconf.SelectIPTraffic(zeroconf.IPv4))
if err != nil {
log.Fatalln("Failed to initialize resolver:", err.Error())
}
Expand All @@ -290,7 +318,8 @@ func main() {
address := entry.HostName
if len(entry.AddrIPv4) > 0 {
address = entry.AddrIPv4[0].String()
// not yet:
// IPv6 support is still very limited
// See https://shelly-api-docs.shelly.cloud/gen2/General/IPv6
//} else if len(entry.AddrIPv6) > 0 {
// address = fmt.Sprintf("[%s]", entry.AddrIPv6[0].String())
}
Expand Down

0 comments on commit acc2f29

Please sign in to comment.