gofmt
This commit is contained in:
parent
11b3622abe
commit
6837a07beb
50
main.go
50
main.go
@ -1,18 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"citadel/heater/pkg/device"
|
||||
"citadel/heater/mqtt"
|
||||
mqttpaho "github.com/eclipse/paho.mqtt.golang"
|
||||
"citadel/heater/pkg/device"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
"strings"
|
||||
|
||||
mqttpaho "github.com/eclipse/paho.mqtt.golang"
|
||||
"github.com/rs/zerolog"
|
||||
"flag"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
@ -115,9 +116,13 @@ func (app *App) onSettingsMessage(ctx context.Context, msg mqttpaho.Message) {
|
||||
Str("device", device_name).
|
||||
Logger()
|
||||
|
||||
logger.Debug().Str("topic", msg.Topic()).Str("payload", string(msg.Payload())).Msg("")
|
||||
ctx = logger.WithContext(ctx)
|
||||
|
||||
logger.Debug().
|
||||
Str("topic", msg.Topic()).
|
||||
Str("payload", string(msg.Payload())).
|
||||
Msg("")
|
||||
|
||||
var device_settings device.DeviceSettings
|
||||
if err := json.Unmarshal(msg.Payload(), &device_settings); err != nil {
|
||||
logger.Error().Err(err).Msg("Parsing payload")
|
||||
@ -129,21 +134,19 @@ func (app *App) onSettingsMessage(ctx context.Context, msg mqttpaho.Message) {
|
||||
}
|
||||
|
||||
if _, ok := app.DeviceManager.Get(device_name); !ok {
|
||||
err := app.DeviceManager.Add(tvr)
|
||||
if err != nil {
|
||||
logger.Error().Err(err).Msg("Unexpected")
|
||||
if err := app.DeviceManager.Add(tvr); err != nil {
|
||||
logger.Error().Err(err).Msg("unexpected, abord")
|
||||
return
|
||||
}
|
||||
} else {
|
||||
err := app.DeviceManager.SetSettings(device_name, device_settings)
|
||||
if err != nil {
|
||||
logger.Error().Err(err).Msg("Unexpected")
|
||||
if err := app.DeviceManager.
|
||||
SetSettings(device_name, device_settings); err != nil {
|
||||
logger.Error().Err(err).Msg("unexpected, abord")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
err := app.DeviceManager.Check(ctx, device_name)
|
||||
if err != nil {
|
||||
if err := app.DeviceManager.Check(ctx, device_name); err != nil {
|
||||
logger.Error().Err(err).Msg("During device `Check`")
|
||||
}
|
||||
}
|
||||
@ -159,28 +162,23 @@ func (app *App) onSetStateMessage(ctx context.Context, msg mqttpaho.Message) {
|
||||
Logger()
|
||||
ctx = logger.WithContext(ctx)
|
||||
|
||||
|
||||
var state device.DeviceState
|
||||
if err := json.Unmarshal(msg.Payload(), &state); err != nil {
|
||||
logger.Error().Err(err).Msg("Error while parsing payload")
|
||||
}
|
||||
|
||||
logger.Info().Interface("state", state).Msg("new state")
|
||||
|
||||
device, ok := app.DeviceManager.Get(device_name)
|
||||
if !ok {
|
||||
logger.Error().Msg("Device not found, abord")
|
||||
return
|
||||
}
|
||||
|
||||
logger.Debug().Interface("state", state).Msg("new state")
|
||||
|
||||
if device, ok := app.DeviceManager.Get(device_name); ok {
|
||||
if err := device.SetState(&logger, state, app.PubChan); err != nil {
|
||||
logger.Error().Err(err).Msg("")
|
||||
logger.Error().Err(err).Msg("unexpected")
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
logger.Error().Msg("Device not found, abord")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func (app *App) Run() {
|
||||
ctx, ctxCancel := context.WithCancel(context.Background())
|
||||
@ -191,7 +189,6 @@ func (app *App) Run() {
|
||||
|
||||
ctx = log.WithContext(ctx)
|
||||
|
||||
|
||||
defer ctxCancel()
|
||||
|
||||
if err := app.mqtt_hub.Connect(ctx); err != nil {
|
||||
@ -271,4 +268,3 @@ func main() {
|
||||
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-quit
|
||||
}
|
||||
|
||||
|
@ -1,13 +1,14 @@
|
||||
package device
|
||||
|
||||
import (
|
||||
"github.com/eclipse/paho.mqtt.golang"
|
||||
"github.com/ohler55/ojg/jp"
|
||||
"github.com/ohler55/ojg/oj"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
"encoding/json"
|
||||
"context"
|
||||
|
||||
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||
"github.com/ohler55/ojg/jp"
|
||||
"github.com/ohler55/ojg/oj"
|
||||
"github.com/rs/zerolog"
|
||||
// "reflect"
|
||||
)
|
||||
@ -16,10 +17,9 @@ var timeNow = func() time.Time {
|
||||
return time.Now()
|
||||
}
|
||||
|
||||
|
||||
type Error string
|
||||
func (e Error) Error() string { return string(e) }
|
||||
|
||||
func (e Error) Error() string { return string(e) }
|
||||
|
||||
type DeviceState struct {
|
||||
Mode string `json:"mode"`
|
||||
@ -62,7 +62,6 @@ func (s *DeviceState) Equivalent(state DeviceState) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
// Internal state
|
||||
type Device struct {
|
||||
Name string
|
||||
@ -71,7 +70,6 @@ type Device struct {
|
||||
State DeviceState
|
||||
}
|
||||
|
||||
|
||||
func (d *Device) StateTopic() string {
|
||||
return fmt.Sprintf("heater/%s/state", d.Name)
|
||||
}
|
||||
@ -80,7 +78,6 @@ func (d Device) ListenTopic() (string, error) {
|
||||
return d.Settings.TVR.FormatTopicState(d.Name)
|
||||
}
|
||||
|
||||
|
||||
func (d *Device) Program() (WeekProgram, error) {
|
||||
// return current device program if specified or default one
|
||||
prog_name := "default"
|
||||
@ -96,8 +93,7 @@ func (d *Device) Program() (WeekProgram, error) {
|
||||
return program, nil
|
||||
}
|
||||
|
||||
|
||||
func (d *Device) ProgramName() (string) {
|
||||
func (d *Device) ProgramName() string {
|
||||
prog_name := "default"
|
||||
if d.State.Program_name != "" {
|
||||
prog_name = d.State.Program_name
|
||||
@ -105,7 +101,6 @@ func (d *Device) ProgramName() (string) {
|
||||
return prog_name
|
||||
}
|
||||
|
||||
|
||||
func (d *Device) publishState(pubchan chan Message) error {
|
||||
|
||||
payload, err := json.Marshal(d.State)
|
||||
@ -122,7 +117,6 @@ func (d *Device) publishState(pubchan chan Message) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
func (d *Device) SetSetpoint(value int, pubchan chan Message) error {
|
||||
topic, err := d.Settings.TVR.FormatTopic(d.Name)
|
||||
if err != nil {
|
||||
@ -142,7 +136,6 @@ func (d *Device) SetSetpoint(value int, pubchan chan Message) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
func (d *Device) SetState(log *zerolog.Logger, state DeviceState, pubchan chan Message) error {
|
||||
// If same state do nothing
|
||||
// else use checksetpoint for changing state
|
||||
@ -166,7 +159,6 @@ func (d *Device) SetState(log *zerolog.Logger, state DeviceState, pubchan chan M
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
func (d *Device) CheckSetpoint(log *zerolog.Logger, pubchan chan Message) error {
|
||||
change, err := d.update(log, pubchan)
|
||||
if err != nil {
|
||||
@ -182,7 +174,6 @@ func (d *Device) CheckSetpoint(log *zerolog.Logger, pubchan chan Message) error
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
func (d *Device) update(log *zerolog.Logger, pubchan chan Message) (bool, error) {
|
||||
// Handle all the update setpoint logic
|
||||
// push in pubChan a Message if setpoint need to be update
|
||||
@ -193,7 +184,7 @@ func (d *Device) update(log *zerolog.Logger, pubchan chan Message) (bool, error)
|
||||
Int("State.Setpoint", d.State.Setpoint).
|
||||
Str("State.Program_name", d.State.Program_name).
|
||||
Logger()
|
||||
log.Info().Msg("Check if setpoint need an update")
|
||||
log.Debug().Msg("check if setpoint need an update")
|
||||
|
||||
switch d.State.Mode {
|
||||
case "always":
|
||||
@ -213,7 +204,6 @@ func (d *Device) update(log *zerolog.Logger, pubchan chan Message) (bool, error)
|
||||
|
||||
}
|
||||
|
||||
|
||||
func (d *Device) onMessage(ctx context.Context, msg mqtt.Message) {
|
||||
log := zerolog.Ctx(ctx).With().
|
||||
Str("action", "device state receive").
|
||||
@ -247,7 +237,6 @@ func (d *Device) onMessage(ctx context.Context, msg mqtt.Message) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
func (d *Device) handle_reset_state(log *zerolog.Logger, pubchan chan Message) (bool, error) {
|
||||
// called when need to fallback to program mode previous or default
|
||||
program, err := d.Program()
|
||||
@ -280,7 +269,6 @@ func (d *Device) handle_reset_state(log *zerolog.Logger, pubchan chan Message) (
|
||||
return true, nil
|
||||
}
|
||||
|
||||
|
||||
func (d *Device) handle_always(log *zerolog.Logger, pubchan chan Message) (bool, error) {
|
||||
// return true if change made
|
||||
|
||||
|
@ -1,9 +1,10 @@
|
||||
package device
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"context"
|
||||
"github.com/eclipse/paho.mqtt.golang"
|
||||
"fmt"
|
||||
|
||||
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
|
@ -1,18 +1,18 @@
|
||||
package device
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
// monday
|
||||
var test_time = time.Date(2022, time.October, 24, 0, 0, 0, 0, time.Local)
|
||||
|
||||
|
||||
var test_presets = []Preset{
|
||||
{Label: "default", Value: 17, Color: "#012a36"},
|
||||
{Label: "normal", Value: 19, Color: "#b6244f"},
|
||||
@ -241,7 +241,6 @@ func TestStateEquivalent(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func TestStateTopic(t *testing.T) {
|
||||
topic := test_device.StateTopic()
|
||||
if topic != "heater/valid/state" {
|
||||
@ -310,7 +309,6 @@ func TestProgram(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func TestUpdate(t *testing.T) {
|
||||
// device 1: currentSetpoint 0 program, not specified expect default
|
||||
// device 2: currentSetpoint 0 program, unknown one, expect error
|
||||
@ -319,7 +317,6 @@ func TestUpdate(t *testing.T) {
|
||||
// device 5: currentSetpoint 17 indem 4 but fixed timeNow-2h
|
||||
// device 6: currentSetpoint 17, until_next set a timeNow, test time time now +1h : no change
|
||||
|
||||
|
||||
timeNow = func() time.Time {
|
||||
return test_time
|
||||
}
|
||||
@ -373,7 +370,6 @@ func TestUpdate(t *testing.T) {
|
||||
Time: timeNow(),
|
||||
}
|
||||
|
||||
|
||||
var tests = []struct {
|
||||
getTime func() time.Time
|
||||
device Device
|
||||
@ -382,7 +378,7 @@ func TestUpdate(t *testing.T) {
|
||||
err error
|
||||
}{
|
||||
{timeNow, device1, []Message{
|
||||
Message{
|
||||
{
|
||||
Payload: []byte("{\"current_heating_setpoint\": 17}"),
|
||||
Topic: "zigbee2mqtt/TVR/1/set",
|
||||
Retain: false,
|
||||
@ -390,7 +386,7 @@ func TestUpdate(t *testing.T) {
|
||||
}, true, nil},
|
||||
{timeNow, device2, []Message{}, false, Error("device 2 don't have unknown program")},
|
||||
{timeNow, device3, []Message{
|
||||
Message{
|
||||
{
|
||||
Payload: []byte("{\"current_heating_setpoint\": 22}"),
|
||||
Topic: "zigbee2mqtt/TVR/3/set",
|
||||
Retain: false,
|
||||
@ -400,7 +396,7 @@ func TestUpdate(t *testing.T) {
|
||||
{timeNow, device5, []Message{}, true, nil},
|
||||
{timeNow, device6, []Message{}, false, nil},
|
||||
{func() time.Time { return test_time.Add(7*time.Hour + 30*time.Minute) }, device6, []Message{
|
||||
Message{
|
||||
{
|
||||
Payload: []byte("{\"current_heating_setpoint\": 19}"),
|
||||
Topic: "zigbee2mqtt/TVR/6/set",
|
||||
Retain: false,
|
||||
|
@ -1,10 +1,10 @@
|
||||
package device
|
||||
|
||||
import (
|
||||
"time"
|
||||
"bytes"
|
||||
"text/template"
|
||||
"fmt"
|
||||
"text/template"
|
||||
"time"
|
||||
)
|
||||
|
||||
type DayOfWeek int
|
||||
@ -25,7 +25,7 @@ func (d DayOfWeek) Previous() DayOfWeek {
|
||||
|
||||
func (d DayOfWeek) DaysBetween(n DayOfWeek) int {
|
||||
var between int
|
||||
if (n < d) {
|
||||
if n < d {
|
||||
between = 7 - int(d-n)
|
||||
} else {
|
||||
between = int(n - d)
|
||||
@ -83,7 +83,6 @@ func (s Setpoint) Value(presets []Preset) (int, error) {
|
||||
return presets[s.Preset_id].Value, nil
|
||||
}
|
||||
|
||||
|
||||
type WeekProgram map[DayOfWeek][]Setpoint
|
||||
|
||||
func (p WeekProgram) Current() Setpoint {
|
||||
@ -135,7 +134,7 @@ func (p WeekProgram) NextTime(t time.Time) (time.Time, error) {
|
||||
weekday = weekday.Next()
|
||||
daytime = 0
|
||||
if weekday == startweekday {
|
||||
return time.Time{}, fmt.Errorf("Shouldn't happen no setpoint found over the week")
|
||||
return time.Time{}, Error("shouldn't happen no setpoint found over the week")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,9 +1,9 @@
|
||||
package device
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func TestDaysBetween(t *testing.T) {
|
||||
@ -29,7 +29,6 @@ func TestDaysBetween(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func TestNextTime(t *testing.T) {
|
||||
|
||||
defaultSetpoints := []Setpoint{
|
||||
@ -43,7 +42,6 @@ func TestNextTime(t *testing.T) {
|
||||
{Start: 0, Preset_id: 0},
|
||||
}
|
||||
|
||||
|
||||
default_program := WeekProgram{
|
||||
Monday: defaultSetpoints,
|
||||
Thuesday: defaultSetpoints,
|
||||
@ -64,7 +62,6 @@ func TestNextTime(t *testing.T) {
|
||||
Sunday: zeroSetpoints,
|
||||
}
|
||||
|
||||
|
||||
var tests = []struct {
|
||||
prog WeekProgram
|
||||
time time.Time
|
||||
|
Loading…
x
Reference in New Issue
Block a user