Compare commits

..

No commits in common. "6591ca67d36d9f2234aac4ae1b28ed8194dddbaf" and "6827006f94a4eaeb7fe40cdd4b6e543b3cb90574" have entirely different histories.

5 changed files with 36 additions and 24 deletions

View File

@ -5,7 +5,7 @@ import (
"net/http"
"strconv"
"strings"
cfg "th7/config"
ccc "th7/config"
"th7/data/config"
"github.com/gin-gonic/gin"
@ -28,6 +28,9 @@ func (g *GinAdapter) getConfigByID(id int) (config.Channel, error) {
// check if a supplied key string is in the device struct (TH7)
func configDeviceIsValidKey(key string) bool {
deviceKeys := []string{
"debug", "nolog", "noweb", "led",
}
key = strings.ToLower(key)
@ -87,6 +90,10 @@ func (g *GinAdapter) validConfigChannel(id int) bool {
}
func configChannelIsValidKey(key string) bool {
// will add more later
channelKeys := []string{
"thermocouple", "gain", "offset",
}
key = strings.ToLower(key)
@ -101,11 +108,9 @@ func configChannelIsValidKey(key string) bool {
func (g *GinAdapter) configChannelUpdateValue(id int, key string, value string) error {
var cfgChannelPtr *config.Channel
key = strings.ToLower(key)
value = strings.ToLower(value)
var cfgChannelPtr *config.Channel
foundChannel := false
// find the channel, given and checked by id
@ -122,7 +127,7 @@ func (g *GinAdapter) configChannelUpdateValue(id int, key string, value string)
switch key {
case "thermocouple":
tc, err := cfg.GetThermocoupleType(value)
tc, err := ccc.GetThermocoupleType(value)
if err != nil {
return err
}
@ -154,13 +159,17 @@ func (g *GinAdapter) GetConfigChannelByIdHandler(c *gin.Context) {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.String(http.StatusBadRequest, err.Error())
c.JSON(http.StatusBadRequest, gin.H{
"error": err,
})
return
}
channel, err := g.getConfigByID(id)
if err != nil {
c.String(http.StatusBadRequest, err.Error())
c.JSON(http.StatusBadRequest, gin.H{
"error": err,
})
return
}
@ -193,13 +202,16 @@ func (g *GinAdapter) GetConfigDBHandler(c *gin.Context) {
var timestamp string
if g.cfg.API[0].Restricted {
c.String(http.StatusForbidden, ErrConfigRestrictedDB.Error())
c.JSON(http.StatusForbidden, gin.H{
"error": ErrConfigRestrictedDB,
})
return
}
timestamp = g.getTimestamp()
c.JSON(http.StatusOK, gin.H{
// "db": g.getConfigDB(),
"db": g.cfg.DB,
"time": timestamp,
})

View File

@ -31,13 +31,17 @@ func (g *GinAdapter) GetChannelByIDHandler(c *gin.Context) {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.String(http.StatusBadRequest, err.Error())
c.JSON(http.StatusBadRequest, gin.H{
"error": err,
})
return
}
channel, err := g.getChannelByID(id)
if err != nil {
c.String(http.StatusBadRequest, err.Error())
c.JSON(http.StatusBadRequest, gin.H{
"error": err,
})
return
}

View File

@ -49,6 +49,8 @@ func NewGinAdapter(corePort ports.CorePort, cfg config.Config) *GinAdapter {
adapter.router.POST("/config/channel/:id", adapter.SetConfigChannelByIdHandler)
adapter.router.POST("/config/db", adapter.SetConfigDBHandler)
//adapter.router.GET("/time", adapter.TimestampHandler)
adapter.router.LoadHTMLGlob("./templates/*.tmpl")
adapter.router.Static("/assets", "./static")
adapter.router.GET("/", adapter.IndexHandler)

View File

@ -7,16 +7,6 @@ type PostRequest struct {
Value string `form:"value" binding:"required"`
}
var (
channelKeys = []string{
"thermocouple", "gain", "offset",
}
deviceKeys = []string{
"debug", "nolog", "noweb", "led",
}
)
func (g *GinAdapter) getTimestamp() string {
return time.Now().Format(time.RFC1123)
}

12
main.go
View File

@ -54,20 +54,24 @@ func main() {
}
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)
fmt.Printf("Started on: %s\n", time.Now().Format(time.DateTime))
color.Unset()
// if noweb is false and HTTP/REST API is enabled then start web service
if !cfg.Board.NoWeb && cfg.API[0].Enabled {
apiPort = http.NewGinAdapter(corePort, cfg)
go apiPort.Run()
color.Set(color.FgHiGreen)
fmt.Printf("TH7 API is live on http://localhost:%d/\n", cfg.API[0].Port)
color.Unset()
}
fmt.Println(cfg.API[0])
sig := <-kill
log.Printf("Caught signal %v", sig)
}