From 0bd6c9bf7336802819e8f1ec184955dd3299e098 Mon Sep 17 00:00:00 2001 From: Mike Shoup Date: Fri, 19 Oct 2018 14:41:44 -0600 Subject: [PATCH] Backend for selectively disabling heating/cooling --- cli.go | 91 ++++++++++++++++++++++++++++++--------------------- config.go | 2 ++ thermostat.go | 36 ++++++++++++-------- 3 files changed, 79 insertions(+), 50 deletions(-) diff --git a/cli.go b/cli.go index 3ad20a8..24b7393 100644 --- a/cli.go +++ b/cli.go @@ -8,7 +8,6 @@ import ( "strings" "github.com/howeyc/gopass" - "github.com/yryz/ds18b20" ) @@ -101,54 +100,72 @@ func PromptForConfiguration() Config { panic("Alias cannot be blank") } - fmt.Print("High temperature: ") - s.HighTemp, err = strconv.ParseFloat(ReadInput(reader, ""), 64) + fmt.Print("Disable cooling? [false]: ") + s.CoolDisable, err = strconv.ParseBool(ReadInput(reader, "false")) if err != nil { panic(err) } - fmt.Print("Cooling minutes: ") - s.CoolMinutes, err = strconv.ParseFloat(ReadInput(reader, ""), 64) + if !s.CoolDisable { + + fmt.Print("High temperature: ") + s.HighTemp, err = strconv.ParseFloat(ReadInput(reader, ""), 64) + if err != nil { + panic(err) + } + + fmt.Print("Cooling minutes: ") + s.CoolMinutes, err = strconv.ParseFloat(ReadInput(reader, ""), 64) + if err != nil { + panic(err) + } + + fmt.Print("Cooling GPIO: ") + resp, err := strconv.ParseInt(ReadInput(reader, ""), 10, 32) + s.CoolGPIO = int32(resp) + if err != nil { + panic(err) + } + + fmt.Print("Invert cooling switch [false]: ") + s.CoolInvert, err = strconv.ParseBool(ReadInput(reader, "false")) + if err != nil { + panic(err) + } + } + + fmt.Print("Disable heating? [false]: ") + s.HeatDisable, err = strconv.ParseBool(ReadInput(reader, "false")) if err != nil { panic(err) } - fmt.Print("Cooling GPIO: ") - resp, err := strconv.ParseInt(ReadInput(reader, ""), 10, 32) - s.CoolGPIO = int32(resp) - if err != nil { - panic(err) - } + if !s.HeatDisable { - fmt.Print("Invert cooling switch [false]: ") - s.CoolInvert, err = strconv.ParseBool(ReadInput(reader, "false")) - if err != nil { - panic(err) - } + fmt.Print("Low temperature: ") + s.LowTemp, err = strconv.ParseFloat(ReadInput(reader, ""), 64) + if err != nil { + panic(err) + } - fmt.Print("Low temperature: ") - s.LowTemp, err = strconv.ParseFloat(ReadInput(reader, ""), 64) - if err != nil { - panic(err) - } + fmt.Print("Heating minutes: ") + s.HeatMinutes, err = strconv.ParseFloat(ReadInput(reader, ""), 64) + if err != nil { + panic(err) + } - fmt.Print("Heating minutes: ") - s.HeatMinutes, err = strconv.ParseFloat(ReadInput(reader, ""), 64) - if err != nil { - panic(err) - } + fmt.Print("Heating GPIO: ") + resp, err := strconv.ParseInt(ReadInput(reader, ""), 10, 32) + s.HeatGPIO = int32(resp) + if err != nil { + panic(err) + } - fmt.Print("Heating GPIO: ") - resp, err = strconv.ParseInt(ReadInput(reader, ""), 10, 32) - s.HeatGPIO = int32(resp) - if err != nil { - panic(err) - } - - fmt.Print("Invert heating switch [false]: ") - s.HeatInvert, err = strconv.ParseBool(ReadInput(reader, "false")) - if err != nil { - panic(err) + fmt.Print("Invert heating switch [false]: ") + s.HeatInvert, err = strconv.ParseBool(ReadInput(reader, "false")) + if err != nil { + panic(err) + } } fmt.Print("Enable verbose logging [false]: ") diff --git a/config.go b/config.go index 11451dc..9582459 100644 --- a/config.go +++ b/config.go @@ -27,9 +27,11 @@ type Sensor struct { Alias string `json:"alias" yaml:"alias"` HighTemp float64 `json:"hightemp" yaml:"hightemp"` LowTemp float64 `json:"lowtemp" yaml:"lowtemp"` + HeatDisable bool `json:"heatdisable" yaml:"heatdisable"` HeatGPIO int32 `json:"heatgpio" yaml:"heatgpio"` HeatInvert bool `json:"heatinvert" yaml:"heatinvert"` HeatMinutes float64 `json:"heatminutes" yaml:"heatminutes"` + CoolDisable bool `json:"cooldisable" yaml:"cooldisable"` CoolGPIO int32 `json:"coolgpio" yaml:"coolgpio"` CoolInvert bool `json:"coolinvert" yaml:"coolinvert"` CoolMinutes float64 `json:"coolminutes" yaml:"coolminutes"` diff --git a/thermostat.go b/thermostat.go index fd01673..91cb683 100644 --- a/thermostat.go +++ b/thermostat.go @@ -70,11 +70,18 @@ func ProcessSensor(sensor Sensor, state State) (State, error) { state.When = time.Now() + var cpin rpio.Pin + var hpin rpio.Pin // Initialize the pins - cpin := rpio.Pin(sensor.CoolGPIO) - cpin.Output() - hpin := rpio.Pin(sensor.HeatGPIO) - hpin.Output() + if !sensor.CoolDisable { + cpin = rpio.Pin(sensor.CoolGPIO) + cpin.Output() + } + + if !sensor.HeatDisable { + hpin = rpio.Pin(sensor.HeatGPIO) + hpin.Output() + } // Calculate duration duration := time.Since(state.Changed).Minutes() @@ -83,14 +90,14 @@ func ProcessSensor(sensor Sensor, state State) (State, error) { case temp > sensor.HighTemp && temp < sensor.LowTemp: log.Println("Invalid state! Temperature is too high AND too low!") // Temperature too high, start cooling - case temp > sensor.HighTemp: + case temp > sensor.HighTemp && !sensor.CoolDisable: PinSwitch(cpin, true, sensor.CoolInvert) state.Cooling = true PinSwitch(hpin, false, sensor.HeatInvert) // Ensure the heater is off state.Heating = false state.Changed = future // Temperature too low, start heating - case temp < sensor.LowTemp: + case temp < sensor.LowTemp && !sensor.HeatDisable: PinSwitch(hpin, true, sensor.HeatInvert) state.Heating = true PinSwitch(cpin, false, sensor.CoolInvert) // Ensure the chiller is off @@ -126,13 +133,16 @@ func ProcessSensor(sensor Sensor, state State) (State, error) { // TurnOffSensor turns off all switches for an individual sensor func TurnOffSensor(sensor Sensor) { - cpin := rpio.Pin(sensor.CoolGPIO) - cpin.Output() - PinSwitch(cpin, false, sensor.CoolInvert) - - hpin := rpio.Pin(sensor.HeatGPIO) - hpin.Output() - PinSwitch(hpin, false, sensor.HeatInvert) + if !sensor.CoolDisable { + cpin := rpio.Pin(sensor.CoolGPIO) + cpin.Output() + PinSwitch(cpin, false, sensor.CoolInvert) + } + if !sensor.HeatDisable { + hpin := rpio.Pin(sensor.HeatGPIO) + hpin.Output() + PinSwitch(hpin, false, sensor.HeatInvert) + } } // TurnOffSensors turns off all sensors defined in the config