Author Topic: JavaScript to access TED  (Read 6329 times)

Banana_Slug

  • Newbie
  • *
  • Posts: 6
  • Karma: +0/-0
JavaScript to access TED
« on: April 13, 2011, 11:34:57 AM »
In my attempt to learn JavaScript, I am attempting to access TED. No success here. Can someone show me how I can get to TED using JavaScript? Here's my code.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>ISBN</title>
</head>
<body>
<div id="data"></div>
<script type="text/javascript">

function readyAJAX() {
    try {
        return new XMLHttpRequest();
    } catch(e) {
        try {
            return new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            try {
                return new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e) {
                return "A newer browser is needed.";
            }
        }
    }
}
var requestObj = readyAJAX();

var gatewayURL ="http://TED5000/";
var SecondHistoryMTU1="history/secondhistory.xml?MTU=0&COUNT=1&INDEX=1";
var url = gatewayURL+SecondHistoryMTU1;
alert("open("+url);
requestObj.open("GET",url,true);
requestObj.send();

requestObj.onreadystatechange = function() {
    if (requestObj.readyState == 4) {
        if (requestObj.status == 200) {
           alert("SUCCESS!! readyState="+requestObj.readyState+" status=" + requestObj.status);
           // if I get here, time to get the xml
           
         } else {
            alert("FAILURE!! readyState="+requestObj.readyState+" status=" + requestObj.status);
        }
    }
}
</script>
</body>
</html>

TedDev

  • TED Software Developent
  • Administrator
  • Full Member
  • *****
  • Posts: 158
  • Karma: +0/-0
Re: JavaScript to access TED
« Reply #1 on: April 14, 2011, 06:45:05 PM »
Unfortunately, XMLHttpRequest is restricted so that it can only request data from the server that fed the original file that included the JavaScript  (to prevent XSS attacks) . So, if this is running on your desktop, you won't be able to pull the data from the gateway.

The only way around this is for the web server that feeds the xml to proxy requests to the Gateway as well.  If you are using Apache HTTP, this can be done w/ ProxyPass.

If you google "XMLHttpRequest" and "ProxyPass" a bunch of examples on how to configure this will show up, including this one.
« Last Edit: April 14, 2011, 07:59:57 PM by TedDev »

rotus8

  • Sr. Member
  • ****
  • Posts: 315
  • Karma: +0/-0
Re: JavaScript to access TED
« Reply #2 on: April 15, 2011, 03:47:14 AM »
You can do it without a proxy by adding a snippet of php code.

Here's an example:
                     <?php
                        $data = simplexml_load_file('http://TED5000/api/LiveData.xml');
                        print ("<tr>");
                        print ("  <td>Solar Generation</td>");
                        print ("  <td><h4>" . $data->Power[0]->MTU2->PowerNow/-1000 . " KW</h4></td>");
                        print ("</tr>");
                        print ("<tr>");
                        print ("  <td>Peak Today</td>");
                        print ("  <td><h4>" . $data->Power[0]->MTU2->PeakTdy/-1000 . " KW</h4></td>");
                        print ("</tr>");
                        print ("<tr>");
                        print ("  <td>House Consumption</td>");
                        print ("  <td><h4>" . $data->Power[0]->MTU1->PowerNow/1000 . " KW</h4></td>");;
                        print ("</tr>");
                        print ("<tr>");
                        print ("  <td>Net Usage</td>");
                        print ("  <td><h4>" . $data->Power[0]->Total->PowerNow/1000 . " KW</h4></td>");
                        print ("</tr>");
                        print ("<tr>");
                        print ("  <td>Sample Time</td>");
                        print ("  <td><h4 id=\"sampTime\"></h4></td>");
                        print ("</tr>");
                        //get the sample time
                        print ("<script type=\"text/javascript\">");
                        print ("  var h=" . $data->GatewayTime[0]->Hour . ";");
                        print ("  var m=" . $data->GatewayTime[0]->Minute . ";");
                        print ("  var s=" . $data->GatewayTime[0]->Second . ";");

                      ?>

