Skip to content

Commit

Permalink
Config UI: unify configured state handling (#18237)
Browse files Browse the repository at this point in the history
  • Loading branch information
naltatis authored Jan 15, 2025
1 parent e6e8aa1 commit 6cdd844
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 95 deletions.
39 changes: 17 additions & 22 deletions assets/js/views/Config.vue
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
>
<template #icon><NotificationIcon /></template>
<template #tags>
<DeviceTags :tags="yamlTags('messaging')" />
<DeviceTags :tags="messagingTags" />
</template>
</DeviceCard>
<DeviceCard
Expand All @@ -210,7 +210,7 @@
>
<template #icon><EebusIcon /></template>
<template #tags>
<DeviceTags :tags="yamlTags('eebus')" />
<DeviceTags :tags="eebusTags" />
</template>
</DeviceCard>
<DeviceCard
Expand All @@ -224,7 +224,7 @@
<template #tags>
<DeviceTags
v-if="circuits.length == 0"
:tags="yamlTags('circuits')"
:tags="{ configured: { value: false } }"
/>
<template
v-for="(circuit, idx) in circuits"
Expand All @@ -249,7 +249,7 @@
>
<template #icon><ModbusProxyIcon /></template>
<template #tags>
<DeviceTags :tags="yamlTags('modbusproxy')" />
<DeviceTags :tags="modbusproxyTags" />
</template>
</DeviceCard>
<DeviceCard
Expand Down Expand Up @@ -390,12 +390,6 @@ export default {
site: { grid: "", pv: [], battery: [] },
deviceValueTimeout: undefined,
deviceValues: {},
yamlConfigState: {
messaging: false,
eebus: false,
circuits: false,
modbusproxy: false,
},
};
},
computed: {
Expand Down Expand Up @@ -462,6 +456,19 @@ export default {
}
return result;
},
eebusTags() {
return { configured: { value: store.state?.eebus || false } };
},
modbusproxyTags() {
const config = store.state?.modbusproxy || [];
if (config.length > 0) {
return { amount: { value: config.length } };
}
return { configured: { value: false } };
},
messagingTags() {
return { configured: { value: store.state?.messaging || false } };
},
},
watch: {
offline() {
Expand All @@ -484,7 +491,6 @@ export default {
await this.loadCircuits();
await this.loadDirty();
await this.updateValues();
await this.updateYamlConfigState();
},
async loadDirty() {
const response = await api.get("/config/dirty");
Expand Down Expand Up @@ -567,7 +573,6 @@ export default {
},
yamlChanged() {
this.loadDirty();
this.updateYamlConfigState();
},
addMeterToSite(type, name) {
if (type === "grid") {
Expand Down Expand Up @@ -636,16 +641,6 @@ export default {
console.error(`modal ${id} not found`);
}
},
updateYamlConfigState() {
const keys = Object.keys(this.yamlConfigState);
keys.forEach(async (key) => {
const res = await api.get(`/config/${key}`);
this.yamlConfigState[key] = !!res.data.result;
});
},
yamlTags(key) {
return { configured: { value: this.yamlConfigState[key] } };
},
circuitTags(circuit) {
const data = store.state?.circuits[circuit.name] || {};
const result = {};
Expand Down
2 changes: 1 addition & 1 deletion cmd/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func runMigrate(cmd *cobra.Command, args []string) {
log.DEBUG.Println("- eebus")
if reset {
settings.Delete(keys.EEBus)
} else if conf.EEBus.URI != "" {
} else if conf.EEBus.Configured() {
_ = settings.SetYaml(keys.EEBus, conf.EEBus)
}

Expand Down
25 changes: 14 additions & 11 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func runRoot(cmd *cobra.Command, args []string) {

// setup modbus proxy
if err == nil {
err = wrapErrorWithClass(ClassModbusProxy, configureModbusProxy(conf.ModbusProxy))
err = wrapErrorWithClass(ClassModbusProxy, configureModbusProxy(&conf.ModbusProxy))
}

// setup site and loadpoints
Expand Down Expand Up @@ -209,14 +209,6 @@ func runRoot(cmd *cobra.Command, args []string) {

// remove previous fatal startup errors
valueChan <- util.Param{Key: keys.Fatal, Val: nil}
// publish initial settings
valueChan <- util.Param{Key: keys.Interval, Val: conf.Interval}
valueChan <- util.Param{Key: keys.Network, Val: conf.Network}
valueChan <- util.Param{Key: keys.Mqtt, Val: conf.Mqtt}
valueChan <- util.Param{Key: keys.Influx, Val: conf.Influx}
valueChan <- util.Param{Key: keys.Hems, Val: conf.HEMS}
// TODO
valueChan <- util.Param{Key: keys.Sponsor, Val: sponsor.Status()}

// setup mqtt publisher
if err == nil && conf.Mqtt.Broker != "" {
Expand All @@ -234,16 +226,27 @@ func runRoot(cmd *cobra.Command, args []string) {

// start HEMS server
if err == nil {
err = wrapErrorWithClass(ClassHEMS, configureHEMS(conf.HEMS, site, httpd))
err = wrapErrorWithClass(ClassHEMS, configureHEMS(&conf.HEMS, site, httpd))
}

// setup messaging
var pushChan chan push.Event
if err == nil {
pushChan, err = configureMessengers(conf.Messaging, site.Vehicles(), valueChan, cache)
pushChan, err = configureMessengers(&conf.Messaging, site.Vehicles(), valueChan, cache)
err = wrapErrorWithClass(ClassMessenger, err)
}

// publish initial settings
valueChan <- util.Param{Key: keys.EEBus, Val: conf.EEBus.Configured()}
valueChan <- util.Param{Key: keys.Hems, Val: conf.HEMS}
valueChan <- util.Param{Key: keys.Influx, Val: conf.Influx}
valueChan <- util.Param{Key: keys.Interval, Val: conf.Interval}
valueChan <- util.Param{Key: keys.Messaging, Val: pushChan != nil}
valueChan <- util.Param{Key: keys.ModbusProxy, Val: conf.ModbusProxy}
valueChan <- util.Param{Key: keys.Mqtt, Val: conf.Mqtt}
valueChan <- util.Param{Key: keys.Network, Val: conf.Network}
valueChan <- util.Param{Key: keys.Sponsor, Val: sponsor.Status()}

// run shutdown functions on stop
var once sync.Once
stopC := make(chan struct{})
Expand Down
69 changes: 8 additions & 61 deletions cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ func configureEnvironment(cmd *cobra.Command, conf *globalconfig.All) (err error

// setup EEBus server
if err == nil {
err = wrapErrorWithClass(ClassEEBus, configureEEBus(conf.EEBus))
err = wrapErrorWithClass(ClassEEBus, configureEEBus(&conf.EEBus))
}

// setup javascript VMs
Expand Down Expand Up @@ -559,14 +559,6 @@ func configureInflux(conf *globalconfig.Influx) (*server.Influx, error) {
return nil, nil
}

// TODO remove yaml file
// // migrate settings
// if !settings.Exists(keys.Influx) {
// if err := settings.SetJson(keys.Influx, conf); err != nil {
// return nil, err
// }
// }

influx := server.NewInfluxClient(
conf.URL,
conf.Token,
Expand All @@ -587,13 +579,6 @@ func configureMqtt(conf *globalconfig.Mqtt) error {
if err := settings.Json(keys.Mqtt, &conf); err != nil {
return err
}

// TODO remove yaml file
// } else {
// // migrate settings & write defaults
// if err := settings.SetJson(keys.Mqtt, conf); err != nil {
// return err
// }
}

if conf.Broker == "" {
Expand Down Expand Up @@ -645,7 +630,7 @@ func configureGo(conf []globalconfig.Go) error {
}

// setup HEMS
func configureHEMS(conf globalconfig.Hems, site *core.Site, httpd *server.HTTPd) error {
func configureHEMS(conf *globalconfig.Hems, site *core.Site, httpd *server.HTTPd) error {
// migrate settings
if settings.Exists(keys.Hems) {
if err := settings.Yaml(keys.Hems, new(map[string]any), &conf); err != nil {
Expand All @@ -657,14 +642,6 @@ func configureHEMS(conf globalconfig.Hems, site *core.Site, httpd *server.HTTPd)
return nil
}

// TODO remove yaml file
// // migrate settings
// if !settings.Exists(keys.Hems) {
// if err := settings.SetYaml(keys.Hems, conf); err != nil {
// return err
// }
// }

hems, err := hems.NewFromConfig(context.TODO(), conf.Type, conf.Other, site, httpd)
if err != nil {
return fmt.Errorf("failed configuring hems: %w", err)
Expand All @@ -681,10 +658,6 @@ func networkSettings(conf *globalconfig.Network) error {
return settings.Json(keys.Network, &conf)
}

// TODO remove yaml file
// // migrate settings
// return settings.SetJson(keys.Network, conf)

return nil
}

Expand All @@ -703,7 +676,7 @@ func configureMDNS(conf globalconfig.Network) error {
}

// setup EEBus
func configureEEBus(conf eebus.Config) error {
func configureEEBus(conf *eebus.Config) error {
// migrate settings
if settings.Exists(keys.EEBus) {
if err := settings.Yaml(keys.EEBus, new(map[string]any), &conf); err != nil {
Expand All @@ -715,16 +688,8 @@ func configureEEBus(conf eebus.Config) error {
return nil
}

// TODO remove yaml file
// // migrate settings
// if !settings.Exists(keys.EEBus) {
// if err := settings.SetYaml(keys.EEBus, conf); err != nil {
// return err
// }
// }

var err error
if eebus.Instance, err = eebus.NewServer(conf); err != nil {
if eebus.Instance, err = eebus.NewServer(*conf); err != nil {
return fmt.Errorf("failed configuring eebus: %w", err)
}

Expand All @@ -735,18 +700,12 @@ func configureEEBus(conf eebus.Config) error {
}

// setup messaging
func configureMessengers(conf globalconfig.Messaging, vehicles push.Vehicles, valueChan chan<- util.Param, cache *util.Cache) (chan push.Event, error) {
func configureMessengers(conf *globalconfig.Messaging, vehicles push.Vehicles, valueChan chan<- util.Param, cache *util.Cache) (chan push.Event, error) {
// migrate settings
if settings.Exists(keys.Messaging) {
if err := settings.Yaml(keys.Messaging, new(map[string]any), &conf); err != nil {
return nil, err
}

// TODO remove yaml file
// } else if len(conf.Services)+len(conf.Events) > 0 {
// if err := settings.SetYaml(keys.Messaging, conf); err != nil {
// return nil, err
// }
}

messageChan := make(chan push.Event, 1)
Expand Down Expand Up @@ -807,12 +766,6 @@ func configureTariffs(conf globalconfig.Tariffs) (*tariff.Tariffs, error) {
if err := settings.Yaml(keys.Tariffs, new(map[string]any), &conf); err != nil {
return nil, err
}

// TODO remove yaml file
// } else if conf.Grid.Type != "" || conf.FeedIn.Type != "" || conf.Co2.Type != "" || conf.Planner.Type != "" {
// if err := settings.SetYaml(keys.Tariffs, conf); err != nil {
// return nil, err
// }
}

tariffs := tariff.Tariffs{
Expand Down Expand Up @@ -853,21 +806,15 @@ func configureDevices(conf globalconfig.All) error {
return nil
}

func configureModbusProxy(conf []globalconfig.ModbusProxy) error {
func configureModbusProxy(conf *[]globalconfig.ModbusProxy) error {
// migrate settings
if settings.Exists(keys.ModbusProxy) {
if err := settings.Yaml(keys.ModbusProxy, new([]map[string]any), &conf); err != nil {
if err := settings.Yaml(keys.ModbusProxy, new([]map[string]any), conf); err != nil {
return err
}

// TODO remove yaml file
// } else if len(conf) > 0 {
// if err := settings.SetYaml(keys.ModbusProxy, conf); err != nil {
// return err
// }
}

for _, cfg := range conf {
for _, cfg := range *conf {
var mode modbus.ReadOnlyMode
mode, err := modbus.ReadOnlyModeString(cfg.ReadOnly)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions i18n/de.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ labelResidualPower = "Residualleistung"
title = "Regelverhalten"

[config.deviceValue]
amount = "Anzahl"
broker = "Broker"
bucket = "Bucket"
capacity = "Kapazität"
Expand Down
1 change: 1 addition & 0 deletions i18n/en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ labelResidualPower = "Residual power"
title = "Control behavior"

[config.deviceValue]
amount = "Amount"
broker = "Broker"
bucket = "Bucket"
capacity = "Capacity"
Expand Down

0 comments on commit 6cdd844

Please sign in to comment.