Snippets

Web Port BastecScheduleScript

Created by Anders Åslund
//TCH_CT1   //EVENT1 till tid
//TCH_CF1  //EVENT1 från tid
//TCH_DT1  //EVENT 1 till dagar
//TCH_DF1 //EVENT1 från dagar


//var nbrOnOff = 1;

//Läs in taggen med dagar
//var daytag1 = 31;
//var time1 = "830";
//var daytag1 = 31;
//var time2 = "1730";

//till / från
//var value = 0;

function read() {
  	
  var ondays = tagValue(prefix + "_DT1");
  var offdays = tagValue(prefix + "_DF1");
  
  var on = tagValue(prefix + "_CT1").Replace(":","");
  var off = tagValue(prefix + "_CF1").Replace(":","");

  for (var i=0; i<=6; i++) {
    //Kolla om dagen aktiv
    var offday = (offdays & 1<<i) > 0; 
    var onday = (ondays & 1<<i) > 0; 
    
    // Både till och från, Lägg till på samma dag. Om endast till
     
    if(offday && onday) {
    	schedule.AddEvent("1", pad(on,4) + "-" + pad(off,4) + ":" + (i+1));
    } else if(offday && !onday) {
     	 schedule.AddEvent("1", "0000" + "-" + pad(off,4) + ":" + (i+1));
    }else if(!offday && onday) {
     	 schedule.AddEvent("1", pad(on,4) + "-" + "0000" + ":" + (i+1));
    }
  }
  
  return true;
}



function write() {
  
  //Loopa igenom alla schedule.Events
  //Lägg alla med samma tilltid till en array?
  
  
  var offdays = 0;
  var ondays = 0;
  var days = 0;
  
  var day = 0;
  var ontime = "0000"
  var offtime = "0000"
  
  if(schedule.Events.Count>0) {
    ontime=  schedule.Events[0].Time.Substring(0,4);
  	offtime = schedule.Events[0].Time.Substring(5,4);  
    day = schedule.Events[0].Time.Substring(10);
  } else {
    tagWrite(prefix + "_DT1",days.toString());
    tagWrite(prefix + "_DF1",days.toString());
 			
    tagWrite(prefix + "_CT1",ontime.toString());
    tagWrite(prefix + "_CF1",offtime.toString());
    return true;
  }
 
  
  if (ontime == "0000" && offtime == "0000") {
    offtime = "2400"; 
  }
  
     if(parseInt(offtime,10)>parseInt(ontime,10)) {
  days += Math.pow(2,parseInt(day-1));
     }
  
	for (var i=1; i<schedule.Events.Count; i++) {
        if (schedule.Events[i].Value != schedule.DefaultValue && schedule.Events[i].Time.Length == 11) {
            day = schedule.Events[i].Time.Substring(10);            
          var on = schedule.Events[i].Time.Substring(0,4);
            var off = schedule.Events[i].Time.Substring(5,4);
                    
          
          if (on == "0000" && off == "0000") {
           	off = "2400"; 
          }
          //Om aktiv för dagen, offtime > ontime
          if(parseInt(off,10)>parseInt(on,10)) {
            ontime = on;
            offtime = off;
            days += Math.pow(2,parseInt(day-1));
            
          }       
          
          
        }
    }
  
  	tagWrite(prefix + "_DT1",days.toString());
    tagWrite(prefix + "_DF1",days.toString());
 			
    tagWrite(prefix + "_CT1",ontime.toString());
    tagWrite(prefix + "_CF1",offtime.toString());
          


	return true;
}




function pad(n, width) {
  var result = '';
  
  for (var i=n.length;i<width;i++) {
  	result += '0';
  }
  result += n;
  return result;
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.