config

package
v0.0.0-...-26973a0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 27, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Init

func Init(instance *gorm.DB) error

func Instances

func Instances[T any](devices []Device[T]) []T

Instances returns the instances of the given devices

func NameForID

func NameForID(id int) string

NameForID returns a unique config name for the given id

func Reset

func Reset()

Types

type Config

type Config struct {
	ID      int `gorm:"primarykey"`
	Class   templates.Class
	Type    string
	Details []ConfigDetail `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
}

func AddConfig

func AddConfig(class templates.Class, typ string, conf map[string]any) (Config, error)

AddConfig adds a new config to the database

func ConfigByID

func ConfigByID(id int) (Config, error)

ConfigByID returns device by id from the database

func ConfigurationsByClass

func ConfigurationsByClass(class templates.Class) ([]Config, error)

ConfigurationsByClass returns devices by class from the database

func (*Config) Delete

func (d *Config) Delete() error

Delete deletes a config from the database

func (*Config) Named

func (d *Config) Named() Named

Named converts device details to named config

func (*Config) Typed

func (d *Config) Typed() Typed

Typed converts device details to typed config

func (*Config) Update

func (d *Config) Update(conf map[string]any) error

Update updates a config's details to the database

type ConfigDetail

type ConfigDetail struct {
	ConfigID int    `gorm:"index:idx_unique"`
	Key      string `gorm:"index:idx_unique"`
	Value    string
}

type ConfigurableDevice

type ConfigurableDevice[T any] interface {
	Device[T]
	ID() int
	Update(map[string]any, T) error
	Delete() error
}

func NewConfigurableDevice

func NewConfigurableDevice[T any](config Config, instance T) ConfigurableDevice[T]

type Device

type Device[T any] interface {
	Config() Named
	Instance() T
}

func NewStaticDevice

func NewStaticDevice[T any](config Named, instance T) Device[T]

type Handler

type Handler[T any] interface {
	Subscribe(fn func(Operation, Device[T]))
	Devices() []Device[T]
	Add(dev Device[T]) error
	Delete(name string) error
	ByName(name string) (Device[T], error)
}

func Chargers

func Chargers() Handler[api.Charger]

func Circuits

func Circuits() Handler[api.Circuit]

func Meters

func Meters() Handler[api.Meter]

func Vehicles

func Vehicles() Handler[api.Vehicle]

type Named

type Named struct {
	Name  string                 `json:"name"`
	Type  string                 `json:"type"`
	Other map[string]interface{} `mapstructure:",remain"`
}

func (Named) Property

func (n Named) Property(key string) any

Property returns the value of the named property

type Operation

type Operation string
const (
	OpAdd    Operation = "add"
	OpDelete Operation = "del"
)

type Typed

type Typed struct {
	Type  string                 `json:"type"`
	Other map[string]interface{} `mapstructure:",remain"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL