Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions qkit/drivers/switch_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def __init__(self, name, url="tcp://localhost:5000"):
self.add_function('enable')
self.add_function('disable')
self.add_function('switch_to')
self.add_function('is_at_position')
self.add_function('ensure_switch_at')

def close(self):
print("closing zmq socket")
Expand Down Expand Up @@ -74,3 +76,16 @@ def reset(self, switch):
def get_position(self,switch):
self.socket.send_string("get/%i" % switch)
return self.socket.recv_string()

def is_at_position(self, switch, port):
self.socket.send_string("get/%i/%i" % (switch, port))
response = self.socket.recv_string() # This is a string of either "1" or "0", as per server code
return bool(int(response)) # convert string ("1"|"0") to in (1|0) to bool (True|False)

def ensure_switch_at(self, switch, port):
"""
Ensures that the switch is at the specified position.
Idempotent.
"""
if not self.is_at_position(switch, port):
self.switch_to(switch, port)