def init_display(self):
        if Config.get("debug.dummy", False):
            self.device = dummy(width=256, height=64, rotate=0, mode="1")
        else:
            serial = spi(bus_speed_hz=Config.get("debug.bus_speed", 16000000))
            self.device = ssd1322(serial, mode="1", rotate=0)

        self.viewport = viewport(self.device,
                                 width=self.device.width,
                                 height=self.device.height)
    def parse_departures(self, state, data):
        destination = Config.get("settings.destination")
        platforms = Config.get("settings.platforms")
        limit = Config.get("settings.services", 3)
        tocs = Config.get("settings.tocs")
        if data["departures"]:
            departures = sorted(data["departures"],
                                key=lambda departure: departure["location"][
                                    "timetable"]["time"])
        else:
            departures = []

        cutoff = datetime.now() + timedelta(
            hours=Config.get("settings.cutoff", 8))

        state.departures = []
        for departure in departures:
            # Hide platforms we don't care about
            if platforms and departure["location"]["forecast"]["plat"][
                    "plat"] not in platforms:
                continue

            # Hide specific tocs
            if tocs and departure["toc"] not in tocs:
                continue

            # Hide Departed
            if "departed" in departure["location"]["forecast"] and departure[
                    "location"]["forecast"]["departed"]:
                continue

            # Hide ones that aren't calling at our destination
            if destination and not self.calls_at(data, departure, destination):
                continue

            # Hide any after our cutoff

            # Calculate a python datetime
            origin_ts = datetime.strptime(
                departure["ssd"] + " " +
                departure["origin"]["timetable"]["time"], "%Y-%m-%d %H:%M:%S")
            depart_ts = datetime.strptime(
                departure["ssd"] + " " + departure["location"]["displaytime"],
                "%Y-%m-%d %H:%M:%S")
            if origin_ts > depart_ts:
                # We need to add a day to the datetime
                depart_ts += timedelta(days=1)
            if depart_ts >= cutoff:
                continue

            state.departures.append(self.create_departure(data, departure))

            if len(state.departures) >= limit:
                return
Example #3
0
    def init_message_carousel(self):
        if len(self.__messages) == 0:
            return

        self.current_message = None
        self.message_transition = datetime.now() + timedelta(
            seconds=Config.get("settings.messages.frequency"))
    def init_powersaving(self):
        self.brightness = Config.get("settings.brightness")
        self.normal_brightness = self.brightness

        self.finish_init = datetime.now() + timedelta(seconds=5)

        start = Config.get("settings.powersaving.start", "01:00")
        end = Config.get("settings.powersaving.end", "07:00")
        self.powersaving_brightness = Config.get(
            "settings.powersaving.brightness", 0)

        self.powersaving_start = dtt(hour=int(start[:2]),
                                     minute=int(start[3:5]))
        self.powersaving_end = dtt(hour=int(end[:2]), minute=int(end[3:5]))

        self.update_powersaving(datetime.now())
def render_departure(canvas, font, order=1, departure=None, ypos=0):
    if not departure:
        return
    
    # Order: Left
    canvas.text((0, ypos), text=utils.ordinal(order), font=font, fill="yellow")

    # Scheduled: Center
    align = utils.align(font, departure["scheduled"], 28, "center")
    canvas.text((17 + align, ypos), text=departure["scheduled"], font=font, fill="yellow")

    # Headcode: Optional
    xpos = 0
    if Config.get("settings.layout.headcodes"):
        xpos += 27
        align = utils.align(font, departure["headcode"], 27, "center")
        canvas.text((45 + align, ypos), text=departure["headcode"], font=font, fill="yellow")

    # Platform: Center
    if departure["platform"]:
      align = utils.align(font, departure["platform"], 19, "center")
      canvas.text((45 + align + xpos, ypos), text=departure["platform"], font=font, fill="yellow")

    # Destination: Left
    canvas.text((64 + xpos, ypos), text=departure["destination"]["abbr_name"], font=font, fill="yellow")

    # Status: Right
    align = utils.align(font, departure["status"], 40, "right")
    canvas.text((216 + align, ypos), text=departure["status"], font=font, fill="yellow")
    def get_state(self, as_dict=False):
        departure = Config.get("settings.departure")
        if Config.get("debug.url"):
            url = Config.get("debug.url")
        else:
            url = "https://ldb.prod.a51.li/boards/{0}?term=false&t={1}000&limit=0".format(
                departure, int(time.time()))

        data = self.get_from_nrea(url)

        state = State()

        self.parse_station(state, data)
        self.parse_messages(state, data)
        self.parse_departures(state, data)

        return state
Example #7
0
    def update_message_carousel(self, timestamp):
        if len(self.messages) == 0:
            return

        if timestamp < self.message_transition:
            return

        if self.current_message == None:
            self.current_message = 0
        else:
            self.current_message += 1

        if self.current_message == len(self.__messages):
            self.current_message = None
            interval = Config.get("settings.messages.frequency")
            self.message_element.hotspot.update_text(self.text)

        else:
            interval = Config.get("settings.messages.interval")
            self.message_element.hotspot.update_text(
                self.__messages[self.current_message])

        self.message_transition = timestamp + timedelta(seconds=interval)
Example #8
0
    def get_calling_at(self, stops):
        showtimes = Config.get("settings.layout.times", False)
        stations = []
        for stop in stops:
            text = stop["location"]["abbr_name"]
            if showtimes:
                text += " ({0})".format(stop["time"])
            stations.append(text)

        if not stations:
            return ""

        last = stations.pop()
        calling_at = last
        if stations:
            calling_at = ", ".join(stations) + " and " + calling_at

        return calling_at
    def show_image(self):
        if not Config.get("debug.dummy", False):
            return

        utils.display_image("Departure Board", self.device.image)
from trains.api import Api
from trains.board import Board
from trains.config import Config

from time import sleep
import sentry_sdk

sentry_sdk.init(
    "https://[email protected]/5275445"
)

board = Board()
board.departure_board()

api = Api()
debug = Config.get("debug.stats", False)

frequency = Config.get("debug.frequency", 60)
framerate = Config.get("debug.framerate", 0)
regulator = framerate_regulator(fps=framerate)
timer = None


def minute_timer():
    if not frequency:
        return
    timestamp = datetime.now()
    board.update_powersaving(timestamp)
    try:
        state = api.get_cached_state(timestamp, frequency, as_dict=True)
        board.update_state(state)