use resource_timeline::storage::{Fixed, FixedPlan};
use resource_timeline::{
ActivityId, Bounds, Demand, Error, Impact, LevelProfile, Plan, SearchScratch, StateId,
StateImpact, StateTimeline, Tick, Timeline, TimelineId, Window,
};
const ROOM: usize = 32;
type Onboard = Plan<FixedPlan<2, 1, ROOM>>;
const ORBIT: Tick = 95;
const SUNLIT: Tick = 60;
const HIDDEN: u32 = 0;
const IN_VIEW: u32 = 1;
const ORBIT_ENV: ActivityId = ActivityId(0);
const IMAGE: ActivityId = ActivityId(1);
const DOWNLINK: ActivityId = ActivityId(2);
struct Lines {
battery: TimelineId,
radio: TimelineId,
station: StateId,
}
fn onboard() -> Result<(Onboard, Lines), Error> {
let mut plan = Onboard::new_in();
let bounds = Bounds::new(120, 400)?;
let lines = Lines {
battery: plan.add_timeline(Timeline::depletable_in(0, 140, bounds, ROOM)?)?,
radio: plan.add_timeline(Timeline::claimable_in(0, 1, ROOM)?)?,
station: plan.add_state(StateTimeline::new_in(0, HIDDEN, ROOM))?,
};
let rate = |start, end, per_tick| Demand::Level {
timeline: lines.battery,
impact: Impact::Rate {
start,
end,
per_tick,
},
};
let set = |at, value| Demand::State {
timeline: lines.station,
impact: StateImpact::Set { at, value },
};
for t0 in [0, ORBIT] {
let orbit = [
rate(t0, t0 + SUNLIT, 3),
rate(t0 + SUNLIT, t0 + ORBIT, -3),
set(t0 + 40, IN_VIEW),
set(t0 + 52, HIDDEN),
];
plan.place(ORBIT_ENV, 0, &orbit)?;
}
Ok((plan, lines))
}
fn replan(plan: &mut Onboard, lines: &Lines) -> Result<(Tick, Tick), Error> {
let image = [Demand::Level {
timeline: lines.battery,
impact: Impact::Rate {
start: 0,
end: 10,
per_tick: -15,
},
}];
let downlink = [
Demand::Level {
timeline: lines.battery,
impact: Impact::Rate {
start: 0,
end: 8,
per_tick: -2,
},
},
Demand::Level {
timeline: lines.radio,
impact: Impact::Claim {
start: 0,
end: 8,
amount: 1,
},
},
Demand::State {
timeline: lines.station,
impact: StateImpact::Require {
start: 0,
end: 8,
value: IN_VIEW,
},
},
];
let mut scratch = SearchScratch::<Fixed<ROOM>>::new();
assert!(!plan.fits_with(&image, SUNLIT, &mut scratch)?);
let image_at = plan
.earliest_start_with(&image, Window::new(0, 150)?, 256, &mut scratch)?
.expect("the second orbit has room");
plan.place(IMAGE, image_at, &image)?;
let window = Window::new(image_at + 10, 180)?;
let downlink_at = plan
.earliest_start_with(&downlink, window, 256, &mut scratch)?
.expect("the second pass carries the downlink");
plan.place(DOWNLINK, downlink_at, &downlink)?;
Ok((image_at, downlink_at))
}
fn telemetry(line: &Timeline<Fixed<ROOM>>, frame: &mut [u8]) -> Result<usize, Error> {
print!("Battery every 20 min:");
for sample in line.samples(0, 20, 10)? {
let (t, level) = sample?;
print!(" {t}:{level}");
}
println!();
let profile = line.export()?;
let length = profile.encode(frame)?;
println!(
"Profile: {} breakpoints for {} impacts, {length} bytes in the frame.",
profile.len(),
line.impacts().len()
);
Ok(length)
}
fn main() -> Result<(), Error> {
let (mut plan, lines) = onboard()?;
let (image_at, downlink_at) = replan(&mut plan, &lines)?;
println!("Image at minute {image_at}, downlink at minute {downlink_at}.");
assert_eq!(plan.first_violation()?, None);
assert_eq!((image_at, downlink_at), (104, 135));
let line = plan.timeline(lines.battery).expect("added above");
let mut frame = [0_u8; 512];
let length = telemetry(line, &mut frame)?;
let received = LevelProfile::decode(&frame[..length])?;
for t in [0, image_at + 10, downlink_at + 8, 2 * ORBIT] {
assert_eq!(received.level_at(t)?, line.level_at(t)?);
}
let lowest = received
.breakpoints()
.min_by_key(|point| point.level)
.expect("at least one breakpoint");
println!(
"Lowest breakpoint on the ground: {} at minute {}.",
lowest.level, lowest.at
);
assert_eq!((lowest.at, lowest.level), (image_at + 10, 122));
let mut full_frame = [0_u8; 1024];
let full_length = line.encode(&mut full_frame)?;
let copy = Timeline::decode(&full_frame[..full_length])?;
println!(
"Full timeline: {} impacts in {full_length} bytes; the ground copy is unviolated: {}.",
copy.impacts().len(),
copy.first_violation()?.is_none()
);
assert_eq!(copy.impacts(), line.impacts());
frame[length / 2] ^= 0x10;
let corrupted = LevelProfile::decode(&frame[..length]);
println!("Corrupted frame: {corrupted:?}");
assert_eq!(corrupted, Err(Error::ChecksumMismatch));
let full = plan.add_timeline(Timeline::claimable_in(0, 1, ROOM)?);
println!("Adding a third numeric timeline: {full:?}");
assert!(full.is_err_and(|error| error.is_capacity()));
Ok(())
}