function decodeUplink(input) {
        return { 
            data: Decode(input.fPort, input.bytes, input.variables)
        };   
}

function Str1(str2){
  var str3 ="";
  for (var i=0;i<str2.length;i++){
    if (str2[i]<=0x0f){
     str2[i]="0"+str2[i].toString(16)+"";
    }
  str3+= str2[i].toString(16)+"";}
  return str3;
}
function str_pad(byte){
    var zero = '00';
    var hex= byte.toString(16);    
    var tmp  = 2-hex.length;
    return zero.substr(0,tmp) + hex + " ";
}


function datalog(i,bytes){
  var aa= (bytes[6])&0x3F;
  var bb={};
  var gg;  var k=0;
	if(((aa>>5)&0x01)==1)
	{
	  	gg=parseFloat(((bytes[0+i+k*2]<<24>>16 | bytes[1+i+k*2])/10).toFixed(1));
		if(k<3)
		{
		  bb[k]=gg;
		  k++;
		}		
	}
	if(((aa>>4)&0x01)==1)
	{
	  	gg=parseFloat(((bytes[0+i+k*2]<<24>>16 | bytes[1+i+k*2])/100).toFixed(2));	
		if(k<3)
		{
		  bb[k]=gg;
		  k++;
		}	
	}
	if(((aa>>3)&0x01)==1)
	{
	  	gg=parseFloat(bytes[0+i+k*2]<<24>>16 | bytes[1+i+k*2]);
		if(k<3)
		{
		  bb[k]=gg;
		  k++;
		}	
	}
	if(((aa>>2)&0x01)==1)
	{
	  	gg=parseFloat((bytes[0+i+k*2]<<24>>16 | bytes[1+i+k*2])*10);
		if(k<3)
		{
		  bb[k]=gg;
		  k++;
		}	
	}
	if(((aa>>1)&0x01)==1)
	{
	  	gg=parseFloat(bytes[0+i+k*2]<<24>>16 | bytes[1+i+k*2]);
		if(k<3)
		{
		  bb[k]=gg;
		  k++;
		}	
	}
	if(((aa)&0x01)==1)
	{
	  	gg=parseFloat(((bytes[0+i+k*2]<<24>>16 | bytes[1+i+k*2])/100).toFixed(2));
		if(k<3)
		{
		  bb[k]=gg;
		  k++;
		}		
	} 
  
  var ee= getMyDate((bytes[7+i]<<24 | bytes[8+i]<<16 | bytes[9+i]<<8 | bytes[10+i]).toString(10));
  var string='['+bb[0]+','+bb[1]+','+bb[2]+','+ee+']'+',';  
  
  return string;
}

function getzf(c_num){ 
  if(parseInt(c_num) < 10)
    c_num = '0' + c_num; 

  return c_num; 
}

function getMyDate(str){ 
  var c_Date;
  if(str > 9999999999)
     c_Date = new Date(parseInt(str));
  else 
     c_Date = new Date(parseInt(str) * 1000);
  
  var c_Year = c_Date.getFullYear(), 
  c_Month = c_Date.getMonth()+1, 
  c_Day = c_Date.getDate(),
  c_Hour = c_Date.getHours(), 
  c_Min = c_Date.getMinutes(), 
  c_Sen = c_Date.getSeconds();
  var c_Time = c_Year +'-'+ getzf(c_Month) +'-'+ getzf(c_Day) +' '+ getzf(c_Hour) +':'+ getzf(c_Min) +':'+getzf(c_Sen); 
  
  return c_Time;
}


