From 074acbd63a208cb76403a35920844d5127035fe6 Mon Sep 17 00:00:00 2001 From: Chris Hutchinson Date: Sat, 8 Jun 2019 12:29:07 +0100 Subject: [PATCH 1/2] feat: Add 'Welcome to -station name-' message when no services are running --- src/main.py | 81 ++++++++++++++++++++++++++++++++++++++++++++++----- src/trains.py | 2 +- 2 files changed, 74 insertions(+), 9 deletions(-) diff --git a/src/main.py b/src/main.py index 65c4f10..ba636ee 100644 --- a/src/main.py +++ b/src/main.py @@ -98,13 +98,70 @@ def renderTime(draw, width, height): font=fontBold, fill="yellow") +def renderWelcomeTo(xOffset): + def drawText(draw, width, height): + text = "Welcome to" + draw.text((int(xOffset), 0), text=text, font=fontBold, fill="yellow") + + return drawText + + +def renderDepartureStation(departureStation, xOffset): + def draw(draw, width, height): + text = departureStation + draw.text((int(xOffset), 0), text=text, font=fontBold, fill="yellow") + + return draw + + +def renderDots(draw, width, height): + text = ". . ." + draw.text((0, 0), text=text, font=fontBold, fill="yellow") + + def loadData(apiConfig, journeyConfig): - departures = loadDeparturesForStation( + departures, stationName = loadDeparturesForStation( journeyConfig, apiConfig["appId"], apiConfig["apiKey"]) + + if len(departures) == 0: + return False, False, stationName + firstDepartureDestinations = loadDestinationsForDeparture( departures[0]["service_timetable"]["id"]) - return departures, firstDepartureDestinations + return departures, firstDepartureDestinations, stationName + + +def drawBlankSignage(device, width, height, departureStation): + global stationRenderCount, pauseCount + + with canvas(device) as draw: + welcomeSize = draw.textsize("Welcome to", fontBold) + + with canvas(device) as draw: + stationSize = draw.textsize(departureStation, fontBold) + + device.clear() + + virtualViewport = viewport(device, width=width, height=height) + + rowOne = snapshot(width, 16, renderWelcomeTo( + (width - welcomeSize[0]) / 2), interval=10) + rowTwo = snapshot(width, 16, renderDepartureStation( + departureStation, (width - stationSize[0]) / 2), interval=10) + rowThree = snapshot(width, 16, renderDots, interval=10) + rowTime = snapshot(width, 14, renderTime, interval=1) + + if len(virtualViewport._hotspots) > 0: + for hotspot, xy in virtualViewport._hotspots: + virtualViewport.remove_hotspot(hotspot, xy) + + virtualViewport.add_hotspot(rowOne, (0, 0)) + virtualViewport.add_hotspot(rowTwo, (0, 16)) + virtualViewport.add_hotspot(rowThree, (0, 32)) + virtualViewport.add_hotspot(rowTime, (0, 50)) + + return virtualViewport def drawSignage(device, width, height, data): @@ -117,7 +174,7 @@ def drawSignage(device, width, height, data): status = "On time" callingAt = "Calling at:" - departures, firstDepartureDestinations = data + departures, firstDepartureDestinations, departureStation = data with canvas(device) as draw: w, h = draw.textsize(callingAt, font) @@ -176,17 +233,25 @@ def drawSignage(device, width, height, data): loop_count = 0 data = loadData(config["transportApi"], config["journey"]) - virtual = drawSignage(device, width=widgetWidth, - height=widgetHeight, data=data) + if data[0] == False: + virtual = drawBlankSignage( + device, width=widgetWidth, height=widgetHeight, departureStation=data[2]) + else: + virtual = drawSignage(device, width=widgetWidth, + height=widgetHeight, data=data) timeAtStart = time.time() timeNow = time.time() while True: if(timeNow - timeAtStart >= config["refreshTime"]): data = loadData(config["transportApi"], config["journey"]) - virtual = drawSignage(device, width=widgetWidth, - height=widgetHeight, data=data) - timeAtStart = time.time() + if data[0] == False: + virtual = drawBlankSignage( + device, width=widgetWidth, height=widgetHeight, departureStation=data[2]) + else: + virtual = drawSignage(device, width=widgetWidth, + height=widgetHeight, data=data) + timeAtStart = time.time() timeNow = time.time() virtual.refresh() diff --git a/src/trains.py b/src/trains.py index c650d31..82f18eb 100644 --- a/src/trains.py +++ b/src/trains.py @@ -26,7 +26,7 @@ def loadDeparturesForStation(journeyConfig, appId, apiKey): if "error" in data: raise ValueError(data["error"]) - return data["departures"]["all"] + return data["departures"]["all"], data["station_name"] def loadDestinationsForDeparture(timetableUrl): From e398f43c189fcdb3d777e182d674fe687c66320c Mon Sep 17 00:00:00 2001 From: Chris Hutchinson Date: Sat, 8 Jun 2019 15:30:22 +0100 Subject: [PATCH 2/2] fix: Issue which prevented the program from refreshing --- src/main.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main.py b/src/main.py index ba636ee..f860f56 100644 --- a/src/main.py +++ b/src/main.py @@ -239,6 +239,7 @@ def drawSignage(device, width, height, data): else: virtual = drawSignage(device, width=widgetWidth, height=widgetHeight, data=data) + timeAtStart = time.time() timeNow = time.time() @@ -251,7 +252,8 @@ def drawSignage(device, width, height, data): else: virtual = drawSignage(device, width=widgetWidth, height=widgetHeight, data=data) - timeAtStart = time.time() + + timeAtStart = time.time() timeNow = time.time() virtual.refresh()