|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +import re |
| 4 | +from datetime import datetime |
| 5 | + |
| 6 | +from senaite.astm import adapter_registry |
| 7 | +from senaite.astm import utils |
| 8 | +from senaite.astm.constants import ENQ |
| 9 | +from senaite.astm.constants import EOT |
| 10 | +from senaite.astm.constants import NAK |
| 11 | +from senaite.astm.interfaces import IDataHandler |
| 12 | +from senaite.astm.utils import f |
| 13 | +from senaite.astm.utils import u |
| 14 | +from zope.interface import implementer |
| 15 | + |
| 16 | +RX = ( |
| 17 | + rb"\x02" # Start of the message |
| 18 | + rb"(?:\x1emt(?P<mt>[^|]*))?" # Message Type (mt) |
| 19 | + rb"(?:\|\x1epi(?P<pi>[^|]*))?" # Patient Identifier (pi) |
| 20 | + rb"(?:\|\x1epn(?P<pn>[^|]*))?" # Patient Name (pn) |
| 21 | + rb"(?:\|\x1epb(?P<pb>[^|]*))?" # Patient Birthdate (pb) |
| 22 | + rb"(?:\|\x1eps(?P<ps>[^|]*))?" # Patient Sex (ps) |
| 23 | + rb"(?:\|\x1eso(?P<so>[^|]*))?" # Sample Origin (so) |
| 24 | + rb"(?:\|\x1esi(?P<si>[^|]*))?" # Specimen separator (si) |
| 25 | + rb"(?:\|\x1eci(?P<ci>[^|]*))?" # Sample Identifier (ci) |
| 26 | + rb"(?:\|\x1ert(?P<rt>[^|]*))?" # Short assay name (rt) |
| 27 | + rb"(?:\|\x1ern(?P<rn>[^|]*))?" # Long assay name (rn) |
| 28 | + rb"(?:\|\x1ett(?P<tt>[^|]*))?" # Test completion time (tt) |
| 29 | + rb"(?:\|\x1etd(?P<td>[^|]*))?" # Test completion date (td) |
| 30 | + rb"(?:\|\x1eql(?P<ql>[^|]*))?" # Qualitative Result (ql) |
| 31 | + rb"(?:\|\x1eqn(?P<qn>[^|]*))?" # Quantitative Result (qn) |
| 32 | + rb"(?:\|\x1ey3(?P<y3>[^|]*))?" # Unit associated with qn (y3) |
| 33 | + rb"(?:\|\x1eqd(?P<qd>[^|]*))?" # Dilution (qd) |
| 34 | + rb"(?:\|\x1enc(?P<nc>[^|]*))?" # Vidas flags (nc) |
| 35 | + rb"(?:\|\x1eid(?P<id>[^|]*))?" # Instrument ID (id) |
| 36 | + rb"(?:\|\x1esn(?P<sn>[^|]*))?" # Serial Number (sn) |
| 37 | + rb"(?:\|\x1em4(?P<m4>[^|]*))?" # Technologist (m4) |
| 38 | + rb"(?:\|\x1d(?P<checksum>[a-fA-F0-9]{2}))$" # Checksum |
| 39 | +) |
| 40 | + |
| 41 | + |
| 42 | +@implementer(IDataHandler) |
| 43 | +class DataHandler: |
| 44 | + """Custom data handler for Biomérieux miniVidas |
| 45 | +
|
| 46 | + We receive from this instrument a non valid ASTM message that need to be |
| 47 | + handled differntly |
| 48 | + """ |
| 49 | + def __init__(self, protocol, data): |
| 50 | + self.protocol = protocol |
| 51 | + self.data = data |
| 52 | + |
| 53 | + def can_handle(self): |
| 54 | + return re.match(RX, self.data) is not None |
| 55 | + |
| 56 | + def to_timestamp(self, date, time): |
| 57 | + """Make a timestamp from the date and time |
| 58 | + """ |
| 59 | + dt = datetime.now() |
| 60 | + if date: |
| 61 | + dt = datetime.strptime(u(date), '%m/%d/%y') |
| 62 | + if time: |
| 63 | + t = datetime.strptime(u(time), '%H:%M').time() |
| 64 | + dt = datetime.combine(dt, t) |
| 65 | + return dt.strftime("%Y%m%d%H%M%S") |
| 66 | + |
| 67 | + def handle_data(self): |
| 68 | + """Create a valid ASTM message of the received data |
| 69 | +
|
| 70 | + 1. Create a static header record |
| 71 | + 2. Create a results record with the given data |
| 72 | + 3. Create a termination record |
| 73 | +
|
| 74 | + """ |
| 75 | + parts = re.match(RX, self.data) |
| 76 | + if not parts: |
| 77 | + return NAK |
| 78 | + |
| 79 | + # initialize the communication if we're not already in transfer state |
| 80 | + # Note: This is mainly a test fixture for the simulator |
| 81 | + if not self.protocol.in_transfer_state: |
| 82 | + self.protocol.on_enq(ENQ) |
| 83 | + |
| 84 | + data = {} |
| 85 | + for k, v in parts.groupdict().items(): |
| 86 | + data[k] = u(v) if v else "" |
| 87 | + |
| 88 | + # convert date and time to a timestamp |
| 89 | + date = data.get("td") |
| 90 | + time = data.get("tt") |
| 91 | + data["ts"] = self.to_timestamp(date, time) |
| 92 | + |
| 93 | + frames = [ |
| 94 | + f("1H|\\^&|||miniVidas^biomerieux^1.0.0|||||||||{ts}{CR}{ETX}", |
| 95 | + **data), |
| 96 | + f("2P|1|||{pi}|{pn}||{pb}|{ps}||||||||||||||||||||||||||{CR}{ETX}", |
| 97 | + **data), |
| 98 | + f("3O|1|{ci}||{rn}||||||||||||||||||{ts}||||||||{CR}{ETX}", |
| 99 | + **data), |
| 100 | + f("4R|1|{rt}|{qn}|||{nc}||{ql}||{m4}||{ts}|{CR}{ETX}", |
| 101 | + **data), |
| 102 | + f("5L|1|N{CR}{ETX}"), |
| 103 | + ] |
| 104 | + messages = [] |
| 105 | + for frame in frames: |
| 106 | + cs = utils.make_checksum(frame) |
| 107 | + messages.append( |
| 108 | + f("{STX}{frame}{cs}{CRLF}", frame=u(frame), cs=u(cs))) |
| 109 | + |
| 110 | + # fill in the full message |
| 111 | + self.protocol.messages = messages |
| 112 | + |
| 113 | + # end the communicaiton |
| 114 | + self.protocol.on_eot(EOT) |
| 115 | + |
| 116 | + |
| 117 | +# register the adapter |
| 118 | +adapter_registry.registerAdapter( |
| 119 | + DataHandler, |
| 120 | + required=(object, object), |
| 121 | + provided=IDataHandler, |
| 122 | + name="mini_vidas", |
| 123 | +) |
0 commit comments