Compare commits
2 Commits
6827006f94
...
6591ca67d3
Author | SHA1 | Date | |
---|---|---|---|
6591ca67d3 | |||
99e29ec2d5 |
@ -5,7 +5,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
ccc "th7/config"
|
cfg "th7/config"
|
||||||
"th7/data/config"
|
"th7/data/config"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@ -28,9 +28,6 @@ func (g *GinAdapter) getConfigByID(id int) (config.Channel, error) {
|
|||||||
|
|
||||||
// check if a supplied key string is in the device struct (TH7)
|
// check if a supplied key string is in the device struct (TH7)
|
||||||
func configDeviceIsValidKey(key string) bool {
|
func configDeviceIsValidKey(key string) bool {
|
||||||
deviceKeys := []string{
|
|
||||||
"debug", "nolog", "noweb", "led",
|
|
||||||
}
|
|
||||||
|
|
||||||
key = strings.ToLower(key)
|
key = strings.ToLower(key)
|
||||||
|
|
||||||
@ -90,10 +87,6 @@ func (g *GinAdapter) validConfigChannel(id int) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func configChannelIsValidKey(key string) bool {
|
func configChannelIsValidKey(key string) bool {
|
||||||
// will add more later
|
|
||||||
channelKeys := []string{
|
|
||||||
"thermocouple", "gain", "offset",
|
|
||||||
}
|
|
||||||
|
|
||||||
key = strings.ToLower(key)
|
key = strings.ToLower(key)
|
||||||
|
|
||||||
@ -108,9 +101,11 @@ func configChannelIsValidKey(key string) bool {
|
|||||||
|
|
||||||
func (g *GinAdapter) configChannelUpdateValue(id int, key string, value string) error {
|
func (g *GinAdapter) configChannelUpdateValue(id int, key string, value string) error {
|
||||||
|
|
||||||
|
var cfgChannelPtr *config.Channel
|
||||||
|
|
||||||
key = strings.ToLower(key)
|
key = strings.ToLower(key)
|
||||||
value = strings.ToLower(value)
|
value = strings.ToLower(value)
|
||||||
var cfgChannelPtr *config.Channel
|
|
||||||
foundChannel := false
|
foundChannel := false
|
||||||
|
|
||||||
// find the channel, given and checked by id
|
// find the channel, given and checked by id
|
||||||
@ -127,7 +122,7 @@ func (g *GinAdapter) configChannelUpdateValue(id int, key string, value string)
|
|||||||
|
|
||||||
switch key {
|
switch key {
|
||||||
case "thermocouple":
|
case "thermocouple":
|
||||||
tc, err := ccc.GetThermocoupleType(value)
|
tc, err := cfg.GetThermocoupleType(value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -159,17 +154,13 @@ func (g *GinAdapter) GetConfigChannelByIdHandler(c *gin.Context) {
|
|||||||
id, err := strconv.Atoi(c.Param("id"))
|
id, err := strconv.Atoi(c.Param("id"))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{
|
c.String(http.StatusBadRequest, err.Error())
|
||||||
"error": err,
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
channel, err := g.getConfigByID(id)
|
channel, err := g.getConfigByID(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{
|
c.String(http.StatusBadRequest, err.Error())
|
||||||
"error": err,
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -202,16 +193,13 @@ func (g *GinAdapter) GetConfigDBHandler(c *gin.Context) {
|
|||||||
var timestamp string
|
var timestamp string
|
||||||
|
|
||||||
if g.cfg.API[0].Restricted {
|
if g.cfg.API[0].Restricted {
|
||||||
c.JSON(http.StatusForbidden, gin.H{
|
c.String(http.StatusForbidden, ErrConfigRestrictedDB.Error())
|
||||||
"error": ErrConfigRestrictedDB,
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
timestamp = g.getTimestamp()
|
timestamp = g.getTimestamp()
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
// "db": g.getConfigDB(),
|
|
||||||
"db": g.cfg.DB,
|
"db": g.cfg.DB,
|
||||||
"time": timestamp,
|
"time": timestamp,
|
||||||
})
|
})
|
||||||
|
@ -31,17 +31,13 @@ func (g *GinAdapter) GetChannelByIDHandler(c *gin.Context) {
|
|||||||
id, err := strconv.Atoi(c.Param("id"))
|
id, err := strconv.Atoi(c.Param("id"))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{
|
c.String(http.StatusBadRequest, err.Error())
|
||||||
"error": err,
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
channel, err := g.getChannelByID(id)
|
channel, err := g.getChannelByID(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{
|
c.String(http.StatusBadRequest, err.Error())
|
||||||
"error": err,
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,8 +49,6 @@ func NewGinAdapter(corePort ports.CorePort, cfg config.Config) *GinAdapter {
|
|||||||
adapter.router.POST("/config/channel/:id", adapter.SetConfigChannelByIdHandler)
|
adapter.router.POST("/config/channel/:id", adapter.SetConfigChannelByIdHandler)
|
||||||
adapter.router.POST("/config/db", adapter.SetConfigDBHandler)
|
adapter.router.POST("/config/db", adapter.SetConfigDBHandler)
|
||||||
|
|
||||||
//adapter.router.GET("/time", adapter.TimestampHandler)
|
|
||||||
|
|
||||||
adapter.router.LoadHTMLGlob("./templates/*.tmpl")
|
adapter.router.LoadHTMLGlob("./templates/*.tmpl")
|
||||||
adapter.router.Static("/assets", "./static")
|
adapter.router.Static("/assets", "./static")
|
||||||
adapter.router.GET("/", adapter.IndexHandler)
|
adapter.router.GET("/", adapter.IndexHandler)
|
||||||
|
@ -7,6 +7,16 @@ type PostRequest struct {
|
|||||||
Value string `form:"value" binding:"required"`
|
Value string `form:"value" binding:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
channelKeys = []string{
|
||||||
|
"thermocouple", "gain", "offset",
|
||||||
|
}
|
||||||
|
|
||||||
|
deviceKeys = []string{
|
||||||
|
"debug", "nolog", "noweb", "led",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
func (g *GinAdapter) getTimestamp() string {
|
func (g *GinAdapter) getTimestamp() string {
|
||||||
return time.Now().Format(time.RFC1123)
|
return time.Now().Format(time.RFC1123)
|
||||||
}
|
}
|
||||||
|
12
main.go
12
main.go
@ -54,24 +54,20 @@ func main() {
|
|||||||
}
|
}
|
||||||
defer dbPort.Close()
|
defer dbPort.Close()
|
||||||
|
|
||||||
// if noweb is false, then start web service
|
|
||||||
if !cfg.Board.NoWeb && cfg.API[0].Enabled {
|
|
||||||
apiPort = http.NewGinAdapter(corePort, cfg)
|
|
||||||
go apiPort.Run()
|
|
||||||
}
|
|
||||||
|
|
||||||
color.Set(color.FgHiRed)
|
color.Set(color.FgHiRed)
|
||||||
fmt.Printf("Started on: %s\n", time.Now().Format(time.DateTime))
|
fmt.Printf("Started on: %s\n", time.Now().Format(time.DateTime))
|
||||||
color.Unset()
|
color.Unset()
|
||||||
|
|
||||||
|
// if noweb is false and HTTP/REST API is enabled then start web service
|
||||||
if !cfg.Board.NoWeb && cfg.API[0].Enabled {
|
if !cfg.Board.NoWeb && cfg.API[0].Enabled {
|
||||||
|
apiPort = http.NewGinAdapter(corePort, cfg)
|
||||||
|
go apiPort.Run()
|
||||||
|
|
||||||
color.Set(color.FgHiGreen)
|
color.Set(color.FgHiGreen)
|
||||||
fmt.Printf("TH7 API is live on http://localhost:%d/\n", cfg.API[0].Port)
|
fmt.Printf("TH7 API is live on http://localhost:%d/\n", cfg.API[0].Port)
|
||||||
color.Unset()
|
color.Unset()
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(cfg.API[0])
|
|
||||||
|
|
||||||
sig := <-kill
|
sig := <-kill
|
||||||
log.Printf("Caught signal %v", sig)
|
log.Printf("Caught signal %v", sig)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user