Implement multiple loadpoints and new UI (#199)

* Support multiple loadpoints

* Finalize loadpoint site split

* Simplify global voltage

* Fix push template error stopping main process

* Show only last error on retry

* Improve influx v1 compatibility

* Update push templates

* Revert influx v1 user change
This commit is contained in:
andig 2020-07-16 09:34:58 +02:00 • committed by GitHub
parent 80c851e423
commit e34167ce7d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
54 changed files with 1945 additions and 1499 deletions

View file

@ -1,35 +1,72 @@
package util
import "sync"
import (
"sync"
)
// Cache is a data store
type Cache struct {
sync.Mutex
val map[string]interface{}
val map[string]Param
}
// NewCache creates cache
func NewCache() *Cache {
return &Cache{val: make(map[string]interface{})}
return &Cache{
val: make(map[string]Param),
}
}
// Run adds input channel's values to cache
func (c *Cache) Run(in <-chan Param) {
log := NewLogger("cache")
for p := range in {
c.Add(p.Key, p)
log.TRACE.Printf("%s: %v", p.Key, p.Val)
c.Add(p.UniqueID(), p)
}
}
// State provides a structured copy of the cached values
// Loadpoints are aggregated as loadpoints array
func (c *Cache) State() map[string]interface{} {
c.Lock()
defer c.Unlock()
res := map[string]interface{}{}
lps := make(map[int]map[string]interface{})
for _, param := range c.val {
if param.LoadPoint == nil {
res[param.Key] = param.Val
} else {
lp, ok := lps[*param.LoadPoint]
if !ok {
lp = make(map[string]interface{})
lps[*param.LoadPoint] = lp
}
lp[param.Key] = param.Val
}
}
// convert map to array
loadpoints := make([]map[string]interface{}, len(lps))
for id, lp := range lps {
loadpoints[id] = lp
}
res["loadpoints"] = loadpoints
return res
}
// All provides a copy of the cached values
func (c *Cache) All() []Param {
c.Lock()
defer c.Unlock()
copy := make([]Param, 0, len(c.val))
for _, v := range c.val {
if param, ok := v.(Param); ok {
copy = append(copy, param)
}
for _, val := range c.val {
copy = append(copy, val)
}
return copy
@ -49,9 +86,7 @@ func (c *Cache) Get(key string) Param {
defer c.Unlock()
if val, ok := c.val[key]; ok {
if param, ok := val.(Param); ok {
return param
}
return val
}
return Param{}