use native_db::{native_db, ToKey};
use native_model::{native_model, Model};
use serde::{Deserialize, Serialize};
pub use super::v6::Tag;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[native_model(id = 17, version = 4)]
#[native_db]
pub struct TagLocal {
pub id: Option<i64>,
#[primary_key]
pub id_local: Option<String>,
pub inserted_at: Option<String>,
pub x: f64,
pub y: f64,
pub width: f64,
pub height: f64,
pub conf: f64,
pub observation_type: super::v1::TagObservationType,
#[serde(rename = "class")]
pub class: String,
#[secondary_key]
pub event_id: Option<i64>,
#[secondary_key]
pub ancestor_id_local: Option<String>,
pub origin_location: Option<String>,
pub origin_pitch: Option<f64>,
pub origin_heading: Option<f64>,
pub origin_roll: Option<f64>,
pub sensor_pitch: Option<f64>,
pub sensor_yaw: Option<f64>,
pub sensor_roll: Option<f64>,
pub origin_height: Option<f64>,
pub subject_location: Option<String>,
pub subject_height: Option<f64>,
pub artifact_id: Option<i64>,
#[serde(default)]
pub detector: String,
pub timestamp_observation: Option<String>,
pub frame_index: Option<i64>,
#[serde(default)]
pub subject_id: Option<i64>,
#[serde(default)]
pub score: Option<f64>,
#[serde(default)]
pub score_source: Option<String>,
}
impl Default for TagLocal {
fn default() -> Self {
Self {
id: None,
id_local: None,
inserted_at: None,
x: 0.0,
y: 0.0,
width: 0.0,
height: 0.0,
conf: 0.0,
observation_type: super::v1::TagObservationType::Auto,
class: String::new(),
event_id: None,
ancestor_id_local: None,
origin_location: None,
origin_pitch: None,
origin_heading: None,
origin_roll: None,
sensor_pitch: None,
sensor_yaw: None,
sensor_roll: None,
origin_height: None,
subject_location: None,
subject_height: None,
artifact_id: None,
detector: String::new(),
timestamp_observation: None,
frame_index: None,
subject_id: None,
score: None,
score_source: None,
}
}
}
impl super::v1::Syncable for TagLocal {
fn id(&self) -> Option<i64> {
self.id
}
fn set_id(&mut self, id: i64) {
self.id = Some(id);
}
fn id_local(&self) -> Option<String> {
self.id_local.clone()
}
fn set_id_local(&mut self, id_local: String) {
self.id_local = Some(id_local);
}
}
impl super::v1::AncestorLocal for TagLocal {
fn ancestor_id_local(&self) -> Option<String> {
self.ancestor_id_local.clone()
}
fn set_ancestor_id_local(&mut self, ancestor_id_local: String) {
self.ancestor_id_local = Some(ancestor_id_local);
}
}
impl From<TagLocal> for super::v6::Tag {
fn from(local: TagLocal) -> Self {
Self {
id: local.id,
inserted_at: local.inserted_at,
x: local.x,
y: local.y,
width: local.width,
height: local.height,
conf: local.conf,
observation_type: local.observation_type,
class: local.class,
event_id: local.event_id,
origin_location: local.origin_location,
origin_pitch: local.origin_pitch,
origin_heading: local.origin_heading,
origin_roll: local.origin_roll,
sensor_pitch: local.sensor_pitch,
sensor_yaw: local.sensor_yaw,
sensor_roll: local.sensor_roll,
origin_height: local.origin_height,
subject_location: local.subject_location,
subject_height: local.subject_height,
artifact_id: local.artifact_id,
detector: local.detector,
timestamp_observation: local.timestamp_observation,
frame_index: local.frame_index,
subject_id: local.subject_id,
score: local.score,
score_source: local.score_source,
}
}
}
impl From<super::v6::Tag> for TagLocal {
fn from(tag: super::v6::Tag) -> Self {
Self {
id: tag.id,
id_local: None,
inserted_at: tag.inserted_at,
x: tag.x,
y: tag.y,
width: tag.width,
height: tag.height,
conf: tag.conf,
observation_type: tag.observation_type,
class: tag.class,
event_id: tag.event_id,
ancestor_id_local: None,
origin_location: tag.origin_location,
origin_pitch: tag.origin_pitch,
origin_heading: tag.origin_heading,
origin_roll: tag.origin_roll,
sensor_pitch: tag.sensor_pitch,
sensor_yaw: tag.sensor_yaw,
sensor_roll: tag.sensor_roll,
origin_height: tag.origin_height,
subject_location: tag.subject_location,
subject_height: tag.subject_height,
artifact_id: tag.artifact_id,
detector: tag.detector,
timestamp_observation: tag.timestamp_observation,
frame_index: tag.frame_index,
subject_id: tag.subject_id,
score: tag.score,
score_source: tag.score_source,
}
}
}
impl From<super::v6::TagLocal> for TagLocal {
fn from(v3: super::v6::TagLocal) -> Self {
Self {
id: v3.id,
id_local: v3.id_local,
inserted_at: v3.inserted_at,
x: v3.x,
y: v3.y,
width: v3.width,
height: v3.height,
conf: v3.conf,
observation_type: v3.observation_type,
class: v3.class,
event_id: v3.event_id,
ancestor_id_local: v3.ancestor_id_local,
origin_location: v3.origin_location,
origin_pitch: v3.origin_pitch,
origin_heading: v3.origin_heading,
origin_roll: v3.origin_roll,
sensor_pitch: v3.sensor_pitch,
sensor_yaw: v3.sensor_yaw,
sensor_roll: v3.sensor_roll,
origin_height: v3.origin_height,
subject_location: v3.subject_location,
subject_height: v3.subject_height,
artifact_id: v3.artifact_id,
detector: v3.detector,
timestamp_observation: v3.timestamp_observation,
frame_index: v3.frame_index,
subject_id: v3.subject_id,
score: None,
score_source: None,
}
}
}
impl From<super::v5::TagLocal> for TagLocal {
fn from(v5: super::v5::TagLocal) -> Self {
super::v6::TagLocal::from(v5).into()
}
}
impl From<super::v1::TagLocal> for TagLocal {
fn from(v1: super::v1::TagLocal) -> Self {
super::v6::TagLocal::from(v1).into()
}
}
impl TagLocal {
pub fn new(
_class_id: i64,
x: f64,
y: f64,
width: f64,
height: f64,
conf: f64,
observation_type: super::v1::TagObservationType,
class: String,
) -> Self {
Self {
x,
y,
width,
height,
conf,
observation_type,
class,
..Default::default()
}
}
pub fn new_with_location(
_class_id: i64,
x: f64,
y: f64,
width: f64,
height: f64,
conf: f64,
observation_type: super::v1::TagObservationType,
class: String,
latitude: f64,
longitude: f64,
) -> Self {
let mut tag = Self::new(
_class_id,
x,
y,
width,
height,
conf,
observation_type,
class,
);
tag.set_location(latitude, longitude);
tag
}
pub fn update_event_id(&mut self, event_id: i64) {
self.event_id = Some(event_id);
}
pub fn update_ancestor_id_local(&mut self, ancestor_id_local: String) {
self.ancestor_id_local = Some(ancestor_id_local);
}
pub fn set_location(&mut self, latitude: f64, longitude: f64) {
self.origin_location = Some(super::v6::Tag::format_location(latitude, longitude));
}
pub fn clear_location(&mut self) {
self.origin_location = None;
}
pub fn format_location(latitude: f64, longitude: f64) -> String {
super::v6::Tag::format_location(latitude, longitude)
}
pub fn parse_location(location: &str) -> Option<(f64, f64)> {
super::v6::Tag::parse_location(location)
}
pub fn get_coordinates(&self) -> Option<(f64, f64)> {
self.origin_location
.as_ref()
.and_then(|loc| super::v6::Tag::parse_location(loc))
}
}