package main import ( "fmt" nut "github.com/robbiet480/go.nut" "github.com/spf13/viper" ) var upsClient nut.Client var ups nut.UPS var upsVars map[string]nut.Variable func init() { viper.SetDefault("UpsHost", "localhost") viper.SetDefault("UpsName", "ups") viper.SetDefault("UpsUsername", "") viper.SetDefault("UpsPassword", "") } func UpsConnect() { var username, password string var connectErr, authenticationError error var u nut.UPS upsClient, connectErr = nut.Connect(viper.GetString("UpsHost")) if connectErr != nil { fmt.Print(connectErr) return } if username = viper.GetString("UpsUsername"); username == "" { return } if password = viper.GetString("UpsPassword"); password == "" { return } _, authenticationError = upsClient.Authenticate(username, password) if authenticationError != nil { fmt.Print(authenticationError) return } upsList, listErr := upsClient.GetUPSList() if listErr != nil { fmt.Print(listErr) return } for _, u = range upsList { if u.Name == viper.GetString("UpsName") { ups = u fmt.Print("UPS connected") UpsReadVars() } } } func UpsReadVars() { upsVars = map[string]nut.Variable{} for _, v := range ups.Variables { upsVars[v.Name] = v } } func UpsReconfigure() { ups = nut.UPS{} upsClient = nut.Client{} fmt.Print("UPS disconnected") go UpsConnect() }