Snippets

Web Port Tidkanalskript för Regin-DUCar

Created by Staffan Backlund last modified
//Use datatype real with format 00.00

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(":","").Replace(",","").Replace(".","");
        var off = tagValue(prefix + "_CF" + day).Replace(":","").Replace(",","").Replace(".","");
  
         
          //var off = "1700";
          
          	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, (parseFloat(on)/100).toFixed(2));
            tagWrite(prefix + "_CF" + day, (parseFloat(off)/100).toFixed(2));
        }
    }
  
    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 (0)

HTTPS SSH

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