Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Xively Helper Class


  • Please log in to reply
6 replies to this topic

#1 iced98lx

iced98lx

    Advanced Member

  • Members
  • PipPipPip
  • 134 posts
  • LocationSouth Dakota

Posted 11 November 2013 - 10:46 PM

I'm not sure where SL is headed with their IOT plans, but that doesn't really help me right at the moment, so my original plan had been to write something fairly similar to (but more purpose built than) Xively in php and use it to data log and be a middleware to sharing current trends within my project. Seems like with a free developer account I don't need to do that and can just use Xively.
 
There are a couple examples of connecting to Xively via .netMF out there (http://forums.netdui...ely/#entry53968 for example) but none that met all my  needs. Many Hat Tips to those out there, as with those as inspiration I'm working to produce the following:


    [*]XivelyConnection class creates and manages a connection to a Xively project. I'll only have one, but lord knows someone will find a need for multiple, so you can do that. You tell it your API bits and how you want to push updates (real time? Queued? Time limit between pushes?) and if you call it properly it'll handle making sure you don't flood Xively and use too many updates. Has a queue of updates within, which when not in direct push mode will allow you build up a few different readings from a few different places and send them all in one update if there are more than one of them (including multiple readings for the same Channel). 
    [*]XivelyMeasurement is a helper class (really probably not required) which just stores the data, date/time, and the channel. Will need to work hard to make sure that memory is freed up quickly after pushing
    [/list]

    Thoughts- do I need to have an array of channels pre-defined somewhere, or do we trust ourselves to pass in the channel name properly with the creation of each measurement? 
     
    I don't like that this model is a turn/burn (measurements will be flushed and wait for GC to come clean them up after each push) but I don't want to limit the ability for the system to handle a bunch of measurements... thoughts?
     
    I need to do some experimenting but it appears that the API assumes you're either pushing a few measurements or the one you are is current- often in my case the readings will all be historical (though you can set it to push on demand) I had figured on this being more a queue and push system, though I guess it could be configured the other way (go and collect when told to push, though lots of work to have both)... I will probably stick with my original plan (easier implementation across projects) but would like to hear thoughts on it.
     
    Probably going to flesh this out tonight and try to get it going.. here is the skeleton:
     

    class XivelyConnection    {        private const string CRLF = "rn";        private static string _ApiKey;        private static string _FeedId;        private static string _UserAgent; // user agent is the project name        private int _pushType; //specifies if we should do updates in real time (0) away or que them up and obey the _delay (1)        private TimeSpan _delay; //delay (in seconds) between possible pushes        private DateTime _lastPush; //last time we pushed information        private ArrayList _commands; //arraylist of updates to push        private static IPAddress remote_IP = IPAddress.Parse("216.52.233.120");        private static IPEndPoint remoteEndPoint = new IPEndPoint(remote_IP, 80);        private Socket serversocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);      	       public XivelyConnection(string ApiKey, string FeedID, string UserAgent, int PushType=1, int delay=10)        {            _ApiKey = ApiKey;            _FeedId = FeedID;            _UserAgent = UserAgent;            _pushType = PushType;            _delay = TimeSpan.FromTicks(delay * 10000000); //no .FromSeconds in .netMF :(        }         public bool CheckAndPush()        {            //if there are commands & push_type is real time OR if there are commands & the delay time has passed            if(               (_commands.Count > 0 && _pushType == 0)                ||               (_commands.Count > 0 && ( (DateTime.Now - _lastPush).Duration()) > _delay))                {                    foreach (XivelyMeasurement measurement in _commands)                    {                    //format commands into XML                    }                }            //make the Push Request            return true;        }        public bool AddElement(XivelyMeasurement reading)        {            _commands.Add(reading); // add it to the things we want to push next time            if (_pushType == 0) //real-time pushing is enabled            {                if (this.CheckAndPush()) //go ahead and push it all up                    return true; //looks like the push was ok                else                    return false; // push failed            }            return true; //command added        }    }


#2 iced98lx

iced98lx

    Advanced Member

  • Members
  • PipPipPip
  • 134 posts
  • LocationSouth Dakota

Posted 11 November 2013 - 11:28 PM

Turns out since they want data for each channel grouped together I'll need to make a change away from the super simplistic 'loop over these' so that all the channels are grouped together. 



#3 iced98lx

iced98lx

    Advanced Member

  • Members
  • PipPipPip
  • 134 posts
  • LocationSouth Dakota

Posted 12 November 2013 - 03:31 AM

Alrighty, so managed to get it to send data to Xively the first time. Then it's disposing of my socket so the next time I try to run CheckAndPush on my xivelyconnection instance it tosses an error.  The code is rather... disgusting at the moment and only works to upload one item on demand, so I won't upload it yet, but I'll keep working on it this week.

 

Feed: https://xively.com/feeds/1162807509



#4 iced98lx

iced98lx

    Advanced Member

  • Members
  • PipPipPip
  • 134 posts
  • LocationSouth Dakota

Posted 12 November 2013 - 04:00 AM

Okay I got antsy and it is now logging temp every 15 seconds but I have a lot of thinking to do on how I want it to handle multiple data points etc going forward.. Good brain exercise anyway.



#5 iced98lx

iced98lx

    Advanced Member

  • Members
  • PipPipPip
  • 134 posts
  • LocationSouth Dakota

Posted 13 November 2013 - 04:57 PM

Alright glad I put this out there, it's caused me to spend more time thinking about how I'd want to use it. I'm going to break up check and push into "push this element" or "Queue this element to be pushed" - I'm looking at the best ways to support historical data and keeping a list of multiple readings from multiple channels before we push them, since they need to be formatted together.

 

I also want to make it so if you've queued things and you do a force push of anything (perhaps a critical measurement?) it is smart enough to push the queued items as well. With Xively the focus seems to be on how many updates per time period you send, not on how many total data points, so grouping them is a big plus.



#6 iced98lx

iced98lx

    Advanced Member

  • Members
  • PipPipPip
  • 134 posts
  • LocationSouth Dakota

Posted 17 November 2013 - 04:20 PM

Posted Image

 

Uploading is stable, though I just realized that Xively developers only get 30 days of data, so...

 

Anyone have a real use for a Xively class? Seems useless to us independents- might be back to plan A and building something similar and open source.



#7 Sambo

Sambo

    New Member

  • Members
  • Pip
  • 6 posts

Posted 07 April 2014 - 08:37 PM

@iced98lx,

 

That's neat!

 

Would you kindly share your latest version?

 

Thanks






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.