use epics_base_rs::error::CaResult;
use epics_base_rs::server::device_support::{DeviceReadOutcome, DeviceSupport};
use epics_base_rs::server::record::{ProcessAction, Record};
use epics_base_rs::types::EpicsValue;
use crate::records::epid::EpidRecord;
pub struct EpidSoftCallbackDeviceSupport {
triggered: bool,
}
impl Default for EpidSoftCallbackDeviceSupport {
fn default() -> Self {
Self::new()
}
}
impl EpidSoftCallbackDeviceSupport {
pub fn new() -> Self {
Self { triggered: false }
}
}
impl DeviceSupport for EpidSoftCallbackDeviceSupport {
fn dtyp(&self) -> &str {
"Epid Async Soft"
}
fn read(&mut self, record: &mut dyn Record) -> CaResult<DeviceReadOutcome> {
let epid = record
.as_any_mut()
.and_then(|a| a.downcast_mut::<EpidRecord>())
.expect("EpidSoftCallbackDeviceSupport requires an EpidRecord");
if !self.triggered {
if !epid.trig.is_empty() {
let actions = vec![
ProcessAction::WriteDbLink {
link_field: "TRIG",
value: EpicsValue::Double(epid.tval),
},
ProcessAction::ReprocessAfter(std::time::Duration::from_millis(1)),
];
self.triggered = true;
return Ok(DeviceReadOutcome::computed_with(actions));
}
}
self.triggered = false;
super::epid_soft::EpidSoftDeviceSupport::do_pid(epid);
Ok(DeviceReadOutcome::computed())
}
fn write(&mut self, _record: &mut dyn Record) -> CaResult<()> {
Ok(())
}
}