Concept plugin layout
This commit is contained in:
committed by
ncthompson
parent
d02de285d9
commit
47e73a4eff
47
plugins/cli/cli.go
Normal file
47
plugins/cli/cli.go
Normal file
@@ -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)
|
||||
}
|
||||
218
plugins/munin/munin.go
Normal file
218
plugins/munin/munin.go
Normal file
@@ -0,0 +1,218 @@
|
||||
/*
|
||||
Copyright (c) 2015, Hendrik van Wyk
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
* Neither the name of invertergui nor the names of its
|
||||
contributors may be used to endorse or promote products derived from
|
||||
this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
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.
|
||||
*/
|
||||
|
||||
package munin
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/diebietse/invertergui/mk2driver"
|
||||
)
|
||||
|
||||
type Munin struct {
|
||||
mk2driver.Mk2
|
||||
muninResponse chan *muninData
|
||||
}
|
||||
|
||||
type muninData struct {
|
||||
status *mk2driver.Mk2Info
|
||||
timesUpdated int
|
||||
}
|
||||
|
||||
func NewMunin(mk2 mk2driver.Mk2) *Munin {
|
||||
m := &Munin{
|
||||
Mk2: mk2,
|
||||
muninResponse: make(chan *muninData),
|
||||
}
|
||||
|
||||
go m.run()
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *Munin) ServeMuninHTTP(rw http.ResponseWriter, r *http.Request) {
|
||||
muninDat := <-m.muninResponse
|
||||
if muninDat.timesUpdated == 0 {
|
||||
rw.WriteHeader(500)
|
||||
_, _ = rw.Write([]byte("No data to return.\n"))
|
||||
return
|
||||
}
|
||||
calcMuninAverages(muninDat)
|
||||
|
||||
status := muninDat.status
|
||||
tmpInput := buildTemplateInput(status)
|
||||
outputBuf := &bytes.Buffer{}
|
||||
fmt.Fprintf(outputBuf, "multigraph in_batvolt\n")
|
||||
fmt.Fprintf(outputBuf, "volt.value %s\n", tmpInput.BatVoltage)
|
||||
fmt.Fprintf(outputBuf, "multigraph in_batcharge\n")
|
||||
fmt.Fprintf(outputBuf, "charge.value %s\n", tmpInput.BatCharge)
|
||||
fmt.Fprintf(outputBuf, "multigraph in_batcurrent\n")
|
||||
fmt.Fprintf(outputBuf, "current.value %s\n", tmpInput.BatCurrent)
|
||||
fmt.Fprintf(outputBuf, "multigraph in_batpower\n")
|
||||
fmt.Fprintf(outputBuf, "power.value %s\n", tmpInput.BatPower)
|
||||
fmt.Fprintf(outputBuf, "multigraph in_mainscurrent\n")
|
||||
fmt.Fprintf(outputBuf, "currentin.value %s\n", tmpInput.InCurrent)
|
||||
fmt.Fprintf(outputBuf, "currentout.value %s\n", tmpInput.OutCurrent)
|
||||
fmt.Fprintf(outputBuf, "multigraph in_mainsvoltage\n")
|
||||
fmt.Fprintf(outputBuf, "voltagein.value %s\n", tmpInput.InVoltage)
|
||||
fmt.Fprintf(outputBuf, "voltageout.value %s\n", tmpInput.OutVoltage)
|
||||
fmt.Fprintf(outputBuf, "multigraph in_mainspower\n")
|
||||
fmt.Fprintf(outputBuf, "powerin.value %s\n", tmpInput.InPower)
|
||||
fmt.Fprintf(outputBuf, "powerout.value %s\n", tmpInput.OutPower)
|
||||
fmt.Fprintf(outputBuf, "multigraph in_mainsfreq\n")
|
||||
fmt.Fprintf(outputBuf, "freqin.value %s\n", tmpInput.InFreq)
|
||||
fmt.Fprintf(outputBuf, "freqout.value %s\n", tmpInput.OutFreq)
|
||||
|
||||
_, err := rw.Write(outputBuf.Bytes())
|
||||
if err != nil {
|
||||
fmt.Printf("%v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Munin) ServeMuninConfigHTTP(rw http.ResponseWriter, r *http.Request) {
|
||||
output := muninConfig
|
||||
_, err := rw.Write([]byte(output))
|
||||
if err != nil {
|
||||
fmt.Printf("%v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Munin) run() {
|
||||
muninValues := &muninData{
|
||||
status: &mk2driver.Mk2Info{},
|
||||
}
|
||||
for {
|
||||
select {
|
||||
case e := <-m.C():
|
||||
if e.Valid {
|
||||
calcMuninValues(muninValues, e)
|
||||
|
||||
}
|
||||
case m.muninResponse <- muninValues:
|
||||
zeroMuninValues(muninValues)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Munin only samples once every 5 minutes so averages have to be calculated for some values.
|
||||
func calcMuninValues(muninDat *muninData, newStatus *mk2driver.Mk2Info) {
|
||||
muninDat.timesUpdated++
|
||||
muninVal := muninDat.status
|
||||
muninVal.OutCurrent += newStatus.OutCurrent
|
||||
muninVal.InCurrent += newStatus.InCurrent
|
||||
muninVal.BatCurrent += newStatus.BatCurrent
|
||||
|
||||
muninVal.OutVoltage += newStatus.OutVoltage
|
||||
muninVal.InVoltage += newStatus.InVoltage
|
||||
muninVal.BatVoltage += newStatus.BatVoltage
|
||||
|
||||
muninVal.InFrequency = newStatus.InFrequency
|
||||
muninVal.OutFrequency = newStatus.OutFrequency
|
||||
|
||||
muninVal.ChargeState = newStatus.ChargeState
|
||||
}
|
||||
|
||||
func calcMuninAverages(muninDat *muninData) {
|
||||
muninVal := muninDat.status
|
||||
muninVal.OutCurrent /= float64(muninDat.timesUpdated)
|
||||
muninVal.InCurrent /= float64(muninDat.timesUpdated)
|
||||
muninVal.BatCurrent /= float64(muninDat.timesUpdated)
|
||||
|
||||
muninVal.OutVoltage /= float64(muninDat.timesUpdated)
|
||||
muninVal.InVoltage /= float64(muninDat.timesUpdated)
|
||||
muninVal.BatVoltage /= float64(muninDat.timesUpdated)
|
||||
}
|
||||
|
||||
func zeroMuninValues(muninDat *muninData) {
|
||||
muninDat.timesUpdated = 0
|
||||
muninVal := muninDat.status
|
||||
muninVal.OutCurrent = 0
|
||||
muninVal.InCurrent = 0
|
||||
muninVal.BatCurrent = 0
|
||||
|
||||
muninVal.OutVoltage = 0
|
||||
muninVal.InVoltage = 0
|
||||
muninVal.BatVoltage = 0
|
||||
|
||||
muninVal.InFrequency = 0
|
||||
muninVal.OutFrequency = 0
|
||||
|
||||
muninVal.ChargeState = 0
|
||||
}
|
||||
|
||||
type templateInput struct {
|
||||
Date string `json:"date"`
|
||||
|
||||
OutCurrent string `json:"output_current"`
|
||||
OutVoltage string `json:"output_voltage"`
|
||||
OutPower string `json:"output_power"`
|
||||
|
||||
InCurrent string `json:"input_current"`
|
||||
InVoltage string `json:"input_voltage"`
|
||||
InPower string `json:"input_power"`
|
||||
|
||||
InMinOut string
|
||||
|
||||
BatVoltage string `json:"battery_voltage"`
|
||||
BatCurrent string `json:"battery_current"`
|
||||
BatPower string `json:"battery_power"`
|
||||
BatCharge string `json:"battery_charge"`
|
||||
|
||||
InFreq string `json:"input_frequency"`
|
||||
OutFreq string `json:"output_frequency"`
|
||||
}
|
||||
|
||||
func buildTemplateInput(status *mk2driver.Mk2Info) *templateInput {
|
||||
outPower := status.OutVoltage * status.OutCurrent
|
||||
inPower := status.InCurrent * status.InVoltage
|
||||
|
||||
newInput := &templateInput{
|
||||
Date: status.Timestamp.Format(time.RFC1123Z),
|
||||
OutCurrent: fmt.Sprintf("%.2f", status.OutCurrent),
|
||||
OutVoltage: fmt.Sprintf("%.2f", status.OutVoltage),
|
||||
OutPower: fmt.Sprintf("%.2f", outPower),
|
||||
InCurrent: fmt.Sprintf("%.2f", status.InCurrent),
|
||||
InVoltage: fmt.Sprintf("%.2f", status.InVoltage),
|
||||
InFreq: fmt.Sprintf("%.2f", status.InFrequency),
|
||||
OutFreq: fmt.Sprintf("%.2f", status.OutFrequency),
|
||||
InPower: fmt.Sprintf("%.2f", inPower),
|
||||
|
||||
InMinOut: fmt.Sprintf("%.2f", inPower-outPower),
|
||||
|
||||
BatCurrent: fmt.Sprintf("%.2f", status.BatCurrent),
|
||||
BatVoltage: fmt.Sprintf("%.2f", status.BatVoltage),
|
||||
BatPower: fmt.Sprintf("%.2f", status.BatVoltage*status.BatCurrent),
|
||||
BatCharge: fmt.Sprintf("%.2f", status.ChargeState*100),
|
||||
}
|
||||
return newInput
|
||||
}
|
||||
82
plugins/munin/muninconfig.go
Normal file
82
plugins/munin/muninconfig.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package munin
|
||||
|
||||
const muninConfig = `multigraph in_batvolt
|
||||
graph_title Battery Voltage
|
||||
graph_vlabel Voltage (V)
|
||||
graph_category inverter
|
||||
graph_info Battery voltage
|
||||
|
||||
volt.info Voltage of battery
|
||||
volt.label Voltage of battery (V)
|
||||
|
||||
multigraph in_batcharge
|
||||
graph_title Battery Charge
|
||||
graph_vlabel Charge (%)
|
||||
graph_category inverter
|
||||
graph_info Battery charge
|
||||
|
||||
charge.info Estimated charge of battery
|
||||
charge.label Battery charge (%)
|
||||
|
||||
multigraph in_batcurrent
|
||||
graph_title Battery Current
|
||||
graph_vlabel Current (A)
|
||||
graph_category inverter
|
||||
graph_info Battery current
|
||||
|
||||
current.info Battery current
|
||||
current.label Battery current (A)
|
||||
|
||||
multigraph in_batpower
|
||||
graph_title Battery Power
|
||||
graph_vlabel Power (W)
|
||||
graph_category inverter
|
||||
graph_info Battery power
|
||||
|
||||
power.info Battery power
|
||||
power.label Battery power (W)
|
||||
|
||||
multigraph in_mainscurrent
|
||||
graph_title Mains Current
|
||||
graph_vlabel Current (A)
|
||||
graph_category inverter
|
||||
graph_info Mains current
|
||||
|
||||
currentin.info Input current
|
||||
currentin.label Input current (A)
|
||||
currentout.info Output current
|
||||
currentout.label Output current (A)
|
||||
|
||||
multigraph in_mainsvoltage
|
||||
graph_title Mains Voltage
|
||||
graph_vlabel Voltage (V)
|
||||
graph_category inverter
|
||||
graph_info Mains voltage
|
||||
|
||||
voltagein.info Input voltage
|
||||
voltagein.label Input voltage (V)
|
||||
voltageout.info Output voltage
|
||||
voltageout.label Output voltage (V)
|
||||
|
||||
multigraph in_mainspower
|
||||
graph_title Mains Power
|
||||
graph_vlabel Power (VA)
|
||||
graph_category inverter
|
||||
graph_info Mains power
|
||||
|
||||
powerin.info Input power
|
||||
powerin.label Input power (VA)
|
||||
powerout.info Output power
|
||||
powerout.label Output power (VA)
|
||||
|
||||
multigraph in_mainsfreq
|
||||
graph_title Mains frequency
|
||||
graph_vlabel Frequency (Hz)
|
||||
graph_category inverter
|
||||
graph_info Mains frequency
|
||||
|
||||
freqin.info In frequency
|
||||
freqin.label In frequency (Hz)
|
||||
freqout.info Out frequency
|
||||
freqout.label Out frequency (Hz)
|
||||
`
|
||||
146
plugins/prometheus/prometheus.go
Normal file
146
plugins/prometheus/prometheus.go
Normal file
@@ -0,0 +1,146 @@
|
||||
/*
|
||||
Copyright (c) 2017, Hendrik van Wyk
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
* Neither the name of invertergui nor the names of its
|
||||
contributors may be used to endorse or promote products derived from
|
||||
this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
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.
|
||||
*/
|
||||
|
||||
package prometheus
|
||||
|
||||
import (
|
||||
"github.com/diebietse/invertergui/mk2driver"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
type Prometheus struct {
|
||||
mk2driver.Mk2
|
||||
batteryVoltage prometheus.Gauge
|
||||
batteryCharge prometheus.Gauge
|
||||
batteryCurrent prometheus.Gauge
|
||||
batteryPower prometheus.Gauge
|
||||
mainsCurrentIn prometheus.Gauge
|
||||
mainsCurrentOut prometheus.Gauge
|
||||
mainsVoltageIn prometheus.Gauge
|
||||
mainsVoltageOut prometheus.Gauge
|
||||
mainsPowerIn prometheus.Gauge
|
||||
mainsPowerOut prometheus.Gauge
|
||||
mainsFreqIn prometheus.Gauge
|
||||
mainsFreqOut prometheus.Gauge
|
||||
}
|
||||
|
||||
func NewPrometheus(mk2 mk2driver.Mk2) {
|
||||
tmp := &Prometheus{
|
||||
Mk2: mk2,
|
||||
batteryVoltage: prometheus.NewGauge(prometheus.GaugeOpts{
|
||||
Name: "battery_voltage_v",
|
||||
Help: "Voltage of the battery.",
|
||||
}),
|
||||
batteryCharge: prometheus.NewGauge(prometheus.GaugeOpts{
|
||||
Name: "battery_charge_percentage",
|
||||
Help: "Remaining battery charge.",
|
||||
}),
|
||||
batteryCurrent: prometheus.NewGauge(prometheus.GaugeOpts{
|
||||
Name: "battery_current_a",
|
||||
Help: "Battery current.",
|
||||
}),
|
||||
batteryPower: prometheus.NewGauge(prometheus.GaugeOpts{
|
||||
Name: "battery_power_w",
|
||||
Help: "Battery power.",
|
||||
}),
|
||||
mainsCurrentIn: prometheus.NewGauge(prometheus.GaugeOpts{
|
||||
Name: "mains_current_in_a",
|
||||
Help: "Mains current flowing into inverter",
|
||||
}),
|
||||
mainsCurrentOut: prometheus.NewGauge(prometheus.GaugeOpts{
|
||||
Name: "mains_current_out_a",
|
||||
Help: "Mains current flowing out of inverter",
|
||||
}),
|
||||
mainsVoltageIn: prometheus.NewGauge(prometheus.GaugeOpts{
|
||||
Name: "mains_voltage_in_v",
|
||||
Help: "Mains voltage at input of inverter",
|
||||
}),
|
||||
mainsVoltageOut: prometheus.NewGauge(prometheus.GaugeOpts{
|
||||
Name: "mains_voltage_out_v",
|
||||
Help: "Mains voltage at output of inverter",
|
||||
}),
|
||||
mainsPowerIn: prometheus.NewGauge(prometheus.GaugeOpts{
|
||||
Name: "mains_power_in_va",
|
||||
Help: "Mains power in",
|
||||
}),
|
||||
mainsPowerOut: prometheus.NewGauge(prometheus.GaugeOpts{
|
||||
Name: "mains_power_out_va",
|
||||
Help: "Mains power out",
|
||||
}),
|
||||
mainsFreqIn: prometheus.NewGauge(prometheus.GaugeOpts{
|
||||
Name: "mains_freq_in_hz",
|
||||
Help: "Mains frequency at inverter input",
|
||||
}),
|
||||
mainsFreqOut: prometheus.NewGauge(prometheus.GaugeOpts{
|
||||
Name: "mains_freq_out_hz",
|
||||
Help: "Mains frequency at inverter output",
|
||||
}),
|
||||
}
|
||||
prometheus.MustRegister(
|
||||
tmp.batteryVoltage,
|
||||
tmp.batteryCharge,
|
||||
tmp.batteryCurrent,
|
||||
tmp.batteryPower,
|
||||
tmp.mainsCurrentIn,
|
||||
tmp.mainsCurrentOut,
|
||||
tmp.mainsVoltageIn,
|
||||
tmp.mainsVoltageOut,
|
||||
tmp.mainsPowerIn,
|
||||
tmp.mainsPowerOut,
|
||||
tmp.mainsFreqIn,
|
||||
tmp.mainsFreqOut,
|
||||
)
|
||||
|
||||
go tmp.run()
|
||||
}
|
||||
|
||||
func (p *Prometheus) run() {
|
||||
for e := range p.C() {
|
||||
if e.Valid {
|
||||
p.updatePrometheus(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Prometheus) updatePrometheus(newStatus *mk2driver.Mk2Info) {
|
||||
s := newStatus
|
||||
p.batteryVoltage.Set(s.BatVoltage)
|
||||
p.batteryCharge.Set(newStatus.ChargeState * 100)
|
||||
p.batteryCurrent.Set(s.BatCurrent)
|
||||
p.batteryPower.Set(s.BatVoltage * s.BatCurrent)
|
||||
p.mainsCurrentIn.Set(s.InCurrent)
|
||||
p.mainsCurrentOut.Set(s.OutCurrent)
|
||||
p.mainsVoltageIn.Set(s.InVoltage)
|
||||
p.mainsVoltageOut.Set(s.OutVoltage)
|
||||
p.mainsPowerIn.Set(s.InVoltage * s.InCurrent)
|
||||
p.mainsPowerOut.Set(s.OutVoltage * s.OutCurrent)
|
||||
p.mainsFreqIn.Set(s.InFrequency)
|
||||
p.mainsFreqOut.Set(s.OutFrequency)
|
||||
}
|
||||
180
plugins/webui/webgui.go
Normal file
180
plugins/webui/webgui.go
Normal file
@@ -0,0 +1,180 @@
|
||||
/*
|
||||
Copyright (c) 2015, 2017 Hendrik van Wyk
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
* Neither the name of invertergui nor the names of its
|
||||
contributors may be used to endorse or promote products derived from
|
||||
this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
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.
|
||||
*/
|
||||
|
||||
package webui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/diebietse/invertergui/mk2driver"
|
||||
"github.com/diebietse/invertergui/websocket"
|
||||
)
|
||||
|
||||
const (
|
||||
LedOff = "dot-off"
|
||||
LedRed = "dot-red"
|
||||
BlinkRed = "blink-red"
|
||||
LedGreen = "dot-green"
|
||||
BlinkGreen = "blink-green"
|
||||
)
|
||||
|
||||
type WebGui struct {
|
||||
mk2driver.Mk2
|
||||
stopChan chan struct{}
|
||||
|
||||
wg sync.WaitGroup
|
||||
hub *websocket.Hub
|
||||
}
|
||||
|
||||
func NewWebGui(source mk2driver.Mk2) *WebGui {
|
||||
w := &WebGui{
|
||||
stopChan: make(chan struct{}),
|
||||
Mk2: source,
|
||||
hub: websocket.NewHub(),
|
||||
}
|
||||
w.wg.Add(1)
|
||||
go w.dataPoll()
|
||||
return w
|
||||
}
|
||||
|
||||
type templateInput struct {
|
||||
Error []error `json:"errors"`
|
||||
|
||||
Date string `json:"date"`
|
||||
|
||||
OutCurrent string `json:"output_current"`
|
||||
OutVoltage string `json:"output_voltage"`
|
||||
OutPower string `json:"output_power"`
|
||||
|
||||
InCurrent string `json:"input_current"`
|
||||
InVoltage string `json:"input_voltage"`
|
||||
InPower string `json:"input_power"`
|
||||
|
||||
InMinOut string
|
||||
|
||||
BatVoltage string `json:"battery_voltage"`
|
||||
BatCurrent string `json:"battery_current"`
|
||||
BatPower string `json:"battery_power"`
|
||||
BatCharge string `json:"battery_charge"`
|
||||
|
||||
InFreq string `json:"input_frequency"`
|
||||
OutFreq string `json:"output_frequency"`
|
||||
|
||||
LedMap map[string]string `json:"led_map"`
|
||||
}
|
||||
|
||||
func (w *WebGui) ServeHub(rw http.ResponseWriter, r *http.Request) {
|
||||
w.hub.ServeHTTP(rw, r)
|
||||
}
|
||||
|
||||
func ledName(led mk2driver.Led) string {
|
||||
name, ok := mk2driver.LedNames[led]
|
||||
if !ok {
|
||||
return "Unknown led"
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
func buildTemplateInput(status *mk2driver.Mk2Info) *templateInput {
|
||||
outPower := status.OutVoltage * status.OutCurrent
|
||||
inPower := status.InCurrent * status.InVoltage
|
||||
|
||||
tmpInput := &templateInput{
|
||||
Error: status.Errors,
|
||||
Date: status.Timestamp.Format(time.RFC1123Z),
|
||||
OutCurrent: fmt.Sprintf("%.2f", status.OutCurrent),
|
||||
OutVoltage: fmt.Sprintf("%.2f", status.OutVoltage),
|
||||
OutPower: fmt.Sprintf("%.2f", outPower),
|
||||
InCurrent: fmt.Sprintf("%.2f", status.InCurrent),
|
||||
InVoltage: fmt.Sprintf("%.2f", status.InVoltage),
|
||||
InFreq: fmt.Sprintf("%.2f", status.InFrequency),
|
||||
OutFreq: fmt.Sprintf("%.2f", status.OutFrequency),
|
||||
InPower: fmt.Sprintf("%.2f", inPower),
|
||||
|
||||
InMinOut: fmt.Sprintf("%.2f", inPower-outPower),
|
||||
|
||||
BatCurrent: fmt.Sprintf("%.2f", status.BatCurrent),
|
||||
BatVoltage: fmt.Sprintf("%.2f", status.BatVoltage),
|
||||
BatPower: fmt.Sprintf("%.2f", status.BatVoltage*status.BatCurrent),
|
||||
BatCharge: fmt.Sprintf("%.2f", status.ChargeState*100),
|
||||
|
||||
LedMap: map[string]string{},
|
||||
}
|
||||
for k, v := range status.LEDs {
|
||||
if k == mk2driver.LedOverload || k == mk2driver.LedTemperature || k == mk2driver.LedLowBattery {
|
||||
switch v {
|
||||
case mk2driver.LedOn:
|
||||
tmpInput.LedMap[ledName(k)] = LedRed
|
||||
case mk2driver.LedBlink:
|
||||
tmpInput.LedMap[ledName(k)] = BlinkRed
|
||||
default:
|
||||
tmpInput.LedMap[ledName(k)] = LedOff
|
||||
}
|
||||
} else {
|
||||
switch v {
|
||||
case mk2driver.LedOn:
|
||||
tmpInput.LedMap[ledName(k)] = LedGreen
|
||||
case mk2driver.LedBlink:
|
||||
tmpInput.LedMap[ledName(k)] = BlinkGreen
|
||||
default:
|
||||
tmpInput.LedMap[ledName(k)] = LedOff
|
||||
}
|
||||
}
|
||||
}
|
||||
return tmpInput
|
||||
}
|
||||
|
||||
func (w *WebGui) Stop() {
|
||||
close(w.stopChan)
|
||||
w.wg.Wait()
|
||||
}
|
||||
|
||||
// dataPoll waits for data from the w.poller channel. It will send its currently stored status
|
||||
// to respChan if anything reads from it.
|
||||
func (w *WebGui) dataPoll() {
|
||||
for {
|
||||
select {
|
||||
case s := <-w.C():
|
||||
if s.Valid {
|
||||
err := w.hub.Broadcast(buildTemplateInput(s))
|
||||
if err != nil {
|
||||
log.Printf("Could not send update to clients: %v", err)
|
||||
}
|
||||
}
|
||||
case <-w.stopChan:
|
||||
w.wg.Done()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
98
plugins/webui/webgui_test.go
Normal file
98
plugins/webui/webgui_test.go
Normal file
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
Copyright (c) 2015, Hendrik van Wyk
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
* Neither the name of invertergui nor the names of its
|
||||
contributors may be used to endorse or promote products derived from
|
||||
this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
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.
|
||||
*/
|
||||
|
||||
package webui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/diebietse/invertergui/mk2driver"
|
||||
)
|
||||
|
||||
func TestWebGui(t *testing.T) {
|
||||
t.Skip("Not yet implimented")
|
||||
//TODO figure out how to test template output.
|
||||
}
|
||||
|
||||
type templateTest struct {
|
||||
input *mk2driver.Mk2Info
|
||||
output *templateInput
|
||||
}
|
||||
|
||||
var fakenow = time.Date(2017, 1, 2, 3, 4, 5, 6, time.UTC)
|
||||
var templateInputTests = []templateTest{
|
||||
{
|
||||
input: &mk2driver.Mk2Info{
|
||||
OutCurrent: 2.0,
|
||||
InCurrent: 2.3,
|
||||
OutVoltage: 230.0,
|
||||
InVoltage: 230.1,
|
||||
BatVoltage: 25,
|
||||
BatCurrent: -10,
|
||||
InFrequency: 50,
|
||||
OutFrequency: 50,
|
||||
ChargeState: 1,
|
||||
LEDs: map[mk2driver.Led]mk2driver.LEDstate{mk2driver.LedMain: mk2driver.LedOn},
|
||||
Errors: nil,
|
||||
Timestamp: fakenow,
|
||||
},
|
||||
output: &templateInput{
|
||||
Error: nil,
|
||||
Date: fakenow.Format(time.RFC1123Z),
|
||||
OutCurrent: "2.00",
|
||||
OutVoltage: "230.00",
|
||||
OutPower: "460.00",
|
||||
InCurrent: "2.30",
|
||||
InVoltage: "230.10",
|
||||
InPower: "529.23",
|
||||
InMinOut: "69.23",
|
||||
BatVoltage: "25.00",
|
||||
BatCurrent: "-10.00",
|
||||
BatPower: "-250.00",
|
||||
InFreq: "50.00",
|
||||
OutFreq: "50.00",
|
||||
BatCharge: "100.00",
|
||||
LedMap: map[string]string{"led_mains": "dot-green"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func TestTemplateInput(t *testing.T) {
|
||||
for i := range templateInputTests {
|
||||
templateInput := buildTemplateInput(templateInputTests[i].input)
|
||||
if !reflect.DeepEqual(templateInput, templateInputTests[i].output) {
|
||||
t.Errorf("buildTemplateInput not producing expected results")
|
||||
fmt.Printf("%v\n%v\n", templateInput, templateInputTests[i].output)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user