-
-
Notifications
You must be signed in to change notification settings - Fork 784
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Relay HEMS for analog interface (#15116)
- Loading branch information
Showing
4 changed files
with
101 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package relay | ||
|
||
import ( | ||
"errors" | ||
"time" | ||
|
||
"github.com/evcc-io/evcc/api" | ||
"github.com/evcc-io/evcc/core/circuit" | ||
"github.com/evcc-io/evcc/core/site" | ||
"github.com/evcc-io/evcc/provider" | ||
"github.com/evcc-io/evcc/util" | ||
) | ||
|
||
type Relay struct { | ||
log *util.Logger | ||
|
||
root api.Circuit | ||
limit func() (bool, error) | ||
maxPower float64 | ||
} | ||
|
||
// New creates an Relay HEMS from generic config | ||
func New(other map[string]interface{}, site site.API) (*Relay, error) { | ||
var cc struct { | ||
MaxPower float64 | ||
Limit provider.Config | ||
} | ||
|
||
if err := util.DecodeOther(other, &cc); err != nil { | ||
return nil, err | ||
} | ||
|
||
// get root circuit | ||
root := circuit.Root() | ||
if root == nil { | ||
return nil, errors.New("hems requires load management- please configure root circuit") | ||
} | ||
|
||
// create new root circuit for LPC | ||
lpc, err := circuit.New(util.NewLogger("lpc"), "relay", 0, 0, nil, time.Minute) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// wrap old root with new pc parent | ||
if err := root.Wrap(lpc); err != nil { | ||
return nil, err | ||
} | ||
site.SetCircuit(lpc) | ||
|
||
// limit getter | ||
limitG, err := provider.NewBoolGetterFromConfig(cc.Limit) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return NewRelay(lpc, limitG, cc.MaxPower) | ||
} | ||
|
||
// NewRelay creates Relay HEMS | ||
func NewRelay(root api.Circuit, limit func() (bool, error), maxPower float64) (*Relay, error) { | ||
c := &Relay{ | ||
log: util.NewLogger("relay"), | ||
root: root, | ||
maxPower: maxPower, | ||
limit: limit, | ||
} | ||
|
||
return c, nil | ||
} | ||
|
||
func (c *Relay) Run() { | ||
for range time.Tick(10 * time.Second) { | ||
if err := c.run(); err != nil { | ||
c.log.ERROR.Println(err) | ||
} | ||
} | ||
} | ||
|
||
func (c *Relay) run() error { | ||
limit, err := c.limit() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var power float64 | ||
if limit { | ||
power = c.maxPower | ||
} | ||
|
||
c.root.SetMaxPower(power) | ||
|
||
return nil | ||
} |