DatagramBuilder
DatagramBuilder is a struct that accumulates the commands to send and converts them into Frames.
It is obtained via Client::datagram_builder.
push accumulates commands, and build converts them into Frames.
The resulting Frames is split into multiple Frames; send them one frame at a time with Client::send / send_checked.
let mut builder = client.datagram_builder();builder.push(SetSilencer::default());let frames = builder.build()?;for frame in &frames { client.send_checked(frame).await?;}builder = client.datagram_builder()builder.push(SetSilencer())frames = builder.build()for frame in frames: await client.send_checked(frame)var builder = client.DatagramBuilder();builder.Push(new SetSilencer());var frames = builder.Build();foreach (var frame in frames){ await client.SendCheckedAsync(frame);}| Method | Description |
|---|---|
push(cmd) |
Accumulate a command common to all devices |
push_each(assign) |
Accumulate a different command per device (see below) |
build() |
Convert the accumulated commands into Frames |
push_each
Section titled “push_each”push_each takes a closure that receives a device and assigns a different command per device.
In Rust it is FnMut(&Device) -> Option<C>, in Python Callable[[Device], Command | None], and in C# Func<Device, ICommand?>.
For a device where None is returned, nothing is assigned in that step.
let mut builder = client.datagram_builder();builder.push_each(|device| { Some(if device.idx() % 2 == 0 { Pattern::new(&left) } else { Pattern::new(&right) })});let frames = builder.build()?;builder = client.datagram_builder()builder.push_each(lambda device: Pattern(left if device.idx() % 2 == 0 else right))frames = builder.build()var builder = client.DatagramBuilder();builder.PushEach(device => device.Idx % 2 == 0 ? new Pattern(left) : new Pattern(right));var frames = builder.Build();In Rust, the return values of the closure must all be the same type.
To mix commands of different types, either unify them into a BoxedCommand with Command::boxed, or call push_each once per type.
Because Python is dynamically typed, commands of different types can be returned per device as-is.
let mut builder = client.datagram_builder();builder.push_each(|device| { Some(if device.idx() % 2 == 0 { Pattern::new(&left).boxed() } else { Modulation::new(SamplingConfig::FREQ_4K, &modulation).boxed() })});let frames = builder.build()?;