<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <title>Status display</title>
  <meta name="description" 
          content="Status display generated by BORA.">
  <link rel="stylesheet" href="{{ static_url("style.css") }}">
  <link rel="stylesheet" href="{{ static_url("jquery-ui.min.css") }}">
  <!--[if IE]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  <![endif]-->
</head>

<body>

<div id="page_info">{{ data['title'] }} data monitoring. Last refreshed</div>
<div id="page_title_cache" style="display:none;">{{ data['title'] }}</div>
    
{% if data['style'] %}
{% for key in data['style'] %}

{% if data['style'][key]['type'] == "calc" %}
<div style='position: absolute; top: {{ data['style'][key]['top'] }}; left:{{
    data['style'][key]['left'] }}; width:{{ data['style'][key]['width'] }}px;
    height:{{ data['style'][key]['height'] }}px;' 
{% if "condition" in data['style'][key] %}
 data-cond="{{ data['style'][key]['condition'] }}"
{% else%}position
{% end%}
{% if "formula" in data['style'][key] %}
 data-formula="{{ data['style'][key]['formula'] }}" tooltip="{{ data['style'][key]['formula'] }}"
{% else %}
{% end %}
{% if "lesser" in data['style'][key] %}
 data-lesser="{{ data['style'][key]['lesser'] }}"
{% else %}
{% end %}
{% if "larger" in data['style'][key] %}
 data-larger="{{ data['style'][key]['larger'] }}"
{% else %}
{% end %}
 data-type="{{ data['style'][key]['type'] }}" class='databox' id="{{ key }}">
<p><span style="color: #000; font-size: {{ data['style'][key]['header']['size'] }}px; font-weight: {{ data['style'][key]['header']['weight'] }};" class="title">{{ data['style'][key]['header']['title'] }} </span><span style="color: #000; font-size: {{ data['style'][key]['unit']['size'] }}px; font-weight: {{ data['style'][key]['unit']['weight'] }};" class="varval"> --- </span><span class='unit_title' style="color: #000; font-size: {{ data['style'][key]['unit']['size'] }}px; font-weight: {{ data['style'][key]['unit']['weight'] }};" > {{ data['style'][key]['unit']['title'] }}</span></p>
</div>
{% else %}
{% end %}
    
{% if data['style'][key]['type'] == "data" %}
{% if "ref" in data['style'][key] %}
<div tooltip="{{ data['style'][key]['ref'] }}" 
{% else %}
<div tooltip="{{ key }}" 
{% end %}
class="databox" style='position: absolute; top: {{ data['style'][key]['top'] }}; left:{{ data['style'][key]['left'] }}; width:{{ data['style'][key]['width'] }}px; height:{{ data['style'][key]['height'] }}px;' 
{% if "condition" in data['style'][key] %}
data-cond="{{ data['style'][key]['condition'] }}"
{% else%}
{% end%}
{% if "formula" in data['style'][key] %}
data-formula="{{ data['style'][key]['formula'] }}"
{% else %}
{% end %}
{% if "lesser" in data['style'][key] %}
data-lesser="{{ data['style'][key]['lesser'] }}"
{% else %}
{% end %}
{% if "larger" in data['style'][key] %}
data-larger="{{ data['style'][key]['larger'] }}"
{% else %}
{% end %}
{% if "exponential" in data['style'][key] %}
 data-exponential="{{ data['style'][key]['exponential'] }}"
{% else %}
{% end %}
{% if "ref" in data['style'][key] %}
data-type="{{ data['style'][key]['type'] }}" onclick="window.open('http://katrin.kit.edu/adei-katrin/#module=graph&{{ data['varname'][data['style'][key]['ref']] }}&experiment=-&window=86400&module=graph&virtual=srctree&srctree=&infomod=legend', '_blank');" id="{{ key }}">
<p><span style="color: #000; font-size: {{ data['style'][key]['header']['size'] }}px; font-weight: {{ data['style'][key]['header']['weight'] }};" class="title">{{ data['style'][key]['header']['title'] }} </span><span style="color: #000; font-size: {{ data['style'][key]['unit']['size'] }}px; font-weight: {{ data['style'][key]['unit']['weight'] }};" class="varval"> --- </span><span class='unit_title' style="color: #000; font-size: {{ data['style'][key]['unit']['size'] }}px; font-weight: {{ data['style'][key]['unit']['weight'] }};"> {{ data['style'][key]['unit']['title'] }}</span></p>
</div>
{% else %}
data-type="{{ data['style'][key]['type'] }}" onclick="window.open('http://katrin.kit.edu/adei-katrin/#module=graph&{{ data['varname'][key]}}&experiment=-&window=86400&module=graph&virtual=srctree&srctree=&infomod=legend', '_blank');" id="{{ key }}">
<p><span style="color: #000; font-size: {{ data['style'][key]['header']['size'] }}px; font-weight: {{ data['style'][key]['header']['weight'] }};" class="title">{{ data['style'][key]['header']['title'] }} </span><span style="color: #000; font-size: {{ data['style'][key]['unit']['size'] }}px; font-weight: {{ data['style'][key]['unit']['weight'] }};" class="varval"> --- </span><span class='unit_title' style="color: #000; font-size: {{ data['style'][key]['unit']['size'] }}px; font-weight: {{ data['style'][key]['unit']['weight'] }};"> {{ data['style'][key]['unit']['title'] }}</span></p>
</div>
{% end %}

