Skip to content

Commit

Permalink
Track relay toggled for ALL changes of the relay
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Townsley authored and Mike Townsley committed Dec 15, 2019
1 parent 2840e56 commit 799e3f4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 21 deletions.
31 changes: 16 additions & 15 deletions src/generics.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,21 +234,21 @@ def function(self):
return self.__function

def addCallback(self, callback):
""" Provide a callback in the form of callback(isOpening: boolean) to
notify when the relay is openign or closing """
""" Provide a callback in the form of callback(relay: GenericRelay) to
notify when the relay is opening or closing """
self.__callbackList.append(callback)

def openRelay(self):
""" Open the relay, break circuit, disabling the function """
for callback in self.__callbackList:
callback(False)
callback(self)
self._openRelay()
self.__isOpen = True

def closeRelay(self):
""" Close the relay, connect circuit, enabling the function """
for callback in self.__callbackList:
callback(True)
callback(self)
self._closeRelay()
self.__isOpen = False

Expand Down Expand Up @@ -282,7 +282,7 @@ def __init__(self,
relays: list):
self.__lcd = lcd
self.__sensor = sensor
self.__relayIsClosing = False
self.__relayToggled = False
self.__relayMap = {r.function: r for r in relays}
for relay in relays:
relay.addCallback(self.__relayCallback)
Expand Down Expand Up @@ -317,8 +317,8 @@ def setServiceProvider(self, provider: ServiceProvider):
frequency=self.__backlightTimeoutDuration,
handlers=self.__backlightTimeout,
oneShot=True)
self.__relayClosingTimeoutInvoker = self._installTimerHandler(
frequency=3.0, handlers=self.__relayClosingTimeout,
self.__relayToggledTimeoutInvoker = self._installTimerHandler(
frequency=3.0, handlers=self.__relayToggledTimeout,
oneShot=True)
self.__drawRowTwoInvoker = self._installTimerHandler(
frequency=3.0,
Expand Down Expand Up @@ -354,8 +354,8 @@ def state(self):
return self.__state

@property
def relayIsClosing(self):
return self.__relayIsClosing
def relayToggled(self):
return self.__relayToggled

def __checkSchedule(self):
settings = self._getService(Settings)
Expand Down Expand Up @@ -476,13 +476,14 @@ def __changeState(self, newState: ThermostatState):
self.__state = newState
self._fireEvent(ThermostatStateChangedEvent(newState))

def __relayCallback(self, isClosing: bool):
if isClosing:
self.__relayIsClosing = True
self.__relayClosingTimeoutInvoker.reset()
def __relayCallback(self, relay: GenericRelay):
self.__relayToggled = True
self.__relayToggledTimeoutInvoker.reset()
log.debug("Driver ENTERING relay toggle timeout")

def __relayClosingTimeout(self):
self.__relayIsClosing = False
def __relayToggledTimeout(self):
self.__relayToggled = False
log.debug("Driver COMPLETED relay toggle timeout")

def __fanRunout(self):
settings = self._getService(Settings)
Expand Down
2 changes: 1 addition & 1 deletion src/hardware.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,6 @@ def __subscribeToButton(self, pin: int, button: Button):
def __buttonCallback(self, channel):
""" Callback happens on another thread, so this method is marshaling
ButtonPressedEvent instances to the main thread to handle """
if not super().relayIsClosing:
if not super().relayToggled:
button = self.__pinToButtonMap[channel]
self._fireEvent(ButtonPressedEvent(button))
2 changes: 1 addition & 1 deletion src/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ def setupLogging(queue: Queue=None):
logging.getLogger('').addHandler(handler)

logging.getLogger('chardet.charsetprober').setLevel(logging.INFO)
logging.getLogger('requests').setLevel(logging.INFO)
logging.getLogger('requests').setLevel(logging.WARNING)
17 changes: 13 additions & 4 deletions src/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ def setBacklight(self, enabled: bool):
self.__backlightEnabled = enabled
self.commit()

def refresh(self):
self.__window.refresh()

def commit(self):
""" Commits all pending changes to the display """
results = super().commit()
Expand Down Expand Up @@ -109,8 +112,9 @@ def __init__(self, stdscr, messageQueue: Queue):
TerminalRelay(ThermostatState.FAN, 3, 2, 32),
)

self.__lcd = TerminalDisplay(self.__displayWin, 20, 4)
super().__init__(
lcd=TerminalDisplay(self.__displayWin, 20, 4),
lcd=self.__lcd,
sensor=self.__environmentSensor,
relays=self.__relayList,
)
Expand Down Expand Up @@ -138,27 +142,30 @@ def __updateDisplay(self):
# Redraw the relay status
for relay in self.__relayList:
relay.redraw()
self.__logWin.refresh()
self.__lcd.refresh()

def __keyPressedHandler(self, event: KeyPressedEvent):
# Handle any key presses
char = event.key
if char == ord('l'):
self.__stdscr.clear()
self.__stdscr.refresh()
self.__updateDisplay()
elif char == ord('9'):
super()._fireEvent(PowerPriceChangedEvent(
price=self.__lastPrice-0.25, nextUpdate=1))
elif char == ord('0'):
super()._fireEvent(PowerPriceChangedEvent(
price=self.__lastPrice+0.25, nextUpdate=1))
elif char == ord('1'):
if not super().relayIsClosing:
if not super().relayToggled:
super()._modifyComfortSettings(1)
elif char == ord('2'):
if not super().relayIsClosing:
if not super().relayToggled:
super()._modifyComfortSettings(-1)
elif char == ord('3'):
if not super().relayIsClosing:
if not super().relayToggled:
super()._nextMode()
else:
log.debug("Ignoring button during relay closure")
Expand Down Expand Up @@ -190,6 +197,8 @@ def __keyPressListener(self):

def __processMessageQueue(self):
# Update any pending log messages to the log window
if self.__messageQueue.qsize():
self.__updateDisplay()
while self.__messageQueue.qsize():
message = self.__messageQueue.get()
y, x = self.__logWin.getmaxyx()
Expand Down

0 comments on commit 799e3f4

Please sign in to comment.