Ardjson: https://ardjson.codeplex.com
This now includes the batch files for this blog.

Previous: Ardjson – Part 2: Morphing The ToDo Sample Mobile Services Project into the Telemetry Project


In my recent Comcamp MVP presentation at Microsoft Melbourne, I introduced participants to the use of cURL.exe as a simple way of interacting with a Microsoft Azure Mobile Service table. The presentation slides, "Doing my first presentation today on IOT", is available through a previous blog on Embedded101. This was a lead into generating and consuming JSon on Arduino devices which will be covered later in this series of blogs. This blog is deep dive into using cURL with the Azure Mobile Service tables from the ToDo and Telemetry Universal Apps as covered in eth previous two blogs in this series.

JSON

  • JSON or JavaScript Object Notation, is an open standard format that uses human-readable text to transmit data objects consisting of attribute–value pairs. It is used primarily to transmit data between a server and web application, as an alternative to XML." Wikipedia
  • Json is used to pass structured data over the internet in a textual format that is human readable and thus easy to create a parser to glean the entities in a Json message.
  • Microsoft Azure Mobile Services, a cloud service, passes data to/from clients using Json and so can be used to connect devices with the cloud that have an implementation of the parser.
  • Connectivity in this project uses REST HTML POST, GET, PUT (actually PATCH) and DELETE with Azure Mobile. This is based upon the sample service that can be created when you first create an Azure Mobile Service, the ToDo app. This app was “morphed” into a Telemetry app in the previous two apps.

The endpoint of this sequence of blogs (Activity 1 in CEJson on Codeplex) is to have an Arduino device collecting telemetry data from sensors and directly storing this in the Telemetry table in the Mobile Service, and to parse the received JSon strings received by the device when it queries the Mobile Service


Some JSon Examles

Objects

{"Name":"Jim", "Age":27, "Address":"Somewhere"}
{"date":"12/01/2015","sensor":"temperature1","alarm":"hi","resolvedq":"true"}

 

Arrays

[1,2,3,4]
["John", "Sue",Harry]
[{"Temp",23.5},{"Temp",23.6},{"Temp",33.2}]

 

An Azure Mobil Service Table

image

"Prettied" up:

image
 
Each line above is one record from the table.

 

cURL.exe

cURL.exe is used in command lines or scripts to transfer data using an URL syntax (c-URL). It can communicate directly with an Azure Mobile Service over a network from command line or batch file to post and retrieve data as well as to modify and delete existing data. All that is needed is the URL for the service its AppKey and the table to be access. The command line syntax is complex and hence its parametrized use in scripts can make things easier.

The following curl.exe action places the name value pair (test,42) into the telemetry table:

curl -v -X POST -H Content-Type:application/json -H X-ZUMO-APPLICATION: NtcMLvQtuAqWtvXOwwZVQtpHevNUnN97 -d "{ \"test\" : 42 }" http://sportronicsdj.azure-mobile.net/tables/telemetry

  • Note: All of the above is on one line (and no space after the colon on the first line).
  • http://sportronicsdj.azure-mobile.net is the URL for the Mobile service that you get from the Azure Portal in the Mobile Service.
  • NtcMLvQtuAqWtvXOwwZVQtpHevNUnN97 is the AppKey that also you get from the Azure Portal.

The following, as part of a batch file will return all of the records in a table:

curl -v -X GET -H Content-Type:application/json -H X-ZUMO-APPLICATION:%AppKey% "%AzureMobileServiceURL%/tables/%MSTable%"

  • The %..% items are the AppKey, the service URL and the target table, respectively, which are set in the batch file prior to this line.

 

A good reference for cURL with Mobile services is Mike Tault’s Blog, “Experimenting with Windows Azure Mobile Services”:
http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2012/09/19/experimenting-with-windows-azure-mobile-services.aspx 
The batch files presented here were based upon some suggestions from his blog, also using much experimentation.


Activity 1.2

This like Activity 1.1 has two parts: The ToDo app and the Telemetry app. The zip file 1.2 Curl, JSON and REST.zip available on the CEJson Codeplex site contains a folder for each with some batch files that are CRUD database examples. cURL.exe is also included in the folders for simplicity but it suggested that you download the latest version from curl.haxx.se.

Rather than reproduce the batch files here one is invited to inspect them as in the zip file. The curl command line for various CRUD actions are listed in the next blog in this series:
CEJson-Part 3b: cURL CRUD Examples

 

ToDo Folder Contents

These work directly with the same Azure Mobile Service ToDoItems table as used by the ToDo Universal app example.

env.bat

  • Sets the Azure Mobile Service URL
  • Sets the app key (Note scrambled from my actual key)
    These need to be set for your Azure Mobile Service from the Azure Portal

 

The next three batch files perform the same actions as the ToDo Universal app.

