moving data between different ports

This commit is contained in:
William Clark 2022-11-12 22:43:16 +00:00
parent 507ba451a2
commit 731f827dbf
5 changed files with 64 additions and 15 deletions

View File

@ -1,9 +1,9 @@
[TH7] [TH7]
port = 8080 port = 8080
logfreq = 60 logfreq = 60
cache = true #cache = true
LED = false #LED = false
debug = true #debug = true
[DB] [DB]
type = "sqlite3" type = "sqlite3"

View File

@ -17,8 +17,9 @@ func (d *DummyAdapter) GetChannel(id int) (core.Channel, error) {
return core.Channel{}, nil return core.Channel{}, nil
} }
func (d *DummyAdapter) GetAll() core.Data { func (d *DummyAdapter) GetChannels() ([]core.Channel, error) {
return core.Data{} dummyData := make([]core.Channel, 0)
return dummyData, nil
} }
func (d *DummyAdapter) GetVref() float64 { func (d *DummyAdapter) GetVref() float64 {

View File

@ -1,6 +1,7 @@
package core package core
import ( import (
"errors"
"sync" "sync"
"th7/data/config" "th7/data/config"
"th7/data/core" "th7/data/core"
@ -10,7 +11,9 @@ import (
type TH7Adapter struct { type TH7Adapter struct {
pcbPort ports.PCBPort pcbPort ports.PCBPort
mu sync.Mutex 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 { 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.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 return &adapter
} }
@ -26,33 +39,37 @@ func (t *TH7Adapter) GetChannel(id int) (core.Channel, error) {
t.mu.Lock() t.mu.Lock()
defer t.mu.Unlock() 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() t.mu.Lock()
defer t.mu.Unlock() defer t.mu.Unlock()
return t.data return t.data, nil
} }
func (t *TH7Adapter) GetVref() float64 { func (t *TH7Adapter) GetVref() float64 {
t.mu.Lock() t.mu.Lock()
defer t.mu.Unlock() defer t.mu.Unlock()
return 0 return t.table.Vref
} }
func (t *TH7Adapter) GetVadj() float64 { func (t *TH7Adapter) GetVadj() float64 {
t.mu.Lock() t.mu.Lock()
defer t.mu.Unlock() defer t.mu.Unlock()
return 0 return t.table.Vadj
} }
func (t *TH7Adapter) GetPivdd() float64 { func (t *TH7Adapter) GetPivdd() float64 {
t.mu.Lock() t.mu.Lock()
defer t.mu.Unlock() defer t.mu.Unlock()
return 0 return t.table.Pivdd
} }

View File

@ -4,7 +4,7 @@ import "th7/data/core"
type CorePort interface { type CorePort interface {
GetChannel(int) (core.Channel, error) GetChannel(int) (core.Channel, error)
GetAll() core.Data GetChannels() ([]core.Channel, error)
GetVref() float64 GetVref() float64
GetVadj() float64 GetVadj() float64
GetPivdd() float64 GetPivdd() float64

View File

@ -2,6 +2,7 @@ package web
import ( import (
"net/http" "net/http"
"strconv"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -15,9 +16,39 @@ type GinAdapter struct {
func (g GinAdapter) registerEndpoints() { func (g GinAdapter) registerEndpoints() {
g.router.GET("/", func(c *gin.Context) { g.router.GET("/channels", func(c *gin.Context) {
c.JSON(http.StatusOK, g.corePort.GetAll()) 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 { func NewGinAdapter(corePort ports.CorePort) *GinAdapter {