chipmunkav.com Home Search Member List Faq Register Login  
Web Server
Re: Feedback to a web page from Chipmunk via AJAX

Thread Starter: GBarnett   Started: 11-13-2006 3:02 AM   Replies: 3
 Chipmunk AV Forums » Plugins » Web Server » Re: Feedback to a web page from Chipmunk via AJAX
 Printable Version    « Previous Thread   Next Thread »
  13 Nov 2006, 3:02 AM
GBarnett is not online. Last active: 15/03/2007 00:32:03 GBarnett

Top 10 Posts
Joined on 11-13-2006
Posts 5
Feedback to a web page from Chipmunk via AJAX
Your examples are great and were quite helpful in understanding the underlying Chipmunk infrastructure. In the index.html AJAX sample, Chipmunk automatically provides feedback that an event was received into the "Feedback" field on the page. I want to create a script that will provide many types of feedback to a web page, such as the current volume level, ON/OFF status of various devices, TV aspect ratio, etc. These values would typically be received from my devices via an RS232 connection.

Are there any examples of how feedback can be sent to specific web page fields/controls at startup or when a macro is executed via AJAX?

  
  17 Nov 2006, 9:45 AM
Chipmunk is not online. Last active: 20/08/2009 20:17:19 Chipmunk



Top 10 Posts
Joined on 07-19-2006
London, England
Posts 167
Re: Feedback to a web page from Chipmunk via AJAX

Sorry for the delay in replying -

The approach to take here would be to extract relevant data received from your RS232 device into Lua global variables. In the RS232 Plugin's Settings dialog, attach a Lua script to parse the incoming data into global variables (ie variables declared without the "local" keyword). This will make the data available to all scripts / plugins. In your Lua Server Page script you can then obtain these variables and format them into XML (see feedback.lsp in the wmp directory).

Take the global variable "ElapsedTime" for example:

Response:Write("<ElapsedTime>"..HttpUtility:HtmlEncode(ElapsedTime).."</ElapsedTime>");

Then create a new ajax javascript modelled on wmp.js found in the scripts directory. Modify it to extract the XML Node value:

xmlhttp.responseXML.getElementsByTagName('ElapsedTime')[0].firstChild.nodeValue;

Whilst Ajax gives the impression that data is being pushed from the server to the web browser, it is actually being requested by the web browser and returned as a response from the server. So the only way to show feedback is for the feedback to be accessible at any given moment for the browser to request. The Web-enabled Windows Media Player scripts do what I think you want to achieve. They will just need a little reworking for your scenario. Instead of requesting Windows Media Player objects, you will be requesting global variables....

I hope this helps. After we have got the November monthly release out the way I would be happy to put together some sample scripts for you...




Chipmunk AV Controller Founder

  
  17 Nov 2006, 2:46 PM
Chipmunk is not online. Last active: 20/08/2009 20:17:19 Chipmunk



Top 10 Posts
Joined on 07-19-2006
London, England
Posts 167
Re: Feedback to a web page from Chipmunk via AJAX

It occurred to me that there is a simpler approach you can take with this. Rather than format the response into XML, you can simply output HTML and dump that into a label on your remote control using Ajax. I've put together a sample for you:

The HTML web page:

<html>
   <head>
      <title>Remote Control With Feedback</title>
      <link href="/style/style.css" type="text/css" rel="stylesheet" media="screen">
   </head>
   <script src="/scripts/remotecontrol.js"></script>
   <script>
      function load() {
         Ajax = new Ajax();
         Ajax.URL = "remotefeedback.lsp";
         Ajax.Interval = 3000;
         Ajax.Feedback = MyFeedback;
         Ajax.Status = MyStatus;
         Ajax.Refresh();
      }
      function MyFeedback(feedback) {
         Connection.innerHTML = feedback;
      }
      function MyStatus(status) {
         Feedback.innerHTML = status;
      }
   </script>
   <body onLoad="load();">
      <div id="Connection"></div>
      <div id="Feedback"></div>
   </body>
</html>

And the remotecontrol.js (ajax script):

