forked from OpenPDU/openpdu
138 lines
2.9 KiB
Go
138 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
// "encoding/json"
|
|
"github.com/go-macaron/pongo2"
|
|
|
|
"gopkg.in/macaron.v1"
|
|
"net/http"
|
|
"log"
|
|
// "periph.io/x/periph"
|
|
"periph.io/x/periph/host"
|
|
"periph.io/x/periph/conn/i2c"
|
|
"periph.io/x/periph/conn/i2c/i2creg"
|
|
"time"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
type Dictionary map[string]interface{}
|
|
|
|
func readConfig() {
|
|
viper.SetConfigName("openpdu.yaml") // name of config file (without extension)
|
|
viper.SetConfigType("yaml")
|
|
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","openpdu")
|
|
}
|
|
|
|
func initI2C() {
|
|
// Make sure periph is initialized.
|
|
if _, err := host.Init(); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// Use i2creg I²C bus registry to find the first available I²C bus.
|
|
b, err := i2creg.Open("/dev/i2c-0")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer b.Close()
|
|
|
|
// bus 0
|
|
// Dev is a valid conn.Conn.
|
|
d := &i2c.Dev{Addr: 0x27, Bus: b}
|
|
|
|
// Send a command 0x10 and expect a 5 bytes reply.
|
|
// write := []byte{0x10}
|
|
write := []byte{0x0}
|
|
// read := make([]byte, 5)
|
|
// if err := d.Tx(write, read); err != nil {
|
|
if _, err := d.Write(write); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
write := []byte{0x0A}
|
|
read := make([]byte, 5)
|
|
if err := d.Tx(write, read); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
log.Printf("%v\n", read)
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// func myHandler(ctx *macaron.Context) string {
|
|
// ctx.Data["name"] = "jeremy"
|
|
// ctx.HTML(200, "hello") // 200 is the response code.
|
|
|
|
// return "The request path is: " + ctx.Req.RequestURI
|
|
// }
|
|
|
|
func startServer() {
|
|
m := macaron.Classic()
|
|
m.Use(pongo2.Pongoer())
|
|
m.Use(macaron.Static("static"))
|
|
// m.Get("/", myHandler)
|
|
|
|
m.Get("/", func(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.
|
|
})
|
|
|
|
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))
|
|
}
|
|
|
|
func main() {
|
|
|
|
readConfig()
|
|
|
|
nam := viper.Get("name")
|
|
log.Printf("name: %v\n", nam)
|
|
|
|
|
|
initI2C()
|
|
startServer()
|
|
}
|
|
|
|
|
|
|
|
// https://github.com/ColorlibHQ/AdminLTE/archive/v2.4.17.tar.gz
|
|
|
|
/* TODO
|
|
|
|
- config reset gpio
|
|
- classi per board
|
|
- classi per outlet
|
|
- fai funzionare toggle
|
|
- scan i2c
|
|
- impostazioni log
|
|
- impostazioni mqtt
|
|
|
|
|
|
*/ |