use opendaq::{Device, FunctionObject, Instance, Value};
fn function_property(device: &Device, name: &str) -> opendaq::Result<FunctionObject> {
device
.property_value(name)?
.into_object()?
.cast::<FunctionObject>()
}
fn main() -> opendaq::Result<()> {
let instance = Instance::new()?;
let device = instance.add_device("daqref://device0")?.expect("device");
let sum = function_property(&device, "Protected.Sum")?;
println!(
"Protected.Sum(7, 5) = {}",
sum.call(&[Value::from(7), Value::from(5)])?
);
println!(
"Protected.Sum(40, 2) = {}",
sum.call(&[Value::from(40), Value::from(2)])?
);
println!(
"Protected.Sum(100, 1) = {}",
function_property(&device, "Protected.Sum")?.call(&[Value::from(100), Value::from(1)])?
);
let sum_list = function_property(&device, "Protected.SumList")?;
println!(
"Protected.SumList([1, 2, 3, 4]) = {}",
sum_list.call(&[Value::from(vec![1, 2, 3, 4])])?
);
println!(
"Protected.SumList([]) = {}",
sum_list.call(&[Value::List(Vec::new())])?
);
let info = device
.property("Protected.Sum")?
.expect("Sum property")
.callable_info()?
.expect("callable info");
let expected = info.arguments()?.len();
let args = [Value::from(1), Value::from(2), Value::from(3)];
if args.len() == expected {
println!("\nProtected.Sum(1, 2, 3) = {}", sum.call(&args)?);
} else {
println!(
"\nWrong arity is rejected: Protected.Sum expects {expected} arguments, got {}.",
args.len()
);
}
Ok(())
}