diff --git a/qkit/drivers/switch_client.py b/qkit/drivers/switch_client.py index 96df9826..21666000 100644 --- a/qkit/drivers/switch_client.py +++ b/qkit/drivers/switch_client.py @@ -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") @@ -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)