chutney/garage_display.py

70 lines
2.2 KiB
Python
Raw Normal View History

2022-12-26 19:37:50 -05:00
import colors
import json
class GarageDisplay:
def __init__(self, characterDisplay, topRow):
self.characterDisplay = characterDisplay
self.topRow = topRow
self.westDoorStartColumn = 0
2022-12-27 10:53:33 -05:00
self.eastDoorStartColumn = 12
2022-12-26 19:37:50 -05:00
self.currentState = {}
self.closedOutlineColor = colors.RED
2022-12-27 10:53:33 -05:00
self.closedFillColor = colors.RED
2022-12-26 19:37:50 -05:00
self.openOutlineColor = colors.WHITE
2022-12-27 10:53:33 -05:00
self.openFillColor = colors.WHITE
2022-12-26 19:37:50 -05:00
self.garageFile = 'garage.json'
def showGarageState(self):
state = self.getGarageState()
# TODO - only update the changed door so the other one doesn't flicker
2022-12-26 19:37:50 -05:00
if state != self.currentState:
self.currentState = state
self.characterDisplay.clearRow(self.topRow, rowHeight=4)
if self.isGarageStateValid(state):
self.showState(state)
def getGarageState(self):
try:
with open(self.garageFile) as file:
return json.load(file)
except Exception as e:
print(e, flush=True)
return {'error': True}
def isGarageStateValid(self, state):
return not 'error' in state
def showState(self, state):
if state["west-door"] == "closed":
self.characterDisplay.displaySquare(
x=self.westDoorStartColumn,
y=self.topRow,
outlineColor=self.closedOutlineColor,
fillColor=self.closedFillColor
)
else:
self.characterDisplay.displaySquare(
x=self.westDoorStartColumn,
y=self.topRow,
outlineColor=self.openOutlineColor,
fillColor=self.openFillColor
)
if state["east-door"] == "closed":
self.characterDisplay.displaySquare(
x=self.eastDoorStartColumn,
y=self.topRow,
outlineColor=self.closedOutlineColor,
fillColor=self.closedFillColor
)
else:
self.characterDisplay.displaySquare(
x=self.eastDoorStartColumn,
y=self.topRow,
outlineColor=self.openOutlineColor,
fillColor=self.openFillColor
)