import colors import json class GarageDisplay: def __init__(self, characterDisplay, topRow): self.characterDisplay = characterDisplay self.topRow = topRow self.westDoorStartColumn = 0 self.eastDoorStartColumn = 12 self.currentState = {} self.closedOutlineColor = colors.RED self.closedFillColor = colors.RED self.openOutlineColor = colors.WHITE self.openFillColor = colors.WHITE self.garageFile = 'garage.json' def showGarageState(self): state = self.getGarageState() # TODO - only update the changed door so the other one doesn't flicker 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 )