def read(stream): user_id, username_size = \ Serialization.read(stream, UserInformation.SERIALIZATION_HEADER) CURRENT_USER_PAYLOAD_FORMAT = UserInformation.SERIALIZATION_PAYLOAD.format( username_size) username, birth_date, gender = \ Serialization.read(stream, CURRENT_USER_PAYLOAD_FORMAT) return UserInfo(user_id, username, datetime.fromtimestamp(birth_date), gender)
def read_stream(stream): user_id, username_size = \ Serialization.read(stream, HelloMessageNative.SERIALIZATION_HEADER) CURRENT_USER_PAYLOAD_FORMAT = HelloMessageNative.SERIALIZATION_PAYLOAD.format( username_size) username, birth_date, gender = \ Serialization.read(stream, CURRENT_USER_PAYLOAD_FORMAT) user_info = UserInfo(user_id, username, birth_date, gender) return HelloMessage(user_info)
def read_stream(stream): fields_number = \ Serialization.read(stream, ConfigMessageNative.SERIALIZATION_HEADER)[0] fields_config = [] for field_index in range(fields_number): field_size = \ Serialization.read(stream, ConfigMessageNative.SERIALIZATION_FIELD_HEADER)[0] FIELD_PAYLOAD_FORMAT = ConfigMessageNative.SERIALIZATION_FIELD_PAYLOAD.format( field_size) field = \ Serialization.read(stream, FIELD_PAYLOAD_FORMAT)[0] fields_config.append(field.decode(ConfigMessageNative.ENCODING)) return ConfigMessage(fields_config)
def read_stream(stream): timestamp, t_x, t_y, t_z, r_x, r_y, r_z, r_w = \ Serialization.read(stream, SnapshotMessageNative.SERIALIZATION_HEADER, expect_eof=True) translation, rotation = \ (t_x, t_y, t_z), (r_x, r_y, r_z, r_w) color_image = ColorImage.read(stream) depth_image = DepthImage.read(stream) (hunger, thirst, exhaustion, happiness) = \ Serialization.read(stream, SnapshotMessageNative.SERIALIZATION_TRAILER) user_feeling = \ (hunger, thirst, exhaustion, happiness) return Snapshot(timestamp, translation, rotation, color_image, depth_image, user_feeling)
def get_current_serialization_format(self): if not hasattr(self, '_current_serialization_format'): raw_color_image_format = Serialization.remove_endianity( self.color_image.get_current_serialization_format()) raw_depth_image_format = Serialization.remove_endianity( self.depth_image.get_current_serialization_format()) # datetime : uint64 # translation : uint32 * 3 # rotation : uint32 * 4 # color image : uint32 * 2 + [array] # depth image : uint32 * 2 + [array] # feeling : uint32 * 4 self._current_serialization_format = SnapshotMessageNative.SERIALIZATION_FORMAT.format( raw_color_image_format, raw_depth_image_format) return self._current_serialization_format
def read(stream, pixel_serialization_format, pixel_elements_count): width, height = \ Serialization.read(stream, SnapshotImage.SERIALIZATION_HEADER) image_size = width * height pixel_array_size = image_size * pixel_elements_count data = [] if 0 != pixel_array_size: SERIALIZATION_PAYLOAD_FORMAT = ( '{0}' + pixel_serialization_format).format(pixel_array_size) data = \ Serialization.read(stream, SERIALIZATION_PAYLOAD_FORMAT) return SnapshotImage(width, height, data, pixel_serialization_format, pixel_elements_count)
def serialize(self): config_message = protocol_proto.ConfigMessage() for field in self.fields: proto_field = config_message.fields_config.fields.add() proto_field.name = field return Serialization.serialize_tunnled_message( config_message.SerializeToString())
def read_stream(stream): config_message_bytes = Serialization.read_tunnled_message(stream) config_message_protobuf = protocol_proto.ConfigMessage() config_message_protobuf.ParseFromString(config_message_bytes) fields_config = [ f.name for f in config_message_protobuf.fields_config.fields ] config_message = ConfigMessage(fields_config) return config_message
def serialize(self): hello_message = protocol_proto.HelloMessage() hello_message.user_data.user_id = self.user_info.user_id hello_message.user_data.username = self.user_info.username hello_message.user_data.birthday = self.user_info.birth_date hello_message.user_data.gender = HelloMessageProto.GENDER_TABLE[ self.user_info.gender].number return Serialization.serialize_tunnled_message( hello_message.SerializeToString())
def write_user_information(self, user_info): proto_user = mind_proto.User() proto_user.user_id = user_info.user_id proto_user.username = user_info.username proto_user.birthday = user_info.birth_date proto_user.gender = ProtobufMindWriter.GENDER_TABLE[user_info.gender] proto_user_bytes = Serialization.serialize_tunnled_message( proto_user.SerializeToString()) self.stream.write(proto_user_bytes) return len(proto_user_bytes)
def read_stream(stream): hello_message_bytes = Serialization.read_tunnled_message(stream) hello_message_protobuf = protocol_proto.HelloMessage() hello_message_protobuf.ParseFromString(hello_message_bytes) user_info = \ UserInfo( \ hello_message_protobuf.user_data.user_id, \ hello_message_protobuf.user_data.username, \ hello_message_protobuf.user_data.birthday, \ protocol_proto._USER_GENDER.values_by_number[hello_message_protobuf.user_data.gender].name.lower()[0] \ ) return HelloMessage(user_info)
def read_user_information(self): user_information_bytes = Serialization.read_tunnled_message(self.stream) user_information_protobuf = mind_proto.User() user_information_protobuf.ParseFromString(user_information_bytes) user_information = \ UserInfo( \ user_information_protobuf.user_id, \ user_information_protobuf.username, user_information_protobuf.birthday, mind_proto._USER_GENDER.values_by_number[user_information_protobuf.gender].name.lower()[0]) return user_information
def write_user_information(self, user_info): username_size = len(user_info.username) birth_date_as_number = user_info.birth_date user_info_bytes_untunneled = \ pack(self.get_user_info_serialization_format(user_info), \ user_info.user_id, \ username_size, \ user_info.username.encode(BinaryMindWriter.ENCODING), \ birth_date_as_number, \ self.gender.encode(BinaryMindWriter.ENCODING)) user_info_bytes = Serialization.serialize_tunnled_message( user_info_bytes_untunneled) self.stream.write(user_info_bytes) return len(user_info_bytes)
def write_snapshot(self, snapshot): header = \ pack(BinaryMindWriter.SERIALIZATION_ENDIANITY + BinaryMindWriter.SERIALIZATION_HEADER_SNAPSHOT, \ snapshot.timestamp, \ *snapshot.pose.translation.get(), \ *snapshot.pose.rotation.get()) body = \ snapshot.color_image.serialize() + snapshot.depth_image.serialize() trailer = \ pack(BinaryMindWriter.SERIALIZATION_ENDIANITY + BinaryMindWriter.SERIALIZATION_TRAILER_SNAPSHOT, \ *snapshot.user_feeling.get()) snapshot_bytes_untunneled = header + body + trailer snapshot_bytes = Serialization.serialize_tunnled_message( snapshot_bytes_untunneled) self.stream.write(snapshot_bytes) return len(snapshot_bytes)
def read_snapshot(self): snapshot_bytes = Serialization.read_tunnled_message(self.stream, expect_eof=True) snapshot_protobuf = mind_proto.Snapshot() snapshot_protobuf.ParseFromString(snapshot_bytes) translation = \ (snapshot_protobuf.pose.translation.x, \ snapshot_protobuf.pose.translation.y, \ snapshot_protobuf.pose.translation.z) rotation = \ (snapshot_protobuf.pose.rotation.x, \ snapshot_protobuf.pose.rotation.y, \ snapshot_protobuf.pose.rotation.z, \ snapshot_protobuf.pose.rotation.w) color_image = \ ColorImage( \ snapshot_protobuf.color_image.width, \ snapshot_protobuf.color_image.height, \ snapshot_protobuf.color_image.data) depth_image = \ DepthImage( \ snapshot_protobuf.depth_image.width, \ snapshot_protobuf.depth_image.height, \ snapshot_protobuf.depth_image.data) depth_image._fix_hardware_size() user_feeling = \ (snapshot_protobuf.feelings.hunger, \ snapshot_protobuf.feelings.thirst, \ snapshot_protobuf.feelings.exhaustion, \ snapshot_protobuf.feelings.happiness) snapshot = \ Snapshot( \ snapshot_protobuf.datetime, \ translation, \ rotation, \ color_image, \ depth_image, \ user_feeling) return snapshot
def write_snapshot(self, snapshot): proto_snapshot = mind_proto.Snapshot() proto_snapshot.datetime = snapshot.timestamp translation = mind_proto.Pose.Translation() translation.x, translation.y, translation.z = snapshot.pose.translation.get( ) rotation = mind_proto.Pose.Rotation() rotation.x, rotation.y, rotation.z, rotation.w = snapshot.pose.rotation.get( ) pose = mind_proto.Pose() pose.rotation.CopyFrom(rotation) pose.translation.CopyFrom(translation) proto_snapshot.pose.CopyFrom(pose) color_image = mind_proto.ColorImage() color_image.width, color_image.height, color_image.data = \ snapshot.color_image.width, snapshot.color_image.height, snapshot.color_image.data proto_snapshot.color_image.CopyFrom(color_image) depth_image = mind_proto.DepthImage() depth_image.width, depth_image.height = \ snapshot.depth_image.width, snapshot.depth_image.height depth_image.data.extend(snapshot.depth_image.data) proto_snapshot.depth_image.CopyFrom(depth_image) feelings = mind_proto.Feelings() feelings.hunger, feelings.thirst, feelings.exhaustion, feelings.happiness = \ snapshot.user_feelings.get() proto_snapshot.feelings.CopyFrom(feelings) proto_snapshot_bytes = Serialization.serialize_tunnled_message( proto_snapshot.SerializeToString()) self.stream.write(proto_snapshot_bytes) return len(proto_snapshot_bytes)