def send_robot_command(self, motor1, motor2, motor3, motor4, robot_name): total_size = 24 + len(robot_name) array = bytearray([0] * total_size) buf = ByteBuffer(array, 0, total_size) buf.put_SLInt32(total_size + 1) buf.put_SLInt32(motor1) buf.put_SLInt32(motor2) buf.put_SLInt32(motor3) buf.put_SLInt32(motor4) buf.put_SLInt32(len(robot_name)) temp = bytearray() temp.extend(robot_name) buf.put_bytes(temp, 0, len(temp)) to_send = bytearray([0] * (total_size + 1)) to_send[0] = 0x08 buf.set_position(0) buf.get(to_send, 1, total_size) self.socket.sendto(to_send, self.server_address)
def request_sensor_information(self, robot, filter, callback): if not callable(callback): print "Invalid callback!" return should_filter = filter != "" filter_size = 0 if should_filter: filter_size = 4 + len(filter) total_size = 9 + len(robot) + filter_size array = bytearray([0] * total_size) buf = ByteBuffer(array, 0, total_size) # 4 bytes buf.put_SLInt32(total_size + 1) # 4 + 4 = 8 buf.put_SLInt32(len(robot)) # 4 + 4 + 1 = 9 tmp = bytearray([0]) if should_filter: tmp[0] = 1 buf.put_bytes(tmp, 0, 1) temp2 = bytearray() temp2.extend(robot) buf.put_bytes(temp2, 0, len(temp2)) if should_filter: buf.put_SLInt32(len(filter)) temp3 = bytearray() temp3.extend(filter) buf.put_bytes(temp3, 0, len(temp3)) to_send = bytearray([0] * (total_size + 1)) to_send[0] = 0x07 buf.set_position(0) buf.get(to_send, 1, total_size) self.socket.sendto(to_send, self.server_address) if robot not in self.sensor_callbacks: self.sensor_callbacks[robot] = list() self.sensor_callbacks[robot].append(callback)
def trigger_event(self, event_id, event_data, filter): event = json.dumps(event_data) should_filter = filter != "" filter_size = 0 if should_filter: filter_size = 4 + len(filter) total_size = 10 + len(event) + filter_size array = bytearray([0] * total_size) buf = ByteBuffer(array, 0, total_size) # 4 bytes buf.put_SLInt32(total_size + 1) # 4 + 2 = 6 temp = bytearray([0] * 2) temp[0] = event_id if should_filter: temp[1] = 1 else: temp[1] = 0 buf.put_bytes(temp, 0, 2) # 4 + 2 + 4 = 10 buf.put_SLInt32(len(event)) temp2 = bytearray() temp2.extend(event) buf.put_bytes(temp2, 0, len(temp2)) if should_filter: buf.put_SLInt32(len(filter)) temp3 = bytearray() temp3.extend(filter) buf.put_bytes(temp3, 0, len(temp3)) to_send = bytearray([0] * (total_size + 1)) to_send[0] = 0x06 buf.set_position(0) buf.get(to_send, 1, total_size) self.socket.sendto(to_send, self.server_address)