qslib 0.15.1

QSlib QuantStudio qPCR machine library
Documentation
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
# SPDX-FileCopyrightText: 2021 - 2023 Constantine Evans <qslib@mb.costi.net>
#
# SPDX-License-Identifier: EUPL-1.2

from __future__ import annotations

import asyncio
import functools
import logging
import os
import re
import shutil
import time
import zipfile
from dataclasses import dataclass
from pathlib import Path
from typing import Dict, Iterable, List, Optional, TextIO, Tuple, Type, Union, cast

import numpy as np
import numpy.typing as npt
from influxdb_client import InfluxDBClient, Point  # , Point, WritePrecision
from influxdb_client.client.write_api import ASYNCHRONOUS
from nio.client import AsyncClient
from nio.client.async_client import AsyncClientConfig
from nio.responses import JoinedRoomsError

from qslib.plate_setup import PlateSetup
from qslib.machine import FilterDataFilename, Machine, CommandError
from qslib.scpi_commands import AccessLevel, ArgList

log = logging.getLogger("monitor")

LEDSTATUS = re.compile(rb"Temperature:([+\-\d.]+) Current:([+\-\d.]+) Voltage:([+\-\d.]+) JuncTemp:([+\-\d.]+)")


@dataclass
class LEDStatus:
    temperature: float
    current: float
    voltage: float
    junctemp: float


@dataclass(frozen=True)
class MatrixConfig:
    password: str
    user: str
    room: str
    host: str
    encryption: bool = False


@dataclass(frozen=True)
class InfluxConfig:
    token: str
    org: str
    bucket: str
    url: str


@dataclass(frozen=True)
class MachineConfig:
    password: Union[str, None] = None
    name: str = "localhost"
    host: str = "localhost"
    ssl: bool | None = True
    port: str | int | None = None
    retries: int = 3
    compile: bool = False


@dataclass(frozen=True)
class SyncConfig:
    completed_directory: Union[str, None] = None
    in_progress_directory: Union[str, None] = None


@dataclass(frozen=True)
class Config:
    matrix: Union[MatrixConfig, None] = None
    influxdb: Union[InfluxConfig, None] = None
    machine: MachineConfig = MachineConfig()
    sync: SyncConfig = SyncConfig()


@dataclass
class RunState:
    name: Optional[str] = None
    stage: Optional[int | str] = None
    cycle: Optional[int] = None
    step: Optional[int] = None
    plate_setup: Optional[PlateSetup] = None

    def refresh(self, c: Machine) -> None:
        runmsg = ArgList.from_string(c.run_command("RunProgress?"))
        name = cast(str, runmsg.opts["RunTitle"])
        if name == "-":
            self.name = None
        else:
            self.name = re.sub(r"(<([\w.]+)>)?([^<]+)(</[\w.]+>)?", r"\3", name)
        stage = runmsg.opts["Stage"]
        if stage == "-":
            self.stage = None
        else:
            self.stage = cast(int, stage)
        cycle = runmsg.opts["Cycle"]
        if cycle == "-":
            self.cycle = None
        else:
            self.cycle = cast(int, cycle)
        step = runmsg.opts["Step"] if self.stage else None
        if step == "-":
            self.step = None
        else:
            self.step = cast(Optional[int], step)
        if self.name:
            try:
                self.plate_setup = PlateSetup.from_machine(c)
            except CommandError:
                self.plate_setup = None

    @classmethod
    def from_machine(cls: Type[RunState], c: Machine) -> RunState:
        n = cls.__new__(cls)
        n.refresh(c)
        return n

    def statemsg(self, timestamp: str) -> str:
        s = f'run_state name="{self.name}"'
        if self.stage:
            s += f",stage={self.stage}i,cycle={self.cycle}i,step={self.step}i"
        else:
            s += ",stage=0i,cycle=0i,step=0i"  # FIXME: not great
        s += f" {timestamp}"
        return s


