Page 4 of 4 FirstFirst ... 234
Results 31 to 40 of 40

Thread: web server; sending events and polling lua variables

  1. #31
    Join Date
    Oct 2005
    Posts
    307

    Default

    Ok, the variable is undefined. If you look in the variable inspector in Girder, is either AD.Value[#14] or AD.Value['14'] defined?

  2. #32
    Join Date
    Aug 2004
    Location
    The Netherlands (Norg, near Assen)
    Posts
    625

    Default

    It is AD.Value[#14] and with data="AD.Value[#14]" I also get the 'Variable' not defined error.

    Her is the code I use

    Code:
    //** Ajax Query **/
    
    function checkGIRDER(tablename) {
            document.getElementById("UpdateStatus").className = 'orangeLight';
        $.ajax({
            type: "GET",
            url: "ajaxreqjson.lhtml?Data=" + escape( tablename ),
            data: null,
            async: true,
            cache: false,
            timeout:3000,
            success: QuerySuccess,
            error: QueryError
        });
    
    }
    
    
    <script type="text/javascript">
        
        QueryData="Last.CPUUsage,AD.Value[#14]";
        QueryRefresh=5000
        AutoUpdate=false
        
        function ProcessData(jsonOBJ) {
                $("#test").text("Last.CPUUsage : "+jsonOBJ['Last.CPUUsage']);
                $("#test2").text("Last.CPUUsage : "+jsonOBJ['AD.Value[#14]']);
        }
    
    
    
    
    </script>
    
    
    <div id="attention"></div>
    <div id="UpdateStatus" class="blackLight" onclick="ToggleUpdate()"></div>
    
    
    <div id="test"></div>
    <div id="test2"></div>


    Is my code the problem (because I changed it al little bit) or is ther something else wrong?
    Geert

  3. #33
    Join Date
    Oct 2005
    Posts
    307

    Default

    Please add your complete .html file.

  4. #34
    Join Date
    Aug 2004
    Location
    The Netherlands (Norg, near Assen)
    Posts
    625

    Default

    This part was missing


    Code:
    //** Ajax Query **/
    function checkGIRDER(tablename) {
            document.getElementById("UpdateStatus").className = 'orangeLight';
    	$.ajax({
    		type: "GET",
    		url: "ajaxreqjson.lhtml?Data=" + escape( tablename ),
    		data: null,
    		async: true,
    		cache: false,
    		timeout:3000,
    		success: QuerySuccess,
    		error: QueryError
    	});
    }
    
    
    function QueryError(XMLHttpRequest, textStatus, errorThrown){
    		$("#attention").text("Timeout ERROR: " + textStatus + " (" + errorThrown + ")");
    
    
    		if (AutoUpdate==true) {
    	        clearTimeout(theTimer); 
                theTimer=setTimeout('checkGIRDER(QueryData)',QueryRefresh);
                setTimeout("document.getElementById('UpdateStatus').className = 'greenLight'",100);
            }
            else {
                document.getElementById('UpdateStatus').className = 'blackLight';
            }
    }
    
    
    function QuerySuccess(data){
            //**alert(data)/**
    		try { 
    			jsonOBJ = jQuery.parseJSON(data);
                ProcessData(jsonOBJ);
            }
                catch(err) {$("#attention").text("Error: "+err.message+"["+data+"]");
            }
            
    		if (AutoUpdate==true) {
    	        clearTimeout(theTimer); 
                theTimer=setTimeout('checkGIRDER(QueryData)',QueryRefresh);
                setTimeout("document.getElementById('UpdateStatus').className = 'greenLight'",100);
            }
            else {
                document.getElementById('UpdateStatus').className = 'blackLight';
            }
    }

    This is the test-HTML page

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="apple-touch-icon" href="img/touchicon.png">
    <link rel="stylesheet" type="text/css" href="main.css" />
    <script type="text/javascript" language="javascript" src="js/main.js"></script>
    <script type="text/javascript" language="javascript" src="js/jquery.js"></script>
    <title>Home Control</title>
    </head>
    <body>
    <h1 id="pageTitle">Test</h1>
    <a id="homeButton" class="button" href="index.lhtml">Home</a>
    <div id="status">
    <a href="Test.lhtml">Up: 71:33 u:m<br>195.241.0.47<br>15:45:59</a>
    </div>
    <script type="text/javascript">
    QueryData="Last.CPUUsage,AD.Value[#14]";
    QueryRefresh=5000
    AutoUpdate=false
    function ProcessData(jsonOBJ) {
    $("#test").text("Last.CPUUsage : "+jsonOBJ['Last.CPUUsage']);
    $("#test2").text("Test : "+jsonOBJ['AD.Value[#14]']);
    }
    </script>
    <div id="attention"></div>
    <div id="UpdateStatus" class="blackLight" onclick="ToggleUpdate()"></div>
    <div id="test"></div>
    <div id="test2"></div>
    <hr><br><center>
    <input type="button" value="Home" onClick="parent.location='index.lhtml'">
    <input type="button" value="Refresh" onClick="parent.location='Test.lhtml'">
    <input type="button" value="Update" onClick="update()">
    </center>
    <br><br>
    </body>
    </html>
    Geert

  5. #35
    Join Date
    Oct 2005
    Posts
    307

    Default

    I found the issue, but had to get some help with the fix. It turns out the function getfield within the original distributed ajaxreq.lhtml has two issues (and ajaxreqjson.lhtml is based on ajaxreq.lhtml):
    1) A table can only be 9 levels deep (A 'level' is going to be anytime there is a . or []'s). This hasn't been a problem for me, but I'll fix it for anyone that needs it fixed...
    2) Only strings can be passed as table names.

    If you replace getfield in ajaxreqjson.lhtml with:

    function getfield (f)
    local v = _G
    for w in string.gfind(f, "['%w_]+") do
    if (string.find(w,"['%a_]")==nil) then
    w = loadstring('return '..w)()
    else
    w = string.gsub(w, "'", "")
    end
    v=v[w]
    end
    return v
    end

    This will allow you request AD.Value[14] and get a proper response. To know when you need to use AD.Value['14'] vs. AD.Value[14] just look in Girder's variable inspector. If the variable is AD ->Value ->#14, then you don't use quotes. If it is AD -> Value -> 14, use quotes...
    Last edited by shaun5; February 18th, 2012 at 04:42 PM.

  6. #36
    Join Date
    Aug 2004
    Location
    The Netherlands (Norg, near Assen)
    Posts
    625

    Default

    Glad you found a solution (and glad I am not crazy).
    I tried it and it works fine here now. Now i have an almost fully operational Home Control webapplication working on my iPhone.
    Thanks again.
    Geert

  7. #37

    Default

    Hi,

    I have tried this myself and it partly works for me.
    I request data from two tables, "weatherdata" and "fifo_temp" and I get the following data to the client,
    {"weatherdata":{"Humidity_Int":22,"DewPoint_Ext":6 ,"TimeStamp":"24.02.2012/10:01:00","Humidity_Ext":85,"Temp_Ext":8,"Temp_Int ":26.1,"DewPoint_Int":3}," fifo_temp":[7.9,7.3,7.2,6.9,6.6,6.9]}

    Extracting data from the "weatherdata" table is no problem but I can not figure out how to get the data from the "fifo_temp" table.
    Please see picture below for table layout,

    Click image for larger version. 

Name:	fifo_temp.JPG 
Views:	105 
Size:	10.5 KB 
ID:	6409

    Any ideas what I need to do?

    Thanks & Regards

    Rickard

  8. #38

    Default

    another piece of informtion that might be important is that I do the following to publish the data to the page,

    Code:
    function checkDATA(data) {
                    alert(data)
                               	try {
    				jsonOBJ = jQuery.parseJSON(data);
    				document.getElementById("Utetemp").innerHTML=(jsonOBJ.weatherdata.Temp_Ext + " &deg;" + "C");
                                    document.getElementById("Innetemp").innerHTML=(jsonOBJ.weatherdata.Temp_Int + " &deg;" + "C");
                                    document.getElementById("Utetemp1").innerHTML=(jsonOBJ.weatherdata.DewPoint_Ext + " &deg;" + "C");
                                    document.getElementById("Innetemp1").innerHTML=(jsonOBJ.weatherdata.DewPoint_Int + " &deg;" + "C");
                                    document.getElementById("Utetemp2").innerHTML=(jsonOBJ.weatherdata.Humidity_Ext + " %");
                                    document.getElementById("Innetemp2").innerHTML=(jsonOBJ.weatherdata.Humidity_Int + " %");
                                    document.getElementById("TimeStamp").innerHTML=("<small><small><small>" + jsonOBJ.weatherdata.TimeStamp + "</small></small></small>");
    I had to do it this way to get it working. I am new to this So I have no clue why?

  9. #39
    Join Date
    Oct 2005
    Posts
    307

    Default

    The forum has formatted your fifo_temp as " fifo_temp". If this is truely the case:

    jsonOBJ[' fifo_temp'][0] is the first element.

    Otherwise without the space:

    jsonOBJ.fifo_temp[0] is the first element.

  10. #40

    Default

    Thansk for having a look at this.
    There should not be a space in the table name and it was.

    Was,
    Code:
    data: {Data : "weatherdata, fifo_temp"},
    Should have been,
    Code:
    data: {Data : "weatherdata,fifo_temp"},
    This is working perfectly now

    Regards

Page 4 of 4 FirstFirst ... 234

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •