Converting a polling based API into a streaming API with the Reactive Extensions

Recently my building has been having issues with its boilers, and the heat has been going out for longer than is comfortable. The superintendent that makes a habit of periodically checking on the status of each of the boilers. A workable approach certainly, but figured this would be ideal for a technology assist.

 

For $9, I purchased a USB thermometer, word on the web is that the software comes with its fairly miserable (and crashed  immediately on machine), but with some Google and reflector, was able to come up with a polling based API to read the temperature

public interface IUsbTEMPer
{
    double Temperature { get; }
}

With the RX, converting the API into a stream of data is just one line:

IObservable<double> ts = Observable.Generate(
Scheduler.Later, 
() => new Notification<double>.OnNext(usbTempReader.Temperature)
).Publish();

And getting some simple alerts is easy too:

ts.Buffer(new TimeSpan(1, 5, 0))
    .Select(fiveminOfTemp => fiveminOfTemp.Average())
    .Where(avgtemp => avgtemp < 65)
    .Subscribe(cold => ToTwiter("buildingstatus account..."));
Published Thursday, January 21, 2010 10:47 PM by Scott Weinstein

Comments

# Twitter Trackbacks for Converting a polling based API into a streaming API with the Reactive Extensions - Scott Weinstein on .Net, [asp.net] on Topsy.com

Pingback from  Twitter Trackbacks for                 Converting a polling based API into a streaming API with the Reactive Extensions - Scott Weinstein on .Net,         [asp.net]        on Topsy.com

Friday, January 22, 2010 1:37 PM by Pat

# re: Converting a polling based API into a streaming API with the Reactive Extensions

If you posted all of the code, it would make this a better reference for RX. Thanks for what you have included.

Friday, January 22, 2010 6:57 PM by Scott Weinstein

# re: Converting a polling based API into a streaming API with the Reactive Extensions

Pat - for sure. I've some code posted at code.msdn.microsoft.com/FSOlapRxDemo and I plan to post more, after the Alt.Net meetup on the topic

# Carolina Hurricanes vs Atlanta Thrashers Live Streaming Jan 21 1 &#8230; | Atlanta Thrashers NHL Announcer

Pingback from  Carolina Hurricanes vs Atlanta Thrashers Live Streaming Jan 21 1 &#8230; | Atlanta Thrashers NHL Announcer

Wednesday, April 14, 2010 11:58 AM by Richard Hein

# re: Converting a polling based API into a streaming API with the Reactive Extensions

Nice!  A friend of mine and I have been thinking about some ideas and this would make it so much easier to try.  Thanks for posting it.

Leave a Comment

(required) 
(required) 
(optional)
(required)