event system
This commit is contained in:
parent
6645ea6f5e
commit
9d1ff0fe95
@ -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
28
src/events/events.go
Normal 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()
|
||||||
|
}
|
||||||
|
}
|
@ -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
|
||||||
|
@ -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()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user