2020-12-27 21:29:16 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
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() {
|
2021-01-04 11:28:20 +00:00
|
|
|
viper.SetDefault("Ups.Host", "localhost")
|
|
|
|
viper.SetDefault("Ups.Name", "ups")
|
|
|
|
viper.SetDefault("Ups.Username", "")
|
|
|
|
viper.SetDefault("Ups.Password", "")
|
2020-12-27 21:29:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func UpsConnect() {
|
|
|
|
var username, password string
|
|
|
|
var connectErr, authenticationError error
|
|
|
|
var u nut.UPS
|
|
|
|
|
2021-01-04 11:28:20 +00:00
|
|
|
upsClient, connectErr = nut.Connect(viper.GetString("Ups.Host"))
|
2020-12-27 21:29:16 +00:00
|
|
|
if connectErr != nil {
|
2020-12-30 13:47:36 +00:00
|
|
|
logErr(connectErr.Error())
|
2020-12-27 21:29:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-04 11:28:20 +00:00
|
|
|
if username = viper.GetString("Ups.Username"); username == "" {
|
2020-12-27 21:29:16 +00:00
|
|
|
return
|
|
|
|
}
|
2021-01-04 11:28:20 +00:00
|
|
|
if password = viper.GetString("Ups.Password"); password == "" {
|
2020-12-27 21:29:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, authenticationError = upsClient.Authenticate(username, password)
|
|
|
|
if authenticationError != nil {
|
2020-12-30 13:47:36 +00:00
|
|
|
logErr(authenticationError.Error())
|
2020-12-27 21:29:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
upsList, listErr := upsClient.GetUPSList()
|
|
|
|
if listErr != nil {
|
2020-12-30 13:47:36 +00:00
|
|
|
logErr(listErr.Error())
|
2020-12-27 21:29:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, u = range upsList {
|
2021-01-04 11:28:20 +00:00
|
|
|
if u.Name == viper.GetString("Ups.Name") {
|
2020-12-27 21:29:16 +00:00
|
|
|
ups = u
|
2020-12-30 13:47:36 +00:00
|
|
|
logNotice("UPS connected")
|
2020-12-27 21:29:16 +00:00
|
|
|
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{}
|
2020-12-30 13:47:36 +00:00
|
|
|
logNotice("UPS disconnected")
|
2020-12-27 21:29:16 +00:00
|
|
|
go UpsConnect()
|
|
|
|
}
|