@dataclass
class MachineState:
    zone_targets: List[float]
    zone_controls: List[bool]
    cover_target: float
    cover_control: bool
    drawer: str

    def refresh(self, c: Machine) -> None:
        targmsg = ArgList.from_string(c.run_command("TBC:SETT?"))
        self.cover_target = cast(float, targmsg.opts["Cover"])
        self.zone_targets = cast(List[float], [targmsg.opts[f"Zone{i}"] for i in range(1, 7)])

        contmsg = ArgList.from_string(c.run_command("TBC:CONT?"))
        self.cover_control = cast(bool, contmsg.opts["Cover"])
        self.zone_controls = cast(List[bool], [contmsg.opts[f"Zone{i}"] for i in range(1, 7)])

        self.drawer = c.run_command("DRAW?")

    @classmethod
    def from_machine(cls: Type[MachineState], c: Machine) -> MachineState:
        n = cast(MachineState, cls.__new__(cls))
        n.refresh(c)
        return n

    # def targetmsg(timestamp):
    #    return ""


@dataclass
class State:
    run: RunState
    machine: MachineState

    @classmethod
    def from_machine(cls: Type[State], c: Machine) -> State:
        run = RunState.from_machine(c)
        machine = MachineState.from_machine(c)
        return cls(run, machine)


# def parse_fd_fn(x: str) -> Tuple[str, int, int, int, int]:
#    s = re.search(r"S(\d{2})_C(\d{3})_T(\d{2})_P(\d{4})_M(\d)_X(\d)_filterdata.xml$", x)
#    assert s is not None
#    return (f"x{s[6]}-m{s[5]}", int(s[1]), int(s[2]), int(s[3]), int(s[4]))


def index_to_filename_ref(i: Tuple[str, int, int, int, int]) -> str:
    x, s, c, t, p = i
    return f"S{s:02}_C{c:03}_T{t:02}_P{p:04}_M{x[4]}_X{x[1]}"


def get_runinfo(c: Machine) -> State:
    state = State.from_machine(c)
    return state