{% else %}
{% end %}


{% if data['style'][key]['type'] == "ventil" %}
<div tooltip="{{ key }}" style='position: absolute; top: {{ data['style'][key]['top'] }}; left:{{ data['style'][key]['left'] }}; width:{{ data['style'][key]['width'] }}px; height:{{ data['style'][key]['height'] }}px;' data-type="{{ data['style'][key]['type'] }}" data-off="{{ data['style'][key]['off'] }}" data-on="{{ data['style'][key]['on'] }}" onclick="window.open('http://katrin.kit.edu/adei-katrin/#module=graph&{{ data['varname'][key]}}&experiment=-&window=86400&module=graph&virtual=srctree&srctree=&infomod=legend', '_blank');" class='databox' id="{{ key }}">
<img width='100%' height='100%' src='{{ static_url("ventil_inactive.png") }}'></img>
</div>
{% else %}
{% end %}

{% if data['style'][key]['type'] == "integer-to-string" %}
<div tooltip="{{ key }}" style='position: absolute; top: {{
    data['style'][key]['top'] }}; left:{{ data['style'][key]['left'] }};
    width:{{ data['style'][key]['width'] }}px; height:{{
    data['style'][key]['height'] }}px;' 
{% if "cond" in data['style'][key] %}
data-cond="{{ data['style'][key]['cond'] }}"
{% else %}
{% end %}
 data-dict="{{ data['style'][key]['dict'] }}" data-type="{{ data['style'][key]['type'] }}" onclick="window.open('http://katrin.kit.edu/adei-katrin/#module=graph&{{ data['varname'][key]}}&experiment=-&window=86400&module=graph&virtual=srctree&srctree=&infomod=legend', '_blank');" class='databox' id="{{ key }}">
<p><span style="color: #000; font-size: {{ data['style'][key]['header']['size'] }}px; font-weight: {{ data['style'][key]['header']['weight'] }};" class="title">{{ data['style'][key]['header']['title'] }} </span><br /><span style="color: #000; font-size: {{ data['style'][key]['unit']['size'] }}px; font-weight: {{ data['style'][key]['unit']['weight'] }};" class="varval">--- </span></p>
</div>
{% else %}
{% end %}

{% if data['style'][key]['type'] == "header" %}
<div style='position: absolute; top: {{ data['style'][key]['top'] }}; left:{{
    data['style'][key]['left'] }}; width:{{ data['style'][key]['width'] }}px;
    height:{{ data['style'][key]['height'] }}px;' data-type="{{
    data['style'][key]['type'] }}" class='databox' id="{{ key }}">
<p><span style="color: #000; font-size: {{ data['style'][key]['header']['size'] }}px; font-weight: {{ data['style'][key]['header']['weight'] }};" class="title">{{ data['style'][key]['header']['title'] }} </span></p>
</div>
{% else %}
{% end %}


{% end %}
{% else %}
{% end %}



