-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
kmtronicrelay: add resource and driver
Signed-off-by: Benjamin B. Frost <[email protected]>
- Loading branch information
1 parent
fff35ee
commit 5714554
Showing
11 changed files
with
200 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import attr | ||
|
||
from .common import Driver | ||
from ..factory import target_factory | ||
from ..step import step | ||
from ..protocol import DigitalOutputProtocol | ||
from ..util.agentwrapper import AgentWrapper | ||
from ..resource.remote import NetworkKMTronicRelay | ||
|
||
|
||
@target_factory.reg_driver | ||
@attr.s(eq=False) | ||
class KMTronicRelayDriver(Driver, DigitalOutputProtocol): | ||
bindings = { | ||
"relay": {"KMTronicRelay", "NetworkKMTronicRelay"}, | ||
} | ||
|
||
def __attrs_post_init__(self): | ||
super().__attrs_post_init__() | ||
self.wrapper = None | ||
|
||
def on_activate(self): | ||
if isinstance(self.relay, NetworkKMTronicRelay): | ||
host = self.relay.host | ||
else: | ||
host = None | ||
self.wrapper = AgentWrapper(host) | ||
self.proxy = self.wrapper.load('kmtronic_relay') | ||
|
||
def on_deactivate(self): | ||
self.wrapper.close() | ||
self.wrapper = None | ||
self.proxy = None | ||
|
||
@Driver.check_active | ||
@step(args=['status']) | ||
def set(self, status): | ||
self.proxy.set(self.relay.path, self.relay.index, status) | ||
|
||
@Driver.check_active | ||
@step(result=True) | ||
def get(self): | ||
status = self.proxy.get(self.relay.path, self.relay.index) | ||
return status |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import serial | ||
|
||
class USBKMTronicRelay: | ||
def set_output(self, path, index, status): | ||
# second and third is index and status on/off | ||
# \xFF\x01\x00 = turn relay 1 off | ||
# \xFF\x01\x01 = turn relay 1 on | ||
cmd = bytes([255, index, int(status == True)]) | ||
with serial.Serial(path, 9600) as ser: | ||
ser.write(cmd) | ||
|
||
def get_output(self, path, index): | ||
# \xFF\x01\x03 will read relay 1 status | ||
cmd = bytes([255, index, 3]) | ||
with serial.Serial(path, 9600) as ser: | ||
ser.write(cmd) | ||
data = ser.read(3) | ||
return data[2] | ||
|
||
_relays = {} | ||
|
||
def _get_relay(path): | ||
if (path) not in _relays: | ||
_relays[(path)] = USBKMTronicRelay() | ||
return _relays[(path)] | ||
|
||
def handle_set(path, index, status): | ||
relay = _get_relay(path) | ||
relay.set_output(path, index, status) | ||
|
||
def handle_get(path, index): | ||
relay = _get_relay(path) | ||
return relay.get_output(path, index) | ||
|
||
methods = { | ||
"set": handle_set, | ||
"get": handle_get, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from labgrid.resource.udev import KMTronicRelay | ||
from labgrid.driver.kmtronicrelay import KMTronicRelayDriver | ||
|
||
|
||
def test_kmtronicrelay_resource(target): | ||
r = KMTronicRelay(target, name=None, match={"ID_SERIAL_SHORT": "AB0LBF2U"}) | ||
|
||
|
||
def test_kmtronicrelay_driver(target): | ||
r = KMTronicRelay(target, name=None, match={"ID_SERIAL_SHORT": "AB0LBF2U"}) | ||
d = KMTronicRelayDriver(target, name=None) | ||
target.activate(d) | ||
|
||
|
||
def test_kmtronicrelay_control(target): | ||
r = KMTronicRelay(target, name=None, match={"ID_SERIAL_SHORT": "AB0LBF2U"}) | ||
d = KMTronicRelayDriver(target, name=None) | ||
target.activate(d) | ||
d.set(1) | ||
assert d.get() == 1 | ||
d.set(0) | ||
assert d.get() == 0 |