<!-- Common functions for device control -->
[%# $Id: common.tt,v 1.57 2025/03/27 09:58:55 sqko Exp $ %]

<script>

var updating = false;

[% IF user_info.auth %]

function set_current(name)
{
   var curr = prompt('New current for '+name+' - use OCV "OCV" to set open circuit','');
   if (curr != null && curr != '')
   {
	updating = false;
	var arr = new Object();
   	var index = 'PSU_'+name;
   	arr[index] = new Object();
   	arr[index].devclass = 'PSU';
   	arr[index].devname = name;
   	arr[index].command = 'set_current';
   	var args = new Array();
   	args.push(curr);
   	arr[index].arguments = args;
   	var json_str = encodeURI(JSON.stringify(arr));
   	run_ajax('json_rig_ui.cgi','&action=set_device&json_data='+json_str,set_data_noblock);   
	return curr;
   }
}

function set_voltage(name)
{
   var volt = prompt('New voltage for '+name+' - use OCV "OCV" to set open circuit','');
   if (volt != null && volt != '')
   {
	updating = false;
	var arr = new Object();
   	var index = 'PSU_'+name;
   	arr[index] = new Object();
   	arr[index].devclass = 'PSU';
   	arr[index].devname = name;
   	arr[index].command = 'set_voltage';
   	var args = new Array();
   	args.push(volt);
   	arr[index].arguments = args;
   	var json_str = encodeURI(JSON.stringify(arr));
   	run_ajax('json_rig_ui.cgi','&action=set_device&json_data='+json_str,set_data_noblock);   
	return curr;
   }
}


function set_temp(name)
{
   var temp = prompt('New temperature setpoint for '+name,'');
   if (temp != null && temp != '')
   {
	updating = false;
	var arr = new Object();
   	var index = 'tempcontrol_'+name;
   	arr[index] = new Object();
   	arr[index].devclass = 'tempcontrol';
   	arr[index].devname = name;
   	arr[index].command = 'set_temp';
   	var args = new Array();
   	args.push(temp);
   	arr[index].arguments = args;
   	var json_str = encodeURI(JSON.stringify(arr));
   	run_ajax('json_rig_ui.cgi','&action=set_device&json_data='+json_str,set_data_noblock);   
	return temp;
   }
}

function set_ramp(name)
{
   var ramp = prompt('New temperature ramprate for '+name,'');
   if (ramp != null && ramp != '')
   {
	updating = false;
	var arr = new Object();
   	var index = 'tempcontrol_'+name;
   	arr[index] = new Object();
   	arr[index].devclass = 'tempcontrol';
   	arr[index].devname = name;
   	arr[index].command = 'set_ramp';
   	var args = new Array();
   	args.push(ramp);
   	arr[index].arguments = args;
   	var json_str = encodeURI(JSON.stringify(arr));
   	run_ajax('json_rig_ui.cgi','&action=set_device&json_data='+json_str,set_data_noblock);   
	return ramp;
   }
}

function set_multiplex(name,gas)
{
	updating = false;
	var arr = new Object();
   	var index = 'multiplex_'+name;
        arr[index] = new Object();
        arr[index].devclass = 'multiplex';
        arr[index].devname = name;
        arr[index].command = 'set';
        var args = new Array();
        args.push(gas);
        arr[index].arguments = args;
        var json_str = encodeURI(JSON.stringify(arr));
        run_ajax('json_rig_ui.cgi','&action=set_device&json_data='+json_str,set_data_noblock);
}

function set_gas(name)
{
   var flow = prompt('New flow for '+name,'');
   if (flow != null && flow != '')
   {
	updating = false;
	var arr = new Object();
   	var index = 'gas_'+name;
   	arr[index] = new Object();
   	arr[index].devclass = 'gas';
   	arr[index].devname = name;
   	arr[index].command = 'setflow';
   	var args = new Array();
   	args.push(flow);
   	arr[index].arguments = args;
   	var json_str = encodeURI(JSON.stringify(arr));
   	run_ajax('json_rig_ui.cgi','&action=set_device&json_data='+json_str,set_data_noblock);   
	return flow;
   }
}



function set_PID(name)
{
   var flow = prompt('New setpoint for '+name,'');
   if (flow != null && flow != '')
   {
	updating = false;
	var arr = new Object();
   	var index = 'PID_'+name;
   	arr[index] = new Object();
   	arr[index].devclass = 'PID';
   	arr[index].devname = name;
   	arr[index].command = 'set';
   	var args = new Array();
   	args.push(flow);
   	arr[index].arguments = args;
   	var json_str = encodeURI(JSON.stringify(arr));
   	run_ajax('json_rig_ui.cgi','&action=set_device&json_data='+json_str,set_data_noblock);   
	return flow;
   }
}

function set_relay(name,status)
{
   updating = false;
   var arr = new Object();
   var index = 'relay_'+name;
   arr[index] = new Object();
   arr[index].devclass = 'relay';
   arr[index].devname = name;
   arr[index].command = 'set';
   var args = new Array();
   if (status)
   {
	args.push(1);
   }
   else
   {
	args.push(0);
   }
   arr[index].arguments = args;
   var json_str = encodeURI(JSON.stringify(arr));
   run_ajax('json_rig_ui.cgi','&action=set_device&json_data='+json_str,set_data_noblock);   
   var classname = 'relay_'+name;
   var l =  document.getElementsByClassName(classname);
   for (var z = 0; z < l.length;z++)
   {
	l[z].style.display = 'none';
   }
   var l =  document.getElementsByClassName(classname+'_on');
   for (var z = 0; z < l.length;z++)
   {
	l[z].style.display = 'none';
   }
   var l =  document.getElementsByClassName(classname+'_off');
   for (var z = 0; z < l.length;z++)
   {
	l[z].style.display = 'none';
   }
   if (status)
   {
       var l =  document.getElementsByClassName(classname+'_on');
       for (var z = 0; z < l.length;z++)
       {
	      l[z].style.display = 'inline-block';
       }
   }
   else
   {
       var l =  document.getElementsByClassName(classname+'_off');
       for (var z = 0; z < l.length;z++)
       {
	      l[z].style.display = 'inline-block';
       }
   }
}


[% END %]

function get_data()
{
   updating = true;
   var arr = new Object();
   var devices = document.getElementsByClassName("device");
   for(var i = 0; i < devices.length; i++)
   {
	var e = devices[i];
	var cl = e.getAttribute("devclass");
	var name = e.getAttribute("devname");
	if (cl != null && name != null)
	{
	   var index = cl+'_'+name;
	   arr[index] = new Object();
	   arr[index].devclass = cl;
	   arr[index].devname = name;
	}
   }
   var e = document.getElementById('select_multiplex');
   if (e != null && e.value != '')
   {
	   var index = 'multiplex_'+e.value;
	   arr[index] = new Object();
	   arr[index].devclass = 'multiplex';
	   arr[index].devname = e.value;	
   }
   var e = document.getElementById('select_gas');
   if (e != null && e.value != '')
   {
	   var index = 'gas_'+e.value;
	   arr[index] = new Object();
	   arr[index].devclass = 'gas';
	   arr[index].devname = e.value;	
   }
   var e = document.getElementById('select_relay');
   if (e != null && e.value != '')
   {
	   var index = 'relay_'+e.value;
	   arr[index] = new Object();
	   arr[index].devclass = 'relay';
	   arr[index].devname = e.value;	
   }
   var e = document.getElementById('select_PID');
   if (e != null && e.value != '')
   {
	   var index = 'PID_'+e.value;
	   arr[index] = new Object();
	   arr[index].devclass = 'PID';
	   arr[index].devname = e.value;	
   }
   var e = document.getElementById('select_PSU');
   if (e != null && e.value != '')
   {
	   var index = 'PSU_'+e.value;
	   arr[index] = new Object();
	   arr[index].devclass = 'PSU';
	   arr[index].devname = e.value;	
   }
   var e = document.getElementById('select_tempcontrol');
   if (e != null && e.value != '')
   {
	   var index = 'tempcontrol_'+e.value;
	   arr[index] = new Object();
	   arr[index].devclass = 'tempcontrol';
	   arr[index].devname = e.value;	
   }
   var json_str = encodeURI(JSON.stringify(arr));
   run_ajax('json_rig_ui.cgi','&action=get_data_list&json_data='+json_str,set_data_reload);
}

function set_data_reload(res)
{
   set_data(res);
   setTimeout(get_data, [% data.reload_interval * 1000 %]);
}

function set_data(res)
{
   if (updating == false)
   {
	return true;
   }
   updating = false;
   set_data_noblock(res);
}

function set_data_noblock(res)
{
   var lst = JSON.parse(res);
   for (x = 0; x < lst.length; x++)
   {  
      var item = lst[x];
      if (item.class == 'gas_list')
      {
          var e = document.getElementById('select_gas');
	  if (e != null)
	  {
	    e.options.length = 1;
	    for (i = 0; i < item.gasses.length;i++)
	    {
              var option = document.createElement("option");
              option.value = item.gasses[i];
              option.text = item.gasses[i];
              e.appendChild(option);
	    }
            continue;
	  }
      }
      var classname = item.class+ '_' + item.name;
      var devices = document.getElementsByClassName(classname);
      for(var i = 0; i < devices.length; i++)
      {
	  var e = devices[i];
	  if (item.class == 'simplechannel')
	  {
              var dlist = document.getElementsByClassName(classname+'_value');
	      for(var j = 0; j < dlist.length; j++)
      	      {
		  var e2 = dlist[j];
		  if (e2 != null)
		  {
		        var val = parseFloat(item.value)
			e2.innerHTML = val.toPrecision(4);
			e2.value = val.toPrecision(4);
		  }
	      }		
	  }
	  if (item.class == 'templog')
	  {
              var dlist = document.getElementsByClassName(classname+'_value');
	      for(var j = 0; j < dlist.length; j++)
      	      {
		  var e2 = dlist[j];
		  if (e2 != null)
		  {
		        var val = parseFloat(item.value)
			if (val > -300)
			{
			   e2.innerHTML = val.toFixed(1);
			   e2.value = val.toFixed(1);
			}
			else
			{
			   e2.innerHTML = 'NaN';
			   e2.value = 'NaN';
			}
		  }
	      }		
	  }
	  if (item.class == 'tempcontrol')
	  {
              var dlist = document.getElementsByClassName(classname+'_value');
	      for(var j = 0; j < dlist.length; j++)
      	      {
		  var e2 = dlist[j];
		  if (e2 != null)
		  {
		        var val = parseFloat(item.value)
			if (val > -300)
			{
			      e2.innerHTML = val.toFixed(0) + ' &deg;C';
			      e2.value = val.toFixed(0);
			}
			else
			{
			      e2.innerHTML = 'NaN &deg;C';	
			      e2.value = 'NaN';
			}
		  }
	      }		
              var dlist = document.getElementsByClassName(classname+'_setpoint');
	      for(var j = 0; j < dlist.length; j++)
      	      {
		  var e2 = dlist[j];
		  if (e2 != null)
		  {
		        var val = parseFloat(item.setpoint)
			if (val > -300)
			{
				e2.innerHTML = val.toFixed(0) + ' &deg;C';
				e2.value = val.toFixed(0);
			}
			else
			{
				e2.innerHTML = 'NaN &deg;C';
				e2.value = 'NaN';
			}
		  }
	      }		
              var dlist = document.getElementsByClassName(classname+'_ramprate');
	      for(var j = 0; j < dlist.length; j++)
      	      {
		  var e2 = dlist[j];
		  if (e2 != null)
		  {
		        var val = parseFloat(item.ramprate)
			if (val > -300)
			{
				e2.innerHTML = val.toFixed(0) + ' &deg;C/h';
				e2.value = val.toFixed(0);
			}
			else
			{
				e2.innerHTML = 'NaN &deg;C/h';
				e2.value = 'NaN';
			}
		  }
	      }		
	  }
	  if (item.class == 'PSU')
	  {
              var dlist = document.getElementsByClassName(classname+'_value');
	      for(var j = 0; j < dlist.length; j++)
      	      {
		  var e2 = dlist[j];
		  if (e2 != null)
		  {
		        var val = parseFloat(item.value)
			e2.innerHTML = val.toPrecision(4);
			e2.value = val.toPrecision(4);
		  }
	      }		
              var dlist = document.getElementsByClassName(classname+'_current');
	      for(var j = 0; j < dlist.length; j++)
      	      {
		  var e2 = dlist[j];
		  if (e2 != null)
		  {
		        var val = parseFloat(item.setpoint)
			e2.innerHTML = val.toPrecision(4) + ' A';
			e2.value = val.toPrecision(4);
		  }
	      }		
              var dlist = document.getElementsByClassName(classname+'_voltage');
	      for(var j = 0; j < dlist.length; j++)
      	      {
		  var e2 = dlist[j];
		  if (e2 != null)
		  {
		        var val = parseFloat(item.setpoint_U);
			e2.innerHTML = val.toPrecision(4) + ' V';
			e2.value = val.toPrecision(4);
		  }
	      }		
              try {
                  document.getElementById('PSU_current_set').value = item.setpoint;
                  document.getElementById('PSU_voltage_set').value = item.setpoint_U;
                  document.getElementById('PSU_voltage_value').innerHTML = item.U.toPrecision(4) + ' V';
              }
              catch (e)
              {
                  // do nothing
              }
	  }
	  if (item.class == 'MFC')
	  {
	      e.setAttribute('gas', item.gas);
	      if (item.value == null)
	      {
		item.value = item.result;
	      }
              var dlist = document.getElementsByClassName(classname+'_value');
	      for(var j = 0; j < dlist.length; j++)
      	      {
		  var e2 = dlist[j];
		  if (e2 != null)
		  {
		        var val = parseFloat(item.value)
			e2.innerHTML = val.toPrecision(4);
			e2.value = val.toPrecision(4);
		  }
	      }
	  }
	  if (item.class == 'multiplex')
	  {
              var dlist = document.getElementsByClassName(classname+'_value');
	      for(var j = 0; j < dlist.length; j++)
      	      {
		  var index = 0;
		  var e2 = dlist[j];
		  if (e2 != null)
		  {
		    e2.options.length = 0;
		    if (e2 != null)
		    {
		      for (var z = 0; z < item.gasses.length;z++)
		      {
               	         var option = document.createElement("option");
                	 option.value = item.gasses[z];
                	 option.text = item.gasses[z];
			 e2.appendChild(option);
			 if (item.gasses[z] == item.value)
			 {
			    index = z;
			 }
		      }
		      e2.value = item.value;
		    }
		  }
		  e2.selectedIndex = index;
	      }	      
	      for (var j = 0; j < item.gasses.length; j++)
              {
                  var dlist = document.getElementsByClassName('gas_'+item.gasses[j]);
                  for (var k = 0; k < dlist.length; k++)
                  {
                      var e2 = dlist[k];
		      if (e2.id != 'select_gas')
		      {
			 e2.title = item.value;
                      	 e2.setAttribute('devname', item.value)
                      	 e2.classList.remove('gas_'+item.gasses[j]);
                      	 e2.classList.add('gas_'+item.value);
                         e2.setAttribute("onClick","set_gas('"+item.value+"')");
		      }
                  }
                  var dlist = document.getElementsByClassName('gas_'+item.gasses[j]+'_value');
                  for (var k = 0; k < dlist.length; k++)
                  {
                      var e2 = dlist[k];
                      e2.classList.remove('gas_'+item.gasses[j]+'_value');
                      e2.classList.add('gas_'+item.value+'_value');
		      var val =	parseFloat(item.flow);
		      e2.innerHTML = val.toPrecision(4);
                  }
              }
	  }
	  if (item.class == 'PID')
	  {
	      console.log(item);
	      if (item.value == null)
	      {
		item.value = item.result;
	      }
              var dlist = document.getElementsByClassName(classname+'_setpoint');
	      for(var j = 0; j < dlist.length; j++)
      	      {
		  var e2 = dlist[j];
		  if (e2 != null)
		  {
		        var val = parseFloat(item.value);
			e2.innerHTML = val.toPrecision(4);
			e2.value = val.toPrecision(4);
		  }
	      }
	      try {
	          document.getElementById('PID_set').value = item.value
	      }
	      catch (e)
	      {
		  // do nothing
	      }
	  }
	  if (item.class == 'gas')
	  {
	      if (item.value == null)
	      {
		item.value = item.result;
	      }
              var dlist = document.getElementsByClassName(classname+'_value');
	      for(var j = 0; j < dlist.length; j++)
      	      {
		  var e2 = dlist[j];
		  if (e2 != null)
		  {
		        var val = parseFloat(item.value);
			e2.innerHTML = val.toPrecision(4);
			e2.value = val.toPrecision(4);
		  }
	      }
              var dlist = document.getElementsByClassName(classname+'_setpoint');
	      for(var j = 0; j < dlist.length; j++)
      	      {
		  var e2 = dlist[j];
		  if (e2 != null)
		  {
		        var val = parseFloat(item.value);
			e2.innerHTML = val.toPrecision(4);
			e2.value = val.toPrecision(4);
		  }
	      }
	      try {
	          document.getElementById('select_gas').value = item.name
	      	  document.getElemetnById('gas_value').innerHTML = item.value;
	      	  document.getElemetnById('gas_set').innerHTML = item.setpoint;
	      }
	      catch (e)
	      {
		  // do nothing
	      }
	  }
	  if (item.class == 'relay')
	  {
	      var l =  document.getElementsByClassName(classname);
	      for (var z = 0; z < l.length;z++)
	      {
	        if (l[z].id != 'select_water'  && l[z].id != 'select_relay')
		   l[z].style.display = 'none';
	      }
	      var l =  document.getElementsByClassName(classname+'_on');
	      for (var z = 0; z < l.length;z++)
	      {
		l[z].style.display = 'none';
	      }
	      var l =  document.getElementsByClassName(classname+'_off');
	      for (var z = 0; z < l.length;z++)
	      {
		l[z].style.display = 'none';
	      }
	      if (item.value == 1)
	      {
		  var l =  document.getElementsByClassName(classname+'_on');
	      	  for (var z = 0; z < l.length;z++)
	      	  {
		      l[z].style.display = 'inline-block';
	      	  }
	      }
	      else if (item.value == 0)
	      {
		  var l =  document.getElementsByClassName(classname+'_off');
	      	  for (var z = 0; z < l.length;z++)
	      	  {
		      l[z].style.display = 'inline-block';
	      	  }
	      }
	      else
	      {
		  var l =  document.getElementsByClassName(classname);
	      	  for (var z = 0; z < l.length;z++)
	      	  {
		      l[z].style.display = 'inline-block';
	      	  }
	      }
	      var dlist = document.getElementsByClassName(classname+'_control');
	      for(var j = 0; j < dlist.length; j++)
      	      {
		  var e2 = dlist[j];
		  if (e2 != null)
		  {
		     if (item.value == 1)
		     {
			e2.checked = true;
		     }
		     else
		     {
			e2.checked = false;
		     }	
	          }
	      }
	      var dlist = document.getElementsByClassName(classname+'_value');
	      for(var j = 0; j < dlist.length; j++)
	      {
		  var e2 = dlist[j];
		  if (e2 != null)
	      	  {
			e2.value = item.value;
		  }
	      }
              try
              {
                  document.getElementById('water_value').value = item.value;
              }
              catch (e)
              {
                  // do nothing
              }	      
	  }
      }
   }
   get_logs();
}


get_data();

</script>


<!-- End common functions -->