From 731f827dbfddda99f74bde91bdfc40b47b47885b Mon Sep 17 00:00:00 2001 From: William Clark Date: Sat, 12 Nov 2022 22:43:16 +0000 Subject: [PATCH] moving data between different ports --- config.toml | 6 +++--- core/dummy.go | 5 +++-- core/th7.go | 31 ++++++++++++++++++++++++------- ports/core.go | 2 +- web/gin.go | 35 +++++++++++++++++++++++++++++++++-- 5 files changed, 64 insertions(+), 15 deletions(-) diff --git a/config.toml b/config.toml index cc28ab3..b13d920 100644 --- a/config.toml +++ b/config.toml @@ -1,9 +1,9 @@ [TH7] port = 8080 logfreq = 60 -cache = true -LED = false -debug = true +#cache = true +#LED = false +#debug = true [DB] type = "sqlite3" diff --git a/core/dummy.go b/core/dummy.go index 0ef0d4f..03705a4 100644 --- a/core/dummy.go +++ b/core/dummy.go @@ -17,8 +17,9 @@ func (d *DummyAdapter) GetChannel(id int) (core.Channel, error) { return core.Channel{}, nil } -func (d *DummyAdapter) GetAll() core.Data { - return core.Data{} +func (d *DummyAdapter) GetChannels() ([]core.Channel, error) { + dummyData := make([]core.Channel, 0) + return dummyData, nil } func (d *DummyAdapter) GetVref() float64 { diff --git a/core/th7.go b/core/th7.go index 955b206..4acb0b3 100644 --- a/core/th7.go +++ b/core/th7.go @@ -1,6 +1,7 @@ package core import ( + "errors" "sync" "th7/data/config" "th7/data/core" @@ -10,7 +11,9 @@ import ( type TH7Adapter struct { pcbPort ports.PCBPort mu sync.Mutex - data core.Data + data []core.Channel + lookup map[int]int + table core.Ratio } func NewTH7Adapter(pcbPort ports.PCBPort, cfg config.Config) *TH7Adapter { @@ -19,6 +22,16 @@ func NewTH7Adapter(pcbPort ports.PCBPort, cfg config.Config) *TH7Adapter { adapter.pcbPort = pcbPort + adapter.lookup = make(map[int]int) + adapter.data = make([]core.Channel, 0) + + for idx, elem := range cfg.Channels { + var channel core.Channel + channel.Id = elem.Id + adapter.lookup[channel.Id] = idx + adapter.data = append(adapter.data, channel) + } + return &adapter } @@ -26,33 +39,37 @@ func (t *TH7Adapter) GetChannel(id int) (core.Channel, error) { t.mu.Lock() defer t.mu.Unlock() - return t.data.Channels[0], nil + if val, ok := t.lookup[id]; ok { + return t.data[val], nil + } + + return core.Channel{}, errors.New("specified channel not configured") } -func (t *TH7Adapter) GetAll() core.Data { +func (t *TH7Adapter) GetChannels() ([]core.Channel, error) { t.mu.Lock() defer t.mu.Unlock() - return t.data + return t.data, nil } func (t *TH7Adapter) GetVref() float64 { t.mu.Lock() defer t.mu.Unlock() - return 0 + return t.table.Vref } func (t *TH7Adapter) GetVadj() float64 { t.mu.Lock() defer t.mu.Unlock() - return 0 + return t.table.Vadj } func (t *TH7Adapter) GetPivdd() float64 { t.mu.Lock() defer t.mu.Unlock() - return 0 + return t.table.Pivdd } diff --git a/ports/core.go b/ports/core.go index 7679645..870e527 100644 --- a/ports/core.go +++ b/ports/core.go @@ -4,7 +4,7 @@ import "th7/data/core" type CorePort interface { GetChannel(int) (core.Channel, error) - GetAll() core.Data + GetChannels() ([]core.Channel, error) GetVref() float64 GetVadj() float64 GetPivdd() float64 diff --git a/web/gin.go b/web/gin.go index aca5b2f..f590f51 100644 --- a/web/gin.go +++ b/web/gin.go @@ -2,6 +2,7 @@ package web import ( "net/http" + "strconv" "github.com/gin-gonic/gin" @@ -15,9 +16,39 @@ type GinAdapter struct { func (g GinAdapter) registerEndpoints() { - g.router.GET("/", func(c *gin.Context) { - c.JSON(http.StatusOK, g.corePort.GetAll()) + g.router.GET("/channels", func(c *gin.Context) { + channels, err := g.corePort.GetChannels() + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{ + "error": err, + }) + } else { + c.JSON(http.StatusOK, channels) + } + }) + + g.router.GET("/channel/:id", func(c *gin.Context) { + id, err := strconv.Atoi(c.Param("id")) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{ + "error": err, + }) + return + } + + channel, err := g.corePort.GetChannel(id) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{ + "error": err, + }) + return + } + + c.JSON(http.StatusOK, channel) + + }) + } func NewGinAdapter(corePort ports.CorePort) *GinAdapter {