function Ajax() {
 var timeoutID;
 var param;
 var polling;
 var ImagePath = '';
 var Interval = 3000;
 var xmlhttp = CreateXMLHttpRequest();
 var URL = "feedback.lsp";

 function CreateXMLHttpRequest() {
  if (window.ActiveXObject) {return new ActiveXObject("Microsoft.XMLHTTP"); }
     if (window.XMLHttpRequest) {return new XMLHttpRequest(); }
  return null;
 }

 this.Start = function() {
  if(!polling)
   polling = setTimeout("Ajax.Refresh()", Ajax.Interval);
 }

 this.Stop = function() {
  clearTimeout(polling);
  polling = null;
  Ajax.Feedback("<font color=red>Disconnected...</font>");
 } 
 
 this.Refresh = function() {
  if(xmlhttp) {
   if(!this.CallInProgress(xmlhttp)) {
    xmlhttp.open("GET", Ajax.URL, true);
    timeoutID = setTimeout('Ajax.OnTimeout()', 5000);
    xmlhttp.onreadystatechange = this.ChipmunkResponse;
    xmlhttp.send(null);
   } else {
    Ajax.Feedback("<font color=red>Slow Connection...</font>");
   }
  } else {
   Ajax.Feedback("<font color=red>Disconnected...</font>");  
  }
  polling = setTimeout("Ajax.Refresh()", Ajax.Interval);
 }

 this.Command = function(query) {
  if(xmlhttp) {
   if(!this.CallInProgress(xmlhttp)) {
    xmlhttp.open("GET", Ajax.URL+"?Action="+query, true);
    timeoutID = setTimeout('Ajax.OnTimeout()', 5000);
    xmlhttp.onreadystatechange = this.ChipmunkResponse;
    xmlhttp.send(null);
   } else {
    Ajax.Feedback("<font color=red>Slow Connection...</font>");
   }
  } else {
   Ajax.Feedback("<font color=red>Disconnected...</font>");  
  }
 }

 this.OnTimeout = function() {
  xmlhttp.abort();
  this.Feedback('<font color=red>Timed Out...</font>');
 }

 this.ChipmunkResponse = function() {
  if(xmlhttp.readyState == 4) {
   window.clearTimeout(timeoutID);
   if (xmlhttp.status==200) {
    if(polling != null)
     Ajax.Feedback("<font color=green>Connected...</font>");
    Ajax.Status(xmlhttp.responseText);
   } else {
    Ajax.Feedback("<font color=red>Connection Lost...</font>");
   }
  }
 }

 this.CallInProgress = function(xmlhttp) {
  if(xmlhttp) {
   switch (xmlhttp.readyState) {
    case 1:
    case 2:
    case 3: return true;
    default: return false;
   }
  } else {
   return false;
  }
 }

 this.Feedback = function(feedback) {}
 this.Status = function(status) {} 

 this.Replace = function(str, old, repl) {
  while((pos = str.indexOf(old)) > -1) {
   str = str.substr(0, pos) + repl + str.substr(pos + old.length);
  }
  return str;
 }
}
var Ajax;


And finally the remotefeedback.lsp page:

<%
Response:SetHeader('Logging', 'Omit');

Response:Write('<span>'..os.date()..'</span>')
Response:Write('<br>')
Response:Write('<span>'.._VERSION..'</span>')
%>

The os.date() Lua function returns the current date/time and _VERSION is a Lua global variable. Just modify the lsp and html pages to your needs. Place the lsp and html pages in the root directory and place remotecontrol.js in the scripts directory. You can adjust the refresh rate by setting the Ajax.Interval property. By default it refreshes the display every 3 seconds. The Response:SetHeader('Logging', 'Omit') line is to prevent the web server logging the request (otherwise the log will rapidly fill up).

You can download these files here:
http://www.chipmunkav.com/support/remotecontrol.zip

Shaun




Chipmunk AV Controller Founder

  
  24 Nov 2006, 9:25 PM
GBarnett is not online. Last active: 15/03/2007 00:32:03 GBarnett

Top 10 Posts
Joined on 11-13-2006
Posts 5
Re: Feedback to a web page from Chipmunk via AJAX
Shaun, this works great. Via global LAU variables, I am now able to change their values in one page within my frames, and poll via another frame page to update the screen. These global variable values will ultimately get changed by my receiver via serail commands sent to Chipmunk via the GC-100 plugin.

At the moment, I'm using three active frames. One of these polls via scripts similar to what you supplied above. The remaining two frame pages communicate with Chipmunk via AJAX, but do not poll. All three seem to be working fine.

  
 Page 1 of 1 (4 items)
Chipmunk AV Forums » Plugins » Web Server » Re: Feedback to a web page from Chipmunk via AJAX

You can add attachments
You can post new topics
You can reply to topics
You can delete your posts
You can edit your posts
You can create polls
You can vote in polls
Forum statistics are enabled
Forum is unmoderated