{% if "background" in data %}
<img src="{{ static_url( data['background'] ) }}"></img>    
{% else %}
<img src="{{ static_url( 'background.png' ) }}"></img>
{% end %}

   
<!-- java script -->
<script src="{{ static_url("jquery-1.12.3.min.js") }}"></script>
<script src="{{ static_url("jquery-ui.min.js") }}"></script>
<script src="{{ static_url("math.min.js") }}"></script>
<script>
jQuery(window).load(function () {

myTimer();
var myVar = setInterval(myTimer, 10000);

function myTimer() {
    var dataToSend = {};
    $.ajax({
        url: '/getdata/',
        type: 'GET',
        data: dataToSend,
        success: function (response) {
            //var objresponse = JSON.parse(response);
            console.log(response);
            for (var key in response) {
                //console.log(key, response[key]); 
                data_type = $("#" + key).attr('data-type');
                if($("#" + key).length > 0) {
                    if (data_type == "data") {
                        //console.log("data");
                        var datavalue, set_color, formula;
                        var formula_attr = $("#" + key).attr('data-formula');
			            if (response[key]) {
                            if (formula_attr) {
                                datavalue = parseFloat(response[key]).toFixed(2);
 			                    formula = formula_attr.replace("x", datavalue.toString());
                                datavalue = math.round(math.eval(formula),2);
                                datavalue = datavalue.toFixed(2);
		    	            } else {
                                if ( ($("#" + key).attr('data-exponential')) == "True" || ($("#" + key).attr('data-exponential')) == "true" ) {
                                    datavalue = parseFloat(response[key]);
                                    datavalue = datavalue.toExponential(2)
                                } else {
                                    datavalue = parseFloat(response[key]).toFixed(2);
                                }
                            }
 		    	            set_color = "green";
		                } else {
			                datavalue = "---";
			                set_color = "black";
		                }
                        $(".varval", "#" + key).text(datavalue);
                        var condition_attr = $("#" + key).attr('data-cond');
                        var lesser_attr = $("#" + key).attr('data-lesser');
                        var larger_attr = $("#" + key).attr('data-larger');
  		    	        //console.log(condition_attr);
                        if (condition_attr) {
 			    if (response[key] == condition_attr) {
 			        set_color = "red";
      			    }
		    	}
 		    	if (lesser_attr) {
 			    if (parseFloat(response[key]) < parseFloat(lesser_attr)) {
 			    	set_color = "red";
      			    }
		    	}
  		        if (larger_attr) {
 			    if (parseFloat(response[key]) > parseFloat(larger_attr)) {
 			    	set_color = "red";
      			    }
		    	}
		    	$(".varval", "#" + key).css("color", set_color);
		    	$(".unit_title", "#" + key).css("color", set_color);
                    
		    } else if (data_type == "ventil") {
                        //console.log("ventil");
                        var on_attr = $("#" + key).attr('data-on');
                        var off_attr = $("#" + key).attr('data-off');
 			if (parseFloat(response[key]) == parseFloat(on_attr)) {
 			    $("img", "#" + key).attr("src", "{{ static_url('ventil_green.png') }}");
      			}
 			if (parseFloat(response[key]) == parseFloat(off_attr)) {
 			    $("img", "#" + key).attr("src", "{{ static_url('ventil_red.png') }}");
      			}
		    } else if (data_type == "integer-to-string") {
                        //console.log("integer-to-string");
                        var dict_attr = JSON.parse($("#" + key).attr('data-dict'));
                        var dict_cond;
                        if ($("#"+key).attr("data-cond")) {
                            dict_cond = JSON.parse($("#" + key).attr('data-cond'));
                        } else {
                            dict_cond = {};
                        }
                        var dict_color = "";
                        if (key in response) {
                            var token = response[key];
                            if (token in dict_cond) {
                                dict_color = dict_cond[token];
                            }
                        }
                        $(".varval", "#" + key).css("color", dict_color);
                        $(".varval", "#" + key).text(dict_attr[response[key]]);
		    }
		}
            }
            $('[id^=virtual_]').each(function( index ) {
                var key = this.id;
                var ref = key.split("_");
                ref = ref[1];
                data_type = $("#" + key).attr('data-type');
                if($("#" + key).length > 0) {
                    if (data_type == "data") {
                        console.log(key);
                        var datavalue, set_color, formula;
                        var formula_attr = $("#" + key).attr('data-formula');
			            if (response[ref]) {
                            if (formula_attr) {
                                datavalue = parseFloat(response[ref]).toFixed(2);
 			                    formula = formula_attr.replace("x", datavalue.toString());
                                datavalue = math.round(math.eval(formula),2);
                                datavalue = datavalue.toFixed(2);
		    	            } else {
			                    datavalue = parseFloat(response[ref]).toFixed(2);
                            }
 		    	            set_color = "green";
			            } else {
			                datavalue = "---";
			                set_color = "black";
			            }
                        $(".varval", "#" + key).text(datavalue);
                        var condition_attr = $("#" + key).attr('data-cond');
                        var lesser_attr = $("#" + key).attr('data-lesser');
                        var larger_attr = $("#" + key).attr('data-larger');
  		    	        //console.log(condition_attr);
                        if (condition_attr) {
 			                if (response[ref] == condition_attr) {
 			    	            set_color = "red";
      			            }
		    	        }
 		    	        if (lesser_attr) {
 			                if (parseFloat(response[ref]) < parseFloat(lesser_attr)) {
 			    	            set_color = "red";
      			            }
		    	        }
  		                if (larger_attr) {
 			                if (parseFloat(response[ref]) > parseFloat(larger_attr)) {
 			    	            set_color = "red";
      			            }
		    	        }
		    	        $(".varval", "#" + key).css("color", set_color);
		    	        $(".unit_title", "#" + key).css("color", set_color);
                    
		            } else if (data_type == "ventil") {
                        var on_attr = $("#" + key).attr('data-on');
                        var off_attr = $("#" + key).attr('data-off');
 			            if (parseFloat(response[ref]) == parseFloat(on_attr)) {
 			                $("img", "#" + key).attr("src", "{{ static_url('ventil_green.png') }}");
      			        }
 			            if (parseFloat(response[ref]) == parseFloat(off_attr)) {
 			                $("img", "#" + key).attr("src", "{{ static_url('ventil_red.png') }}");
      			        }
		            } else if (data_type == "integer-to-string") {
                        var dict_attr = JSON.parse($("#" + key).attr('data-dict'));
                        var dict_cond;
                        if ($("#"+key).attr("data-cond")) {
                            dict_cond = JSON.parse($("#" + key).attr('data-cond'));
                        } else {
                            dict_cond = {};
                        }
                        var dict_color = "";
                        if (ref in response) {
                            var token = response[ref];
                            if (token in dict_cond) {
                                dict_color = dict_cond[token];
                            }
                        }
                        $(".varval", "#" + key).css("color", dict_color);
                        $(".varval", "#" + key).text(dict_attr[response[ref]]);
		            }
		        }
            });
            
            $('[id^=calc_]').each(function( index ) {
                console.log(this.id);
                console.log($(this).attr('data-formula'));
                var myformula = $(this).attr('data-formula').trim();
                /*
                myformula = myformula.replace(/\[.*?\]/g, '__');
                console.log(myformula);
                */
                var data = myformula;
                var re= /\[(.*?)\]/g;
                var buffer = []
                for(m = re.exec(data); m; m = re.exec(data)){
                    buffer.push(m[1]);
                }
                console.log("Debug:");
                console.log(buffer);
                var finalstring = "";
                var pattern = /\[(.*?)\]/g;
                var teststring = myformula.split(/[\[\]]+/);
                console.log(teststring);
                console.log(finalstring);
                for (var i = 0; i < teststring.length; i++) {
                    var val = "";
                    
                    if (teststring[i] in response) {
                        val = response[teststring[i]];
                    } else {
                        val = teststring[i];
                    }

                    finalstring += val;
                    finalstring += " ";
                }
                finalstring = finalstring.trim();
                console.log(finalstring);
                var myvalue = math.round(math.eval(finalstring),2);
                myvalue = myvalue.toFixed(2);
 		    	set_color = "green";
                console.log(myvalue);
                $(".varval", "#" + this.id).text(myvalue);
                var condition_attr = $("#" + this.id).attr('data-cond');
                var lesser_attr = $("#" + this.id).attr('data-lesser');
                var larger_attr = $("#" + this.id).attr('data-larger');
  		        //console.log(condition_attr);
                if (condition_attr) {
 			        if (myvalue == condition_attr) {
 			            set_color = "red";
      		        }
		        }
 		    	if (lesser_attr) {
 			        if (parseFloat(myvalue) < parseFloat(lesser_attr)) {
 			            set_color = "red";
      		        }
		        }
  		        if (larger_attr) {
 			        if (parseFloat(myvalue) > parseFloat(larger_attr)) {
 			            set_color = "red";
      			    }
		        }
		        $(".varval", "#" + this.id).css("color", set_color);
		        $(".unit_title", "#" + this.id).css("color", set_color);
                
            });
            var page_title_cache = $("#page_title_cache").text();
	    var datetime = page_title_cache + " data monitoring page. Last Sync: " + response["time"];
   	    $("#page_info").text(datetime);
            console.log(datetime);
        },
        error: function () {
            console.log("Error.")
        }
    });
}

});


</script>
</body>
</html>
