|
| 1 | +import os |
| 2 | +import time |
| 3 | +import uuid |
| 4 | + |
| 5 | +import attr |
| 6 | + |
| 7 | +from ..factory import target_factory |
| 8 | +from ..protocol import EnergyAnalyzerProtocol, PowerProtocol |
| 9 | +from ..resource.joulescope import JoulescopeDevice |
| 10 | +from ..resource.remote import NetworkJoulescopeDevice |
| 11 | +from ..step import step |
| 12 | +from ..util.agentwrapper import AgentWrapper |
| 13 | +from ..util.ssh import sshmanager |
| 14 | +from .common import Driver |
| 15 | + |
| 16 | + |
| 17 | +@target_factory.reg_driver |
| 18 | +@attr.s(eq=False) |
| 19 | +class JoulescopeDriver(Driver, EnergyAnalyzerProtocol, PowerProtocol): |
| 20 | + """The JoulescopeDriver controls a Joulescope energy analyzer. |
| 21 | +
|
| 22 | + It wraps ``pyjoulescope_driver`` to stream measurement statistics |
| 23 | + (current, voltage, power and accumulated charge/energy), to capture |
| 24 | + high-rate samples to a JLS file, and to connect/disconnect the device |
| 25 | + current path (downstream power) as a :class:`PowerProtocol` power switch. |
| 26 | +
|
| 27 | + ``pyjoulescope_driver`` runs on the host the Joulescope is attached to |
| 28 | + through labgrid's agent mechanism, so the same driver works for a locally |
| 29 | + attached device and for one shared over the distributed infrastructure via |
| 30 | + a :class:`~labgrid.resource.remote.NetworkJoulescopeDevice`. Only the host |
| 31 | + with the device attached needs the ``joulescope`` extra installed. |
| 32 | +
|
| 33 | + Power switching (``on``/``off``/``cycle``) controls downstream power to the |
| 34 | + device under test: the JS110 uses the current range ``select`` and the JS220 |
| 35 | + and JS320 use the current range ``mode``. |
| 36 | +
|
| 37 | + Args: |
| 38 | + frequency (float): statistics update frequency in Hz |
| 39 | + delay (float): delay between off and on during a power cycle |
| 40 | + """ |
| 41 | + |
| 42 | + bindings = {"device": {JoulescopeDevice, NetworkJoulescopeDevice}} |
| 43 | + frequency = attr.ib(default=2.0, validator=attr.validators.instance_of(float)) |
| 44 | + delay = attr.ib(default=2.0, validator=attr.validators.instance_of(float)) |
| 45 | + |
| 46 | + def __attrs_post_init__(self): |
| 47 | + super().__attrs_post_init__() |
| 48 | + self.wrapper = None |
| 49 | + self.proxy = None |
| 50 | + |
| 51 | + # -- life cycle --------------------------------------------------------- |
| 52 | + |
| 53 | + def on_activate(self): |
| 54 | + host = self.device.host if isinstance(self.device, NetworkJoulescopeDevice) else None |
| 55 | + self.wrapper = AgentWrapper(host) |
| 56 | + try: |
| 57 | + self.proxy = self.wrapper.load("joulescope") |
| 58 | + self.proxy.open(self.device.serial, self.device.model, self.frequency) |
| 59 | + except Exception: |
| 60 | + # on_deactivate() only runs once the driver is active, so clean up |
| 61 | + # the (possibly remote) agent subprocess if opening the device fails. |
| 62 | + self.wrapper.close() |
| 63 | + self.wrapper = None |
| 64 | + self.proxy = None |
| 65 | + raise |
| 66 | + |
| 67 | + def on_deactivate(self): |
| 68 | + try: |
| 69 | + self.proxy.close(self.device.serial, self.device.model) |
| 70 | + finally: |
| 71 | + self.wrapper.close() |
| 72 | + self.wrapper = None |
| 73 | + self.proxy = None |
| 74 | + |
| 75 | + # -- statistics --------------------------------------------------------- |
| 76 | + |
| 77 | + @Driver.check_active |
| 78 | + @step(result=True) |
| 79 | + def get_statistics(self): |
| 80 | + return self.proxy.get_statistics(self.device.serial, self.device.model) |
| 81 | + |
| 82 | + @Driver.check_active |
| 83 | + @step() |
| 84 | + def start(self): |
| 85 | + self.proxy.start(self.device.serial, self.device.model) |
| 86 | + |
| 87 | + @Driver.check_active |
| 88 | + @step(result=True) |
| 89 | + def stop(self): |
| 90 | + return self.proxy.stop(self.device.serial, self.device.model) |
| 91 | + |
| 92 | + # -- sample capture ----------------------------------------------------- |
| 93 | + |
| 94 | + @Driver.check_active |
| 95 | + @step(args=["filename", "duration"]) |
| 96 | + def capture(self, filename, signals=None, duration=None, frequency=None): |
| 97 | + if duration is None: |
| 98 | + raise ValueError("capture() requires a duration in seconds") |
| 99 | + if isinstance(self.device, NetworkJoulescopeDevice): |
| 100 | + # Record on the host the device is attached to, then copy the JLS |
| 101 | + # file back to the client and remove the remote copy. |
| 102 | + remote = f"/tmp/labgrid-joulescope-{uuid.uuid4()}.jls" |
| 103 | + self.proxy.capture(self.device.serial, self.device.model, remote, signals, duration, frequency) |
| 104 | + try: |
| 105 | + sshmanager.get_file(self.device.host, remote, filename) |
| 106 | + finally: |
| 107 | + self.proxy.remove(remote) |
| 108 | + else: |
| 109 | + self.proxy.capture( |
| 110 | + self.device.serial, self.device.model, os.fspath(filename), signals, duration, frequency |
| 111 | + ) |
| 112 | + |
| 113 | + # -- power switch (PowerProtocol) -------------------------------------- |
| 114 | + |
| 115 | + @Driver.check_active |
| 116 | + @step() |
| 117 | + def on(self): |
| 118 | + self.proxy.set_power(self.device.serial, self.device.model, True) |
| 119 | + |
| 120 | + @Driver.check_active |
| 121 | + @step() |
| 122 | + def off(self): |
| 123 | + self.proxy.set_power(self.device.serial, self.device.model, False) |
| 124 | + |
| 125 | + @Driver.check_active |
| 126 | + @step() |
| 127 | + def cycle(self): |
| 128 | + self.off() |
| 129 | + time.sleep(self.delay) |
| 130 | + self.on() |
0 commit comments