event system

This commit is contained in:
Paolo Asperti 2021-09-28 18:59:03 +02:00
parent 6645ea6f5e
commit 9d1ff0fe95
Signed by: paspo
GPG Key ID: 06D46905D19D5182
4 changed files with 51 additions and 4 deletions

View File

@ -3,7 +3,9 @@ package main
import ( import (
"fmt" "fmt"
"git.openpdu.org/OpenPDU/openpdu/events"
"git.openpdu.org/OpenPDU/openpdu/syslog" "git.openpdu.org/OpenPDU/openpdu/syslog"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -16,4 +18,10 @@ func init() {
if err != nil { // Handle errors reading the config file if err != nil { // Handle errors reading the config file
syslog.Err(fmt.Sprintf("Can't read config file: %s \n", err.Error())) syslog.Err(fmt.Sprintf("Can't read config file: %s \n", err.Error()))
} }
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
//The Viper configuration has changed to perform the responding operation
fmt.Println("Config file changed:", e.Name)
events.FireEvent("config_changed")
})
} }

28
src/events/events.go Normal file
View File

@ -0,0 +1,28 @@
package events
var handlers map[string][]func()
func init() {
handlers = make(map[string][]func())
}
func AddListener(event string, f func()) {
l, isPresent := handlers[event]
if !isPresent {
handlers[event] = []func(){
f,
}
} else {
handlers[event] = append(l, f)
}
}
func FireEvent(event string) {
l, isPresent := handlers[event]
if !isPresent {
return
}
for _, v := range l {
v()
}
}

View File

@ -12,6 +12,7 @@ require (
golang.org/x/image v0.0.0-20201208152932-35266b937fa6 golang.org/x/image v0.0.0-20201208152932-35266b937fa6
gopkg.in/macaron.v1 v1.4.0 gopkg.in/macaron.v1 v1.4.0
periph.io/x/periph v3.6.7+incompatible periph.io/x/periph v3.6.7+incompatible
github.com/fsnotify/fsnotify v1.4.7
) )
replace git.openpdu.org/OpenPDU/openpdu => ./src replace git.openpdu.org/OpenPDU/openpdu => ./src

View File

@ -1,6 +1,7 @@
package ups package ups
import ( import (
"git.openpdu.org/OpenPDU/openpdu/events"
"git.openpdu.org/OpenPDU/openpdu/syslog" "git.openpdu.org/OpenPDU/openpdu/syslog"
nut "github.com/robbiet480/go.nut" nut "github.com/robbiet480/go.nut"
"github.com/spf13/viper" "github.com/spf13/viper"
@ -10,11 +11,18 @@ var upsClient nut.Client
var ups nut.UPS var ups nut.UPS
var Vars map[string]nut.Variable var Vars map[string]nut.Variable
var defaults = map[string]interface{}{
"Ups.Host": "localhost",
"Ups.Name": "ups",
"Ups.Username": "",
"Ups.Password": "",
}
func init() { func init() {
viper.SetDefault("Ups.Host", "localhost") for k, v := range defaults {
viper.SetDefault("Ups.Name", "ups") viper.SetDefault(k, v)
viper.SetDefault("Ups.Username", "") }
viper.SetDefault("Ups.Password", "") events.AddListener("config_changed", Reconfigure)
} }
func UpsConnect() { func UpsConnect() {
@ -51,6 +59,7 @@ func UpsConnect() {
if u.Name == viper.GetString("Ups.Name") { if u.Name == viper.GetString("Ups.Name") {
ups = u ups = u
syslog.Notice("UPS connected") syslog.Notice("UPS connected")
events.FireEvent("ups_connected")
UpsReadVars() UpsReadVars()
} }
} }
@ -67,5 +76,6 @@ func Reconfigure() {
ups = nut.UPS{} ups = nut.UPS{}
upsClient = nut.Client{} upsClient = nut.Client{}
syslog.Notice("UPS disconnected") syslog.Notice("UPS disconnected")
events.FireEvent("ups_disconnected")
go UpsConnect() go UpsConnect()
} }