Adding A HealthVault DataType to MoodTracker #4

Last time we figured out how to associate our application with HealthVault. In this post we will focus on reading data from HealthVault.

So the data type we settled on was EmotionalState, we can find a sample xml for this type on the HealthVault Developer Center.

Out first goal is to be able to display this type in our application. We need test data for emotional state so we add test information to our developer account from type samples.

image

Fig 1. Adding Emotional State sample to HealthVault record.

Please note: You need to be signed in to developer.healthvault.com to add the above sample.

We can verify that this sample is added to our record by using the HealthExplorer.

image

Fig 2. Emotional State Samples in developer account.

The easiest way to get-things from HealthVault is using the GetThings version 3 method. A Get on things can be performed using various filters for the purposes of this application which just retrieve all the active things of type Emotional State.

                HealthVaultMethods.GetThings(EmotionalStateModel.TypeId, GetThingsCompleted);

To make it easier to work with GetThings I implemented a simple abstraction on the method in HealthVaultMethods class.

Now once we can get thing emotional state things we need to perform two things on the client side.

1. Sort them in the date order and pick the latest one

For this item Linq to XML comes in very handy, just in three lines we can make this query work!

 // using linq to get the latest reading of emotional state
 XElement latestEmotion = (from thingNode in responseNode.Descendants("thing")
         orderby Convert.ToDateTime(thingNode.Element("eff-date").Value) descending
         select thingNode).FirstOrDefault<XElement>();

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

2. Parse them for the mood, stress and wellbeing data.

We can achieve this my creating a model for Emotional State. Have a good model makes if super easy to parse the XElement to the object we need —

 EmotionalStateModel emotionalState =
     new EmotionalStateModel();
     emotionalState.Parse(latestEmotion.Descendants("data-xml").Descendants("emotion").Single());

XElement and lazy evaluation makes our job super-simple, compared to XPathNavigator –

public void Parse(XElement emotionalState) { this.Mood = (Mood) System.Enum.Parse(typeof(Mood), ((XElement)emotionalState.Element("mood")).Value, true); this.Stress = (Stress) System.Enum.Parse(typeof(Stress), ((XElement)emotionalState.Element("stress")).Value, true); this.Wellbeing = (Wellbeing) System.Enum.Parse(typeof(Wellbeing), ((XElement)emotionalState.Element("wellbeing")).Value, true); }

 

Now we are ready to show the latest emotional state reading!!

 

image

Fig 3. Latest Emotional State Reading in Mood Tracker!

Next Time: We will look at how we can enter new data with Mood Tracker!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s