|
@ -117,7 +117,7 @@ func (r *DeviceRepo) FetchByReference(ctx context.Context, kind models.Reference |
|
|
return r.populate(ctx, records) |
|
|
return r.populate(ctx, records) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (r *DeviceRepo) Save(ctx context.Context, device *models.Device) error { |
|
|
|
|
|
|
|
|
func (r *DeviceRepo) Save(ctx context.Context, device *models.Device, mode models.SaveMode) error { |
|
|
tx, err := r.DBX.Beginx() |
|
|
tx, err := r.DBX.Beginx() |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return dbErr(err) |
|
|
return dbErr(err) |
|
@ -149,14 +149,19 @@ func (r *DeviceRepo) Save(ctx context.Context, device *models.Device) error { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Let's just be lazy for now, optimize later if need be.
|
|
|
// Let's just be lazy for now, optimize later if need be.
|
|
|
|
|
|
if mode == 0 || mode&models.SMTags != 0 { |
|
|
_, err = tx.ExecContext(ctx, "DELETE FROM device_tag WHERE device_id=?", record.ID) |
|
|
_, err = tx.ExecContext(ctx, "DELETE FROM device_tag WHERE device_id=?", record.ID) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return dbErr(err) |
|
|
return dbErr(err) |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if mode == 0 || mode&models.SMProperties != 0 { |
|
|
_, err = tx.ExecContext(ctx, "DELETE FROM device_property WHERE device_id=?", record.ID) |
|
|
_, err = tx.ExecContext(ctx, "DELETE FROM device_property WHERE device_id=?", record.ID) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return dbErr(err) |
|
|
return dbErr(err) |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
res, err := tx.NamedExecContext(ctx, ` |
|
|
res, err := tx.NamedExecContext(ctx, ` |
|
|
INSERT INTO device (bridge_id, internal_id, icon, name, capabilities, button_names) |
|
|
INSERT INTO device (bridge_id, internal_id, icon, name, capabilities, button_names) |
|
@ -175,13 +180,16 @@ func (r *DeviceRepo) Save(ctx context.Context, device *models.Device) error { |
|
|
device.ID = int(lastID) |
|
|
device.ID = int(lastID) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if mode == 0 || mode&models.SMTags != 0 { |
|
|
for _, tag := range device.Tags { |
|
|
for _, tag := range device.Tags { |
|
|
_, err := tx.ExecContext(ctx, "INSERT INTO device_tag (device_id, tag_name) VALUES (?, ?)", record.ID, tag) |
|
|
_, err := tx.ExecContext(ctx, "INSERT INTO device_tag (device_id, tag_name) VALUES (?, ?)", record.ID, tag) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return dbErr(err) |
|
|
return dbErr(err) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if mode == 0 || mode&models.SMProperties != 0 { |
|
|
for key, value := range device.UserProperties { |
|
|
for key, value := range device.UserProperties { |
|
|
_, err := tx.ExecContext(ctx, "INSERT INTO device_property (device_id, prop_key, prop_value, is_user) VALUES (?, ?, ?, 1)", |
|
|
_, err := tx.ExecContext(ctx, "INSERT INTO device_property (device_id, prop_key, prop_value, is_user) VALUES (?, ?, ?, 1)", |
|
|
record.ID, key, value, |
|
|
record.ID, key, value, |
|
@ -206,7 +214,9 @@ func (r *DeviceRepo) Save(ctx context.Context, device *models.Device) error { |
|
|
return dbErr(err) |
|
|
return dbErr(err) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if mode == 0 || mode&models.SMState != 0 { |
|
|
_, err = tx.NamedExecContext(ctx, ` |
|
|
_, err = tx.NamedExecContext(ctx, ` |
|
|
REPLACE INTO device_state(device_id, hue, saturation, kelvin, power, intensity) |
|
|
REPLACE INTO device_state(device_id, hue, saturation, kelvin, power, intensity) |
|
|
VALUES (:device_id, :hue, :saturation, :kelvin, :power, :intensity) |
|
|
VALUES (:device_id, :hue, :saturation, :kelvin, :power, :intensity) |
|
@ -221,6 +231,7 @@ func (r *DeviceRepo) Save(ctx context.Context, device *models.Device) error { |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return dbErr(err) |
|
|
return dbErr(err) |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return tx.Commit() |
|
|
return tx.Commit() |
|
|
} |
|
|
} |
|
|