Waiting for the first .NET wrist watch

Almost a year ago there was a Kickstarter campaign to found a first .NET Micro Framework watch: Agent smartwatch. Nice thing about it is that you will be able to program it using C# and Visual Studio. While we are still waiting for the product there is a SDK with an emulator. It is from the same guys that gave us Netduino! I decided to check it out.

Think about it: you have a Continuous Integration server running your builds and you want to monitor it on the fly. Is there a better device to do it than a wrist watch? So I thought and decided to check it out.

Here is a quick project I’ve hacked to proof the concept. But before we begin let me show you the result:

image

Neat! Isn’t it?

I’m using Jenkins as my Continuous Integration server. It has a set of APIs for the developer to use. I decided to give Json API a try.

I typed:

http://jenkins_url/api/json?tree=jobs[name,lastBuild[building,result]]

What gave me nice Json result:

{

  • "jobs": [
    • {
      • "name": "Demo4Dev1",
      • "lastBuild": {
        • "building": false,
        • "result": "SUCCESS"

        }

      },

    • {
      • "name": "Demo4Dev2",
      • "lastBuild": {
        • "building": false,
        • "result": "SUCCESS"

        }

      },

    • {
      • "name": "DemoTest1",
      • "lastBuild": {
        • "building": false,
        • "result": "SUCCESS"

        }

      }

    ]

}

I went to the Agent website and got the SDK. I fired up Visual Studio and wen New Project –> Visual C# –> Micro Framework –> AGENT Watch Application

image

Which gave me a Hello World application.

I added System.Http and System.IO references and headed straight to get the HTTP response and read the response stream to the end. Like this:

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(JenkinsApiUrl);
WebResponse resp = req.GetResponse();
StreamReader sr = new StreamReader(resp.GetResponseStream());
string respStr = sr.ReadToEnd();

Now I needed something to parse the Json text. Luckily for me I wasn’t the only one. There is nice NuGet project with Json parser. To get it issue:

PM> Install-Package Json.NetMF

Having it I head straight to deserialization:

Hashtable deserializedObject = Json.NETMF.JsonSerializer.DeserializeString(respStr) as Hashtable;

Now I went to hack and slash over over the result to find out everything is all right.

// Assume success
bool generalFailure = false;
foreach (DictionaryEntry de in deserializedObject)
{
    foreach (Hashtable ht in de.Value as ArrayList)
    {
        foreach (DictionaryEntry job in ht)
        {
            if (!job.Key.ToString().Equals("name"))
            {
                Hashtable ht2 = job.Value as Hashtable;
                if (ht2 == null) continue;
                foreach (DictionaryEntry results in ht2)
                {
                    if (!results.Key.ToString().Equals("building"))
                    {
                        if (results.Value.ToString().Equals("FAILURE"))
                            generalFailure = true;
                    }
                }
            }
        }
    }
}

I have added two result images to the resources:

image

And headed to show the result:

// initialize display buffer
_display = new Bitmap(Bitmap.MaxWidth, Bitmap.MaxHeight);
// Show result
_display.Clear();
Font fontNinaB = Resources.GetFont(Resources.FontResources.NinaB);
_display.DrawText("Jenkins", fontNinaB, Color.White, 35, 10);
if (generalFailure)
{
    _display.DrawText("FAIL!", fontNinaB, Color.White, 35, _display.Height - 20);
    Bitmap image =
        new Bitmap(Resources.GetBytes(Resources.BinaryResources.storm), Bitmap.BitmapImageType.Bmp);
    _display.DrawImage(_display.Width / 2 - image.Width / 2,
        _display.Height / 2 - image.Height / 2,
        image, 0, 0, image.Width, image.Height);
}
else
{
    _display.DrawText("SUCCESS!", fontNinaB, Color.White, 35, _display.Height - 20);
    Bitmap image =
        new Bitmap(Resources.GetBytes(Resources.BinaryResources.sun), Bitmap.BitmapImageType.Bmp);
    _display.DrawImage(_display.Width / 2 - image.Width / 2,
        _display.Height / 2 - image.Height / 2,
        image, 0, 0, image.Width, image.Height);
}
_display.Flush();
I packed everything in a never ending while loop with small delay:
while (true)
{
  // ... code ...
  Thread.Sleep(10000);
}

Done!

That’s the screen with the failure notice.

image

I can’t wait to get the Agent Watch to make the final app!

Leave a Reply

Your email address will not be published. Required fields are marked *