forked from OpenPDU/openpdu
new source promoted
This commit is contained in:
@@ -1,8 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
// Board definition
|
|
||||||
type Board struct {
|
|
||||||
ID string `mapstructure:"id"`
|
|
||||||
Name string `mapstructure:"name"`
|
|
||||||
Type string `mapstructure:"type"`
|
|
||||||
}
|
|
||||||
@@ -1,78 +1,41 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"encoding/json"
|
||||||
"log"
|
"io/ioutil"
|
||||||
|
|
||||||
"github.com/spf13/viper"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config definition
|
// Configuration def
|
||||||
type Config struct {
|
type Configuration struct {
|
||||||
hostname string `mapstructure:"hostname"`
|
Hostname string `json:"hostname"`
|
||||||
boards []*Board `flow,mapstructure:"boards"`
|
Outlets map[(uint)]Outlet `json:"outlets"`
|
||||||
|
Boards []*Board `json:"boards"`
|
||||||
|
MQTT MQTTConfig `json:"mqtt"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Configuration container
|
func loadConfig(filename string) (Configuration, error) {
|
||||||
var Configuration Config
|
bytes, err := ioutil.ReadFile(filename)
|
||||||
|
|
||||||
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
|
|
||||||
if err != nil { // Handle errors reading the config file
|
|
||||||
log.Printf("Fatal error config file: %s \n", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
if err != nil {
|
||||||
fmt.Printf("unable to decode into config struct, %v", err)
|
return Configuration{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
viper.WriteConfigAs("openpdu_out.yaml")
|
var c Configuration
|
||||||
|
err = json.Unmarshal(bytes, &c)
|
||||||
log.Printf("All settings - hostname: %v \n", Configuration.hostname)
|
if err != nil {
|
||||||
for idx, b := range Configuration.boards {
|
return Configuration{}, err
|
||||||
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() {
|
return c, nil
|
||||||
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")
|
|
||||||
|
|
||||||
|
func saveConfig(c Configuration, filename string) error {
|
||||||
|
bytes, err := json.MarshalIndent(c, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return ioutil.WriteFile(filename, bytes, 0644)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TheConfig def
|
||||||
|
var TheConfig Configuration
|
||||||
|
|||||||
@@ -1,19 +1,38 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
|
||||||
"log"
|
|
||||||
|
|
||||||
"github.com/spf13/viper"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Dictionary definition
|
// Dictionary definition
|
||||||
type Dictionary map[string]interface{}
|
type Dictionary map[string]interface{}
|
||||||
|
|
||||||
|
func initOutlets() {
|
||||||
|
for _, o := range TheConfig.Outlets {
|
||||||
|
o.PowerInitial()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
nam := viper.Get("hostname")
|
var err error
|
||||||
log.Printf("hostname: %v\n", nam)
|
|
||||||
|
var c1 Configuration
|
||||||
|
|
||||||
|
c1 = createMockConfig()
|
||||||
|
|
||||||
|
err = saveConfig(c1, "t.json")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
TheConfig, err = loadConfig("t.json")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = saveConfig(TheConfig, "t1.json")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
startServer()
|
startServer()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://github.com/ColorlibHQ/AdminLTE/archive/v2.4.17.tar.gz
|
// https://github.com/ColorlibHQ/AdminLTE/archive/v2.4.17.tar.gz
|
||||||
|
|||||||
@@ -1,64 +0,0 @@
|
|||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,51 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
113
source/webui.go
113
source/webui.go
@@ -1,113 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/go-macaron/pongo2"
|
|
||||||
"gopkg.in/macaron.v1"
|
|
||||||
)
|
|
||||||
|
|
||||||
func statusPage(ctx *macaron.Context) {
|
|
||||||
ctx.Data["pluglist"] = []Dictionary{
|
|
||||||
{"id": 1, "description": "p1"},
|
|
||||||
{"id": 2, "description": "p2"},
|
|
||||||
{"id": 3, "description": "p3"},
|
|
||||||
{"id": 4, "description": "p4"},
|
|
||||||
{"id": 5, "description": "p5"},
|
|
||||||
{"id": 6, "description": "p6"},
|
|
||||||
{"id": 7, "description": "p7"},
|
|
||||||
{"id": 8, "description": "p8"},
|
|
||||||
}
|
|
||||||
ctx.HTML(200, "status") // 200 is the response code.
|
|
||||||
}
|
|
||||||
|
|
||||||
func jsonStatus(ctx *macaron.Context) {
|
|
||||||
|
|
||||||
p0, err := MyBoard.channelStatus(0)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
p1, err := MyBoard.channelStatus(1)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
p2, err := MyBoard.channelStatus(2)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
p3, err := MyBoard.channelStatus(3)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
p4, err := MyBoard.channelStatus(4)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
p5, err := MyBoard.channelStatus(5)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
p6, err := MyBoard.channelStatus(6)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
p7, err := MyBoard.channelStatus(7)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx.JSON(http.StatusOK, Dictionary{
|
|
||||||
"data": [][]string{
|
|
||||||
{"0", "p0", fmt.Sprint(p0)},
|
|
||||||
{"1", "p1", fmt.Sprint(p1)},
|
|
||||||
{"2", "p2", fmt.Sprint(p2)},
|
|
||||||
{"3", "p3", fmt.Sprint(p3)},
|
|
||||||
{"4", "p4", fmt.Sprint(p4)},
|
|
||||||
{"5", "p5", fmt.Sprint(p5)},
|
|
||||||
{"6", "p6", fmt.Sprint(p6)},
|
|
||||||
{"7", "p7", fmt.Sprint(p7)},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func jsonOutletToggle(ctx *macaron.Context) {
|
|
||||||
id, err := strconv.ParseUint(ctx.Params(":id"), 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
err = MyBoard.channelToggle(uint(id))
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
ctx.JSON(http.StatusOK, Dictionary{
|
|
||||||
"data": "ok",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func startServer() {
|
|
||||||
m := macaron.Classic()
|
|
||||||
m.Use(pongo2.Pongoer())
|
|
||||||
m.Use(macaron.Static("static"))
|
|
||||||
// m.Get("/", myHandler)
|
|
||||||
|
|
||||||
m.Get("/", statusPage)
|
|
||||||
m.Get("/json/status", jsonStatus)
|
|
||||||
m.Post("/json/outlet/:id/toggle", jsonOutletToggle)
|
|
||||||
|
|
||||||
m.Get("/boards", func(ctx *macaron.Context) {
|
|
||||||
ctx.HTML(200, "boards") // 200 is the response code.
|
|
||||||
})
|
|
||||||
|
|
||||||
log.Println("Server is running...")
|
|
||||||
log.Println(http.ListenAndServe("0.0.0.0:4000", m))
|
|
||||||
}
|
|
||||||
41
t/config.go
41
t/config.go
@@ -1,41 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"io/ioutil"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Configuration def
|
|
||||||
type Configuration struct {
|
|
||||||
Hostname string `json:"hostname"`
|
|
||||||
Outlets map[(uint)]Outlet `json:"outlets"`
|
|
||||||
Boards []*Board `json:"boards"`
|
|
||||||
MQTT MQTTConfig `json:"mqtt"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func loadConfig(filename string) (Configuration, error) {
|
|
||||||
bytes, err := ioutil.ReadFile(filename)
|
|
||||||
if err != nil {
|
|
||||||
return Configuration{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var c Configuration
|
|
||||||
err = json.Unmarshal(bytes, &c)
|
|
||||||
if err != nil {
|
|
||||||
return Configuration{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return c, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func saveConfig(c Configuration, filename string) error {
|
|
||||||
bytes, err := json.MarshalIndent(c, "", " ")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return ioutil.WriteFile(filename, bytes, 0644)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TheConfig def
|
|
||||||
var TheConfig Configuration
|
|
||||||
36
t/main.go
36
t/main.go
@@ -1,36 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
// Dictionary definition
|
|
||||||
type Dictionary map[string]interface{}
|
|
||||||
|
|
||||||
func initOutlets() {
|
|
||||||
for _, o := range TheConfig.Outlets {
|
|
||||||
o.PowerInitial()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
var err error
|
|
||||||
|
|
||||||
var c1 Configuration
|
|
||||||
|
|
||||||
c1 = createMockConfig()
|
|
||||||
|
|
||||||
err = saveConfig(c1, "t.json")
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
TheConfig, err = loadConfig("t.json")
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = saveConfig(TheConfig, "t1.json")
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
startServer()
|
|
||||||
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user