diff --git a/source/boards.go b/source/boards.go new file mode 100644 index 0000000..2423b41 --- /dev/null +++ b/source/boards.go @@ -0,0 +1,8 @@ +package main + +// Board definition +type Board struct { + ID string `mapstructure:"id"` + Name string `mapstructure:"name"` + Type string `mapstructure:"type"` +} diff --git a/source/config.go b/source/config.go index e15b9a1..4d428c4 100644 --- a/source/config.go +++ b/source/config.go @@ -1,14 +1,24 @@ package main import ( + "fmt" "log" "github.com/spf13/viper" ) -func readConfig() { - viper.SetConfigName("openpdu.yaml") // name of config file (without extension) - viper.SetConfigType("yaml") +// Config definition +type Config struct { + hostname string `mapstructure:"hostname"` + boards []*Board `flow,mapstructure:"boards"` +} + +// Configuration container +var Configuration Config + +func init() { + viper.SetConfigName("openpdu") // name of config file (without extension) + viper.SetConfigType("yaml") // type of config file viper.AddConfigPath("/etc/openpdu/") // path to look for the config file in viper.AddConfigPath(".") // optionally look for config in the working directory err := viper.ReadInConfig() // Find and read the config file @@ -16,5 +26,53 @@ func readConfig() { log.Printf("Fatal error config file: %s \n", err) } - viper.SetDefault("hostname", "openpdu") + viper.SetDefault("hostname", "openpdu1") + br := viper.Get("boards").([]interface{}) + for k, v := range br { + log.Printf("Key: %v", k) + log.Printf(" Value: %v", v) + var ba Board + viper.UnmarshalKey("boards."+string(k), ba) + log.Printf(" Valueid: %v", ba.ID) + } + + log.Printf("basdo: %v", viper.Get("boards[0]")) + log.Printf("todo: %v", viper.AllSettings()) + + Configuration := Config{} + err = viper.Unmarshal(&Configuration) + if err != nil { + fmt.Printf("unable to decode into config struct, %v", err) + } + + viper.WriteConfigAs("openpdu_out.yaml") + + log.Printf("All settings - hostname: %v \n", Configuration.hostname) + for idx, b := range Configuration.boards { + log.Printf("b %v: %v (%v)", idx, b.Name, b.Type) + + } + // var b []Board + // var brd Board + // b = viper.Get("boards").([]Board) + // for brd := range viper.Get("boards") { + // log.Printf("brd: %v", brd) + // } + // log.Printf("bo: %v", viper.Get("boards")) + // log.Printf("bo: %v", b) + log.Printf("cfg: %v", Configuration) + log.Printf("cfgb: %v", Configuration.boards) +} + +func writeConfig() { + viper.SetConfigName("openpdu") // name of config file (without extension) + viper.SetConfigType("yaml") + viper.AddConfigPath("/etc/openpdu/") // path to look for the config file in + viper.SetDefault("hostname", "openpdu") + err := viper.WriteConfig() + if err != nil { // Handle errors reading the config file + log.Printf("Fatato error config file: %s \n", err) + } + log.Printf("ok credo \n") + } diff --git a/source/i2c.go b/source/i2c.go index db93da2..201c2ba 100644 --- a/source/i2c.go +++ b/source/i2c.go @@ -72,7 +72,7 @@ func (b I2CBoard) channelToggle(ch uint) error { return nil } -func initI2C() { +func init() { // Make sure periph is initialized. if _, err := host.Init(); err != nil { log.Fatal(err) @@ -101,14 +101,4 @@ func initI2C() { MyBoard.i2cdev = *mydevice MyBoard.data = make([]byte, (MyBoard.channels-1)/8+1) - go func() { - var i uint - for i = 0; i < 8; i++ { - v, err := MyBoard.channelStatus(i) - if err != nil { - log.Fatal(err) - } - log.Printf("Channel %d status: %v", i, v) - } - }() } diff --git a/source/main.go b/source/main.go index c88bc81..5e1c552 100644 --- a/source/main.go +++ b/source/main.go @@ -1,26 +1,18 @@ package main import ( - // "encoding/json" "log" - // "periph.io/x/periph" - "github.com/spf13/viper" ) -// Dictionary aaa +// Dictionary definition type Dictionary map[string]interface{} func main() { - - readConfig() - nam := viper.Get("hostname") log.Printf("hostname: %v\n", nam) - initI2C() - startServer() } diff --git a/source/outlets.go b/source/outlets.go new file mode 100644 index 0000000..d0940f7 --- /dev/null +++ b/source/outlets.go @@ -0,0 +1,64 @@ +package main + +import ( + "log" + + "github.com/spf13/viper" +) + +// Outlet definition +type Outlet interface { + On() error + Off() error + Toggle() (bool, error) + Name() (string, error) + Status() (bool, error) + Location() string + Board() Board + Channel() uint +} + +type config struct { + outlets []g +} +type g struct { + n string + k string +} + +func inita() { + viper.SetConfigName("openpdu") + viper.SetConfigType("json") + viper.AddConfigPath(".") + viper.ReadInConfig() + + viper.Set("sboards[0].name", "ciao") + viper.Set("sboards[0].type", "virtual") + viper.Set("sboards[1].name", "ciao1") + viper.Set("sboards[1].type", "virtual1") + viper.WriteConfigAs("openpdu_out.json") + + allSettings := viper.AllSettings() + log.Printf("All settings: %v \n", allSettings) +} + +func inzxcit() { + var err error + var C = config{ + outlets: []g{ + {n: "pio1", k: "pao1"}, + {n: "pio2", k: "pao2"}, + {n: "pio3", k: "pao3"}, + }, + } + + // err = viper.Marshal(&C) + // if err != nil { + // log.Printf("unable to decode into struct, %v", err) + // } + + err = viper.Unmarshal(&C) + if err != nil { + log.Printf("unable to decode into struct, %v", err) + } +} diff --git a/source/virtualoutlet.go b/source/virtualoutlet.go new file mode 100644 index 0000000..027d49b --- /dev/null +++ b/source/virtualoutlet.go @@ -0,0 +1,51 @@ +package main + +// VirtualOutlet definition +type VirtualOutlet struct { + name string + status bool + location string +} + +// On - Switch on the virtual outlet +func (o VirtualOutlet) On() error { + o.status = true + return nil +} + +// Off - Switch off the virtual outlet +func (o VirtualOutlet) Off() error { + o.status = false + return nil +} + +// Toggle - Toggle the virtual outlet and return actual status +func (o VirtualOutlet) Toggle() (bool, error) { + o.status = !o.status + return o.status, nil +} + +// Name - Returns the virtual outlet name +func (o VirtualOutlet) Name() (string, error) { + return o.name, nil +} + +// Status - Returns the virtual outlet status +func (o VirtualOutlet) Status() (bool, error) { + return o.status, nil +} + +// Location - Returns the virtual outlet location (set in configuration) +func (o VirtualOutlet) Location() string { + return o.location +} + +// Board - There's no physical board, so it's always an empty Board +func (o VirtualOutlet) Board() Board { + return Board{} +} + +// Channel - There's no physical channel, so it's always zero +func (o VirtualOutlet) Channel() uint { + return 0 +}