Official website

Showing posts with label WoT. Show all posts
Showing posts with label WoT. Show all posts

Thursday, August 18, 2016

Consuming RESTful data, create widgets and more

A common way to offer server-generated data to browsers, so that it can be used in client-side JavaScript is by formatting the data as JSON, and making it accessible through its own URL. A web server that respects itself, even a lightweight like Jubito, should conformed with these standards.

Until now, the core of Jubito was loading data from the server using a HTTP GET request over jQuery.get(). The return type was a simple html. In fact, still used and applied when mode is set to html (i.e. http://localhost:8080/www/?cmd=<commands>&mode=html). By default is text, or by setting mode to text. In case of html, the only element presented is the '<br />'. The main "disadvantage" is that there is no object to handle and each command need its own call, or a manipulation of the result in case of multiple commands (e.g. splitting the breaks '<br />').

In this post, I'm going to present native APIs for consuming data from the framework and the web. That way we'll be able to customize Jubito by creating widgets and UIs.

Before we begin, let's do some REST style examples from the browser in order to understand the behavior. URI has a typical structure and addressed like this:
http://<host>:<port>/www/?cmd=<sequence of commands as parameters delimited by ampersand>&<mode=json> 
e.g.: http://localhost:8080/www/?cmd=judo%20serial%20state%&%judo%20server%20state&%whereami%&%whoami%&mode=json


Explaining the functionality of each command:
judo serial state is an API call that returns the state/status of the serial port.
judo server state is an API call that returns the state/status of the web server.
%whereami% is a built-in function that returns the user status.
%whoami% is a built-in function that returns the user login.

Parameters in API respond:
1. Pair name - A simplified name of the requested command
2. Key - Represents the requested command
3. Value - Result of the requested command

For more information about built-in functions and API please visit Wiki

Next step is to create a jQuery.ajax() request and see how to consume the response data. It provides a simple process of fetching and parsing JSON data through Ajax. An ideal and straightforward way to achieve our goal for tailor made widgets and UIs.

Sample HTML:
<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8" />
  <title></title>
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script> 
 </head>
 <body>
  <script type="text/javascript">
   $(document).ready(function() {
    getData();
   });
   
   setTimeout (function () {
    $("#data").empty();
    getData();
   }, 10000);
   
   function getData () {
    $.ajax({
     type: 'GET',
     contentType: 'application/json; charset=utf-8',
     dataType: "json",
     // requested built-in functions: %whereami% %whoami% %todayconditions% %todaylow% %todayhigh% %about%
     // request on judo API: judo server state
     // wiki https://github.com/jambelnet/janet-framework/wiki
     // uri form: <host>:<port>/www/?cmd=<sequence of commands as parameters>&<mode=json>
     url: "http://localhost:8080/www/?cmd=%whereami%&%whoami%&%todayconditions%&%todaylow%&%todayhigh%&judo%20server%20state&%about%&mode=json",
     }).done(function (data) {
      var template = 'Status: ' + data.whereami.Value + '<br/>User: ' + data.whoami.Value + '<br />';
      template += 'Conditions: ' + data.todayconditions.Value + '<br />';
      template += 'Low: ' + data.todaylow.Value + 'C&deg;<br />';
      template += 'High: ' + data.todayhigh.Value + 'C&deg;<br />';
      template += 'Server status: ' + data.judo_server_state.Value + '<br />';
      template += 'About: ' + data.about.Value + '<br />';
      
      $("#data").append(template);
     }).fail(function() {
      alert( "Oops!" );
     });
    }
  </script>
  
  <pre id="data"></pre>
 </body>
</html>
http://hilite.me/

The sample above will serve a simple page with data displayed and refreshed every 10 seconds:


Another powerful feature which you may find interesting, is the HTTP GET request supported by the API. It provides raw data from any endpoint.
judo http get <endpoint>


An example of its use can be found here on how to send and read data from RaZberry Z-Wave controller.

So, to recap, there is a lot of new things to expect, but till then, you should probably start honing your skills and finally create your own UI or a widget to the existing.


I tried to describe this post as much as possible less technical, in order to be understood by beginners.

Thursday, March 5, 2015

Extracting data from JSON and XML

Web API connector is released and supports both JSON and XML response parsing. This feature opens up the possibility of connecting to a huge range of potential APIs and therefore reducing the cost for additional equipment. For example, if you want to create some tasks based on weather conditions, you can achieve that without a weather station, but by gathering information from the cloud.

In addition to this post let's do some JSON examples from Yahoo Weather API. I will also use the Code Beautify online code editor for a good human-readable representation of the response.

Step 1: Copy and Paste the endpoint URI from yahoo developer website and paste it to the editor.



Step 2: Go to Control Panel->Instructions->Add New Web Service. Address the node you want to retrieve and fill the form items (Name, Endpoint, Node Path). Notice that if the response have a tree structure, you should point the position index of your target in Node Path as seen below. By default will get the first element [index: 0].


By default some editors starts the tree viewer with root/array and then with the absolute tree structure. So, at the "Node Path" field of Jubito, ignore the above of starting path.

Step 3: Go to terminal tab and debug the new Instruction Set. If you get the value then everything went well.


Step 4: Now, let's make an interface and a scenario. Again from Control Panel->Instructions->Add New Instruction Set. Unfold the Add Action(s) and Add to Dashboard and follow the screenshot.


Save and return. Now you're able to make a call from the Dashboard screen.


Step 5: Some other things you can do, is to make a schedule to evaluate the weather and send an email notification when a specified weather acknowledgement applies. Let's take a look to the Evaluator. Once again Control Panel->Instructions->Add New Evaluator. Select the yahoo-test Instruction Set and add some additional text if you wish. As I always remind, the asterisk in front of the Instruction Set is the reflector of a value.


'Then' condition corresponds to another Instruction Set that is responsible to send mail. You can search this blog with 'send mail' keyword to see how to make one.

Step 6: Finally there all set to create the Schedule. Go to Control Panel->Scheduler->Add New. Type a name, repeat, interval at your choice (3600000ms is one hour) and select the Evaluator's Instruction Set.


This schedule will run every hour and evaluate the condition.

All of the above steps are described as a proof of concept. The use-cases may vary depending your needs.

The same logic applies also to XML. You can be advised by this post. If you find any difficulties or discover a bug, please do not hesitate to drop me a message.

Changes applied to jaNET Framework 0.2.6.555+. To check version go to terminal tab and type %about%.

Thursday, November 27, 2014

Web of Things and what's new on the upcoming version

In our days, there is a trend to talk about the Internet of Things, Machine to Machine, Web of Things, etc. However all of the above are considered an integral part of the IoT. I won't discuss any of them in detail, since there are too many articles over the internet.

A vast amount of information, is distributed via web services. Web services are an efficient way of communication, that support interoperable machine-to-machine interaction, over a network. As a developer, I need my platform to be able to consume all these services and produce innovative use-cases.

In this post I would like to share some of the latest developments, that would be available in the next version of Jubito. I'm talking about tailor made methods for native XML and JSON deserialization from web service/web API responses.

Some first tests using Yahoo Weather API.

XML:


JSON:


The current release, based on 0.2.5 jaNET Framework comes with full partial support [ Still under development ]. To check the version, go to Terminal tab and type %about%.