Да, действительно.
Всё получилось.
Спасибо.
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)