function Decode(fPort, bytes, variables) {
	var data = {};
	var decode = {};
   if(fPort==0x02)
     {
	decode.BatV=((bytes[0]<<8 | bytes[1]) & 0x3FFF)/1000;//Battery,units:V

	var value=bytes[2]<<8 | bytes[3];
	if(bytes[2] & 0x80)
	{value |= 0xFFFF0000;}
  decode.temp_DS18B20=(value/10).toFixed(2);//DS18B20,temperature
  decode.i_flag = (bytes[4]>>7)&0x01;
 	var sensor_type=(bytes[4])&0x3F;
	var j=5;
	if(((sensor_type>>5)&0x01)==1)
	{
	  	decode.turbidity=(bytes[j]<<8 | bytes[j+1])/10;
	  	j=2+j;
	}
	if(((sensor_type>>4)&0x01)==1)
	{
	  	decode.dissolved_oxygen=(bytes[j]<<8 | bytes[j+1])/100;
	  	decode.do_temp=(bytes[j+2]<<8 | bytes[j+3])/100;
	  	j=4+j;
	}
	if(((sensor_type>>3)&0x01)==1)
	{
	  	decode.ORP=(bytes[j]<<24>>16 | bytes[j+1]);	
	  	j=2+j;
	}
	if(((sensor_type>>2)&0x01)==1)
	{
	  	decode.EC_K10=(bytes[j]<<8 | bytes[j+1])*10;
	  	decode.EC10_Temp=(bytes[j+2]<<8 | bytes[j+3])/10;
	  	j=4+j;	  	
	}
	if(((sensor_type>>1)&0x01)==1)
	{
	  	decode.EC_K1=bytes[j]<<8 | bytes[j+1];	
	  	decode.EC1_Temp=(bytes[j+2]<<8 | bytes[j+3])/10;		
	  	j=4+j;	
	}
	if(((sensor_type)&0x01)==1)
	{
	  	decode.PH=(bytes[j]<<8 | bytes[j+1])/100;	
	  	decode.PH_Temp=(bytes[j+2]<<8 | bytes[j+3])/10;		
	  	j=4+j;	  	
	}
	 return decode;
     }
  
  else if(fPort==3)  
  {
    var pnack= ((bytes[6]>>7)&0x01) ? "True":"False";
    for(var i=0;i<bytes.length;i=i+11)
    {
      var data= datalog(i,bytes);
      if(i=='0')
        data_sum=data;
      else
        data_sum+=data;
    }
    return{
    DATALOG:data_sum,
    PNACKMD:pnack,
    };    
  }
else if(fPort==0x05)
  {
    var sub_band;
    var freq_band;
    var sensor;
    
    if(bytes[0]==0x3C)
       sensor= "WQS01-LB";
      
    if(bytes[4]==0xff)
      sub_band="NULL";
    else
      sub_band=bytes[4];
    
    if(bytes[3]==0x01)
      freq_band="EU868";
    else if(bytes[3]==0x02)
      freq_band="US915";
    else if(bytes[3]==0x03)
      freq_band="IN865";
    else if(bytes[3]==0x04)
      freq_band="AU915";
    else if(bytes[3]==0x05)
      freq_band="KZ865";
    else if(bytes[3]==0x06)
      freq_band="RU864";
    else if(bytes[3]==0x07)
      freq_band="AS923";
    else if(bytes[3]==0x08)
      freq_band="AS923_1";
    else if(bytes[3]==0x09)
      freq_band="AS923_2";
    else if(bytes[3]==0x0A)
      freq_band="AS923_3";
    else if(bytes[3]==0x0B)
      freq_band="CN470";
    else if(bytes[3]==0x0C)
      freq_band="EU433";
    else if(bytes[3]==0x0D)
      freq_band="KR920";
    else if(bytes[3]==0x0E)
      freq_band="MA869";
      
    var firm_ver= (bytes[1]&0x0f)+'.'+(bytes[2]>>4&0x0f)+'.'+(bytes[2]&0x0f);
     bat= (bytes[5]<<8 | bytes[6])/1000;
    return {
    SENSOR_MODEL:sensor,
    FIRMWARE_VERSION:firm_ver,
    FREQUENCY_BAND:freq_band,
    SUB_BAND:sub_band,
    BAT:bat,
    };

}

 }