Snippets

Web Port Tidkanalskript för AirSite-aggregat

Created by Staffan Backlund last modified
// Only adapted for 1 on/off per day
//use Format 00
//CT1-CT7 = Hour on day 1-7
//CT11-CT17 = Minute on day 1-7
//CF1-CF7 = Hour off day 1-7
//CF17-CF17 = Minute off day 1-7
//00:00 - 24:00 = always on
//00:00 - 00:00 = always off

function read() {
    schedule.DefaultValue = "0";
        for (var i=1; i<=7; i++) {
          	var day = i;
          
            var onHours = tagValue(prefix + "_CT" + day);
			var onMinutes = tagValue(prefix + "_CT1" + day);
			
            var offHours = tagValue(prefix + "_CF" + day);
          	var offMinutes = tagValue(prefix + "_CF1" + day);
  
          	onHours = pad(onHours, 2);
          	onMinutes = pad(onMinutes, 2);     
          	offHours = pad(offHours, 2);
          	offMinutes = pad(offMinutes, 2);
          	
          	on = onHours + "" +  onMinutes;
          	off = offHours + "" + offMinutes;

            if (onHours == "00" && onMinutes == "00" && offHours == "00" && offMinutes == "00") {
              // do nothing
            }
            else if (offHours == "24" && offMinutes == "00")
              schedule.AddEvent("1", on + "-0000:" + i);
            else
              schedule.AddEvent("1", on + "-" + off + ":" + i);
        }
 	return true;
}

function write() {
    var days = [0, 0, 0 ,0, 0, 0, 0, 0, 0];
    var day = 0;
  
	for (var i=0; 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 d = day - 1;  
	        day += days[d] * 10;
            days[d]++;
            var onHours = schedule.Events[i].Time.Substring(0,2);
          	var onMinutes = schedule.Events[i].Time.Substring(2,2);
            var offHours = schedule.Events[i].Time.Substring(5,2);
          	var offMinutes = schedule.Events[i].Time.Substring(7,2);
          
            if (offHours == "00" && offMinutes == "00")
              offHours = "24";
          
            tagWrite(prefix + "_CT" + day, onHours);
          	tagWrite(prefix + "_CT1" + day, onMinutes);
            tagWrite(prefix + "_CF" + day, offHours);
          	tagWrite(prefix + "_CF1" + day, offMinutes);
        }
    }
  
    for (var i=1; i<=7; i++) {
        for (var n=days[i-1]; n<nbrOnOff; n++) {
        	day = i + n*10;      
            tagWrite(prefix + "_CT" + day, "00");
      		tagWrite(prefix + "_CT1" + day, "00");      
            tagWrite(prefix + "_CF" + day, "00");          
      		tagWrite(prefix + "_CF1" + day, "00");    
        }
    } 
	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.