class Collector:
    def __init__(self, config: Config):
        self.config = config

        if self.config.influxdb:
            self.idbclient = InfluxDBClient(
                url=self.config.influxdb.url,
                token=self.config.influxdb.token,
                org=self.config.influxdb.org,
            )
            self.idbw = self.idbclient.write_api(write_options=ASYNCHRONOUS)
        else:
            self.idbw = None

        if self.config.matrix:
            self.matrix_config = AsyncClientConfig(encryption_enabled=self.config.matrix.encryption)

            self.matrix_client = AsyncClient(
                self.config.matrix.host,
                self.config.matrix.user,
                store_path="./matrix_store/",
                config=self.matrix_config,
            )

        log.info(config.sync)

        self.run_log_file: TextIO | None = None

    def inject(self, t: str | Iterable[str | Point] | Point, flush: bool = False) -> None:
        if self.idbw:
            self.idbw.write(bucket=self.config.influxdb.bucket, record=t)  # type:ignore
            if flush:
                self.idbw.flush()
        else:
            pass

    async def matrix_announce(self, msg: str) -> None:
        assert self.config.matrix
        await self.matrix_client.room_send(
            room_id=self.config.matrix.room,
            message_type="m.room.message",
            content={"msgtype": "m.text", "body": msg},
            ignore_unverified_devices=True,
        )

        await self.matrix_client.sync()

    def setup_new_rundir(
        self,
        connection: Machine,
        name: str,
        *,
        firstmsg: str | None = None,
        overwrite: bool = False,
    ) -> None:
        # name = name.replace(" ", "_")

        assert self.ipdir is not None

        if not self.ipdir.is_dir():
            log.error(f"Can't open in-progress directory {self.ipdir}.")
            return

        dirpath = self.ipdir / name

        if dirpath.exists() and (not overwrite):
            log.error(f"In-progress directory for {name} already exists.")
            return
        elif dirpath.exists():
            assert dirpath != self.ipdir
            shutil.rmtree(dirpath)

        dirpath.mkdir()
        zf = connection.read_dir_as_zip(name, "experiment")
        target = dirpath.resolve()
        for member in zf.namelist():
            member_path = (target / member).resolve()
            if not str(member_path).startswith(str(target)):
                raise ValueError(f"ZIP member {member!r} would extract outside target directory")
        zf.extractall(dirpath)

        (dirpath / "apldbio" / "sds" / "quant").mkdir(exist_ok=True)
        (dirpath / "apldbio" / "sds" / "filter").mkdir(exist_ok=True)
        (dirpath / "apldbio" / "sds" / "calibrations").mkdir(exist_ok=True)

        self.run_log_file = (dirpath / "apldbio" / "sds" / "messages.log").open("a")

        if firstmsg is not None:
            self.run_log_file.write(firstmsg)
            self.run_log_file.flush()

    @property
    def ipdir(self) -> Path | None:
        x = self.config.sync.in_progress_directory
        if x is None:
            return None
        else:
            return Path(x)

    def run_ip_path(self, name: str) -> Path:
        # name = name.replace(" ", "_")
        if (ipdir := self.ipdir) is None:
            raise ValueError
        return ipdir / name / "apldbio" / "sds"

    def compile_eds(self, connection: Machine, name: str) -> None:
        # name = name.replace(" ", "_")

        # Wait 5 minutes in case machine compiles it (AB sofware run)
        time.sleep(300.0)

        try:
            connection.set_access_level(AccessLevel.Controller)
            connection.compile_eds(name)
        except FileNotFoundError as e:
            raise e
        finally:
            connection.set_access_level(AccessLevel.Observer)

    def sync_completed(self, connection: Machine, name: str) -> None:
        # name = name.replace(" ", "_")

        try:
            self.compile_eds(connection, name)
        except FileNotFoundError:
            pass

        dir = Path(cast(str, self.config.sync.completed_directory))

        if not dir.is_dir():
            log.error(f"Can't sync completed EDS to invalid path {dir}.")
            return

        path = dir / (name + ".eds")

        if path.exists():
            log.error(f"Completed EDS already exists for {name}.")
            return

        try:
            with path.open("wb") as f:
                edsfile = connection.read_file(f"public_run_complete:{name}.eds")
                f.write(edsfile)
        except Exception as e:
            log.error(f"Error synchronizing completed EDS {name}: {e}")
            return

        if self.ipdir:
            import shutil

            if (self.ipdir / name).exists():
                shutil.rmtree(self.ipdir / name)
            if (x := (self.ipdir / (name + ".eds"))).exists():
                x.unlink()

    def docollect(
        self,
        args: Dict[str, Union[str, int, bool, float]],
        state: State,
        connection: Machine,
    ) -> None:
        if state.run.plate_setup:
            pa: npt.NDArray[np.object_] | None = state.run.plate_setup.well_samples_as_array()
        else:
            pa = None

        run = cast(str, args["run"])

        if run.startswith('"'):
            run = run[1:-1]

        del args["run"]
        for k, v in args.items():
            if k != "run":
                args[k] = int(v)
        pl = [
            FilterDataFilename.fromstring(x)
            for x in connection.get_expfile_list(
                "{run}/apldbio/sds/filter/S{stage:02}_C{cycle:03}_T{step:02}_P{point:04}_*_filterdata.xml".format(
                    run=run, **cast(Dict[str, int], args)
                ),
                allow_nomatch=True,
            )
        ]
        pl.sort()
        toget = [x for x in pl if x.is_same_point(pl[-1])]

        lp: List[str] = []
        files: list[tuple[str, bytes]] = []

        if (
            self.ipdir
            and (
                self.ipdir / run / "apldbio" / "sds" / "filter"  # .replace(" ", "_")
            ).exists()
        ):
            for fdf in toget:
                fdr, files_one = connection.get_filterdata_one(fdf, return_files=True)
                lp += fdr.to_lineprotocol(run_name=run, sample_array=pa)
                files += files_one
        else:
            for fdf in toget:
                lp += (connection.get_filterdata_one(fdf)).to_lineprotocol(run_name=run, sample_array=pa)

        self.inject(lp, flush=True)

        for path, data in files:
            fullpath = self.run_ip_path(run) / path
            with fullpath.open("wb") as f:
                f.write(data)

        if (
            self.ipdir
            and (
                self.ipdir / run / "apldbio" / "sds" / "filter"  # .replace(" ", "_")
            ).exists()
        ):
            saferun = run  # .replace(" ", "_")
            ipp = self.ipdir / saferun
            with zipfile.ZipFile(self.ipdir / (saferun + ".eds"), "w") as z:
                for root, _, zfiles in os.walk(ipp):
                    for zfile in zfiles:
                        fpath = os.path.join(root, zfile)
                        z.write(fpath, os.path.relpath(fpath, ipp))

    def handle_run_msg(
        self: Collector,
        state: State,
        c: Machine,
        topic: bytes,
        message: bytes,
        timestamp: float | None,
    ) -> None:
        topic_str = topic.decode()
        message_str = message.decode()

        # Are we logging?
        if self.run_log_file is not None:
            self.run_log_file.write(f"{topic_str} {timestamp} {message_str}")
            self.run_log_file.flush()

        assert timestamp is not None
        timestamp = int(1e9 * timestamp)
        msg = ArgList.from_string(message_str)
        log.debug(msg)
        contents = msg.args
        action = cast(str, contents[0])
        if action == "Stage":
            assert isinstance(contents[1], (str, int))
            state.run.stage = contents[1]
            self.inject(
                Point("run_action")
                .tag("type", action.lower())
                .field(action.lower(), contents[1])
                .time(timestamp)
                .to_line_protocol()
            )
            self.inject(
                Point("run_status")
                .tag("type", action.lower())
                .field(action.lower(), contents[1])
                .time(timestamp)
                .to_line_protocol()
            )
        elif action == "Cycle":
            state.run.cycle = int(contents[1])
            self.inject(
                Point("run_action")
                .tag("type", action.lower())
                .field(action.lower(), contents[1])
                .time(timestamp)
                .to_line_protocol()
            )
            self.inject(
                Point("run_status")
                .tag("type", action.lower())
                .field(action.lower(), contents[1])
                .time(timestamp)
                .to_line_protocol()
            )
        elif action == "Step":
            state.run.step = int(contents[1])
            self.inject(
                Point("run_status")
                .tag("type", action.lower())
                .field(action.lower(), contents[1])
                .time(timestamp)
                .to_line_protocol()
            )
            self.inject(
                Point("run_action")
                .tag("type", action.lower())
                .field(action.lower(), contents[1])
                .time(timestamp)
                .to_line_protocol()
            )
        elif action == "Holding":
            self.inject(
                f"run_action,type=Holding holdtime={msg.opts['time']} {timestamp}"  # noqa: E501
            )
        elif action == "Ramping":
            # TODO: check zones
            state.machine.zone_targets = [
                float(x)
                for x in cast(list[float], msg.opts["targets"][0])  # type: ignore
            ]
            self.inject(
                f'run_action,type={action} run_name="{state.run.name}" {timestamp}'  # noqa: E501
            )
        elif action == "Acquiring":
            self.inject(
                f'run_action,type={action} run_name="{state.run.name}" {timestamp}'  # noqa: E501
            )
        elif action in ["Error", "Ended", "Aborted", "Stopped", "Starting"]:
            self.inject(
                f'run_action,type={action} run_name="{state.run.name}" {timestamp}'  # noqa: E501
            )
            asyncio.tasks.create_task(
                self.matrix_announce(
                    f"{self.config.machine.name} status: {action} {' '.join(str(x) for x in contents[1:])}"  # noqa: E501
                )
            )
            if action == "Ended":
                self.run_log_file = None

                if self.config.machine.compile:
                    assert state.run.name
                    compdir = self.config.sync.completed_directory

                    if compdir != "":
                        # This will need to compile and sync
                        loop = asyncio.get_event_loop()
                        loop.run_in_executor(None, self.sync_completed, c, state.run.name)
                    else:
                        # No sync; just compile
                        loop = asyncio.get_event_loop()
                        loop.run_in_executor(None, self.compile_eds, c, state.run.name)
            elif action == "Starting":
                if self.ipdir:
                    newname: str = cast(str, contents[1])
                    newname = newname.strip('"')

                    loop = asyncio.get_event_loop()
                    loop.run_in_executor(
                        None,
                        functools.partial(
                            self.setup_new_rundir,
                            c,
                            newname,
                            firstmsg=f"\n{topic_str} {timestamp} {message_str}",
                        ),
                    )

        elif action == "Collected":
            self.inject(
                f'run_action,type={action} run_name="{state.run.name}" {timestamp}'  # noqa: E501
            )
            asyncio.tasks.create_task(self.docollect(msg.opts, state, c))
        else:
            self.inject(
                Point("run_action")
                .tag("type", "Other")
                .tag("run_name", state.run.name)
                .field("message", " ".join(str(x) for x in contents))
                .time(timestamp)
            )

        state.run.refresh(c)
        state.machine.refresh(c)

        log.info(message_str)
        self.inject(state.run.statemsg(str(timestamp)))

        if state.run.plate_setup:
            self.inject(state.run.plate_setup.to_lineprotocol(timestamp, state.run.name))

        if self.idbw:
            self.idbw.flush()

    def handle_led(self, topic: bytes, message: bytes, timestamp: float | None) -> None:
        # Are we logging?
        if self.run_log_file is not None:
            self.run_log_file.write(f"{topic.decode()} {timestamp} {message.decode()}")
            self.run_log_file.flush()
        assert timestamp is not None
        ls = LEDSTATUS.match(message)
        assert ls
        p = (
            Point("lamp")
            .field("temperature", float(ls[1].decode()))
            .field("current", float(ls[2].decode()))
            .field("voltage", float(ls[3].decode()))
            .field("junctemp", float(ls[4].decode()))
            .time(int(1e9 * timestamp))
        )
        self.inject(p, flush=True)

    def handle_msg(
        self,
        state: State,
        c: Machine,
        topic: bytes,
        message: bytes,
        timestamp: float | None,
    ) -> None:
        # Are we logging?
        if self.run_log_file is not None:
            self.run_log_file.write(f"{topic.decode()} {timestamp} {message.decode()}")
            self.run_log_file.flush()
        assert timestamp is not None
        args = ArgList.from_string(message.decode()).opts
        log.debug(f"Handling message {topic.decode()} {message.decode()}")
        if topic == b"Temperature":
            recs = []
            for i, (s, b, t) in enumerate(
                zip(
                    # FIXME: parsing weirdness: these are single-element tuples
                    [float(x) for x in cast(list[float], args["sample"][0])],  # type: ignore
                    [float(x) for x in cast(list[float], args["block"][0])],  # type: ignore
                    state.machine.zone_targets,
                )
            ):
                recs.append(
                    f"temperature,loc=zones,zone={i} sample={s},block={b},target={t} {int(1e9 * timestamp)}"  # noqa: E501
                )
            recs.append(Point("temperature").tag("loc", "cover").field("cover", args["cover"]))
            recs.append(Point("temperature").tag("loc", "heatsink").field("heatsink", args["heatsink"]))
            self.inject(recs)
        elif topic == b"Time":
            p = Point("run_time")
            for key in ["elapsed", "remaining", "active"]:
                if key in args.keys():
                    p = p.field(key, args[key])
                p.time(int(1e9 * timestamp))
            self.inject(p)
        if self.idbw:
            self.idbw.flush()

    async def monitor(self, connected_fut: asyncio.Future[bool] | None = None) -> None:
        if self.config.matrix is not None:
            await self.matrix_client.login(self.config.matrix.password)
            joinedroomresp = await self.matrix_client.joined_rooms()
            if isinstance(joinedroomresp, JoinedRoomsError):
                log.error(joinedroomresp)
                joinedrooms = []
            else:
                joinedrooms = joinedroomresp.rooms
            if self.config.matrix.room not in joinedrooms:
                await self.matrix_client.join(self.config.matrix.room)

        with Machine(
            host=self.config.machine.host,
            port=(int(self.config.machine.port) if self.config.machine.port is not None else None),
            ssl=self.config.machine.ssl,
            password=self.config.machine.password,
        ) as c:
            log.info("monitor connected")
            # Are we currently *in* a run? If so, we'll need to get info.
            state = get_runinfo(c)
            log.info(f"status info: {state}")

            self.inject(state.run.statemsg(str(time.time_ns())))

            if state.run.plate_setup:
                self.inject(state.run.plate_setup.to_lineprotocol(time.time_ns(), state.run.name))

            if self.idbw:
                self.idbw.flush()

            # Setup directory if run already started:
            if state.run.name and self.ipdir:
                self.setup_new_rundir(c, state.run.name, overwrite=True)

            c.run_command("SUBS -timestamp Temperature Time Run LEDStatus")
            log.debug("subscriptions made")

            for t in [b"Temperature", b"Time"]:
                c._protocol.topic_handlers[t] = functools.partial(self.handle_msg, state, c)
            c._protocol.topic_handlers[b"Run"] = functools.partial(self.handle_run_msg, state, c)

            c._protocol.topic_handlers[b"LEDStatus"] = self.handle_led

            log.info(c._protocol.topic_handlers)

            if connected_fut is not None:
                connected_fut.set_result(True)

            log_conn = c.connection.subscribe_log()

            ok = True
            while ok:
                nextlog = next(log_conn)
                log.info(f"log: {nextlog}")

                # Have we lost the connection?
                if c._protocol.lostconnection.done():
                    log.error("Lost connection.")
                    ok = False

                # Are we actually fine?
                if time.time() - c._protocol.last_received <= 60.0:
                    continue

                # No, we have a sleep timeout.  Send a test command.
                try:
                    await c.run_command_bytes_with_timeout(b"ISTAT?", 30)
                except TimeoutError:
                    log.error("No data received in 5 minutes and ISTAT? test timed out.  Trying to disconnect.")
                    c.disconnect()
                    raise TimeoutError

    async def reliable_monitor(self, connected_fut: asyncio.Future[bool] | None = None) -> None:
        log.info("starting reconnectable monitoring")

        restart = True
        successive_failures = 0
        while restart:
            try:
                await self.monitor(connected_fut=connected_fut)
            except asyncio.exceptions.TimeoutError as e:
                successive_failures = 0
                log.warning(f"lost connection with timeout {e}", exc_info=True)
            except OSError as e:
                log.error(f"connection error {e}, retrying", exc_info=True)
            except Exception as e:
                if self.config.machine.retries - successive_failures > 0:
                    log.error(
                        f"Error {repr(e)}\nRetrying {self.config.machine.retries - successive_failures} times",
                        exc_info=True,
                    )
                    successive_failures += 1
                else:
                    log.critical(f"giving up, error {e}", exc_info=True)
                    if self.matrix_client and self.matrix_config:
                        try:
                            await self.matrix_client.room_send(
                                room_id=self.matrix_config.room,
                                message_type="m.room.message",
                                content={
                                    "msgtype": "m.text",
                                    "body": f"Unrecoverable error in QS monitoring (tried 3 times), giving up: {e}, {e.__traceback__}",
                                },
                            )
                        except Exception as matrix_e:
                            log.error(f"Failed to send Matrix message: {matrix_e}")
                    restart = False
            log.debug("awaiting retry")
            await asyncio.sleep(30)