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.
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
{"Name":"Jim", "Age":27, "Address":"Somewhere"} {"date":"12/01/2015","sensor":"temperature1","alarm":"hi","resolvedq":"true"}
[1,2,3,4] ["John", "Sue",Harry] [{"Temp",23.5},{"Temp",23.6},{"Temp",33.2}]
"Prettied" up:
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
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%"
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.
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
These work directly with the same Azure Mobile Service ToDoItems table as used by the ToDo Universal app example.
env.bat
The next three batch files perform the same actions as the ToDo Universal app.
toDoAdd.bat
* As per the ToDo app, adding a ToDoItem in left pane.
toDoGetActive.bat
* As per [Refresh] in the ToDo app
toDoSetComplete.bat
* As per checking a checkbox in the ToDo app
The following batch files exemplify other CRUD database actions.
toDoGetAll.bat
toDoGetComplete.bat
toDoRename.bat
toDoDelete.bat
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.
These need to be set for your Azure Mobile Service from the Azure Portal
telemetry.bat
Performs a range of REST actions including
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
MiniJson.exe
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
Next: Ardjson-Part 3b: cURL CRUD Examples