I am consuming 3-axis accelerometer data using Rx. I need to set up some unit tests. The data frames come in fast, with the median timespan between frames being 80ms, but on occasion it comes in at 120ms. Also, it is never exactly 80ms, but hovers in that range.
So I have created a class that subscribes to the IObservable and records the dataframes sequentially in a .csv file. One of the fields in the .csv file is the start time of the frame with the first frame having start time = 0.0.
Now I want to read that file and stream it again for the purpose of testing and development. I want to use the StartTime field as the schedule for when I will fire any given Accelerometer frame when testing.
I looked over the answers in this question, Scheduling a IEnumerable periodically with .NET reactive extensions but it seems only to address constant timespans.
Question: Is there already a canonical and preferred way to schedule frame pushes at irregular (but known) intervals in the Rx framework, or should I just roll my own somehow?
Edit 2: I am interested in something which could be as simple as:
IObservable<T> AsObservable(
IEnumerable<T> source, Func<T, TimeSpan> getTimeDelta)
{
var retVal = ColdObservableVaryingTime();
foreach(var frame in source)
{
retVal.AddScheduled(getTimeDelta, frame);
}
return retVal;
}
Edit 1: what I call "frames" in this question, Rx documentation calls TState.
The answer provided by James Lucas is a good pointer in the right direction, but the answer that succeded is more involved.
After I populate my IEnumerable with values from the csv file, I have to populate an array of
I then pass the array as parameter to scheduler CreateColdAbservable. After this has been accomplished, the cold observable is sitting there waiting to be started. In my particular situation, the code looks like this:
//// when I am ready to start the cold observable, I call
Help on putting this all together comes from
(Phil Haak) http://haacked.com/archive/2014/03/10/master-time-with-reactive-extensions/
and https://msdn.microsoft.com/en-us/library/hh229343(v=vs.103).aspx
It is also necessary to install another Nuget package:
http://www.nuget.org/packages/reactiveui-testing/
As Phil Haak says in the linked blog post, "Unfortunately, [TestScheduler] is a bit of a pain to use as-is which is why Paul Betts took it upon himself to write some useful TestScheduler extension methods"