From f58ed3c71738c543fdda32807e210a6e2d819444 Mon Sep 17 00:00:00 2001 From: Sinus Date: Thu, 20 Apr 2023 10:57:00 +0200 Subject: [PATCH 1/2] Adds idempotent setter for microwave switch --- qkit/drivers/switch_client.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/qkit/drivers/switch_client.py b/qkit/drivers/switch_client.py index 96df9826..4ba9804c 100644 --- a/qkit/drivers/switch_client.py +++ b/qkit/drivers/switch_client.py @@ -74,3 +74,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) From 37a18f8973635233c6f924cf80bf32e6fe077a0f Mon Sep 17 00:00:00 2001 From: Sinus Date: Thu, 20 Apr 2023 10:58:39 +0200 Subject: [PATCH 2/2] Register functions. --- qkit/drivers/switch_client.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/qkit/drivers/switch_client.py b/qkit/drivers/switch_client.py index 4ba9804c..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")