OCPP/EEBus: lazy-start servers so the CLI works against a running evcc (#30839)
Co-authored-by: Michael Geers <michael@geers.tv>
This commit is contained in:
parent
518556e6b8
commit
fc84062905
23 changed files with 237 additions and 159 deletions
55
cmd/root.go
55
cmd/root.go
|
|
@ -200,28 +200,39 @@ func runRoot(cmd *cobra.Command, args []string) {
|
|||
valueChan := make(chan util.Param, 64)
|
||||
go tee.Run(valueChan)
|
||||
|
||||
// start OCPP server
|
||||
ocppCS := ocpp.Instance()
|
||||
ocppCS.SetUpdated(func() {
|
||||
// republish when OCPP state updates
|
||||
valueChan <- util.Param{Key: keys.Ocpp, Val: globalconfig.ConfigStatus{
|
||||
Config: ocpp.CurrentConfig(),
|
||||
Status: ocpp.GetStatus(),
|
||||
}}
|
||||
})
|
||||
log.INFO.Printf("OCPP local url: ws://127.0.0.1:%d/<stationId>", conf.Ocpp.Port)
|
||||
if ocpp.ExternalUrl() != "" {
|
||||
log.INFO.Printf("OCPP external url: %s/<stationId>", ocpp.ExternalUrl())
|
||||
}
|
||||
// register the callback even with no rules so runtime additions are pushed
|
||||
ocpp.SetForwarderUpdated(func() {
|
||||
valueChan <- util.Param{Key: keys.OcppForwarder, Val: globalconfig.ConfigStatus{
|
||||
Config: lo.Map(ocpp.ForwarderRules(), func(r ocpp.ForwarderRule, _ int) ocpp.ForwarderRule { return r.Redacted() }),
|
||||
Status: ocpp.GetForwarderStatus(),
|
||||
}}
|
||||
})
|
||||
if ocpp.ForwarderEnabled() {
|
||||
log.INFO.Printf("OCPP forwarder: %d rule(s) active", len(ocpp.ForwarderRules()))
|
||||
// start OCPP and EEBus servers (skipped in degraded mode where setup failed,
|
||||
// so a misconfigured instance serves only the offline UI)
|
||||
if err == nil {
|
||||
cs, ocppErr := ocpp.Instance()
|
||||
if ocppErr != nil {
|
||||
log.ERROR.Println("ocpp:", ocppErr)
|
||||
} else {
|
||||
cs.SetUpdated(func() {
|
||||
// republish when OCPP state updates
|
||||
valueChan <- util.Param{Key: keys.Ocpp, Val: globalconfig.ConfigStatus{
|
||||
Config: ocpp.CurrentConfig(),
|
||||
Status: ocpp.GetStatus(),
|
||||
}}
|
||||
})
|
||||
log.INFO.Printf("OCPP local url: ws://127.0.0.1:%d/<stationId>", conf.Ocpp.Port)
|
||||
if ocpp.ExternalUrl() != "" {
|
||||
log.INFO.Printf("OCPP external url: %s/<stationId>", ocpp.ExternalUrl())
|
||||
}
|
||||
}
|
||||
// register the callback even with no rules so runtime additions are pushed
|
||||
ocpp.SetForwarderUpdated(func() {
|
||||
valueChan <- util.Param{Key: keys.OcppForwarder, Val: globalconfig.ConfigStatus{
|
||||
Config: lo.Map(ocpp.ForwarderRules(), func(r ocpp.ForwarderRule, _ int) ocpp.ForwarderRule { return r.Redacted() }),
|
||||
Status: ocpp.GetForwarderStatus(),
|
||||
}}
|
||||
})
|
||||
if ocpp.ForwarderEnabled() {
|
||||
log.INFO.Printf("OCPP forwarder: %d rule(s) active", len(ocpp.ForwarderRules()))
|
||||
}
|
||||
|
||||
if _, eebusErr := eebus.Instance(); eebusErr != nil {
|
||||
log.ERROR.Println("eebus:", eebusErr)
|
||||
}
|
||||
}
|
||||
|
||||
// value cache
|
||||
|
|
|
|||
16
cmd/setup.go
16
cmd/setup.go
|
|
@ -853,11 +853,16 @@ func configureOCPP(cfg *ocpp.Config, externalUrl string) {
|
|||
log.WARN.Printf("ocpp: failed to load settings: %v", err)
|
||||
}
|
||||
}
|
||||
ocpp.Init(*cfg, externalUrl)
|
||||
ocpp.NewServer(*cfg, externalUrl)
|
||||
|
||||
// Load proxy forwarding rules from DB if present.
|
||||
var rules []ocpp.ForwarderRule
|
||||
if err := settings.Json(keys.OcppForwarder, &rules); err == nil {
|
||||
if err := settings.Json(keys.OcppForwarder, &rules); err == nil && len(rules) > 0 {
|
||||
// the forwarder relays through the central system; abort if it failed to start
|
||||
if _, err := ocpp.Instance(); err != nil {
|
||||
log.ERROR.Printf("ocpp: forwarder disabled: %v", err)
|
||||
return
|
||||
}
|
||||
ocpp.ApplyForwarderRules(rules)
|
||||
}
|
||||
}
|
||||
|
|
@ -885,13 +890,12 @@ func configureEEBus(conf *eebus.Config) error {
|
|||
}
|
||||
}
|
||||
|
||||
var err error
|
||||
if eebus.Instance, err = eebus.NewServer(*conf); err != nil {
|
||||
srv, err := eebus.NewServer(*conf)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed configuring eebus: %w", err)
|
||||
}
|
||||
|
||||
eebus.Instance.Run()
|
||||
shutdown.Register(eebus.Instance.Shutdown)
|
||||
shutdown.Register(srv.Shutdown)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue