/*
 
 * Red Pitaya TCPSOCKET service manager
 
 *
 
 * Author: Artem Kokos <a.kokos@integrasources.eu>
 
 *
 
 * (c) Red Pitaya  http://www.redpitaya.com
 
 *
 
 */
 
 
 
function   parseIP(msg) {
 
 
 
            var res = msg.responseText.split(";");
 
            var ethIP  = res[0].match(/inet([\s\S]*?)brd/);
 
            var wlanIP = res[1].match(/inet([\s\S]*?)brd/);
 
 
 
            if (ethIP != null){
 
                ethIP = ethIP[0].split(" ")[1].split("/")[0];
 
                $('#ip-addr').text(ethIP);
 
            }
 
            else if (wlanIP != null){
 
                wlanIP = wlanIP[0].split(" ")[1].split("/")[0];
 
                $('#ip-addr').text(wlanIP);
 
            }
 
            else $('#ip-addr').text( window.location.host );
 
 
 
            
 
}
 
 
 
 
 
(function(TCPSOCKET, $, undefined) {
 
    TCPSOCKET.CheckServerStatus = function() {
 
        $.ajax({
 
                url: '/get_tcpsocket_status',
 
                type: 'GET',
 
                timeout: 1500
 
            })
 
            .fail(function(msg) {
 
                if (msg.responseText.split('\n')[0] == "active") {
 
                    $('#TCPSOCKET_RUN').hide();
 
                    $('#TCPSOCKET_STOP').css('display', 'block');
 
                    $('#label-is-runnung').hide();
 
                    $('#label-is-not-runnung').show();
 
                } else {
 
                    $('#TCPSOCKET_STOP').hide();
 
                    $('#TCPSOCKET_RUN').css('display', 'block');
 
                    $('#label-is-not-runnung').hide();
 
                    $('#label-is-runnung').show();
 
                }
 
            })
 
    }
 
 
 
    TCPSOCKET.StartServer = function() {
 
        $.ajax({
 
                url: '/start_tcpsocket_manager',
 
                type: 'GET',
 
                timeout: 2500
 
            })
 
            .fail(function(msg) {
 
                if (msg.responseText) {} else {}
 
            })
 
    }
 
 
 
    TCPSOCKET.StopServer = function() {
 
        $.ajax({
 
                url: '/stop_tcpsocket_manager',
 
                type: 'GET',
 
                timeout: 2500
 
            })
 
            .fail(function(msg) {
 
                if (msg.responseText) {} else {}
 
            })
 
    }
 
 
 
    TCPSOCKET.GetIP = function() {
 
        $.ajax({
 
            url: '/get_rpip',
 
            type: 'GET',
 
        }).fail( parseIP ).done( parseIP ) 
 
    }
 
 
 
}(window.TCPSOCKET = window.TCPSOCKET || {}, jQuery));
 
 
 
 
 
 
 
 
 
// Page onload event handler
 
$(function() {
 
 
 
    // Init help
 
    Help.init(helpListTCPSOCKET);
 
    Help.setState("idle");
 
 
 
    TCPSOCKET.CheckServerStatus();
 
    setInterval(TCPSOCKET.GetIP, 3000);
 
    setInterval(TCPSOCKET.CheckServerStatus, 3333);
 
 
 
    $('#TCPSOCKET_RUN').click(TCPSOCKET.StartServer);
 
 
 
    $('#TCPSOCKET_STOP').click(TCPSOCKET.StopServer);
 
 
 
    $('#TCPSOCKET_EXAMPLES').click(function(){
 
        window.open('http://redpitaya.com/examples-new/','_blank');
 
    });
 
});