Skip to content

Instantly share code, notes, and snippets.

@devfire
Created March 29, 2023 01:06
Show Gist options
  • Save devfire/9d90f3b279e6ce1a970de3570a6b2567 to your computer and use it in GitHub Desktop.
Save devfire/9d90f3b279e6ce1a970de3570a6b2567 to your computer and use it in GitHub Desktop.
for i in 0..vec_of_ts_cameras.len() {
for j in (i + 1)..vec_of_ts_cameras.len() {
// First, let's calculate the average speed between two observations
let average_speed = calculate_average_speed(
&vec_of_ts_cameras[i],
&vec_of_ts_cameras[j],
plate_road,
)
.await
.expect("Failed to calculate average speed");
info!(
"For {:?} comparing {:?} with {:?} avg speed {}",
plate_road,
vec_of_ts_cameras[i],
vec_of_ts_cameras[j],
average_speed
);
// calculate the days for both observations
let day1 =
(vec_of_ts_cameras[i].timestamp as f32 / 86400.0).floor() as u32;
let day2 =
(vec_of_ts_cameras[j].timestamp as f32 / 86400.0).floor() as u32;
if let Some(days) = state.issued_tickets_day.get(plate_road) {
for day in days.iter() {
// skip if day 1 matches, or
// day 2 matches, or
//
if *day == day2 {
warn!(
"{:?} was previously issued tickets on days {:?}, exiting.",
plate_road, days
);
return None;
}
}
}
if average_speed > common_limit.into() {
let mut mile1: Mile = 0;
let mut mile2: Mile = 0;
info!(
"For {:?} between {:?} and {:?} average speed was {}",
plate_road,
vec_of_ts_cameras[i],
vec_of_ts_cameras[j],
average_speed
);
if let InboundMessageType::IAmCamera {
road: _,
mile,
limit: _,
} = vec_of_ts_cameras[i].camera
{
mile1 = mile;
};
if let InboundMessageType::IAmCamera {
road: _,
mile,
limit: _,
} = vec_of_ts_cameras[j].camera
{
mile2 = mile;
};
// mile1 and timestamp1 must refer to the earlier of the 2 observations (the smaller timestamp),
// and mile2 and timestamp2 must refer to the later of the 2 observations (the larger timestamp).
let timestamp1 = vec_of_ts_cameras[i].timestamp;
let timestamp2 = vec_of_ts_cameras[j].timestamp;
// mile1 and timestamp1 must refer to the earlier of the 2 observations (the smaller timestamp),
// and mile2 and timestamp2 must refer to the later of the 2 observations (the larger timestamp).
if timestamp1 > timestamp2 {
// observation 1 > observation 2, need to swap mile1 & mile2
(mile1, mile2) = (mile2, mile1);
}
let new_ticket = OutboundMessageType::Ticket {
plate: plate_road.plate.clone(),
road: plate_road.road,
mile1,
timestamp1: timestamp1.min(timestamp2),
mile2,
timestamp2: timestamp1.max(timestamp2),
speed: (average_speed * 100) as Speed,
};
info!(
"{:?} ready, storing day1: {} day2: {}, dispatching.",
new_ticket, day1, day2
);
state
.issued_tickets_day
.entry(plate_road.to_owned())
.or_default()
.push(day1);
state
.issued_tickets_day
.entry(plate_road.to_owned())
.or_default()
.push(day2);
ticket = Some(new_ticket);
break;
} else {
info!(
"{:?} from {:?} to {:?} had avg speed of {} limit {}, no ticket.",
plate_road, vec_of_ts_cameras[i], vec_of_ts_cameras[j], average_speed, common_limit
);
}
}
if ticket.is_some() {
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment