chutney/chutney/garage/garage_display.py

68 lines
2.0 KiB
Python

import chutney.colors as colors
import json
class GarageDisplay:
def __init__(self, symbolDisplay, topRow):
self.symbolDisplay = symbolDisplay
self.topRow = topRow
self.westDoorStartColumn = 1
self.eastDoorStartColumn = 12
self.currentState = {}
self.closedOutlineColor = colors.RED
self.closedFillColor = colors.RED
self.openOutlineColor = colors.WHITE
self.openFillColor = colors.WHITE
self.garageFile = 'data/garage.json'
self.symbolDisplay.clearRow(self.topRow, rowHeight=4)
def showGarageState(self):
state = self.getGarageState()
if state != self.currentState:
self.currentState = state
if self.isGarageStateValid(state):
self.showState(state)
else:
self.symbolDisplay.clearRow(self.topRow, rowHeight=4)
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.showClosedDoor(self.westDoorStartColumn)
else:
self.showOpenDoor(self.westDoorStartColumn)
if state["east-door"] == "closed":
self.showClosedDoor(self.eastDoorStartColumn)
else:
self.showOpenDoor(self.eastDoorStartColumn)
def showOpenDoor(self, column):
self.symbolDisplay.displaySquare(
x=column,
y=self.topRow,
outlineColor=self.openOutlineColor,
fillColor=self.openFillColor
)
def showClosedDoor(self, column):
self.symbolDisplay.displaySquare(
x=column,
y=self.topRow,
outlineColor=self.closedOutlineColor,
fillColor=self.closedFillColor
)