Guest User

Domoticz Dash javascript

a guest
Jan 30th, 2015
544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Zorg ervoor dat links niet extern worden geopend in WebApp
  2. (function(a,b,c){if(c in b&&b[c]){var d,e=a.location,f=/^(a|html)$/i;a.addEventListener("click",function(a){d=a.target;while(!f.test(d.nodeName))d=d.parentNode;"href"in d&&(d.href.indexOf("http")||~d.href.indexOf(e.host))&&(a.preventDefault(),e.href=d.href)},!1)}})(document,window.navigator,"standalone")
  3.  
  4. function lightbox_open(){
  5.     window.scrollTo(0,0);
  6.     $('#light').fadeIn('fast');
  7.     $('#fade').fadeIn('fast');
  8.     return setTimeout(function() {
  9.             // run close function on completion of timeout0-9p
  10.             lightbox_close();
  11.         }, 2000);
  12. }
  13.  
  14. //Create popup
  15. function lightbox_open(id, timeout, txt){
  16.     window.scrollTo(0,0);
  17.     if (typeof txt != 'undefined') {
  18.         $('#popup_'+id).html('<div>'+txt+'</div>'); }
  19.     $('#popup_'+id).fadeIn('fast');
  20.     $('#fade').fadeIn('fast');
  21.     return setTimeout(function() {
  22.             lightbox_close(id);
  23.         }, timeout);
  24. }
  25. //Close popup
  26. function lightbox_close(id){
  27.     $('#popup_'+id).fadeOut('fast');
  28.     $('#fade').fadeOut('fast');
  29. }
  30.  
  31.  
  32. function RefreshData()
  33. {
  34.     clearInterval($.refreshTimer);
  35.     var jurl=$.domoticzurl+"/json.htm?type=devices&plan="+$.roomplan+"&jsoncallback=?";
  36.     $.getJSON(jurl,
  37.     {
  38.         format: "json"
  39.     },
  40.     function(data) {
  41.         if (typeof data.result != 'undefined') {
  42.            
  43.             $.each(data.result, function(i,item){
  44.                 for( var ii = 0, len = $.PageArray.length; ii < len; ii++ ) {
  45.                     if( $.PageArray[ii][0] === item.idx ) {     // Domoticz idx number
  46.                         var vtype=  $.PageArray[ii][1];         // Domoticz type (like Temp, Humidity)
  47.                         var vlabel= $.PageArray[ii][2];         // cell number from HTML layout
  48.                         var vdesc=  $.PageArray[ii][3];         // description
  49.                         var vattr=  $.PageArray[ii][4];         // extra css attributes
  50.                         var valarm= $.PageArray[ii][5];         // alarm value to turn text to red
  51.                         var vdata=  item[vtype];                // current value
  52.                         if (typeof vdata == 'undefined') {
  53.                             vdata="??";
  54.                         } else {
  55.                             // remove too much text
  56.                             vdata=new String(vdata).split("Lux",1)[0];
  57.                             vdata=new String(vdata).split("Watt",1)[0];
  58.                             vdata=new String(vdata).split("kWh",1)[0];
  59.                             vdata=new String(vdata).split("m3",1)[0];
  60.                             vdata=new String(vdata).split(" Level:",1)[0];
  61.                             vdata=new String(vdata).replace("Set","On");
  62.                             vdata=new String(vdata).replace("true","protected");
  63.                            
  64.                             //If IDX matches '' (RAM usage) or '' (CPU usage), then strip off the '%' and round the numbers
  65.                             if(item.idx == '101' || item.idx == '99'){
  66.                             vdata=new String(vdata).split("%",1)[0];
  67.                             vdata=Math.round(vdata);
  68.                             }  
  69.                        
  70.                         }
  71.                        
  72.                         // replace forecast (text) with an image                       
  73.                             if (vdata == 'Sunny' && item.idx == '32' && vdata == 'On') {
  74.                             vdata=new String(vdata).replace( "Sunny","<img src=images/weer-iconen/zonnig.png width=104 height=104>");  
  75.                             }
  76.                             if (vdata == 'Partly Cloudy' ) {
  77.                             vdata=new String(vdata).replace( "Partly Cloudy","<img src=images/weer-iconen/deels-bewolkt-zon.png width=104 height=104>");
  78.                             }
  79.                             if (vdata == 'Cloudy' ) {
  80.                             vdata=new String(vdata).replace( "Cloudy","<img src=images/weer-iconen/bewolkt.png width=104 height=104>");
  81.                             }
  82.                             if (vdata == 'Clear' ) {
  83.                             vdata=new String(vdata).replace( "Clear","<img src=images/weer-iconen/helder.png width=104 height=104>");  
  84.                             }
  85.                             if (vdata == 'Rain' ) {
  86.                             vdata=new String(vdata).replace( "Rain","<img src=images/weer-iconen/regen.png width=104 height=104>");
  87.                             }
  88.                             if (vdata == 'Snow' ) {
  89.                             vdata=new String(vdata).replace( "Snow","<img src=images/weer-iconen/sneeuw.png width=104 height=104>");   
  90.                             }
  91.                             if (vdata == 'Fog' ) {
  92.                             vdata=new String(vdata).replace( "Fog","<img src=images/weer-iconen/mistig-zon.png width=104 height=104>");
  93.                             }
  94.                             if (vdata == 'Hail' ) {
  95.                             vdata=new String(vdata).replace( "Hail","<img src=images/weer-iconen/hagel.png width=104 height=104>");
  96.                             }
  97.                             if (vdata == 'Thunderstom' ) {
  98.                             vdata=new String(vdata).replace( "Thunderstorm","<img src=images/weer-iconen/onweer.png width=104 height=104>");   
  99.                             }
  100.                             if (vdata == 'Sleet' ) {
  101.                             vdata=new String(vdata).replace( "Sleet","<img src=images/weer-iconen/ijzel.png width=104 height=104>");   
  102.                             }
  103.                    
  104.                             // change background color, depending on sunset and sunrise using idx 32: virtual switch named DonkerBuiten knop (vir)
  105.                             if(item.idx == '32' && vdata == 'On'){
  106.                             document.body.style.backgroundColor="#3B3B3B"   // night
  107.                             }                                      
  108.                             if(item.idx == '32' && vdata == 'Off'){
  109.                             document.body.style.backgroundColor="#BFBFBF"   // day
  110.                             }
  111.                         //If IDX matches '48' (Electricity today) or '90' (Gas today), then round the numbers with 1 decimal
  112.                         if(item.idx == '48' || item.idx == '90'){
  113.                             vdata=new String(vdata).split("kWh",1)[0];
  114.                             vdata=new String(vdata).split("m3",1)[0];
  115.                             vdata=Math.round(vdata * 10) / 10;
  116.                            
  117.                         }
  118.                    
  119.                         //If IDX matches '380' (Water) covert m3 to liters
  120.                         if(item.idx == '380'){
  121.                             vdata=new String(vdata).split("m3",1)[0];
  122.                             vdata=Math.round(vdata * 1000);
  123.                         }
  124.                        
  125.                         // create switchable value when item is switch
  126.                         switchclick='';
  127.                         if (vdata == 'Off' ) {
  128.                             switchclick = 'onclick="SwitchToggle('+item.idx+', \'On\');"';
  129.                         }
  130.                         if (vdata == 'On' ) {
  131.                             switchclick = 'onclick="SwitchToggle('+item.idx+', \'Off\');"';
  132.                         }
  133.                                                
  134.                         // if alarm threshold is defined, make value red
  135.                         //alarmcss='';
  136.                         //if (typeof valarm != 'undefined') {
  137.                         //  if ( eval(vdata + valarm)) {  // note orig:  vdata > alarm
  138.                         //      alarmcss=';color:red;';
  139.                         //  }
  140.                         //}
  141.  
  142.                         // if switch is on, make green, if off, make red. Apply background when switch is not protected.
  143.                         alarmcss='';
  144.                                 if (item.Protected == true) {
  145.                                     if (vdata == 'On') {
  146.                                         alarmcss=';color:#1B9772;';
  147.                                         vdata = 'Aan';
  148.                                     }
  149.                                     if (vdata == 'Off') {
  150.                                         alarmcss=';color:#E24E2A;';
  151.                                         vdata = 'Uit';
  152.                                     }
  153.                                 } else {
  154.                                 if (vdata == 'On') {
  155.                                     alarmcss=';color:#1B9772;';
  156.                                     vdata = 'Aan';
  157.                                     }
  158.                                 if (vdata == 'Off') {
  159.                                     alarmcss=';color:#E24E2A;';
  160.                                     vdata = 'Uit';
  161.                                     }
  162.                                 }
  163.                            
  164.                         // if extra css attributes. Make switch not switchable when it is protected.
  165.                         if (typeof vattr == 'undefined') {
  166.                             if (item.Protected == true) {
  167.                                     $('#'+vlabel).html('<div onClick="lightbox_open();" style='+alarmcss+'>'+vdata+'</div>');
  168.                             } else {
  169.                                     $('#'+vlabel).html('<div '+switchclick+' style='+alarmcss+'>'+vdata+'</div>');
  170.                             }
  171.                         }
  172.                         if (item.Protected == true) {
  173.                             $('#'+vlabel).html( '<div onClick="lightbox_open();" style='+vattr+alarmcss+'>'+vdata+'</div>');
  174.                         } else {
  175.                             $('#'+vlabel).html( '<div '+switchclick+' style='+vattr+alarmcss+'>'+vdata+'</div>');
  176.                         }
  177.                        
  178.                         $('#desc_'+vlabel).html(vdesc);
  179.                     }
  180.                 }
  181.             });
  182.         }
  183.     });
  184.    
  185. // PageArray voor scenes
  186.     var jurl=$.domoticzurl+"/json.htm?type=scenes&plan="+$.roomplan+"&jsoncallback=?";
  187.     $.getJSON(jurl,
  188.     {
  189.         format: "json"
  190.     },
  191.     function(data) {
  192.         if (typeof data.result != 'undefined') {
  193.            
  194.             $.each(data.result, function(i,item){
  195.                 for( var ii = 0, len = $.PageArray_Scenes.length; ii < len; ii++ ) {
  196.                     if( $.PageArray_Scenes[ii][0] === item.idx ) {      // Domoticz idx number
  197.                         var vtype=  $.PageArray_Scenes[ii][1];      // Domoticz type (like Temp, Humidity)
  198.                         var vlabel= $.PageArray_Scenes[ii][2];          // cell number from HTML layout
  199.                         var vdesc=  $.PageArray_Scenes[ii][3];          // description
  200.                         var vattr=  $.PageArray_Scenes[ii][4];          // extra css attributes
  201.                         var valarm= $.PageArray_Scenes[ii][5];      // alarm value to turn text to red
  202.                         var vdata=  item[vtype];                // current value
  203.                         if (typeof vdata == 'undefined') {
  204.                             vdata="?!";
  205.                         } else {
  206.                             // remove too much text
  207.                             vdata=new String(vdata).split("Watt",1)[0];
  208.                             vdata=new String(vdata).split("kWh",1)[0];
  209.                         }
  210.  
  211.                         // create switchable value when item is scene
  212.                         switchclick='';
  213.                         alarmcss='';
  214.                         if (vdata == 'Off'  || vdata == 'Mixed' ) {
  215.                             switchclick = 'onclick="SceneToggle('+item.idx+', \'On\');lightbox_open(\'switch\', '+switch_on_timeout+', '+txt_switch_on+')"';
  216.                             alarmcss=';color:#E24E2A;';
  217.                             vdata = 'Uit';
  218.                         }
  219.                         if (vdata == 'On' ) {
  220.                             switchclick = 'onclick="SceneToggle('+item.idx+', \'Off\');lightbox_open(\'switch\', '+switch_off_timeout+', '+txt_switch_off+')"';
  221.                             alarmcss=';color:#1B9772;';
  222.                             vdata = 'Aan';
  223.                         }
  224.                        
  225.                        
  226.                         // if alarm threshold is defined, make value red
  227.                         if (typeof valarm != 'undefined') {
  228.                             alarmcss='';
  229.                             if ( eval(vdata + valarm)) {  // note orig:  vdata > alarm
  230.                                 alarmcss=';color:red;';
  231.                             }
  232.                         }
  233.                        
  234.                         // if extra css attributes
  235.                         if (typeof vattr == 'undefined') {
  236.                             $('#'+vlabel).html('<div '+switchclick+' style='+alarmcss+'>'+vdata+'</div>');
  237.                         } else {
  238.                             $('#'+vlabel).html( '<div '+switchclick+' style='+vattr+alarmcss+'>'+vdata+'</div>');
  239.                         }
  240.                        
  241.                         $('#desc_'+vlabel).html(vdesc);
  242.                     }
  243.                 }
  244.             });
  245.         }
  246.     });
  247.     $.refreshTimer = setInterval(RefreshData, 8000);
  248. }
  249.  
  250. // ############################################################################################################
  251. // #### vvvvv   USER VALUES below vvvvv   #######
  252. // ############################################################################################################
  253.  
  254. $(document).ready(function() {
  255.     $.roomplan=0;       // define roomplan in Domoticz and create items below.
  256.     $.domoticzurl="http://192.168.2.9:8090";
  257.     //format: idx, value, label, description, [override css], [alarm value]
  258.     $.PageArray = [
  259.         //Page 1
  260.         ['11',  'Data',             'cell1',    'CPU verbruik RPI',],
  261.         ['27',  'Temp',             'cell2',    'Temperatuur trappengat (&deg;C)'],
  262.         ['26',  'Data',             'cell3',    'Trap lichtintensiteit (Lux)',],
  263.  
  264.         ['50',  'Temp',             'cell4',    'Buitentemperatuur (&deg;C)',],
  265.         ['29',  'Data',             'cell5',    'Tot. verbr. LED trap (kWh)',],
  266.         ['51',  'Chill',            'cell6',    'Buiten gevoelstemp. (&deg;C)',],
  267.         ['50',  'ForecastStr',      'cell8b',   'Voorspelling',],
  268.         ['51',  'Speed',            'cell7',    'Windsnelheid (m/s)'],     
  269.        
  270.         ['52',  'Rain',             'cell9',    'Regen (mm/uur)'],
  271.         ['50',  'Humidity',         'cell13',   'Luchtv. buiten (%)'],
  272.    
  273.         ['38',  'Status',           'cell14',   'Verlichting bank'],
  274.         ['49',  'Status',           'cell15',   'Verlichting draaitafel',],
  275.         ['39',  'Status',           'cell16',   'Licht eettafel muur',],
  276.         ['40',  'Status',           'cell17',   'Licht eettafel keuken',],
  277.         ['37',  'Status',           'cell18',   'Verlichting achterraam'],
  278.         //Page 2
  279.         ['32',  'Status',           'cell2-1',  'DonkerBuiten knop (vir)'],
  280.         ['IDX', 'Status',           'cell2-2',  'Verlichting achterraam'],
  281.         ['45',  'Status',           'cell2-3',  'Bewegingssensor trap'],
  282.         ['IDX', 'Status',           'cell2-4',  'Verlichting achterraam'],
  283.         ['IDX', 'Status',           'cell2-5',  'Verlichting achterraam'],
  284.         ['IDX', 'Status',           'cell2-6',  'Verlichting achterraam'],
  285.         ['IDX', 'Status',           'cell2-7',  'Verlichting achterraam'],
  286.         ['IDX', 'Status',           'cell2-8',  'Verlichting achterraam'],
  287.         ['IDX', 'Status',           'cell2-9',  'Verlichting achterraam'],
  288.         ['IDX', 'Status',           'cell2-10', 'Verlichting achterraam'],
  289.         ['IDX', 'Status',           'cell2-11', 'Verlichting achterraam'],
  290.         ['IDX', 'Status',           'cell2-12', 'Verlichting achterraam'],
  291.         ['IDX', 'Status',           'cell2-13', 'Verlichting achterraam'],
  292.         ['IDX', 'Status',           'cell2-14', 'Verlichting achterraam'],
  293.         ['IDX', 'Status',           'cell2-15', 'Verlichting achterraam'],
  294.         ['IDX', 'Status',           'cell2-16', 'Verlichting achterraam'],
  295.         ['IDX', 'Status',           'cell2-17', 'Verlichting achterraam'],
  296.     ];
  297.    
  298.     $.PageArray_Scenes = [
  299.         ['1',   'Status',           'cell10',   'Verlichting lage stand'],
  300.         ['2',   'Status',           'cell11',   'Verlichting middelstand'],
  301.         ['3',   'Status',           'cell12',   'Verlichting hoge stand'],
  302.     ];
  303.  
  304. // ###########################################################################################################
  305. // #### ^^^^^   USER VALUES above ^^^^^   #######
  306. // ############################################################################################################
  307.  
  308.  
  309.     RefreshData();
  310. });  
  311.  
  312.  function SceneToggle(idx, switchcmd)
  313. {
  314.      $.ajax({
  315.      url: "json.htm?type=command&param=switchscene" + "&idx=" + idx + "&switchcmd=" + switchcmd + "&level=0",
  316.      async: false,
  317.      dataType: 'json',
  318.      success: function(){
  319.         console.log('SUCCES');
  320.      },
  321.      error: function(){
  322.         console.log('ERROR');
  323.      }
  324.     });
  325.     RefreshData();
  326. }
  327.  
  328.  
  329.  function SwitchToggle(idx, switchcmd)
  330.     {
  331.              console.log('function called');
  332.              $.ajax({
  333.          url: "json.htm?type=command&param=switchlight" + "&idx=" + idx + "&switchcmd=" + switchcmd + "&level=0",
  334.         async: false,
  335.         dataType: 'json',
  336.         success: function(){
  337.             console.log('SUCCES');
  338.          },
  339.          error: function(){
  340.             console.log('ERROR');
  341.          }
  342.             });
  343.             RefreshData();
  344.     }
Advertisement
Add Comment
Please, Sign In to add comment