You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 lines
2.8 KiB
95 lines
2.8 KiB
import sartoriusb |
|
|
|
from sartorius_logger.datalogger import DataLogger |
|
|
|
|
|
class DummyLogger(DataLogger): |
|
""" A DataLogger implementation for testing """ |
|
|
|
def __init__(self, *args, **kargs): |
|
super().__init__("log/file.txt", end="") |
|
self.handle = StubFile() |
|
|
|
def open(self): |
|
""" simulates opening the log file for writing """ |
|
pass |
|
|
|
def close(self): |
|
""" simulates closing the log file for writing """ |
|
pass |
|
|
|
|
|
class StubFile: |
|
""" a stub file handle that captures everything written to it """ |
|
|
|
def __init__(self): |
|
self.captured = [] |
|
|
|
def write(self, data): |
|
""" simulates a filehandle write """ |
|
self.captured.append(data) |
|
|
|
def __eq__(self, other): |
|
""" quick way of comparing the captured output to something other """ |
|
return self.captured == other |
|
|
|
|
|
class DummyScale: |
|
""" simulates a sartoriusb.SartoriusUSB class """ |
|
|
|
_responses = { |
|
sartoriusb.CMD_INFO_TYPE: "Model: Quintix-1\r\n", |
|
sartoriusb.CMD_INFO_SNR: "SerialNumber: 0815 \r", |
|
sartoriusb.CMD_INFO_VERSION_SCALE: "ScaleVersion beta1\n", |
|
sartoriusb.CMD_INFO_VERSION_CONTROL_UNIT: "Version.command 1.2.3", |
|
} |
|
|
|
_measurements = [ |
|
"G + 850.1 \r\n", |
|
"G + 851.0 \r\n", |
|
"G + 852.2 \r\n", |
|
"G + 853.0 mg \r\n", |
|
"G + 854.4 mg \r\n", |
|
"G + 857.0 mg \r\n", |
|
"G + 857.0 mg \r\n", |
|
"G + 858.7 mg \r\n", |
|
"G + 859.0 mg \r\n", |
|
"G - 1000.0 mg \r\n", |
|
" Low \r\n", |
|
"G 0.0 \r\n", |
|
"G 0.0 mg \r\n", |
|
] |
|
|
|
def __init__(self, *args, measurements=None, **kargs): |
|
""" intializes the dummy scale |
|
|
|
:params measurements: overides for the build in measurements |
|
:params responses: overides for the build in responses |
|
""" |
|
if measurements is None: |
|
self.measurements = self._measurements.copy() |
|
else: |
|
self.measurements = measurements |
|
self.responses = self._responses.copy() |
|
self.iterator = iter(self.measurements) |
|
|
|
def get(self, command): |
|
""" simulates the get() method """ |
|
response = self.responses.get(command, False) |
|
return [response] if response else [] |
|
|
|
def measure(self): |
|
""" simulates the measure() method """ |
|
try: |
|
value = next(self.iterator) |
|
return sartoriusb.parse_measurement(value) |
|
except StopIteration: |
|
return sartoriusb.Measurement( |
|
None, None, None, None, "Connection Timeout" |
|
) |
|
|
|
def __enter__(self): |
|
return self |
|
|
|
def __exit__(self, *args, **kargs): |
|
pass
|
|
|