Feat: update check and print device info

This commit is contained in:
kayos@tcp.direct 2022-10-30 04:04:20 -07:00
parent bdcdadce52
commit c2ea5b3a95
Signed by: kayos
GPG Key ID: 4B841471B4BEE979
3 changed files with 117 additions and 1 deletions

@ -82,7 +82,10 @@ func init() {
Commands["adopt"] = newZiggsCommand(cmdAdopt, "adopt new lights to the bridge")
Commands["dump"] = newZiggsCommand(cmdDump, "dump target object JSON to a file")
Commands["load"] = newZiggsCommand(cmdLoad, "load JSON from a file into the bridge")
Commands["set"] = newZiggsCommand(cmdSet, "update object properties in bridge", "update")
Commands["set"] = newZiggsCommand(cmdSet, "update object properties in bridge")
Commands["fwupdate"] = newZiggsCommand(cmdFirmwareUpdate, "inform bridge to check for updates",
"fwup", "upgrade")
Commands["info"] = newZiggsCommand(cmdInfo, "show information about a bridge", "uname")
initCompletion()
}
@ -129,6 +132,10 @@ func initCompletion() {
suggestions[1] = []*completion{
{Suggest: cli.Suggest{Text: "group", Description: "target group"}},
{Suggest: cli.Suggest{Text: "light", Description: "target light"}},
{Suggest: cli.Suggest{Text: "scene", Description: "target scene"}},
{Suggest: cli.Suggest{Text: "schedule", Description: "target schedule"}},
{Suggest: cli.Suggest{Text: "sensor", Description: "target sensor"}},
{Suggest: cli.Suggest{Text: "config", Description: "target bridge config"}},
}
for _, sug := range suggestions[1] {
sug.requires = map[int][]string{0: {"delete", "del", "set", "s", "rename", "mv", "dump", "load"}}

67
internal/cli/info.go Normal file

@ -0,0 +1,67 @@
package cli
import (
"github.com/amimof/huego"
"git.tcp.direct/kayos/ziggs/internal/ziggy"
)
func printUpdateInfo(c *huego.Config) {
log.Info().Msgf("Software version: %s", c.SwVersion)
log.Info().Msgf("API version: %s", c.APIVersion)
log.Info().Msgf("Datastore version: %s", c.DatastoreVersion)
log.Info().Msgf("Update state: %v", c.SwUpdate2.State)
log.Info().Msgf("Auto install enabled: %t", c.SwUpdate2.AutoInstall.On)
log.Info().Msgf("Auto install time: %v", c.SwUpdate2.AutoInstall.UpdateTime)
log.Info().Msgf("Last update: %v", c.SwUpdate2.LastInstall)
}
func printNetworkInfo(c *huego.Config) {
log.Info().Msgf("Zigbee channel: %d", c.ZigbeeChannel)
log.Info().Msgf("IP address: %s", c.IPAddress)
log.Info().Msgf("Netmask: %s", c.NetMask)
log.Info().Msgf("Gateway: %s", c.Gateway)
log.Info().Msgf("DHCP enabled: %t", c.Dhcp)
log.Info().Msgf("Proxy address: %s", c.ProxyAddress)
log.Info().Msgf("Proxy port: %d", c.ProxyPort)
}
func printRemoteServicesInfo(c *huego.Config) {
log.Info().Msgf("WAN state: %s", c.InternetService.Internet)
log.Info().Msgf("Remote access: %s", c.InternetService.RemoteAccess)
log.Info().Msgf("NTP: %s", c.InternetService.Time)
log.Info().Msgf("Update server: %s", c.InternetService.SwUpdate)
}
func printPortalInfo(c *huego.Config) {
log.Info().Msgf("Portal connection: %s", c.PortalState.Communication)
log.Info().Msgf("Portal signed on: %t", c.PortalState.SignedOn)
log.Info().Msgf("Portal I/O: %t/%t", c.PortalState.Incoming, c.PortalState.Outgoing)
}
func cmdInfo(br *ziggy.Bridge, args []string) error {
return printBridgeInfo(br)
}
func printBridgeInfo(br *ziggy.Bridge) error {
c, err := br.GetConfig()
if err != nil {
return err
}
log.Info().Msgf("Name: %s", c.Name)
log.Info().Msgf("ID: %s", c.BridgeID)
log.Info().Msgf("MAC: %s", c.Mac)
log.Info().Msgf("Model: %s", c.ModelID)
println()
printUpdateInfo(c)
println()
printNetworkInfo(c)
println()
printRemoteServicesInfo(c)
println()
printPortalInfo(c)
println()
log.Info().Msgf("Local Time: %s", c.LocalTime)
log.Info().Msgf("Link Button Enabled: %t", c.LinkButton)
return nil
}

42
internal/cli/update.go Normal file

@ -0,0 +1,42 @@
package cli
import (
"fmt"
"time"
"github.com/amimof/huego"
"github.com/davecgh/go-spew/spew"
"git.tcp.direct/kayos/ziggs/internal/ziggy"
)
func cmdFirmwareUpdate(br *ziggy.Bridge, args []string) error {
log.Trace().Msg("retrieving bridge config...")
c, err := br.GetConfig()
if err != nil {
return err
}
printUpdateInfo(c)
log.Info().Msg("attempting to trigger a firmware update...")
log.Debug().Msgf("current bridge update state:\n%s", spew.Sdump(c.SwUpdate2))
var resp *huego.Response
if c.SwUpdate2.CheckForUpdate {
return fmt.Errorf("bridge is already set to check for updates")
}
if resp, err = br.UpdateConfig(&huego.Config{SwUpdate2: huego.SwUpdate2{CheckForUpdate: true}}); err == nil {
log.Info().Msgf("response: %v", resp)
}
log.Info().Msg("waiting for bridge to check for updates...")
time.Sleep(5 * time.Second)
log.Trace().Msg("retrieving bridge config...")
var cNew *huego.Config
cNew, err = br.GetConfig()
if err != nil {
return err
}
log.Debug().Msgf("new bridge update state:\n%s", spew.Sdump(cNew.SwUpdate2))
log.Info().Msgf("New software version: %s", c.SwVersion)
log.Info().Msgf("New update state: %v", c.SwUpdate2.State)
return err
}