Files
invertergui/mk2core/core.go
Nathan Coad 1c15ff5911
Some checks failed
continuous-integration/drone/push Build is failing
Add read-only mode support and enhance logging throughout the application
2026-02-19 12:36:52 +11:00

65 lines
1.2 KiB
Go

package mk2core
import (
"git.coadcorp.com/nathan/invertergui/mk2driver"
"github.com/sirupsen/logrus"
)
var log = logrus.WithField("ctx", "inverter-gui-core")
type Core struct {
mk2driver.Mk2
plugins map[*subscription]bool
register chan *subscription
}
func NewCore(m mk2driver.Mk2) *Core {
core := &Core{
Mk2: m,
register: make(chan *subscription, 255),
plugins: map[*subscription]bool{},
}
log.Info("Core initialized")
go core.run()
return core
}
func (c *Core) NewSubscription() mk2driver.Mk2 {
sub := &subscription{
send: make(chan *mk2driver.Mk2Info),
}
c.register <- sub
log.Debug("New plugin subscription registered")
return sub
}
func (c *Core) run() {
for {
select {
case r := <-c.register:
c.plugins[r] = true
log.WithField("subscribers", len(c.plugins)).Debug("Subscription added")
case e := <-c.C():
for plugin := range c.plugins {
select {
case plugin.send <- e:
default:
log.WithField("subscribers", len(c.plugins)).Warn("Dropping update for a slow subscriber")
}
}
}
}
}
type subscription struct {
send chan *mk2driver.Mk2Info
}
func (s *subscription) C() chan *mk2driver.Mk2Info {
return s.send
}
func (s *subscription) Close() {
close(s.send)
}