-
Notifications
You must be signed in to change notification settings - Fork 2
/
redis_display.py
54 lines (43 loc) · 1.6 KB
/
redis_display.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import logging
import sys
import os
import json
import redis
import time
from luma.core.virtual import terminal
from subprocess import check_output
from joedisplay import get_device, StageController
from transport.stages import TrainDepartureBoardStage
from joedisplay.stages import MetricsStage
"""
Display exposed as an Redis channel subscriber. Will render any `Stage` registered within the controller.
Any stage registered below with `add_stage` will work.
PUBLISH display-input '{"stage": "text", "message": "Hello from Redis."}'
"""
logger = logging.getLogger(__name__)
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
redis_host = os.environ.get('REDIS_HOST', 'localhost')
display_topic = os.environ.get('DISPLAY_CHANNEL', 'display-input')
device = get_device()
term = terminal(device, None)
term.println(
f"Display ready for events on channel:\n{display_topic}\nRedis host: {redis_host}")
term.flush()
controller = StageController(device)
controller.add_stage(TrainDepartureBoardStage)
controller.add_stage(MetricsStage)
if __name__ == "__main__":
# Subscribe to Redis topic for display updates
r = redis.Redis(host=redis_host, port=6379, db=0)
p = r.pubsub(ignore_subscribe_messages=True)
p.subscribe(display_topic)
logger.info(f"Waiting for display events on {display_topic}...")
while True:
message = p.get_message()
if message is not None:
try:
event = json.loads(message['data'])
controller.event_handler(event, None)
except Exception as e:
logger.exception(e)
time.sleep(0.025)