openpdu/src/ups/ups.go

91 lines
1.9 KiB
Go
Raw Normal View History

2021-01-09 18:38:57 +00:00
package ups
2020-12-27 21:29:16 +00:00
import (
2021-09-28 16:59:03 +00:00
"git.openpdu.org/OpenPDU/openpdu/events"
2021-01-09 18:38:57 +00:00
"git.openpdu.org/OpenPDU/openpdu/syslog"
2020-12-27 21:29:16 +00:00
nut "github.com/robbiet480/go.nut"
"github.com/spf13/viper"
)
var upsClient nut.Client
var ups nut.UPS
2021-01-09 18:38:57 +00:00
var Vars map[string]nut.Variable
2021-10-01 15:45:37 +00:00
var Connected bool
2020-12-27 21:29:16 +00:00
2021-09-28 16:59:03 +00:00
var defaults = map[string]interface{}{
"Ups.Host": "localhost",
2021-10-01 15:45:37 +00:00
"Ups.Port": 3493, // not yet implemented in github.com/robbiet480/go.nut, see this PR https://github.com/robbiet480/go.nut/pull/5
2021-09-28 16:59:03 +00:00
"Ups.Name": "ups",
"Ups.Username": "",
"Ups.Password": "",
}
2020-12-27 21:29:16 +00:00
func init() {
2021-09-28 16:59:03 +00:00
for k, v := range defaults {
viper.SetDefault(k, v)
}
2021-10-01 15:45:37 +00:00
Connected = false
2021-09-28 16:59:03 +00:00
events.AddListener("config_changed", Reconfigure)
2020-12-27 21:29:16 +00:00
}
func UpsConnect() {
var username, password string
var connectErr, authenticationError error
var u nut.UPS
2021-10-01 15:45:37 +00:00
upsClient, connectErr = nut.Connect(viper.GetString("Ups.Host"), viper.GetInt("Ups.Port"))
2020-12-27 21:29:16 +00:00
if connectErr != nil {
2021-01-09 18:38:57 +00:00
syslog.Err(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 {
2021-01-09 18:38:57 +00:00
syslog.Err(authenticationError.Error())
2020-12-27 21:29:16 +00:00
return
}
upsList, listErr := upsClient.GetUPSList()
if listErr != nil {
2021-01-09 18:38:57 +00:00
syslog.Err(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
2021-10-01 15:45:37 +00:00
Connected = true
2021-01-09 18:38:57 +00:00
syslog.Notice("UPS connected")
2021-09-28 16:59:03 +00:00
events.FireEvent("ups_connected")
2020-12-27 21:29:16 +00:00
UpsReadVars()
}
}
}
func UpsReadVars() {
2021-01-09 18:38:57 +00:00
Vars = map[string]nut.Variable{}
2020-12-27 21:29:16 +00:00
for _, v := range ups.Variables {
2021-01-09 18:38:57 +00:00
Vars[v.Name] = v
2020-12-27 21:29:16 +00:00
}
}
2021-01-09 18:38:57 +00:00
func Reconfigure() {
2020-12-27 21:29:16 +00:00
ups = nut.UPS{}
upsClient = nut.Client{}
2021-01-09 18:38:57 +00:00
syslog.Notice("UPS disconnected")
2021-09-28 16:59:03 +00:00
events.FireEvent("ups_disconnected")
2021-10-01 15:45:37 +00:00
Connected = false
for key, _ := range Vars {
delete(Vars, key)
}
// Vars = make(map[string]nut.Variable)
2020-12-27 21:29:16 +00:00
go UpsConnect()
}