Home of the Squeezebox™ & Transporter® network music players.
Page 3 of 3 FirstFirst 123
Results 21 to 28 of 28
  1. #21
    Senior Member meep's Avatar
    Join Date
    Aug 2007
    Location
    Ireland
    Posts
    203
    Thanks again to you both for this.

    I managed to get my plugin settings page sending some data to a web service on 'Apply' and refreshing a status field with the server response whenever it arrives.


    ALEXA LMS SKILL: http://www.hab-tunes.com | Twitter: #habtunes
    Personal HA BLOG: http://mediaserver8.blogspot.com

    Squeezebox | Squeezebox Radio x 2 | Squeezebox Duet

  2. #22
    Senior Member
    Join Date
    Jan 2011
    Location
    Germany
    Posts
    226
    Hey meep,

    I am glad that the code helped you.
    May a have a sneek into it?
    I am struggeling with updating HTML while there is a discover process running in the background. Maybe you can now help me ;o)

  3. #23
    Senior Member meep's Avatar
    Join Date
    Aug 2007
    Location
    Ireland
    Posts
    203
    Quote Originally Posted by chincheta0815 View Post
    Hey meep,

    I am glad that the code helped you.
    May a have a sneek into it?
    I am struggeling with updating HTML while there is a discover process running in the background. Maybe you can now help me ;o)
    For sure.

    ive got it wrapped up in a bunch of other stuff now but I'll try to abstract it, comment it up and post here tomorrow.

    this is in a development version that I haven't released to testers yet but if you want a peek at the full thing, pm me your email and I'll add you to the teamwork project portal we're using to co-ordinate testing.


    ALEXA LMS SKILL: http://www.hab-tunes.com | Twitter: #habtunes
    Personal HA BLOG: http://mediaserver8.blogspot.com

    Squeezebox | Squeezebox Radio x 2 | Squeezebox Duet

  4. #24
    Senior Member meep's Avatar
    Join Date
    Aug 2007
    Location
    Ireland
    Posts
    203
    So, in my case, I need to send some data the user enters in the plugin settings page off to a web service. Rather than lock up the LMS waiting for a response from the remote server when the user hits apply, I wanted to fire the HTTP post request as a 'thread', show the user something was happening (an animated gif) and refresh the settings page when a response came back or we timed out. (the whole plugin is based around running a proxy server to accept incoming commands from an Alexa skill)

    I figured I needed some kind of Ajax solution but was dreading implementation as JS is not really my thing. Thanks to the posts and sample code from your self and Michael in this thread, though, I got it working really well and surprisingly quickly.

    I'm not sure if this is exactly what you are looking for, but here's how I set it up;

    First, the CLI setup in initPlugin()

    Code:
    Slim::Control::Request::addDispatch(['hab', 'proxy', 'returnprefs', '?'],[0, 1, 0, \&waitForReturnCompletion]);

    Inside my settings.pm file, I fire off the web request via SimplyAsyncHTTP when the user hits apply;

    Code:
    my $http_prefSend = Slim::Networking::SimpleAsyncHTTP -> new(\&http_prefSend_success, \&http_prefSend_failure);
    $http_prefSend -> post($url, 'Content-Type' => 'application/json', 'Referer' => 'http://'.$publicAddress, $jsonn );
    $returningPrefs = "Contacting skill...";
    The $returningPrefs variable there is used to track the current status of the request. It's default is 'no', indicating that there's nothing to report. Once I fire the request, I set it to "Contacting Skill...". This is trapped in the waitForReturnCompletion() routine in Plugin.pm, specified as the CLI callback (above).;

    Code:
    01: #CLI command handler
    02: sub waitForReturnCompletion {
    03: 
    04:     my $request = shift;
    05: 
    06:     myDebug('Polling for data return from skill. returningPrefs is '.$Plugins::Alexa::Settings::returningPrefs);
    07:     $request->addResult( '_habProxyStatusArea', "no" ); #set default
    08:    
    09: 
    10:     if ($Plugins::Alexa::Settings::returningPrefs ne "no"){ #something has come back from prefs send to skill
    11:     
    12:      if ($Plugins::Alexa::Settings::returningPrefs  eq "Contacting skill...") { # we've just made the call in settings.pm
    13: 
    14:       $request->addResult( '_habProxyStatusArea', "Contacting skill..." ); # in page heading 
    15:       $request->addResult( '_habProxyError', ''); # error area under description
    16:       $request->addResult( '_habProxyProxyStatus',"Checking proxy status..."); # the proxy status text area
    17: 
    18:      }else { # is an OK response or an error...
    19: 
    20:      #determine what should be displayed in top status area
    21:      if ($Plugins::Alexa::Settings::returningPrefs eq "OK") {
    22:        
    23:        $request->addResult( '_habProxyStatusArea', "<span style='color:green;'>IP Details sucessfully registered with skill</span>" );
    24:        my $proxyStatus   = proxyCheck(); # do proxy start / stop tasks
    25:        $request->addResult( '_habProxyProxyStatus', $proxyStatus);
    26:        $request->addResult( '_habProxyError', '');
    27:       
    28:      }else{ # looks like an error back
    29:       
    30:       $request->addResult( '_habProxyStatusArea', "<span style = 'color:red;'>Uh oh. Something went wrong...</span>" );
    31:       $request->addResult( '_habProxyError', "</br>ERROR:</br>There was a problem registering your IP address/port details with the skill. </br>Please check internet connectivity and ensure that the User ID is correct and has been registered through Alexa.</br>The specific error returned is: ".$Plugins::Alexa::Settings::returningPrefs);
    32:          
    33:          #switch off proxy as there's an issue
    34:     if (defined $HabProxy::habproxyproc) {
    35:     HabProxy->disableProxy(); 
    36:    }
    37: 
    38:    $request->addResult( '_habProxyProxyStatus',"Proxy stopped (error)");
    39:      }
    40: 
    41:  }
    42: }
    43: 
    44:     $Plugins::Alexa::Settings::returningPrefs = "no"; # all done, set the var back to no response pending
    45:     $request->setStatusDone();
    46: 
    47: }

    This is where I've added in a lot of my own code. In total, I'm returning up to 3x pieces of information to the settings page;


    • _habProxyStatusArea - text to add to the statusArea section of the settings page header. A 1 line report on the outcome of the operation
    • _habProxyError - a detailed description of anny error that might occur
    • _habProxyProxyStatus - a report on the running/non-running state of my Proxy server


    You can see that the first thing I do is check where there's any change to the returningPrefs variable from the settings.pm file (line 10). If the state is unchanged, nothing is return.

    If the state has changed, it's going to be one of three states;


    • The initial 'contacting still...' setting, in which case pass two strings and no error back to the settings page (lines 14-16)
    • An OK status, in which case pass two strings and no error back to the settings page (lines 23-26)
    • An error, in which case, pass an error status and a detailed error description, stop the proxy and set proxy status accordingly (lines 30-38)


    (the call at line 24 calls a routine that determines if the proxy needs to be started, stopped , restarted etc. and returns a string with the decision)

    Finally, having processed the state defined in the $returnPrefs variable, I set it back to it's default value so this function won't trigger again until something else happens.

    Here's the routine in Settings.pm that processes the response to the HTTP call and sets the variable (not necessary that you absorb this, just know that it happens)

    Code:
    sub http_prefSend_success {
        
        # IP Updated Suvccess
      my $error = shift;
      my $content = $error->content();
        
         if ($content eq "OK") {
           $returningPrefs = "OK";
         }else{
            $returningPrefs = $content;
         }
         
         myDebug($returningPrefs, "info");
     
    }
    You can see it sets the value as either OK or the full content which is likely an error (looking at this now, I should change the if/else structure to simply $returningPrefs = $content )

    Anyway, that's it for the code in the .pm files. Here's the corresponding javascript in basic.html;



    Code:
    01: [% extJsScripts = BLOCK %]
    02: 
    03:     <script TYPE="text/javascript">
    04: 
    05:         Ext.onReady(function(){
    06: 
    07:             new Ext.util.TaskRunner().start({
    08: 
    09:                 run: function(items){
    10: 
    11:                     SqueezeJS.Controller.request({
    12: 
    13:                         params: [ '', [ "hab", "proxy", "returnprefs", "?" ] ],
    14: 
    15:                         success: function(response) {
    16: 
    17:                             response = Ext.util.JSON.decode(response.responseText);
    18: 
    19:                             if (response && response.result) {
    20: 
    21:                              if (response.result['_habProxyStatusArea'] != 'no') {
    22:                               
    23:                                     document.getElementById("spinner").style.visibility = "hidden";
    24:                               document.getElementById("statusarea").innerHTML = response.result['_habProxyStatusArea'];
    25:                              }
    26: 
    27:                              if (response.result['_habProxyStatusArea'] == 'Contacting skill...') {
    28:                               
    29:          document.getElementById("spinner").style.visibility = "visible";
    30:                              }
    31: 
    32: 
    33:                              if (response.result['_habProxyProxyStatus']) {
    34:                               
    35:          document.getElementById("proxyStatus").innerHTML = response.result['_habProxyProxyStatus'];
    36:                              }
    37: 
    38: 
    39:                               if (response.result['_habProxyError']) {
    40:                               
    41:          document.getElementById("errorStatus").innerHTML = response.result['_habProxyError'];
    42:                              }
    43: 
    44: 
    45:                             }
    46: 
    47:                         }
    48: 
    49:                     })
    50: 
    51:                 },
    52: 
    53:                 args: [ Ext.select('.disableOnHueBridgePairing') ],
    54: 
    55:                 interval: 5000
    56: 
    57:             });
    58: 
    59:         });
    60: 
    61:     </script>
    62: 
    63: [% END %]
    This should be familiar. The first relevant part is lines 27-30. This responds to the HTTP request initialization status and makes an animated gif visible to let the user know something is happening. It's set to hidden by default in the basic.html page code;

    Code:
    <span id="spinner" style="visibility:hidden; float:left; margin-left:20px"><img  src="[% webroot %]plugins/Alexa/html/wheels.gif"></span>
    The next two if statements check if there's anything passed in the other two parameters and, if so, set the relevant text by id.

    Then, backing up to lines 21-25, If the _habProxyStausArea value passed back is not the default, this is going to be either a success or failure so I set the visibility of the animation off (we're done) and set the relevant text.

    I hope this makes some sense and helps in some way?

    On a sidenote, I have NO IDEA what line 52 is doing - you'll see it's still got your code in there. Any pointers on what that's about?


    ALEXA LMS SKILL: http://www.hab-tunes.com | Twitter: #habtunes
    Personal HA BLOG: http://mediaserver8.blogspot.com

    Squeezebox | Squeezebox Radio x 2 | Squeezebox Duet

  5. #25
    Babelfish's Best Boy mherger's Avatar
    Join Date
    Apr 2005
    Location
    Switzerland
    Posts
    19,697
    Line 52 is empty. It doesn't do anything.
    Michael

    http://www.herger.net/slim-plugins - MusicArtistInfo, MusicInfoSCR

  6. #26
    Senior Member meep's Avatar
    Join Date
    Aug 2007
    Location
    Ireland
    Posts
    203
    Quote Originally Posted by mherger View Post
    Line 52 is empty. It doesn't do anything.
    You sir, are correct.

    That should have read Line 53. For clarity, this bit;
    Code:
    args: [ Ext.select('.disableOnHueBridgePairing') ],
    Thanks


    ALEXA LMS SKILL: http://www.hab-tunes.com | Twitter: #habtunes
    Personal HA BLOG: http://mediaserver8.blogspot.com

    Squeezebox | Squeezebox Radio x 2 | Squeezebox Duet

  7. #27
    Babelfish's Best Boy mherger's Avatar
    Join Date
    Apr 2005
    Location
    Switzerland
    Posts
    19,697

    Code Explanation Needed: Disable Submit Buttons onWebUI. How to?

    > args: [ Ext.select('.disableOnHueBridgePairing') ],

    These "args" are what the run() function would receive. I've called the
    received parameter "items". Ext.select('.disable...') would return a
    list of all nodes in the DOM with the class .disable... applied. In the
    original code this list would have been used to disable those buttons.
    As you're using getElementById, you could remove the args parameter.

    --

    Michael

  8. #28
    Senior Member meep's Avatar
    Join Date
    Aug 2007
    Location
    Ireland
    Posts
    203
    Quote Originally Posted by mherger View Post
    > args: [ Ext.select('.disableOnHueBridgePairing') ],

    These "args" are what the run() function would receive. I've called the
    received parameter "items". Ext.select('.disable...') would return a
    list of all nodes in the DOM with the class .disable... applied. In the
    original code this list would have been used to disable those buttons.
    As you're using getElementById, you could remove the args parameter.

    --

    Michael
    Thanks, makes sense now.


    ALEXA LMS SKILL: http://www.hab-tunes.com | Twitter: #habtunes
    Personal HA BLOG: http://mediaserver8.blogspot.com

    Squeezebox | Squeezebox Radio x 2 | Squeezebox Duet

Posting Permissions

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