toDoAdd.bat

  • Adds a new ToDoItem
  • Requires the ToDoItem text as a command line parameter

        * As per the ToDo app, adding a ToDoItem in left pane.

 

toDoGetActive.bat

  • Gets all toDos not marked as complete as a Json string

        * As per [Refresh] in the ToDo app

 

toDoSetComplete.bat

  • Sets a toDoItem as complete
  • Requires the record's GUID as a command line parameter (without braces).

        * As per checking a checkbox in the ToDo app

 

The following batch files exemplify other CRUD database actions.

toDoGetAll.bat

  • Gets all toDoItem records in the table (as specified in the file) as a Json string

 

toDoGetComplete.bat

  • - Gets all toDos marked as complete as a Json string

 

toDoRename.bat

  • Updates a record
  • Renames a ToDoItem.text to Read
  • Requires the record's GUID as a command line parameter (without braces).

 

toDoDelete.bat

  • Deletes a ToDoItem
  • Requires the record's GUID as a command line parameter (without braces).

 

Telemetry Folder Contents

This contains two batch files both of which run through a battery of CRUD actions. The difference is that the second one, after getting and displaying a JSon string, then does a “mini-parse” of it so as to display each record on a separate line, and does a heuristic count of the records. As records are added, deleted and modified, it is easier to see the changes with the second batch file.

These work directly with the same Azure Mobile Service Telemetry table as used by the Telemetry Universal app discussed previously in this blog series.

 

env.bat

  • Sets the Azure Mobile Service URL
  • Sets the app key (Note scrambled from my actual key)

        These need to be set for your Azure Mobile Service from the Azure Portal

 

telemetry.bat

Performs a range of REST actions including

  • Get all records
  • -dd a new record
  • Get records based upon a numeric field (EQ and GT)
  • Get records based upon a string field (startswith)
  • Update a record
  • Delete a record

Notes :Update (Patch) and Delete actions require a specified GUID for the record to be actioned.
             The GUID is passed as a command line parameter to the batch file

 

telemetryWithDisplay.bat

  • As per telemetry.bat
  • Each Json string is captured into json.txt file
  • And n then displayed
  • Also parsed by MiniJson.exe to separate into records (one per line)

 

MiniJson.exe

  • - The simple parser of string into records
  • - Also counts number of records
  • - Could be extended to do full Json parse

MiniJson Subfolder

  • - The C# .NET 4.5 Visual Studio 2013 Project for the MiniJson app
  • - A Console App
  • - Could easily be modified for earlier versions of .NET and Visual Studio
MiniJsin Console App C# Code
  1. using System;
  2. using System.IO;
  3.  
  4. namespace MiniJson
  5. {
  6.     class Program
  7.     {
  8.         /// <summary>
  9.         /// MinimalJson
  10.         /// Take a Json string and formats so that each record is on a separate line so that can see records.
  11.         /// Could be extended to do full JSon parse.
  12.         /// In batch file capture the cURL output to the temp file then call this app to display the results
  13.         /// </summary>
  14.         /// <param name="args">The name of the temporary file that contains the JSon string</param>
  15.         static void Main(string args)
  16.         {
  17.             // Json string to be read from temp file
  18.             StreamReader sr = new StreamReader(args[0]);
  19.             string jsn = sr.ReadToEnd();
  20.  
  21.             //Set of records starts with [ and ends with ]
  22.             jsn = jsn.Replace("", "\r\n********************* Records Done  \r\n");
  23.           
  24.             //Each record ends in } so place CR LF after so each record is on one line
  25.             jsn = jsn.Replace("},", "}\r\n");
  26.  
  27.             //Each record begins with { so count them in the string
  28.             int numRecords  = jsn.Split('{').Length ;
  29.            
  30.  
  31.             //Display results
  32.             Console.WriteLine(jsn);
  33.             Console.WriteLine("\r\nNumber of Records= {0}\r\n", numRecords);
  34.         }
  35.     }
  36. }

 

Discussion

To run these batch files (in either folder), you need to set the URL and AppKey in env.bat, which is called by all of the other batch files. The ToDo batch files each have a separate action whereas the telemetry batch files run through a suite of actions.

The update and delete batch files require the id which is a GUID of the specific record to be actioned upon. No filters can be used for these actions.
There is a syntax for filters with GET though:

<The table URL>?$filter=(complete+eq+true)" gets all of the items marked as complete.
<The table URL>?$filter=(complete+eq+false)" gets all of the items that are still “to do”.
<The table URL> ?$filter=(startswith(sensor,'Temperature')) gets all records whose sensor field starts with Temperature
<The table URL> ?$filter=(value%%20eq%%20137)" gets all records whose value field equal 137
<The table URL> ?$filter=(value%%20gt%%2060)" gets all records whose value field is greater than 60

Enjoy Smile



Next: Ardjson-Part 3b: cURL CRUD Examples