Range на SVG-панели

defineVirtualDevice("test_inc", {
  title: "Test Increment",
  cells: {
    Up: {
      type: "switch",
      value: false,
      order: 1
    },
    StopUp: {
      type: "pushbutton",
      order: 2
    },
    Value: {
      type: "range",
      value: 0,
      min: 0,
      max: 1000,
      order: 3
    }
  }
});

defineRule({
  whenChanged: "test_inc/Up",
  then: function(newValue, devName, cellName) {
    if (newValue) {
      var upInterval = setInterval(function() {
        if (dev["test_inc/Up"]) {
          dev["test_inc"]["Value"] += 1;
        }
      }, 100); // Изменять значение каждые 100 мс
    } 
  }
});

defineRule({
  whenChanged: "test_inc/StopUp",
  then: function(newValue, devName, cellName) {
    if (newValue) {
      if (!dev["test_inc"]["Up"]) {
        dev["test_inc"]["Value"] += 1;
      } else {
        dev["test_inc"]["Up"] = false;
      }
    }
  }
});

Обработчики кнопки вверх.

1 лайк

Да, действительно.
Всё получилось.
Спасибо.


function log(message) {
  var command = 'echo "' + message + '" >> /tmp/test.log'.format(message);
  runShellCommand(command);
}

function init_holding_dimmer(id, powerChannel, dimmingChannel, max, min, step, delay) {

  var timer = null
  var devName = "test_dimmer_" + id

  function reset() {
    if (timer) {
      clearInterval(timer);
    }
    dev["buzzer/enabled"] = false;
    dev[devName + "/up"] = false
    dev[devName + "/down"] = false
    dev[devName + "/stop"] = false
  }

  function dim(st) {
    timer = setInterval(function () {
      oldValue = dev[devName + "/value"];
      var newValue = oldValue + st
      if (newValue < min) {
        newValue = min
        dev[powerChannel] = false
      }
      if (newValue > max) {
        newValue = max
      }
      dev[devName + "/value"] = newValue;
      log("dim new value: " + newValue)
      if (newValue == max || newValue == min) {
        reset()
      }
    }, delay);
  }

  defineVirtualDevice(devName, {
    title: "Test Dimmer (" + id + ")",
    cells: {
      up: {
        type: "switch",
        value: false,
      },
      down: {
        type: "switch",
        value: false,
      },
      stop: {
        type: "pushbutton",
        value: false,
      },
      value: {
        title: {'en': 'Value', 'ru': 'Значение'},
        type:  'value',
        order: 1,
        value: dev[dimmingChannel],
        max:   max,
        min:   min
      },
    }
  });

  defineRule({
    whenChanged: devName + "/up",
      then: function (newValue, devName, cellName) {
        if (newValue) {
          dev["buzzer/enabled"] = true;
          dev[powerChannel] = true
          dim(step)
        }
      }
  });

  defineRule({
    whenChanged: devName + "/down",
      then: function (newValue, devName, cellName) {
        if (newValue) {
          dev["buzzer/enabled"] = true;
          dev[powerChannel] = true
          dim(-step)
        }
      }
  });

  defineRule({
    whenChanged: devName + "/stop",
      then: function (newValue, devName, cellName) {
        if (newValue) {
          dev["buzzer/enabled"] = false;
          if (timer) {
            clearInterval(timer);
          }
        }
        dev[devName + "/up"] = false
        dev[devName + "/down"] = false
        dev[devName + "/stop"] = false
      }
  });

  defineRule({
    whenChanged: devName + '/value',
    then: function (newValue, devName, cellName) {
      log("lamp new value: " + newValue)
      dev[dimmingChannel] = newValue;
    },
  });

}

var DIMMER_ID      = 1
var DIMMER_MAX     = 100
var DIMMER_MIN     = 30
var DIMMER_STEP    = 1
var POWER_CHANNEL = 'wb-mdm3_144/K1'
var DIMMER_CHANNEL = 'wb-mdm3_144/Channel 1'
var DIMMER_DELAY_MS = 100;

init_holding_dimmer(DIMMER_ID, POWER_CHANNEL, DIMMER_CHANNEL, DIMMER_MAX, DIMMER_MIN, DIMMER_STEP, DIMMER_DELAY_MS)

2 лайка