forked from OpenPDU/openpdu
I'm stuck
This commit is contained in:
8
source/boards.go
Normal file
8
source/boards.go
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
// Board definition
|
||||||
|
type Board struct {
|
||||||
|
ID string `mapstructure:"id"`
|
||||||
|
Name string `mapstructure:"name"`
|
||||||
|
Type string `mapstructure:"type"`
|
||||||
|
}
|
||||||
@@ -1,14 +1,24 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
func readConfig() {
|
// Config definition
|
||||||
viper.SetConfigName("openpdu.yaml") // name of config file (without extension)
|
type Config struct {
|
||||||
viper.SetConfigType("yaml")
|
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("/etc/openpdu/") // path to look for the config file in
|
||||||
viper.AddConfigPath(".") // optionally look for config in the working directory
|
viper.AddConfigPath(".") // optionally look for config in the working directory
|
||||||
err := viper.ReadInConfig() // Find and read the config file
|
err := viper.ReadInConfig() // Find and read the config file
|
||||||
@@ -16,5 +26,53 @@ func readConfig() {
|
|||||||
log.Printf("Fatal error config file: %s \n", err)
|
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")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -72,7 +72,7 @@ func (b I2CBoard) channelToggle(ch uint) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func initI2C() {
|
func init() {
|
||||||
// Make sure periph is initialized.
|
// Make sure periph is initialized.
|
||||||
if _, err := host.Init(); err != nil {
|
if _, err := host.Init(); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
@@ -101,14 +101,4 @@ func initI2C() {
|
|||||||
MyBoard.i2cdev = *mydevice
|
MyBoard.i2cdev = *mydevice
|
||||||
MyBoard.data = make([]byte, (MyBoard.channels-1)/8+1)
|
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)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,26 +1,18 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// "encoding/json"
|
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
// "periph.io/x/periph"
|
|
||||||
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Dictionary aaa
|
// Dictionary definition
|
||||||
type Dictionary map[string]interface{}
|
type Dictionary map[string]interface{}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
readConfig()
|
|
||||||
|
|
||||||
nam := viper.Get("hostname")
|
nam := viper.Get("hostname")
|
||||||
log.Printf("hostname: %v\n", nam)
|
log.Printf("hostname: %v\n", nam)
|
||||||
|
|
||||||
initI2C()
|
|
||||||
|
|
||||||
startServer()
|
startServer()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
64
source/outlets.go
Normal file
64
source/outlets.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
51
source/virtualoutlet.go
Normal file
51
source/virtualoutlet.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user