Added basic multidevice support
This commit is contained in:
parent
d0b0aba30d
commit
7feeacea42
7 changed files with 505 additions and 186 deletions
36
main.go
36
main.go
|
|
@ -9,6 +9,8 @@ import (
|
|||
"log/slog"
|
||||
"os"
|
||||
"path"
|
||||
"slices"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
const VERSION = "v1"
|
||||
|
|
@ -23,7 +25,7 @@ var (
|
|||
initLogger *slog.Logger
|
||||
logger *slog.Logger
|
||||
|
||||
device RouterosDevice
|
||||
devices map[string]RouterosDevice
|
||||
)
|
||||
|
||||
func init() { // {{{
|
||||
|
|
@ -73,13 +75,45 @@ func initLogging(config Config) *slog.Logger { // {{{
|
|||
func main() {
|
||||
initLogger.Info("application", "version", VERSION)
|
||||
|
||||
devices = make(map[string]RouterosDevice)
|
||||
/*
|
||||
device.Host = config.Device.Address
|
||||
device.Port = config.Device.Port
|
||||
device.Username = config.Device.Username
|
||||
device.Password = config.Device.Password
|
||||
device.Timeout = config.Device.Timeout
|
||||
device.Init()
|
||||
*/
|
||||
|
||||
registerWebserverHandlers()
|
||||
startWebserver()
|
||||
}
|
||||
|
||||
func routerosDevice(name string) (dev RouterosDevice, err error) {
|
||||
var found bool
|
||||
if dev, found = devices[name]; found {
|
||||
logger.Debug("routeros", "op", "connection", "name", name, "cached", true)
|
||||
return
|
||||
}
|
||||
|
||||
i := slices.IndexFunc(config.Devices, func(d Device) bool {
|
||||
return d.Name == name
|
||||
})
|
||||
if i == -1 {
|
||||
err = fmt.Errorf("Unknown device '%s'", name)
|
||||
logger.Error("routeros", "op", "connection", "error", err)
|
||||
return
|
||||
}
|
||||
|
||||
logger.Debug("routeros", "name", name, "cached", false)
|
||||
confDev := config.Devices[i]
|
||||
dev.Host = confDev.Address
|
||||
dev.Port = confDev.Port
|
||||
dev.Username = confDev.Username
|
||||
dev.Password = confDev.Password
|
||||
dev.Timeout = confDev.Timeout
|
||||
dev.Init()
|
||||
devices[name] = dev
|
||||
|
||||
return
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue