Skip to content

Commit

Permalink
Concept plugin layout
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas Thompson authored and ncthompson committed Mar 31, 2019
1 parent d02de28 commit 47e73a4
Show file tree
Hide file tree
Showing 12 changed files with 362 additions and 286 deletions.
14 changes: 3 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,13 @@
#OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
#OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

.PHONY: test test-race vet install gofmt docker statik lint clean guimock invertergui invertercli build
.PHONY: test test-race vet install gofmt docker statik lint clean invertergui

.DEFAULT_GOAL = build

guimock:
go build ./cmd/guimock/
.DEFAULT_GOAL = invertergui

invertergui:
go build ./cmd/invertergui/

invertercli:
go build ./cmd/invertercli/

build: guimock invertergui invertercli

all: build gofmt test

gofmt:
Expand All @@ -62,4 +54,4 @@ lint:
golangci-lint run

clean:
rm ./guimock ./invertercli ./invertergui
rm ./invertergui
29 changes: 0 additions & 29 deletions cmd/guimock/main.go

This file was deleted.

90 changes: 0 additions & 90 deletions cmd/invertercli/invertercli.go

This file was deleted.

53 changes: 39 additions & 14 deletions cmd/invertergui/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ import (
"net/http"

"github.com/diebietse/invertergui/frontend"
"github.com/diebietse/invertergui/mk2core"
"github.com/diebietse/invertergui/mk2driver"
"github.com/diebietse/invertergui/webgui"
"github.com/diebietse/invertergui/plugins/cli"
"github.com/diebietse/invertergui/plugins/munin"
"github.com/diebietse/invertergui/plugins/prometheus"
"github.com/diebietse/invertergui/plugins/webui"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/tarm/serial"
)
Expand All @@ -50,14 +54,44 @@ func main() {
tcp := flag.Bool("tcp", false, "Use TCP instead of TTY")
ip := flag.String("ip", "localhost:8139", "IP to connect when using tcp connection.")
dev := flag.String("dev", "/dev/ttyUSB0", "TTY device to use.")
mock := flag.Bool("mock", false, "Creates a mock device for test puposes")
cliEnable := flag.Bool("cli", false, "Enable CLI output")
flag.Parse()

var mk2 mk2driver.Mk2
if *mock {
mk2 = mk2driver.NewMk2Mock()
} else {
mk2 = getMk2Device(*tcp, *ip, *dev)
}

defer mk2.Close()

core := mk2core.NewCore(mk2)

if *cliEnable {
cli.NewCli(core.NewSubscription())
}

gui := webui.NewWebGui(core.NewSubscription())
mu := munin.NewMunin(core.NewSubscription())
prometheus.NewPrometheus(core.NewSubscription())

http.Handle("/", frontend.NewStatic())
http.Handle("/ws", http.HandlerFunc(gui.ServeHub))
http.Handle("/munin", http.HandlerFunc(mu.ServeMuninHTTP))
http.Handle("/muninconfig", http.HandlerFunc(mu.ServeMuninConfigHTTP))
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(*addr, nil))
}

func getMk2Device(tcp bool, ip, dev string) mk2driver.Mk2 {
var p io.ReadWriteCloser
var err error
var tcpAddr *net.TCPAddr

if *tcp {
tcpAddr, err = net.ResolveTCPAddr("tcp", *ip)
if tcp {
tcpAddr, err = net.ResolveTCPAddr("tcp", ip)
if err != nil {
panic(err)
}
Expand All @@ -66,25 +100,16 @@ func main() {
panic(err)
}
} else {
serialConfig := &serial.Config{Name: *dev, Baud: 2400}
serialConfig := &serial.Config{Name: dev, Baud: 2400}
p, err = serial.OpenPort(serialConfig)
if err != nil {
panic(err)
}
}
defer p.Close()
mk2, err := mk2driver.NewMk2Connection(p)
if err != nil {
panic(err)
}
defer mk2.Close()

gui := webgui.NewWebGui(mk2)

http.Handle("/", frontend.NewStatic())
http.Handle("/ws", http.HandlerFunc(gui.ServeHub))
http.Handle("/munin", http.HandlerFunc(gui.ServeMuninHTTP))
http.Handle("/muninconfig", http.HandlerFunc(gui.ServeMuninConfigHTTP))
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(*addr, nil))
return mk2
}
57 changes: 57 additions & 0 deletions mk2core/core.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package mk2core

import (
"github.com/diebietse/invertergui/mk2driver"
)

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{},
}
go core.run()
return core
}

func (c *Core) NewSubscription() mk2driver.Mk2 {
sub := &subscription{
send: make(chan *mk2driver.Mk2Info),
}
c.register <- sub
return sub
}

func (c *Core) run() {
for {
select {
case r := <-c.register:
c.plugins[r] = true
case e := <-c.C():
for plugin := range c.plugins {
select {
case plugin.send <- e:
default:
}
}
}
}
}

type subscription struct {
send chan *mk2driver.Mk2Info
}

func (s *subscription) C() chan *mk2driver.Mk2Info {
return s.send
}

func (s *subscription) Close() {
close(s.send)
}
2 changes: 0 additions & 2 deletions mk2driver/mockmk2.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package mk2driver

import (
"fmt"
"time"
)

Expand Down Expand Up @@ -64,7 +63,6 @@ func (m *mock) genMockValues() {
if mult < 0 {
mult = 1.0
}
fmt.Printf("Sending\n")
m.c <- input
time.Sleep(1 * time.Second)
}
Expand Down
47 changes: 47 additions & 0 deletions plugins/cli/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cli

import (
"fmt"
"log"

"github.com/diebietse/invertergui/mk2driver"
)

type Cli struct {
mk2driver.Mk2
}

func NewCli(mk2 mk2driver.Mk2) {
newCli := &Cli{
Mk2: mk2,
}
go newCli.run()
}

func (c *Cli) run() {
for e := range c.C() {
if e.Valid {
printInfo(e)
}
}
}

func printInfo(info *mk2driver.Mk2Info) {
out := fmt.Sprintf("Version: %v\n", info.Version)
out += fmt.Sprintf("Bat Volt: %.2fV Bat Cur: %.2fA \n", info.BatVoltage, info.BatCurrent)
out += fmt.Sprintf("In Volt: %.2fV In Cur: %.2fA In Freq %.2fHz\n", info.InVoltage, info.InCurrent, info.InFrequency)
out += fmt.Sprintf("Out Volt: %.2fV Out Cur: %.2fA Out Freq %.2fHz\n", info.OutVoltage, info.OutCurrent, info.OutFrequency)
out += fmt.Sprintf("In Power %.2fW Out Power %.2fW\n", info.InVoltage*info.InCurrent, info.OutVoltage*info.OutCurrent)
out += fmt.Sprintf("Charge State: %.2f%%\n", info.ChargeState*100)
out += "LEDs state:"
for k, v := range info.LEDs {
out += fmt.Sprintf(" %s %s", mk2driver.LedNames[k], mk2driver.StateNames[v])
}

out += "\nErrors:"
for _, v := range info.Errors {
out += " " + v.Error()
}
out += "\n"
log.Printf("System Info: \n%v", out)
}
Loading

0 comments on commit 47e73a4

Please sign in to comment.