Snippets

Web Port Tidkanalskript Web Port-standard

Created by Staffan Backlund last modified
//use Format 00:00
//00:00 - 24:00 = always on
//00:00 - 00:00 = always off

function read() {
    schedule.DefaultValue = "0";
    for (var n=0; n<nbrOnOff; n++) {
        for (var i=1; i<=7; i++) {
          	var day = i + n * 10;
          
            var on = tagValue(prefix + "_CT" + day).Replace(":","");
            var off = tagValue(prefix + "_CF" + day).Replace(":","");
  
          	on = pad(on, 4);
          	off = pad(off, 4);

            if (on == "0000" && off == "0000") {
              // do nothing
            }
            else if (off == "2400")
              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 on = schedule.Events[i].Time.Substring(0,4);
            var off = schedule.Events[i].Time.Substring(5,4);
          
            if (off == "0000")
              off = "2400";
          
            tagWrite(prefix + "_CT" + day, on);
            tagWrite(prefix + "_CF" + day, off);
        }
    }
  
    for (var i=1; i<=7; i++) {
        for (var n=days[i-1]; n<=nbrOnOff; n++) {
        	day = i + n*10;
            tagWrite(prefix + "_CT" + day, "0000");
            tagWrite(prefix + "_CF" + day, "0000");          
        }
    }
  
	return true;
}

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

Comments (2)

HTTPS SSH

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