6 from threading
import Thread
22 packet = self.request[0]
25 size_packet = int.from_bytes(packet[:4],
"little")
29 if size_packet == len(packet[4:]):
31 timestamp = time.time()
35 if data_type == RANGE_TYPE:
36 if self.server.enable_range:
38 self.server.range_buffer.put_nowait((timestamp, data))
40 self.server.range_buffer.get()
41 self.server.range_buffer.put_nowait((timestamp, data))
46 elif data_type == IMAGE_TYPE:
47 if self.server.enable_image:
49 timestamp_img = int.from_bytes(packet[4:8],
"little")
57 self.server.last_timestamp_img = timestamp_img
58 self.server.last_idx_img = idx_img
59 self.server.last_half_img = packet[10:]
62 if timestamp_img == self.server.last_timestamp_img:
64 half_img_bin_0 = self.server.last_half_img
66 half_img_bin_1 = packet[10:]
68 half_img_bin_0 = np.asarray(
69 bytearray(half_img_bin_0), dtype=
"uint8"
71 half_img_bin_1 = np.asarray(
72 bytearray(half_img_bin_1), dtype=
"uint8"
75 half_img_dec_0 = cv2.imdecode(
76 half_img_bin_0, cv2.IMREAD_UNCHANGED
78 half_img_dec_0 = np.dstack(
80 half_img_dec_0[:, :, 2],
81 half_img_dec_0[:, :, 1],
82 half_img_dec_0[:, :, 0],
86 half_img_dec_1 = cv2.imdecode(
87 half_img_bin_1, cv2.IMREAD_UNCHANGED
89 half_img_dec_1 = np.dstack(
91 half_img_dec_1[:, :, 2],
92 half_img_dec_1[:, :, 1],
93 half_img_dec_1[:, :, 0],
98 combined_image = np.vstack(
99 (half_img_dec_1, half_img_dec_0)
103 self.server.image_buffer.put_nowait(
104 (timestamp, combined_image)
107 self.server.image_buffer.get()
108 self.server.image_buffer.put_nowait(
109 (timestamp, combined_image)
115 elif data_type == AUDIO_TYPE:
116 if self.server.enable_audio:
118 self.server.audio_buffer.put_nowait((timestamp, data))
120 self.server.audio_buffer.get_nowait()
121 self.server.audio_buffer.put_nowait((timestamp, data))
126 elif data_type == IMU_TYPE:
127 if self.server.enable_imu:
129 self.server.imu_buffer.put_nowait((timestamp, data))
131 self.server.imu_buffer.get_nowait()
132 self.server.imu_buffer.put_nowait((timestamp, data))
138 "Warning: received packet of length {}, but expected length was {}!".format(
139 len(packet[4:]), size_packet
156 super().
__init__((server_ip, server_port), UDPHandler)
216 self.request.settimeout(5.0)
217 self.server.nicla_disconnect =
False
221 packet = self.request.recv(65000)
224 timestamp = time.time()
225 timestamp = struct.pack(
">d", timestamp)
226 packet = timestamp + packet
227 self.server.receiving_buffer.put_nowait(packet)
228 except socket.timeout:
229 print(
"Warning: Nicla disconnected! Resetting server... ")
230 self.server.receiving_buffer.queue.clear()
231 self.server.nicla_disconnect =
True
233 except Exception
as e:
234 print(f
"Exception: {e}")
235 self.server.receiving_buffer.queue.clear()
236 self.server.nicla_disconnect =
True
252 super().
__init__((server_ip, server_port), TCPHandler)
288 bkp_bytes_packets = bytes([])
296 timestamp = struct.unpack(
">d", bytes_packets[:8])[0]
297 bytes_packets = bytes_packets[8:]
300 bytes_packets = bytes([])
301 bkp_bytes_packets = bytes([])
305 bytes_packets = bkp_bytes_packets + bytes_packets
306 bkp_bytes_packets = bytes([])
308 if len(bytes_packets) < 9:
309 print(
"Got a packet from receiver less than header size!")
312 total_length = len(bytes_packets)
313 loop_termination_flag =
True
315 while loop_termination_flag:
316 size_packet = int.from_bytes(bytes_packets[:4],
"little")
318 if total_length - 4 >= size_packet:
319 packet = bytes_packets[4 : size_packet + 4]
320 bytes_packets = bytes_packets[size_packet + 4 :]
324 data_type = int.from_bytes(packet[4:5],
"little")
327 if data_type == RANGE_TYPE:
341 elif data_type == IMAGE_TYPE:
344 half_img_bin = np.asarray(
345 bytearray(data[1:]), dtype=
"uint8"
348 half_img_dec = cv2.imdecode(
349 half_img_bin, cv2.IMREAD_UNCHANGED
352 half_img_dec = np.dstack(
354 half_img_dec[:, :, 2],
355 half_img_dec[:, :, 1],
356 half_img_dec[:, :, 0],
361 half_img = half_img_dec
365 combined_image = np.vstack(
366 (half_img_dec, half_img)
370 (timestamp, combined_image)
375 (timestamp, combined_image)
382 elif data_type == AUDIO_TYPE:
397 elif data_type == IMU_TYPE:
412 bkp_bytes_packets = bytes_packets
413 loop_termination_flag =
False
415 total_length = len(bytes_packets)
416 if total_length == 0:
417 loop_termination_flag =
False