def setup(self): # --- Socket setup --- self.conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.conn.connect((HOST, PORT)) self.send({"src": "Canvas"}) sleep(1) threading._start_new_thread(self.update_requester, ()) # --- JSON setup --- self.decoder = json.JSONDecoder() # --- guiObjects setup --- # Randomly positioned for now... get real values later self.target = guiTarget(random.randint(0, self.wx), random.randint(0, self.wy), 0, 10) self.target.setup() self.guiObjectsList.append(self.target) for i in range(self.num_drones): self.drone = guiDrone(random.randint(0, self.wx), random.randint(0, self.wy), 10, 0, i + 1) self.drone.setup() self.guiObjectsList.append(self.drone) for j in range(self.num_drones): self.camera_view = guiCameraView(self.vx, self.vy, j + 1) self.camera_view.setup() self.guiObjectsList.append(self.camera_view)
def setup(self): # Randomly positioned for now... get real values later self.target = guiTarget(random.randint(0, self.wx), random.randint(0, self.wy), 0, 10) self.target.setup() self.guiObjectsList.append(self.target) for i in range(self.num_drones): self.drone = guiDrone(random.randint(0, self.wx), random.randint(0, self.wy), 10, 0, i + 1) self.drone.setup() self.guiObjectsList.append(self.drone)
def setup(self): # --- Socket setup --- self.conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.conn.connect((HOST, PORT)) self.send({"src": "Canvas"}) # by kdw # sleep(1) # threading._start_new_thread(self.update_requester, ()) threading._start_new_thread(self.recv_update, ()) # --- JSON setup --- self.decoder = json.JSONDecoder() # --- guiObjects setup --- # Randomly positioned for now... get real values later self.target_cnt = 1 # Allow only one target self.target_size_px = 20 # The size of the target in pixels try: data = self.conn.recv(1024) if data: while data: try: j_msg, idx = self.decoder.raw_decode(data) print(j_msg) except ValueError: print("JSON Error") if j_msg: # --- update positions with socket-received data --- update = j_msg["payload"]["data"] for key in update: if (key == "target"): if self.target_cnt > 0: self.target = guiTarget( update[key]["x"] + self.x_corrector, self.y_corrector - update[key]["y"], update[key]["z"]) self.target.setup() self.guiObjectsList.insert(0, self.target) # Make the target always the first element in the guiObjectsList self.target_cnt -= 1 if ("drone" in key): self.drone = guiDrone( update[key]["x"] + self.x_corrector, self.y_corrector - update[key]["y"], update[key]["z"], self.angle_corrector - update[key]["a"], int(key[-1]) + 1) self.drone.setup() self.guiObjectsList.append(self.drone) for key in update: if ("drone" in key): self.camera_view = guiCameraView( ci=update[key]["center"], si=int(sqrt(update[key]["size"])), view_id=int(key[-1]) + 1) self.camera_view.setup() self.guiObjectsList.append(self.camera_view) data = data[idx:].lstrip() else: pass except (KeyboardInterrupt, SystemExit): raise print(self.guiObjectsList)