rework color system and make it work with drivers.
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-02-21 19:27:42 +01:00
parent d5cf30ea4b
commit df865574bf
16 changed files with 468 additions and 179 deletions
+12 -12
View File
@@ -30,6 +30,7 @@ type deviceStateRecord struct {
Power bool `db:"power"`
Intensity float64 `db:"intensity"`
Temperature int `db:"temperature"`
Color string `db:"color"`
}
type devicePropertyRecord struct {
@@ -228,13 +229,14 @@ func (r *DeviceRepo) SaveMany(ctx context.Context, mode models.SaveMode, devices
if mode == 0 || mode&models.SMState != 0 {
_, err = tx.NamedExecContext(ctx, `
REPLACE INTO device_state(device_id, hue, saturation, kelvin, power, intensity, temperature)
VALUES (:device_id, :hue, :saturation, :kelvin, :power, :intensity, :temperature)
REPLACE INTO device_state(device_id, hue, saturation, kelvin, power, intensity, color, temperature)
VALUES (:device_id, :hue, :saturation, :kelvin, :power, :intensity, :color, :temperature)
`, deviceStateRecord{
DeviceID: record.ID,
Hue: device.State.Color.Hue,
Saturation: device.State.Color.Saturation,
Kelvin: device.State.Color.Kelvin,
Hue: 40,
Saturation: 0,
Kelvin: 0,
Color: device.State.Color.String(),
Power: device.State.Power,
Intensity: device.State.Intensity,
Temperature: device.State.Temperature,
@@ -373,14 +375,12 @@ func (r *DeviceRepo) populate(ctx context.Context, records []deviceRecord) ([]mo
for _, state := range states {
if state.DeviceID == record.ID {
color, _ := models.ParseColorValue(state.Color)
device.State = models.DeviceState{
Power: state.Power,
Color: models.ColorValue{
Hue: state.Hue,
Saturation: state.Saturation,
Kelvin: state.Kelvin,
},
Intensity: state.Intensity,
Power: state.Power,
Color: color,
Intensity: state.Intensity,
Temperature: state.Temperature,
}
}
+10 -11
View File
@@ -12,6 +12,7 @@ type presetRecord struct {
Hue float64 `db:"hue"`
Saturation float64 `db:"saturation"`
Kelvin int `db:"kelvin"`
Value string `db:"value"`
}
type ColorPresetRepo struct {
@@ -42,8 +43,8 @@ func (c *ColorPresetRepo) Save(ctx context.Context, preset *models.ColorPreset)
if preset.ID > 0 {
_, err := c.DBX.ExecContext(
ctx,
"UPDATE color_preset SET name = ?, hue = ?, saturation = ?, kelvin = ? WHERE id = ?",
preset.Name, preset.Value.Hue, preset.Value.Saturation, preset.Value.Kelvin, preset.ID,
"UPDATE color_preset SET name = ?, value = ? WHERE id = ?",
preset.Name, preset.Value.String(), preset.ID,
)
if err != nil {
@@ -52,8 +53,8 @@ func (c *ColorPresetRepo) Save(ctx context.Context, preset *models.ColorPreset)
} else {
rs, err := c.DBX.ExecContext(
ctx,
"INSERT INTO color_preset (name, hue, saturation, kelvin) VALUES (?, ?, ?, ?)",
preset.Name, preset.Value.Hue, preset.Value.Saturation, preset.Value.Kelvin,
"INSERT INTO color_preset (name, value, hue, saturation, kelvin) VALUES (?, ?, 0, 0, 0)",
preset.Name, preset.Value.String(),
)
if err != nil {
@@ -84,14 +85,12 @@ func (c *ColorPresetRepo) Delete(ctx context.Context, preset *models.ColorPreset
func (c *ColorPresetRepo) fromRecords(records ...presetRecord) []models.ColorPreset {
newList := make([]models.ColorPreset, len(records), len(records))
for i, record := range records {
color, _ := models.ParseColorValue(record.Value)
newList[i] = models.ColorPreset{
ID: record.ID,
Name: record.Name,
Value: models.ColorValue{
Hue: record.Hue,
Saturation: record.Saturation,
Kelvin: record.Kelvin,
},
ID: record.ID,
Name: record.Name,
Value: color,
}
}