parent
ba66fdd15f
commit
c693d11a60
4 changed files with 230 additions and 0 deletions
1
go.mod
1
go.mod
|
|
@ -50,6 +50,7 @@ require (
|
|||
github.com/manifoldco/promptui v0.8.0
|
||||
github.com/mattn/go-runewidth v0.0.13 // indirect
|
||||
github.com/mitchellh/mapstructure v1.4.1
|
||||
github.com/mlnoga/rct v0.0.0-20210927211248-455af36ba3ad
|
||||
github.com/muka/go-bluetooth v0.0.0-20210508070623-03c23c62f181
|
||||
github.com/mxschmitt/golang-combinations v1.1.0
|
||||
github.com/nirasan/go-oauth-pkce-code-verifier v0.0.0-20170819232839-0fbfe93532da
|
||||
|
|
|
|||
2
go.sum
2
go.sum
|
|
@ -506,6 +506,8 @@ github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RR
|
|||
github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY=
|
||||
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
|
||||
github.com/mjibson/esc v0.2.0/go.mod h1:9Hw9gxxfHulMF5OJKCyhYD7PzlSdhzXyaGEBRPH1OPs=
|
||||
github.com/mlnoga/rct v0.0.0-20210927211248-455af36ba3ad h1:Day8m/hpA40ofm/cYfJU9ac8onwWDtm+XlaWSFBm9WM=
|
||||
github.com/mlnoga/rct v0.0.0-20210927211248-455af36ba3ad/go.mod h1:vHVb62/NOw0QkfTMCNxRSxMqvm4+4WQ8HkZQ4Z540GE=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
|
|
|
|||
158
meter/rct.go
Normal file
158
meter/rct.go
Normal file
|
|
@ -0,0 +1,158 @@
|
|||
package meter
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/evcc-io/evcc/api"
|
||||
"github.com/evcc-io/evcc/util"
|
||||
|
||||
"github.com/mlnoga/rct"
|
||||
)
|
||||
|
||||
/*
|
||||
This meter supports devices implementing the RCT communication protocol, e.g. the RCT PS 6.0 with / without battery.
|
||||
|
||||
** Usages **
|
||||
The following usages are supported:
|
||||
- grid ... for reading the power imported or exported to the grid
|
||||
- pv ... for reading the power produced by the pv
|
||||
- battery ... for reading the power imported or exported to the battery
|
||||
|
||||
** Example configuration **
|
||||
meters:
|
||||
- name: GridMeter
|
||||
type: rct
|
||||
uri: 192.168.1.23
|
||||
cache: 2s
|
||||
usage: grid
|
||||
- name: PvMeter
|
||||
type: rct
|
||||
uri: 192.168.1.23
|
||||
cache: 2s
|
||||
usage: pv
|
||||
- name: BatteryMeter
|
||||
type: rct
|
||||
uri: 192.168.1.23
|
||||
cache: 2s
|
||||
usage: battery
|
||||
*/
|
||||
|
||||
// RCT implements the api.Meter interface
|
||||
type RCT struct {
|
||||
conn *rct.Connection // connection with the RCT device
|
||||
usage string // grid, pv, battery
|
||||
}
|
||||
|
||||
func init() {
|
||||
registry.Add("rct", NewRCTFromConfig)
|
||||
}
|
||||
|
||||
//go:generate go run ../cmd/tools/decorate.go -f decorateRCT -b *RCT -r api.Meter -t "api.MeterEnergy,TotalEnergy,func() (float64, error)" -t "api.Battery,SoC,func() (float64, error)"
|
||||
|
||||
// NewRCTFromConfig creates an RCT from generic config
|
||||
func NewRCTFromConfig(other map[string]interface{}) (api.Meter, error) {
|
||||
cc := struct {
|
||||
Uri, Usage string
|
||||
Cache time.Duration
|
||||
}{
|
||||
Cache: time.Second,
|
||||
}
|
||||
|
||||
if err := util.DecodeOther(other, &cc); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if cc.Usage == "" {
|
||||
return nil, errors.New("missing usage")
|
||||
}
|
||||
|
||||
return NewRCT(cc.Uri, cc.Usage, cc.Cache)
|
||||
}
|
||||
|
||||
// NewRCT creates an RCT meter
|
||||
func NewRCT(uri, usage string, cache time.Duration) (api.Meter, error) {
|
||||
conn, err := rct.NewConnection(uri, cache)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
m := &RCT{
|
||||
usage: strings.ToLower(usage),
|
||||
conn: conn,
|
||||
}
|
||||
|
||||
// decorate api.MeterEnergy
|
||||
var totalEnergy func() (float64, error)
|
||||
if usage == "grid" {
|
||||
totalEnergy = m.totalEnergy
|
||||
}
|
||||
|
||||
// decorate api.BatterySoC
|
||||
var batterySoC func() (float64, error)
|
||||
if usage == "battery" {
|
||||
batterySoC = m.batterySoC
|
||||
}
|
||||
|
||||
return decorateRCT(m, totalEnergy, batterySoC), nil
|
||||
}
|
||||
|
||||
// CurrentPower implements the api.Meter interface
|
||||
func (m *RCT) CurrentPower() (float64, error) {
|
||||
switch m.usage {
|
||||
case "grid":
|
||||
res, err := m.conn.QueryFloat32(rct.TotalGridPowerW)
|
||||
return float64(res), err
|
||||
|
||||
case "pv":
|
||||
a, err := m.conn.QueryFloat32(rct.SolarGenAPowerW)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
b, err := m.conn.QueryFloat32(rct.SolarGenBPowerW)
|
||||
return float64(a + b), err
|
||||
|
||||
case "battery":
|
||||
res, err := m.conn.QueryFloat32(rct.BatteryPowerW)
|
||||
return float64(res), err
|
||||
|
||||
default:
|
||||
return 0, fmt.Errorf("invalid usage: %s", m.usage)
|
||||
}
|
||||
}
|
||||
|
||||
// totalEnergy implements the api.MeterEnergy interface
|
||||
func (m *RCT) totalEnergy() (float64, error) {
|
||||
switch m.usage {
|
||||
case "grid":
|
||||
res, err := m.conn.QueryFloat32(rct.TotalEnergyGridWh)
|
||||
return float64(res / 1000), err
|
||||
|
||||
case "pv":
|
||||
a, err := m.conn.QueryFloat32(rct.TotalEnergySolarGenAWh)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
b, err := m.conn.QueryFloat32(rct.TotalEnergySolarGenBWh)
|
||||
return float64((a + b) / 1000), err
|
||||
|
||||
case "battery":
|
||||
in, err := m.conn.QueryFloat32(rct.TotalEnergyBattInWh)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
out, err := m.conn.QueryFloat32(rct.TotalEnergyBattOutWh)
|
||||
return float64((in - out) / 1000), err
|
||||
|
||||
default:
|
||||
return 0, fmt.Errorf("invalid usage: %s", m.usage)
|
||||
}
|
||||
}
|
||||
|
||||
// batterySoC implements the api.Battery interface
|
||||
func (m *RCT) batterySoC() (float64, error) {
|
||||
res, err := m.conn.QueryFloat32(rct.BatterySoC)
|
||||
return float64(res * 100), err
|
||||
}
|
||||
69
meter/rct_decorators.go
Normal file
69
meter/rct_decorators.go
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
package meter
|
||||
|
||||
// Code generated by github.com/evcc-io/evcc/cmd/tools/decorate.go. DO NOT EDIT.
|
||||
|
||||
import (
|
||||
"github.com/evcc-io/evcc/api"
|
||||
)
|
||||
|
||||
func decorateRCT(base *RCT, meterEnergy func() (float64, error), battery func() (float64, error)) api.Meter {
|
||||
switch {
|
||||
case battery == nil && meterEnergy == nil:
|
||||
return base
|
||||
|
||||
case battery == nil && meterEnergy != nil:
|
||||
return &struct {
|
||||
*RCT
|
||||
api.MeterEnergy
|
||||
}{
|
||||
RCT: base,
|
||||
MeterEnergy: &decorateRCTMeterEnergyImpl{
|
||||
meterEnergy: meterEnergy,
|
||||
},
|
||||
}
|
||||
|
||||
case battery != nil && meterEnergy == nil:
|
||||
return &struct {
|
||||
*RCT
|
||||
api.Battery
|
||||
}{
|
||||
RCT: base,
|
||||
Battery: &decorateRCTBatteryImpl{
|
||||
battery: battery,
|
||||
},
|
||||
}
|
||||
|
||||
case battery != nil && meterEnergy != nil:
|
||||
return &struct {
|
||||
*RCT
|
||||
api.Battery
|
||||
api.MeterEnergy
|
||||
}{
|
||||
RCT: base,
|
||||
Battery: &decorateRCTBatteryImpl{
|
||||
battery: battery,
|
||||
},
|
||||
MeterEnergy: &decorateRCTMeterEnergyImpl{
|
||||
meterEnergy: meterEnergy,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type decorateRCTBatteryImpl struct {
|
||||
battery func() (float64, error)
|
||||
}
|
||||
|
||||
func (impl *decorateRCTBatteryImpl) SoC() (float64, error) {
|
||||
return impl.battery()
|
||||
}
|
||||
|
||||
type decorateRCTMeterEnergyImpl struct {
|
||||
meterEnergy func() (float64, error)
|
||||
}
|
||||
|
||||
func (impl *decorateRCTMeterEnergyImpl) TotalEnergy() (float64, error) {
|
||||
return impl.meterEnergy()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue