diff --git a/pkg/device/device.go b/pkg/device/device.go index 9db1b22..45a7435 100644 --- a/pkg/device/device.go +++ b/pkg/device/device.go @@ -26,7 +26,7 @@ type DeviceState struct { Setpoint int `json:"setpoint"` Time time.Time `json:"time"` Program_name string `json:"program_name"` - Until_time time.Time `json:"until_time"` + UntilTime time.Time `json:"until_time"` } func (s *DeviceState) Equivalent(state DeviceState) bool { @@ -50,7 +50,7 @@ func (s *DeviceState) Equivalent(state DeviceState) bool { if state.Setpoint != s.Setpoint { return false } - if !state.Until_time.Equal(s.Until_time) { + if !state.UntilTime.Equal(s.UntilTime) { return false } case "program": @@ -94,11 +94,10 @@ func (d *Device) Program() (WeekProgram, error) { } func (d *Device) ProgramName() string { - prog_name := "default" if d.State.Program_name != "" { - prog_name = d.State.Program_name + return d.State.Program_name } - return prog_name + return "default" } func (d *Device) publishState(pubchan chan Message) error { @@ -152,11 +151,7 @@ func (d *Device) SetState(log *zerolog.Logger, state DeviceState, pubchan chan M return err } - if err := d.publishState(pubchan); err != nil { - return err - } - - return nil + return d.publishState(pubchan) } func (d *Device) CheckSetpoint(log *zerolog.Logger, pubchan chan Message) error { @@ -166,9 +161,7 @@ func (d *Device) CheckSetpoint(log *zerolog.Logger, pubchan chan Message) error } if change { - if err := d.publishState(pubchan); err != nil { - return err - } + return d.publishState(pubchan) } return nil @@ -284,9 +277,9 @@ func (d *Device) handle_always(log *zerolog.Logger, pubchan chan Message) (bool, } func (d *Device) handle_until_time(log *zerolog.Logger, pubchan chan Message) (bool, error) { - *log = log.With().Time("until_time", d.State.Until_time).Logger() + *log = log.With().Time("until_time", d.State.UntilTime).Logger() - if d.State.Until_time.Before(timeNow()) { + if d.State.UntilTime.Before(timeNow()) { log.Info().Msg("until_time passed, reset") return d.handle_reset_state(log, pubchan) } diff --git a/pkg/device/device_test.go b/pkg/device/device_test.go index 96e809e..930e2a2 100644 --- a/pkg/device/device_test.go +++ b/pkg/device/device_test.go @@ -132,7 +132,7 @@ func TestStateEquivalent(t *testing.T) { Setpoint: 14, Time: test_time, Program_name: "other", - Until_time: test_time, + UntilTime: test_time, }, true, }, @@ -147,7 +147,7 @@ func TestStateEquivalent(t *testing.T) { Setpoint: 15, Time: test_time, Program_name: "other", - Until_time: test_time, + UntilTime: test_time, }, false, }, @@ -162,7 +162,7 @@ func TestStateEquivalent(t *testing.T) { Setpoint: 14, Time: test_time, Program_name: "other", - Until_time: test_time, + UntilTime: test_time, }, true, }, @@ -177,55 +177,55 @@ func TestStateEquivalent(t *testing.T) { Setpoint: 13, Time: test_time, Program_name: "other", - Until_time: test_time, + UntilTime: test_time, }, false, }, { DeviceState{ - Mode: "until_time", - Setpoint: 14, - Time: test_time, - Until_time: test_time.Add(1 * time.Hour), + Mode: "until_time", + Setpoint: 14, + Time: test_time, + UntilTime: test_time.Add(1 * time.Hour), }, DeviceState{ Mode: "until_time", Setpoint: 14, Time: test_time, Program_name: "other", - Until_time: test_time.Add(1 * time.Hour), + UntilTime: test_time.Add(1 * time.Hour), }, true, }, { DeviceState{ - Mode: "until_time", - Setpoint: 14, - Time: test_time, - Until_time: test_time.Add(1 * time.Hour), + Mode: "until_time", + Setpoint: 14, + Time: test_time, + UntilTime: test_time.Add(1 * time.Hour), }, DeviceState{ Mode: "until_time", Setpoint: 13, Time: test_time, Program_name: "other", - Until_time: test_time.Add(1 * time.Hour), + UntilTime: test_time.Add(1 * time.Hour), }, false, }, { DeviceState{ - Mode: "until_time", - Setpoint: 14, - Time: test_time, - Until_time: test_time.Add(1 * time.Hour), + Mode: "until_time", + Setpoint: 14, + Time: test_time, + UntilTime: test_time.Add(1 * time.Hour), }, DeviceState{ Mode: "until_time", Setpoint: 14, Time: test_time, Program_name: "other", - Until_time: test_time.Add(2 * time.Hour), + UntilTime: test_time.Add(2 * time.Hour), }, false, }, @@ -345,20 +345,20 @@ func TestUpdate(t *testing.T) { device4.Name = "4" device4.CurrentSetpoint = 22 device4.State = DeviceState{ - Mode: "until_time", - Setpoint: 22, - Time: timeNow(), - Until_time: timeNow().Add(2 * time.Hour), + Mode: "until_time", + Setpoint: 22, + Time: timeNow(), + UntilTime: timeNow().Add(2 * time.Hour), } device5 := test_device device5.Name = "5" device5.CurrentSetpoint = 17 device5.State = DeviceState{ - Mode: "until_time", - Setpoint: 22, - Time: timeNow().Add(-2 * time.Hour), - Until_time: timeNow().Add(-1 * time.Minute), + Mode: "until_time", + Setpoint: 22, + Time: timeNow().Add(-2 * time.Hour), + UntilTime: timeNow().Add(-1 * time.Minute), } device6 := test_device diff --git a/pkg/device/settings.go b/pkg/device/settings.go index 7dd1b73..86f828b 100644 --- a/pkg/device/settings.go +++ b/pkg/device/settings.go @@ -34,13 +34,13 @@ func (d DayOfWeek) DaysBetween(n DayOfWeek) int { } const ( - Monday DayOfWeek = 0 - Thuesday DayOfWeek = 1 - Wednesday DayOfWeek = 2 - Thursday DayOfWeek = 3 - Friday DayOfWeek = 4 - Saturday DayOfWeek = 5 - Sunday DayOfWeek = 6 + Monday DayOfWeek = iota + Thuesday + Wednesday + Thursday + Friday + Saturday + Sunday ) func WeekDayEnToFr(weekday time.Weekday) DayOfWeek {