Compare commits
3 Commits
0d83d14329
...
d101053188
Author | SHA1 | Date | |
---|---|---|---|
d101053188 | |||
ee87938c93 | |||
4c290b0467 |
@ -22,11 +22,11 @@ type Error string
|
||||
func (e Error) Error() string { return string(e) }
|
||||
|
||||
type DeviceState struct {
|
||||
Mode string `json:"mode"`
|
||||
Setpoint int `json:"setpoint"`
|
||||
Time time.Time `json:"time"`
|
||||
Program_name string `json:"program_name"`
|
||||
Until_time time.Time `json:"until_time"`
|
||||
Mode string `json:"mode"`
|
||||
Setpoint int `json:"setpoint"`
|
||||
Time time.Time `json:"time"`
|
||||
ProgramName string `json:"program_name"`
|
||||
UntilTime time.Time `json:"until_time"`
|
||||
}
|
||||
|
||||
func (s *DeviceState) Equivalent(state DeviceState) bool {
|
||||
@ -50,11 +50,11 @@ 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":
|
||||
if state.Program_name != s.Program_name {
|
||||
if state.ProgramName != s.ProgramName {
|
||||
return false
|
||||
}
|
||||
}
|
||||
@ -81,8 +81,8 @@ func (d Device) ListenTopic() (string, error) {
|
||||
func (d *Device) Program() (WeekProgram, error) {
|
||||
// return current device program if specified or default one
|
||||
prog_name := "default"
|
||||
if d.State.Program_name != "" {
|
||||
prog_name = d.State.Program_name
|
||||
if d.State.ProgramName != "" {
|
||||
prog_name = d.State.ProgramName
|
||||
}
|
||||
|
||||
program, ok := d.Settings.Programs[prog_name]
|
||||
@ -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
|
||||
if d.State.ProgramName != "" {
|
||||
return d.State.ProgramName
|
||||
}
|
||||
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
|
||||
@ -182,7 +175,7 @@ func (d *Device) update(log *zerolog.Logger, pubchan chan Message) (bool, error)
|
||||
Int("current_setpoint", d.CurrentSetpoint).
|
||||
Str("State.Mode", d.State.Mode).
|
||||
Int("State.Setpoint", d.State.Setpoint).
|
||||
Str("State.Program_name", d.State.Program_name).
|
||||
Str("State.Program_name", d.State.ProgramName).
|
||||
Logger()
|
||||
log.Debug().Msg("check if setpoint need an update")
|
||||
|
||||
@ -252,10 +245,10 @@ func (d *Device) handle_reset_state(log *zerolog.Logger, pubchan chan Message) (
|
||||
}
|
||||
|
||||
d.State = DeviceState{
|
||||
Setpoint: value,
|
||||
Mode: "program",
|
||||
Program_name: d.ProgramName(),
|
||||
Time: timeNow(),
|
||||
Setpoint: value,
|
||||
Mode: "program",
|
||||
ProgramName: d.ProgramName(),
|
||||
Time: timeNow(),
|
||||
}
|
||||
|
||||
if d.CurrentSetpoint != value {
|
||||
@ -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)
|
||||
}
|
||||
@ -336,7 +329,7 @@ func (d *Device) handle_until_next(log *zerolog.Logger, pubchan chan Message) (b
|
||||
}
|
||||
|
||||
func (d *Device) handle_program(log *zerolog.Logger, pubchan chan Message) (bool, error) {
|
||||
*log = log.With().Str("program", d.State.Program_name).Logger()
|
||||
*log = log.With().Str("program", d.State.ProgramName).Logger()
|
||||
|
||||
program, err := d.Program()
|
||||
if err != nil {
|
||||
@ -349,13 +342,13 @@ func (d *Device) handle_program(log *zerolog.Logger, pubchan chan Message) (bool
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
d.State.Setpoint = value
|
||||
|
||||
if d.CurrentSetpoint != value {
|
||||
log.Info().Msg("publish setpoint update")
|
||||
if err := d.SetSetpoint(value, pubchan); err != nil {
|
||||
return false, err
|
||||
}
|
||||
d.State.Setpoint = value
|
||||
return true, nil
|
||||
}
|
||||
|
||||
|
@ -48,10 +48,10 @@ var test_device = Device{
|
||||
},
|
||||
CurrentSetpoint: 0,
|
||||
State: DeviceState{
|
||||
Mode: "program",
|
||||
Setpoint: 14,
|
||||
Time: test_time,
|
||||
Program_name: "default",
|
||||
Mode: "program",
|
||||
Setpoint: 14,
|
||||
Time: test_time,
|
||||
ProgramName: "default",
|
||||
},
|
||||
}
|
||||
|
||||
@ -64,10 +64,10 @@ func TestStateEquivalent(t *testing.T) {
|
||||
}{
|
||||
{
|
||||
DeviceState{
|
||||
Mode: "program",
|
||||
Setpoint: 14,
|
||||
Time: test_time,
|
||||
Program_name: "default",
|
||||
Mode: "program",
|
||||
Setpoint: 14,
|
||||
Time: test_time,
|
||||
ProgramName: "default",
|
||||
},
|
||||
DeviceState{
|
||||
Mode: "always",
|
||||
@ -78,46 +78,46 @@ func TestStateEquivalent(t *testing.T) {
|
||||
},
|
||||
{
|
||||
DeviceState{
|
||||
Mode: "program",
|
||||
Setpoint: 14,
|
||||
Time: test_time,
|
||||
Program_name: "default",
|
||||
Mode: "program",
|
||||
Setpoint: 14,
|
||||
Time: test_time,
|
||||
ProgramName: "default",
|
||||
},
|
||||
DeviceState{
|
||||
Mode: "program",
|
||||
Setpoint: 14,
|
||||
Time: test_time.Add(1 * time.Minute),
|
||||
Program_name: "default",
|
||||
Mode: "program",
|
||||
Setpoint: 14,
|
||||
Time: test_time.Add(1 * time.Minute),
|
||||
ProgramName: "default",
|
||||
},
|
||||
false,
|
||||
},
|
||||
{
|
||||
DeviceState{
|
||||
Mode: "program",
|
||||
Setpoint: 14,
|
||||
Time: test_time,
|
||||
Program_name: "default",
|
||||
Mode: "program",
|
||||
Setpoint: 14,
|
||||
Time: test_time,
|
||||
ProgramName: "default",
|
||||
},
|
||||
DeviceState{
|
||||
Mode: "program",
|
||||
Setpoint: 13,
|
||||
Time: test_time,
|
||||
Program_name: "default",
|
||||
Mode: "program",
|
||||
Setpoint: 13,
|
||||
Time: test_time,
|
||||
ProgramName: "default",
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
DeviceState{
|
||||
Mode: "program",
|
||||
Setpoint: 14,
|
||||
Time: test_time,
|
||||
Program_name: "default",
|
||||
Mode: "program",
|
||||
Setpoint: 14,
|
||||
Time: test_time,
|
||||
ProgramName: "default",
|
||||
},
|
||||
DeviceState{
|
||||
Mode: "program",
|
||||
Setpoint: 13,
|
||||
Time: test_time,
|
||||
Program_name: "other",
|
||||
Mode: "program",
|
||||
Setpoint: 13,
|
||||
Time: test_time,
|
||||
ProgramName: "other",
|
||||
},
|
||||
false,
|
||||
},
|
||||
@ -128,11 +128,11 @@ func TestStateEquivalent(t *testing.T) {
|
||||
Time: test_time,
|
||||
},
|
||||
DeviceState{
|
||||
Mode: "always",
|
||||
Setpoint: 14,
|
||||
Time: test_time,
|
||||
Program_name: "other",
|
||||
Until_time: test_time,
|
||||
Mode: "always",
|
||||
Setpoint: 14,
|
||||
Time: test_time,
|
||||
ProgramName: "other",
|
||||
UntilTime: test_time,
|
||||
},
|
||||
true,
|
||||
},
|
||||
@ -143,11 +143,11 @@ func TestStateEquivalent(t *testing.T) {
|
||||
Time: test_time,
|
||||
},
|
||||
DeviceState{
|
||||
Mode: "always",
|
||||
Setpoint: 15,
|
||||
Time: test_time,
|
||||
Program_name: "other",
|
||||
Until_time: test_time,
|
||||
Mode: "always",
|
||||
Setpoint: 15,
|
||||
Time: test_time,
|
||||
ProgramName: "other",
|
||||
UntilTime: test_time,
|
||||
},
|
||||
false,
|
||||
},
|
||||
@ -158,11 +158,11 @@ func TestStateEquivalent(t *testing.T) {
|
||||
Time: test_time,
|
||||
},
|
||||
DeviceState{
|
||||
Mode: "until_next",
|
||||
Setpoint: 14,
|
||||
Time: test_time,
|
||||
Program_name: "other",
|
||||
Until_time: test_time,
|
||||
Mode: "until_next",
|
||||
Setpoint: 14,
|
||||
Time: test_time,
|
||||
ProgramName: "other",
|
||||
UntilTime: test_time,
|
||||
},
|
||||
true,
|
||||
},
|
||||
@ -173,59 +173,59 @@ func TestStateEquivalent(t *testing.T) {
|
||||
Time: test_time,
|
||||
},
|
||||
DeviceState{
|
||||
Mode: "until_next",
|
||||
Setpoint: 13,
|
||||
Time: test_time,
|
||||
Program_name: "other",
|
||||
Until_time: test_time,
|
||||
Mode: "until_next",
|
||||
Setpoint: 13,
|
||||
Time: test_time,
|
||||
ProgramName: "other",
|
||||
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),
|
||||
Mode: "until_time",
|
||||
Setpoint: 14,
|
||||
Time: test_time,
|
||||
ProgramName: "other",
|
||||
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),
|
||||
Mode: "until_time",
|
||||
Setpoint: 13,
|
||||
Time: test_time,
|
||||
ProgramName: "other",
|
||||
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),
|
||||
Mode: "until_time",
|
||||
Setpoint: 14,
|
||||
Time: test_time,
|
||||
ProgramName: "other",
|
||||
UntilTime: test_time.Add(2 * time.Hour),
|
||||
},
|
||||
false,
|
||||
},
|
||||
@ -262,7 +262,7 @@ func TestListenTopic(t *testing.T) {
|
||||
func TestProgram(t *testing.T) {
|
||||
//case 1: no program set in state return default
|
||||
case1_device := test_device
|
||||
case1_device.State.Program_name = ""
|
||||
case1_device.State.ProgramName = ""
|
||||
|
||||
//case 2: program set "confort" must return it
|
||||
var test_confort_weekprogram = WeekProgram{
|
||||
@ -279,11 +279,11 @@ func TestProgram(t *testing.T) {
|
||||
"default": test_weekprogram,
|
||||
"confort": test_confort_weekprogram,
|
||||
}
|
||||
case2_device.State.Program_name = "confort"
|
||||
case2_device.State.ProgramName = "confort"
|
||||
|
||||
//case 3: program set "confort" but not exist
|
||||
case3_device := test_device
|
||||
case3_device.State.Program_name = "confort"
|
||||
case3_device.State.ProgramName = "confort"
|
||||
|
||||
var tests = []struct {
|
||||
name string
|
||||
@ -324,15 +324,15 @@ func TestUpdate(t *testing.T) {
|
||||
device1 := test_device
|
||||
device1.Name = "1"
|
||||
device1.State = DeviceState{
|
||||
Mode: "program",
|
||||
Setpoint: 0,
|
||||
Time: test_time,
|
||||
Program_name: "",
|
||||
Mode: "program",
|
||||
Setpoint: 0,
|
||||
Time: test_time,
|
||||
ProgramName: "",
|
||||
}
|
||||
|
||||
device2 := test_device
|
||||
device2.Name = "2"
|
||||
device2.State.Program_name = "unknown"
|
||||
device2.State.ProgramName = "unknown"
|
||||
|
||||
device3 := test_device
|
||||
device3.Name = "3"
|
||||
@ -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
|
||||
|
@ -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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user