chore: persist device details as json (#15224)
This commit is contained in:
parent
78a78b3fa3
commit
4005b3836a
5 changed files with 118 additions and 54 deletions
|
|
@ -324,10 +324,15 @@ export default {
|
|||
return this.templateParams.filter((p) => p.Advanced);
|
||||
},
|
||||
apiData() {
|
||||
return {
|
||||
const data = {
|
||||
template: this.templateName,
|
||||
...this.values,
|
||||
};
|
||||
// trim and remove empty lines
|
||||
if (Array.isArray(data.identifiers)) {
|
||||
data.identifiers = data.identifiers.map((i) => i.trim()).filter((i) => i);
|
||||
}
|
||||
return data;
|
||||
},
|
||||
isNew() {
|
||||
return this.id === undefined;
|
||||
|
|
|
|||
|
|
@ -22,7 +22,11 @@
|
|||
</button>
|
||||
{{ $t("offline.restarting") }}
|
||||
</div>
|
||||
<div v-else-if="restartNeeded" class="d-flex align-items-center">
|
||||
<div
|
||||
v-else-if="restartNeeded"
|
||||
class="d-flex align-items-center"
|
||||
data-testid="restart-needed"
|
||||
>
|
||||
<button
|
||||
class="btn btn-secondary me-2 btn-sm d-flex align-items-center"
|
||||
type="button"
|
||||
|
|
@ -40,6 +44,7 @@
|
|||
<div
|
||||
v-else-if="showError"
|
||||
class="d-flex align-items-start container px-4 justify-content-center"
|
||||
data-testid="fatal-error"
|
||||
>
|
||||
<shopicon-regular-car1
|
||||
size="m"
|
||||
|
|
|
|||
|
|
@ -1,9 +1,7 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/evcc-io/evcc/util/config"
|
||||
|
|
@ -48,16 +46,7 @@ func runDevice(cmd *cobra.Command, args []string) {
|
|||
|
||||
for _, d := range devs {
|
||||
fmt.Printf("%d. %s\n", d.ID, d.Type)
|
||||
|
||||
details := d.Details
|
||||
slices.SortFunc(details, func(i, j config.ConfigDetail) int {
|
||||
return cmp.Compare(i.Key, j.Key)
|
||||
})
|
||||
|
||||
for _, d := range details {
|
||||
fmt.Printf("%s: %s\n", d.Key, d.Value)
|
||||
}
|
||||
|
||||
fmt.Println(d.Value)
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -167,4 +167,34 @@ test.describe("vehicles", async () => {
|
|||
await expect(vehicleModal.getByLabel("Cache optional")).toBeVisible();
|
||||
await expect(vehicleModal.getByLabel("Default mode")).toBeVisible();
|
||||
});
|
||||
|
||||
test("save and restore rfid identifiers", async ({ page }) => {
|
||||
await page.goto("/#/config");
|
||||
await login(page);
|
||||
await enableExperimental(page);
|
||||
|
||||
await page.getByTestId("add-vehicle").click();
|
||||
const vehicleModal = page.getByTestId("vehicle-modal");
|
||||
|
||||
// generic
|
||||
await vehicleModal.getByLabel("Manufacturer").selectOption("Generic vehicle");
|
||||
await vehicleModal.getByLabel("Title").fill("RFID Car");
|
||||
await page.getByRole("button", { name: "Show advanced settings" }).click();
|
||||
await vehicleModal.getByLabel("RFID identifiers").fill("aaa\nbbb \n ccc\n\nddd\n");
|
||||
await vehicleModal.getByRole("button", { name: "Validate & save" }).click();
|
||||
await expect(page.getByTestId("restart-needed")).toBeVisible();
|
||||
|
||||
// restart evcc
|
||||
await restart(CONFIG_GRID_ONLY);
|
||||
await page.reload();
|
||||
|
||||
await page
|
||||
.locator(`[data-testid="vehicle"]:has-text("RFID Car")`)
|
||||
.getByRole("button", { name: "edit" })
|
||||
.click();
|
||||
await page.getByRole("button", { name: "Show advanced settings" }).click();
|
||||
await expect(vehicleModal.getByLabel("RFID identifiers")).toHaveValue("aaa\nbbb\nccc\nddd");
|
||||
await page.getByTestId("vehicle-modal").getByLabel("Close").click();
|
||||
await expect(page.getByTestId("fatal-error")).not.toBeVisible();
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/evcc-io/evcc/util/templates"
|
||||
|
|
@ -8,13 +9,14 @@ import (
|
|||
)
|
||||
|
||||
type Config struct {
|
||||
ID int `gorm:"primarykey"`
|
||||
Class templates.Class
|
||||
Type string
|
||||
Details []ConfigDetail `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
|
||||
ID int `gorm:"primarykey"`
|
||||
Class templates.Class
|
||||
Type string
|
||||
Value string
|
||||
}
|
||||
|
||||
type ConfigDetail struct {
|
||||
// TODO remove- migration only
|
||||
type ConfigDetails struct {
|
||||
ConfigID int `gorm:"index:idx_unique"`
|
||||
Key string `gorm:"index:idx_unique"`
|
||||
Value string
|
||||
|
|
@ -41,20 +43,17 @@ func (d *Config) Typed() Typed {
|
|||
|
||||
// detailsAsMap converts device details to map
|
||||
func (d *Config) detailsAsMap() map[string]any {
|
||||
res := make(map[string]any, len(d.Details))
|
||||
for _, detail := range d.Details {
|
||||
res[detail.Key] = detail.Value
|
||||
res := make(map[string]any)
|
||||
if err := json.Unmarshal([]byte(d.Value), &res); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
// detailsFromMap converts map to device details
|
||||
func detailsFromMap(config map[string]any) []ConfigDetail {
|
||||
res := make([]ConfigDetail, 0, len(config))
|
||||
for k, v := range config {
|
||||
res = append(res, ConfigDetail{Key: k, Value: fmt.Sprintf("%v", v)})
|
||||
}
|
||||
return res
|
||||
func detailsFromMap(config map[string]any) (string, error) {
|
||||
b, err := json.Marshal(config)
|
||||
return string(b), err
|
||||
}
|
||||
|
||||
// Update updates a config's details to the database
|
||||
|
|
@ -65,11 +64,11 @@ func (d *Config) Update(conf map[string]any) error {
|
|||
return err
|
||||
}
|
||||
|
||||
if err := tx.Delete(new(ConfigDetail), ConfigDetail{ConfigID: d.ID}).Error; err != nil {
|
||||
val, err := detailsFromMap(conf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
d.Details = detailsFromMap(conf)
|
||||
d.Value = val
|
||||
|
||||
return tx.Save(&d).Error
|
||||
})
|
||||
|
|
@ -77,38 +76,71 @@ func (d *Config) Update(conf map[string]any) error {
|
|||
|
||||
// Delete deletes a config from the database
|
||||
func (d *Config) Delete() error {
|
||||
return db.Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Delete(new(ConfigDetail), ConfigDetail{ConfigID: d.ID}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Delete(Config{ID: d.ID}).Error
|
||||
})
|
||||
return db.Delete(Config{ID: d.ID}).Error
|
||||
}
|
||||
|
||||
var db *gorm.DB
|
||||
|
||||
func Init(instance *gorm.DB) error {
|
||||
db = instance
|
||||
m := db.Migrator()
|
||||
|
||||
for old, new := range map[string]string{
|
||||
"devices": "configs",
|
||||
"device_details": "config_details",
|
||||
} {
|
||||
if m := db.Migrator(); m.HasTable(old) {
|
||||
if m.HasTable(old) {
|
||||
if err := m.RenameTable(old, new); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
err := db.AutoMigrate(new(Config), new(ConfigDetail))
|
||||
err := m.AutoMigrate(new(Config))
|
||||
|
||||
if err == nil && db.Migrator().HasConstraint(new(ConfigDetail), "fk_devices_details") {
|
||||
err = db.Migrator().DropConstraint(new(ConfigDetail), "fk_devices_details")
|
||||
if err == nil && m.HasTable("config_details") {
|
||||
err = m.AutoMigrate(new(ConfigDetails))
|
||||
|
||||
if err == nil && m.HasConstraint(new(ConfigDetails), "fk_devices_details") {
|
||||
err = m.DropConstraint(new(ConfigDetails), "fk_devices_details")
|
||||
}
|
||||
if err == nil && m.HasColumn(new(ConfigDetails), "device_id") {
|
||||
err = m.DropColumn(new(ConfigDetails), "device_id")
|
||||
}
|
||||
}
|
||||
if err == nil && db.Migrator().HasColumn(new(ConfigDetail), "device_id") {
|
||||
err = db.Migrator().DropColumn(new(ConfigDetail), "device_id")
|
||||
|
||||
if err == nil && m.HasTable("config_details") {
|
||||
var devices []Config
|
||||
if err := db.Where(&Config{}).Find(&devices).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// migrate ConfigDetails into Config.Value
|
||||
for _, dev := range devices {
|
||||
var details []ConfigDetails
|
||||
if err := db.Where(&ConfigDetails{ConfigID: dev.ID}).Find(&details).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
res := make(map[string]any)
|
||||
for _, detail := range details {
|
||||
res[detail.Key] = detail.Value
|
||||
}
|
||||
|
||||
if len(res) > 0 {
|
||||
val, err := detailsFromMap(res)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
dev.Value = val
|
||||
|
||||
if err := db.Save(&dev).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
err = m.DropTable("config_details")
|
||||
}
|
||||
|
||||
return err
|
||||
|
|
@ -122,12 +154,12 @@ func NameForID(id int) string {
|
|||
// ConfigurationsByClass returns devices by class from the database
|
||||
func ConfigurationsByClass(class templates.Class) ([]Config, error) {
|
||||
var devices []Config
|
||||
tx := db.Where(&Config{Class: class}).Preload("Details").Order("id").Find(&devices)
|
||||
tx := db.Where(&Config{Class: class}).Find(&devices)
|
||||
|
||||
// remove devices without details
|
||||
res := make([]Config, 0, len(devices))
|
||||
for _, dev := range devices {
|
||||
if len(dev.Details) > 0 {
|
||||
if len(dev.Value) > 0 {
|
||||
res = append(res, dev)
|
||||
}
|
||||
}
|
||||
|
|
@ -138,21 +170,24 @@ func ConfigurationsByClass(class templates.Class) ([]Config, error) {
|
|||
// ConfigByID returns device by id from the database
|
||||
func ConfigByID(id int) (Config, error) {
|
||||
var config Config
|
||||
tx := db.Where(&Config{ID: id}).Preload("Details").First(&config)
|
||||
tx := db.Where(&Config{ID: id}).First(&config)
|
||||
return config, tx.Error
|
||||
}
|
||||
|
||||
// AddConfig adds a new config to the database
|
||||
func AddConfig(class templates.Class, typ string, conf map[string]any) (Config, error) {
|
||||
config := Config{
|
||||
Class: class,
|
||||
Type: typ,
|
||||
Details: detailsFromMap(conf),
|
||||
val, err := detailsFromMap(conf)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
err := db.Transaction(func(tx *gorm.DB) error {
|
||||
return tx.Create(&config).Error
|
||||
})
|
||||
config := Config{
|
||||
Class: class,
|
||||
Type: typ,
|
||||
Value: val,
|
||||
}
|
||||
|
||||
err = db.Create(&config).Error
|
||||
|
||||
return config, err
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue