Snippets

Web Port Tidkanalskript för Trend-DUCar

Created by Joakim Nilén
function read() {
    schedule.DefaultValue = "0";
    for (var n=0; n<nbrOnOff; n++) {
        for (var i=1; i<=7; i++) {
          	var day = i + n * 10;
            
          tags[prefix + "_CT" + day].ReadValue(Moldeo.WebPortCommon.ReadTypes.PRIORITY);
            tags[prefix + "_CF" + day].ReadValue(Moldeo.WebPortCommon.ReadTypes.PRIORITY);
            var on = tags[prefix + "_CT" + day].Value.Replace(":","");
            var off = tags[prefix + "_CF" + day].Value.Replace(":","");
  
          	on = pad(on, 4);
          	off = pad(off, 4);

          if(on == "0000" && off == "2400") {
            on= "0000";
            off= "0000";
          }          
          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 != "0" && schedule.Events[i].Time.Length == 11) {
          	day = schedule.Events[i].Time.Substring(10); // ex. 0600-1000:2
            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);
          
            var onh = schedule.Events[i].Time.Substring(0,2);
            var onm = schedule.Events[i].Time.Substring(2,2);
            var offh = schedule.Events[i].Time.Substring(5,2);
            var offm = schedule.Events[i].Time.Substring(7,2);
          
          if (off == "0000" && on == "0000") {
            on = "00:00";
            off = "24:00";
          } else {
            on = onh + ":" + onm;
            off = offh + ":" + offm;
          }
          
          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, "00:00");
            tagWrite(prefix + "_CF" + day, "00: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.