import socket
import json

HOST, PORT = "localhost", 42424

def move_grid(x, y, orientation, dimx, dimy):
    print("move grid")
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((HOST, PORT))
    payload = json.dumps({"x": x, "y": y, "dimx": dimx, "dimy": dimy, "orientation": orientation})
    sock.sendall(f"move_grid_blocking;{payload}\n".encode())
    target_reached = False
    try:
        while not target_reached:
            reply = sock.recv(1024)
            if reply == b'':
                raise RuntimeError
            #print("got reply: ", reply)
            target_reached = reply.decode() == 'ack\n'
        print("target_reached = ", target_reached)
    except RuntimeError:
        # try again
        print("controlling failed -> trying again")
        move_grid(x, y, orientation, dimx, dimy)

if __name__ == "__main__":
    move_grid(3,2,'v', 7, 4)