A real advantage of Internet powered TV is the opportunity for personalisation and customisation to make it a more compelling and meaningful experience for the viewer, but to support this it helps to have a flexible solution to query the data about what is on offer. Linked Data could be that flexible solution as it makes it possible to send a quite complex query, possibly generated on the fly to a data store. With this in mind I have been experimenting with consuming linked data on a cheap and cheerful blu-ray player that supports the Samsung Internet@TV platform. Using a web developer skill set it is possible to build a web application that runs on the device that has the ability to pass a query directly to a SPARQL endpoint and parse the results.
If you haven't developed anything for the Samsung Internet@TV platform before then please read my earlier post: “A "Hello World" app for Samsung Internet@TV” as it describes how to write a basic application and get it onto an actual device. I'll assume you know all of this for the rest of this post!
The example here does not do much, it simply runs a query on the Open University's Linked Data store and then parses the results and displays them on screen. Note this is a direct call, there is no proxy script (unlike the one needed to use Linked Data with App Inventor) so all of the processing takes place on the device itself. If you are using this method with large data sets you might want to be careful of the physical restrictions of the hardware you are using such as limited memory.
Samsung Internet@TV apps are web applications that run inside a browser called MAPLE (MArkup engine PLatform for Embedded systems). This seems to be quite a powerful browser that also includes a rich set of APIs for interacting with features such as the remote control and built in video player. Unlike normal web development we don't just create a page and load it up in the browser, instead we construct a small web application and tell MAPLE that we are ready for it to be displayed.
To get started on the widget, start up the Samsung TV Apps SDK and create a new project. Call it something like “LinkedDataDemo”. Create two folders; “css” and “js”. In the “css” folder create a file named “main.css” with the following contents:
body {
margin: 0;
padding: 0;
background-color: transparent;
}
#results {
position: absolute;
left: 10px;
top: 10px;
width: 800px;
height: 600px;
background-color: #AFAFAF;
color: #99FFFF;
font-size: 20px;
text-align: left;
}
Now create an “index.html” page in the root of your application with the following contents:
To get the data I used an XMLHttpRequest object that some of you may have seen elsewhere. This is pretty much a standard JavaScript feature in browsers now and lets you grab an XML document from a server. Usually XMLHttpRequest cannot make cross domain requests (it cannot get data from servers not on the same domain as the web page it belongs to) so it might surprise you that this restriction does not cause a problem here. I think the reason for this is that this code is running in a web application rather than a web page, so it gets treated differently.
The JavaScript file should be called “main.js” and be in the “js” folder. The contents look like this:
var Main = {
// Main object
}
// Create Common module
var widgetAPI = new Common.API.Widget();
Main.onLoad = function () {
// Send ready message to Application Manager
// this causes widget to be displayed
widgetAPI.sendReadyEvent();
// called by
// Build the SPARQL query
var sparql = "PREFIX dc:
"SELECT DISTINCT ?podcast ?title ?download "+
"FROM
"WHERE { " +
" ?podcast dc:title ?title . " +
" ?podcast
" ?podcast dc:subject
"} " +
"ORDER BY ?title";
// build the URL
var query_url = "http://data.open.ac.uk/query?query=" + escape(sparql);
// Make the request to the data server
// alert here doesn't bring up a dialog box but instead is used for debug output in Log Manager in the SDK
alert("Making request for data");
xhr=new XMLHttpRequest();
xhr.open("GET", query_url, false);
xhr.send();
// results
xmlDoc=xhr.responseXML;
alert('Response received, now parsing');
// build a string that we can just dump on the screen
var results=xmlDoc.getElementsByTagName("result");
var results_display = '';
// loop round each results line
for(i=0; i
var bindings=results[i].getElementsByTagName("binding");
for (j=0; j
if (bindings[j].getElementsByTagName("uri").length > 0) {
results_display =results_display + bindings[j].getElementsByTagName("uri")[0].childNodes[0].nodeValue + "
";
}
if (bindings[j].getElementsByTagName("literal").length > 0) {
results_display =results_display + bindings[j].getElementsByTagName("literal")[0].childNodes[0].nodeValue + "
";
}
}
results_display =results_display + "-----------------
";
}
// put data on screen
var results = document.getElementById("results");
results.innerHTML = results_display;
alert("Finished");
}
The code in this example is very similar to code that could be used in a normal web page, however one important difference is that we have to call widgetAPI.sendReadyEvent() to tell the Application Manager on the device to display the web widget. If we don't, the screen will be blank! In this example a simple SPARQL query to return a list of podcasts is embedded in the code, but in a real life case you could build this queries dynamically in order to build menus for the user. Then we call the OU's SPARQL endpoint using an XMLHttpRequest object. When the results are received they will be in XML format, so these are parsed and turned into a simple text string that can be displayed on screen. Again in a real life application you could use this information a bit more usefully, for example passing a URL to the video player and displaying details on screen in a more polished way.
One minor difference between MAPLE's JavaScript and other browsers is the alert() function. Unlike most browsers this does not display a dialogue box with a message, instead it is used to pass text to the Log Manager window in the Samsung TV Apps SDK for debugging purposes. When we have finished with the XMLHttpRequest object the non-standard (MAPLE only) destroy() method is called to release memory on the device as soon as possible. Memory and resources on devices like this are often quite restricted compared to other types of computer so it is good practice to release resources as soon as you can.
When you run the app you should see a listing of some of the data returned from the query on the screen. Only the first few rows will be visible as that is all that will fit. The above example ran pretty quickly on my blu-ray player and the whole exercise was much easier to achieve than I thought it would be. There are many ways this script could be improved (e.g. by running the query in an asynchronous XMLHttpRequest object) but hopefully it serves as a starting point that could be used to build powerful applications for TVs using Linked Data, not just on Samsung products but any platform (possibly including YouView) that support web applications.
Re: How to use Linked Data on the Samsung Internet@TV platform
Don't forget to sandwich your requests by turning the watchdog off and then on, otherwise a net hiccup or timeout will force the app manager to kill your app.
Re: How to use Linked Data on the Samsung Internet@TV platform
Thanks! That's a great tip which will save a lot of problems.
how to display the xml
how to display the xml document content?
how to display the xml
how to display the xml document content?