|
| 1 | +from dali.command import Command |
| 2 | +from dali.driver.base import SyncDALIDriver, DALIDriver |
| 3 | +from dali.frame import BackwardFrame |
| 4 | +import logging |
| 5 | +import sys |
| 6 | + |
| 7 | +import serial |
| 8 | +import threading |
| 9 | +import time |
| 10 | + |
| 11 | +DALI_PACKET_SIZE = {"j": 8, "h": 16, "l": 24, "m": 25} |
| 12 | +DALI_PACKET_PREFIX = {v: k for k, v in DALI_PACKET_SIZE.items()} |
| 13 | + |
| 14 | + |
| 15 | +class DaliHatSerialDriver(DALIDriver): |
| 16 | + """Driver for communicating with DALI devices over a serial connection.""" |
| 17 | + |
| 18 | + def __init__(self, port="/dev/ttyS0", LOG=None): |
| 19 | + """Initialize the serial connection to the DALI interface.""" |
| 20 | + self.port = port |
| 21 | + self.lock = threading.RLock() |
| 22 | + self.buffer = [] |
| 23 | + if not LOG: |
| 24 | + self.LOG = logging.getLogger("AtxLedDaliDriver") |
| 25 | + handler = logging.StreamHandler(sys.stdout) |
| 26 | + self.LOG.addHandler(handler) |
| 27 | + else: |
| 28 | + self.LOG = LOG |
| 29 | + try: |
| 30 | + self.conn = serial.Serial( |
| 31 | + port=self.port, |
| 32 | + baudrate=19200, |
| 33 | + parity=serial.PARITY_NONE, |
| 34 | + stopbits=serial.STOPBITS_ONE, |
| 35 | + bytesize=serial.EIGHTBITS, |
| 36 | + timeout=0.2, |
| 37 | + ) |
| 38 | + except Exception as e: |
| 39 | + self.LOG.exception("Could not open serial connection: %s", e) |
| 40 | + self.conn = None |
| 41 | + |
| 42 | + def read_line(self): |
| 43 | + """Read the next line from the buffer, refilling the buffer if necessary.""" |
| 44 | + with self.lock: |
| 45 | + while not self.buffer: |
| 46 | + line = self.conn.read_until(b"\n").decode("ascii") |
| 47 | + if not line: |
| 48 | + return "" |
| 49 | + self.buffer.append(line) |
| 50 | + return self.buffer.pop(0) |
| 51 | + |
| 52 | + def construct(self, command): |
| 53 | + """Construct a DALI command to be sent over the serial connection.""" |
| 54 | + assert isinstance(command, Command) |
| 55 | + f = command.frame |
| 56 | + packet_size = len(f) |
| 57 | + prefix = DALI_PACKET_PREFIX[packet_size] |
| 58 | + if command.sendtwice and packet_size == 16: |
| 59 | + prefix = "t" |
| 60 | + data = "".join(["{:02X}".format(byte) for byte in f.pack]) |
| 61 | + command_str = (f"{prefix}{data}\n").encode("ascii") |
| 62 | + return command_str |
| 63 | + |
| 64 | + def extract(self, data): |
| 65 | + """Parse the response from the serial device and return the corresponding frame.""" |
| 66 | + if data.startswith("J"): |
| 67 | + try: |
| 68 | + data = int(data[1:], 16) |
| 69 | + return BackwardFrame(data) |
| 70 | + except ValueError as e: |
| 71 | + self.LOG.error(f"Failed to parse response '{data}': {e}") |
| 72 | + return None |
| 73 | + |
| 74 | + def close(self): |
| 75 | + """Close the serial connection.""" |
| 76 | + if self.conn: |
| 77 | + self.conn.close() |
| 78 | + |
| 79 | + |
| 80 | +class SyncDaliHatDriver(DaliHatSerialDriver, SyncDALIDriver): |
| 81 | + """Synchronous DALI driver.""" |
| 82 | + |
| 83 | + def send(self, command: Command): |
| 84 | + """Send a command to the DALI interface and wait for a response.""" |
| 85 | + with self.lock: |
| 86 | + lines = [] |
| 87 | + last_resp = None |
| 88 | + send_twice = command.sendtwice |
| 89 | + cmd = self.construct(command) |
| 90 | + self.LOG.debug("command string sent: %r", cmd) |
| 91 | + self.conn.write(cmd) |
| 92 | + REPS = 5 |
| 93 | + i = 0 |
| 94 | + already_resent = False |
| 95 | + resent_times = 0 |
| 96 | + resp = None |
| 97 | + while i < REPS: |
| 98 | + i += 1 |
| 99 | + resp = self.read_line() |
| 100 | + self.LOG.debug("raw response received: %r", resp) |
| 101 | + resend = False |
| 102 | + if cmd[:3] not in ["hB1", "hB3", "hB5"]: |
| 103 | + if resp and resp[0] in {"N", "J"}: |
| 104 | + if send_twice: |
| 105 | + if last_resp: |
| 106 | + if last_resp == resp: |
| 107 | + resp = self.extract(resp) |
| 108 | + break |
| 109 | + resend = True |
| 110 | + last_resp = None |
| 111 | + else: |
| 112 | + last_resp = resp |
| 113 | + else: |
| 114 | + resp = self.extract(resp) |
| 115 | + break |
| 116 | + elif resp and resp[0] in {"X", "Z", ""}: |
| 117 | + time.sleep(0.1) |
| 118 | + collision_bytes = None |
| 119 | + while collision_bytes != "": |
| 120 | + collision_bytes = self.read_line() |
| 121 | + if resp[0] == "X": |
| 122 | + break |
| 123 | + self.LOG.info( |
| 124 | + "got conflict (%s) sending %r, sending again", resp, cmd |
| 125 | + ) |
| 126 | + last_resp = None |
| 127 | + resend = True |
| 128 | + elif resp: |
| 129 | + lines.append(resp) |
| 130 | + |
| 131 | + resp = None |
| 132 | + if resend and not already_resent: |
| 133 | + self.conn.write((cmd).encode("ascii")) |
| 134 | + REPS += 1 + send_twice |
| 135 | + already_resent = True |
| 136 | + else: |
| 137 | + if resp and resp[0] == "N": |
| 138 | + resp = self.extract(resp) |
| 139 | + break |
| 140 | + elif resp and resp[0] in {"X", "Z", ""}: |
| 141 | + time.sleep(0.1) |
| 142 | + collision_bytes = None |
| 143 | + while collision_bytes != "": |
| 144 | + collision_bytes = self.read_line() |
| 145 | + elif resp: |
| 146 | + last_resp = None |
| 147 | + resend = True |
| 148 | + if resend and resent_times < 5: |
| 149 | + self.conn.write(cmd.encode("ascii")) |
| 150 | + REPS += 1 + send_twice |
| 151 | + resent_times += 1 |
| 152 | + if command.is_query: |
| 153 | + return command.response(resp) |
| 154 | + return resp |
0 commit comments