There are a couple of prerequisites - this has to be served by a server that supports php, and you need to name your source xxx.php, not xxx.html.
« Last Edit: April 15, 2011, 03:49:00 AM by rotus8 »

Banana_Slug

  • Newbie
  • *
  • Posts: 6
  • Karma: +0/-0
Re: JavaScript to access TED
« Reply #3 on: April 16, 2011, 04:20:24 AM »
Thanks, I am slowly beginning to understand (or am I?). So am I correct in thinking that I have to execute my JavaScript on the same IP as I am accessing? In my case since the TED is at 192.168.1.7 and my desktop is at 192.168.1.2 I will not be able to access. But here's where my confusion continues. How does a Windows Gadget get around this? I am thinking of the  Watt-Cost gadget. Does it do all the following you have referred to?

Unfortunately, XMLHttpRequest is restricted so that it can only request data from the server that fed the original file that included the JavaScript  (to prevent XSS attacks) . So, if this is running on your desktop, you won't be able to pull the data from the gateway.

The only way around this is for the web server that feeds the xml to proxy requests to the Gateway as well.  If you are using Apache HTTP, this can be done w/ ProxyPass.

If you google "XMLHttpRequest" and "ProxyPass" a bunch of examples on how to configure this will show up, including this one.

Banana_Slug

  • Newbie
  • *
  • Posts: 6
  • Karma: +0/-0
Re: JavaScript to access TED
« Reply #4 on: April 16, 2011, 04:23:16 AM »
Thanks, but I am trying to get this done using JavaScript.  I have seen reference to PHP and I am not yet ready to learn yet another language.

TedDev

  • TED Software Developent
  • Administrator
  • Full Member
  • *****
  • Posts: 158
  • Karma: +0/-0
Re: JavaScript to access TED
« Reply #5 on: April 16, 2011, 06:36:08 AM »
Thanks, I am slowly beginning to understand (or am I?). So am I correct in thinking that I have to execute my JavaScript on the same IP as I am accessing? In my case since the TED is at 192.168.1.7 and my desktop is at 192.168.1.2 I will not be able to access. But here's where my confusion continues. How does a Windows Gadget get around this? I am thinking of the  Watt-Cost gadget. Does it do all the following you have referred to?

That's correct.....XMLHttpRequest can only retrieve data from the same IP it was served on. Technically, the Javascript is still running locally on your PC, but the browser checks the server it orginated from. The PHP example or the Apache ProxyPass example basically hides the TED behind the same IP as the server, so the browser doesn't know its coming from a different device.

Banana_Slug

  • Newbie
  • *
  • Posts: 6
  • Karma: +0/-0
Re: JavaScript to access TED
« Reply #6 on: April 23, 2011, 05:13:36 AM »
Ok I have it working in Windows gadgets. Can it be because of a security setting? I basically stole a windows gadget that was talking to another IP  I changed it to get my TEDs and it's working as a windows gadget. The only thing I found that may be why I am getting access is in gadget.xml I find an entry <permissions>Full</permissions>. Maybe this is it

<?xml version="1.0" encoding="utf-8" ?>
<gadget>
  <name>Home Power</name>
  <namespace>windows.sdk</namespace>
  <version>1.0.0.0</version>
  <author name="Al S">
    <info  />
    <logo src="/images/logo.png" />
  </author>
  <copyright>&#169; 2011</copyright>
  <description>Power reported by the available TED5000s</description>
  <icons>
    <icon height="48" width="48" src="/images/icon.png" />
  </icons>
  <hosts>
    <host name="sidebar">
      <base type="HTML" apiVersion="1.0.0" src="HouseMon.html" />
      <permissions>Full</permissions>
      <platform minPlatformVersion="1.0" />
      <defaultImage src="/images/icon.png" />
    </host>
  </hosts>
</gadget>

sandeen

  • Newbie
  • *
  • Posts: 6
  • Karma: +0/-0
Re: JavaScript to access TED
« Reply #7 on: May 04, 2011, 07:11:30 PM »
Unfortunately, XMLHttpRequest is restricted so that it can only request data from the server that fed the original file that included the JavaScript  (to prevent XSS attacks) .

JSON/JSONP would sure be nice as an interface ... hint hint :)