JSON.stringify нагружает процессор

Здравствуйте! Пишу скрипт для ИК передатчика HEIMAN HS2IRC.
IR.rar (1,3 КБ)
При использовнии publish(…JSON.stringify()); (команда Learn) процессор нагружается на 180%


Если закомментировать данную команду (команда Play) проблем нет. Подскажите, пожалуйста, что делаю не так?
diag_AWHDMA4M_2024-05-14-15.10.51.zip (168,7 КБ)

Попробуйте заменить JSON.Stringify работой с текстовыми строками.

Подскажите, пожалуйста, что Вы имеете в виду? Попоробовал команду runShellCommand('mosquitto_pub -t \'zigbee2mqtt/0x000d6f00153489ce/set\' -m \'{"send_key": {"id": 1,"key_code": 5}}\'');
Процессор перегружается после 5 выполнений

Вот эта простынища:

	defineVirtualDevice("ir_learn_"+num, {
		title: "ir_learn_"+num,
		cells: {
		Learn_10 : {
			type : "pushbutton",
			value : false
		}, 
		Learn_11 : { 	// 
			type : "pushbutton",
			value : false
		},
		Learn_12 : { 	// 
			type : "pushbutton",
			value : false
		},
		Learn_13 : { 	// 
			type : "pushbutton",
			value : false
		}, 
		Learn_14 : {
			type : "pushbutton",
			value : false
		}, 
		Learn_15 : {
			type : "pushbutton",
			value : false
		}, 
		Learn_16 : {
			type : "pushbutton",
			value : false
		}, 
		Learn_17 : { 	// 
			type : "pushbutton",
			value : false
		}, 
		Learn_18 : {
			type : "pushbutton",
			value : false
		}, 
		Learn_19 : {
			type : "pushbutton",
			value : false
		}, 
		Learn_20 : {
			type : "pushbutton",
			value : false
		}, 
		Learn_21 : { 	// 
			type : "pushbutton",
			value : false
		},
		Learn_22 : { 	// 
			type : "pushbutton",
			value : false
		},
		Learn_23 : { 	// 
			type : "pushbutton",
			value : false
		}, 
		Learn_24 : {
			type : "pushbutton",
			value : false
		}, 
		Learn_25 : {
			type : "pushbutton",
			value : false
		}, 
		Learn_26 : {
			type : "pushbutton",
			value : false
		}, 
		Learn_27 : { 	// 
			type : "pushbutton",
			value : false
		}, 
		Learn_28 : {
			type : "pushbutton",
			value : false
		}, 
		Learn_29 : {
			type : "pushbutton",
			value : false
		}, 
		Learn_30 : {
			type : "pushbutton",
			value : false
		},
		Learn_31 : { 	// 
			type : "pushbutton",
			value : false
		},
		Learn_32 : { 	// 
			type : "pushbutton",
			value : false
		},
		Learn_33 : { 	// 
			type : "pushbutton",
			value : false
		}, 
		Learn_34 : {
			type : "pushbutton",
			value : false
		}, 
		Learn_35 : {
			type : "pushbutton",
			value : false
		}, 
		Learn_36 : {
			type : "pushbutton",
			value : false
		}, 
		Learn_37 : { 	// 
			type : "pushbutton",
			value : false
		}, 
		Learn_38 : {
			type : "pushbutton",
			value : false
		}, 
		Learn_39 : {
			type : "pushbutton",
			value : false
		}, 
		Play_10 : {
			type : "pushbutton",
			value : false
		},
		Play_11 : {
			type : "pushbutton",
			value : false
		},
		Play_12 : {
			type : "pushbutton",
			value : false
		},
		Play_13 : {
			type : "pushbutton",
			value : false
		},
		Play_14 : {
			type : "pushbutton",
			value : false
		},
		Play_15 : {
			type : "pushbutton",
			value : false
		},
		Play_16 : {
			type : "pushbutton",
			value : false
		},
		Play_17 : {
			type : "pushbutton",
			value : false
		},
		Play_18 : {
			type : "pushbutton",
			value : false
		},
		Play_19 : {
			type : "pushbutton",
			value : false
		},
		Play_20 : {
			type : "pushbutton",
			value : false
		},
		Play_21 : {
			type : "pushbutton",
			value : false
		},
		Play_22 : {
			type : "pushbutton",
			value : false
		},
		Play_23 : {
			type : "pushbutton",
			value : false
		},
		Play_24 : {
			type : "pushbutton",
			value : false
		},
		Play_25 : {
			type : "pushbutton",
			value : false
		},
		Play_26 : {
			type : "pushbutton",
			value : false
		},
		Play_27 : {
			type : "pushbutton",
			value : false
		},
		Play_28 : {
			type : "pushbutton",
			value : false
		},
		Play_29 : {
			type : "pushbutton",
			value : false
		},
		Play_30 : {
			type : "pushbutton",
			value : false
		},
		Play_31 : {
			type : "pushbutton",
			value : false
		},
		Play_32 : {
			type : "pushbutton",
			value : false
		},
		Play_33 : {
			type : "pushbutton",
			value : false
		},
		Play_34 : {
			type : "pushbutton",
			value : false
		},
		Play_35 : {
			type : "pushbutton",
			value : false
		},
		Play_36 : {
			type : "pushbutton",
			value : false
		},
		Play_37 : {
			type : "pushbutton",
			value : false
		},
		Play_38 : {
			type : "pushbutton",
			value : false
		},
		Play_39 : {
			type : "pushbutton",
			value : false
		},
        Init : {
			type : "pushbutton",
			value : false
		},
	}
	})

Отлично меняется на одну функцию, банально так:

function makeNewVirtualControl(vdName, nameControl, typeControl){
  //log.info("GetDevice", getDevice("vdName"))
  if (getDevice(vdName) === undefined) {
    log.info("Define new")
    defineVirtualDevice(vdName, {
      title: vdName,
      cells: {},
    })
  }
     //Тут проверим есть ли уже контрол и если нет - создадим.
    if (!getDevice(vdName).isControlExists(nameControl)) {
      //log.debug("Контрола "+nameControl+" нет, создаем.")
      getDevice(vdName).addControl(nameControl, typeControl);
    }
}


function IR(num, ir_name){

  for (var i=10; i<40; i+=1){
    log.info("var i=", i)
    makeNewVirtualControl("ir_learn_"+num, "Learn_"+i, {type: "pushbutton", value: false});
    makeNewVirtualControl("ir_learn_"+num, "Play_"+i, {type: "pushbutton", value: false});
  }
  makeNewVirtualControl("ir_learn_"+num, "Init", {type: "pushbutton", value: false});
  

	
	defineRule ("commands_"+num, {
		whenChanged: 	["ir_learn_"+num+"/Learn_10", "ir_learn_"+num+"/Learn_11", "ir_learn_"+num+"/Learn_12", "ir_learn_"+num+"/Learn_13", "ir_learn_"+num+"/Learn_14", "ir_learn_"+num+"/Learn_15", "ir_learn_"+num+"/Learn_16", "ir_learn_"+num+"/Learn_17", "ir_learn_"+num+"/Learn_18", "ir_learn_"+num+"/Learn_19", "ir_learn_"+num+"/Learn_20", "ir_learn_"+num+"/Learn_21", "ir_learn_"+num+"/Learn_22", "ir_learn_"+num+"/Learn_23", "ir_learn_"+num+"/Learn_24", "ir_learn_"+num+"/Learn_25", "ir_learn_"+num+"/Learn_26", "ir_learn_"+num+"/Learn_27", "ir_learn_"+num+"/Learn_28", "ir_learn_"+num+"/Learn_29", "ir_learn_"+num+"/Learn_30", "ir_learn_"+num+"/Learn_31", "ir_learn_"+num+"/Learn_32", "ir_learn_"+num+"/Learn_33", "ir_learn_"+num+"/Learn_34", "ir_learn_"+num+"/Learn_35", "ir_learn_"+num+"/Learn_36", "ir_learn_"+num+"/Learn_37", "ir_learn_"+num+"/Learn_38", "ir_learn_"+num+"/Learn_39", "ir_learn_"+num+"/Play_10", "ir_learn_"+num+"/Play_11", "ir_learn_"+num+"/Play_12", "ir_learn_"+num+"/Play_13", "ir_learn_"+num+"/Play_14", "ir_learn_"+num+"/Play_15", "ir_learn_"+num+"/Play_16", "ir_learn_"+num+"/Play_17", "ir_learn_"+num+"/Play_18", "ir_learn_"+num+"/Play_19", "ir_learn_"+num+"/Play_20", "ir_learn_"+num+"/Play_21", "ir_learn_"+num+"/Play_22", "ir_learn_"+num+"/Play_23", "ir_learn_"+num+"/Play_24", "ir_learn_"+num+"/Play_25", "ir_learn_"+num+"/Play_26", "ir_learn_"+num+"/Play_27", "ir_learn_"+num+"/Play_28", "ir_learn_"+num+"/Play_29", "ir_learn_"+num+"/Play_30", "ir_learn_"+num+"/Play_31", "ir_learn_"+num+"/Play_32", "ir_learn_"+num+"/Play_33", "ir_learn_"+num+"/Play_34", "ir_learn_"+num+"/Play_35", "ir_learn_"+num+"/Play_36", "ir_learn_"+num+"/Play_37", "ir_learn_"+num+"/Play_38", "ir_learn_"+num+"/Play_39"],
		then: function (newValue, devName, cellName) {
            nm = cellName.substring(0,1);
			if (nm == "P") {
				number = Number(cellName.substring(5,7)) - 9;
				log.info(cellName + " == " + newValue);
				//publish("zigbee2mqtt/"+ir_name+"/set", JSON.stringify({"send_key": {"id": 1,"key_code": number - 9}} ));
			}
            else if (nm == "I") {
				log.info(cellName);
				publish("zigbee2mqtt/"+ir_name+"/set", JSON.stringify({"create": {"model_type": 1}} ), 2, false);
			}
			else {
				number = Number(cellName.substring(6,8)) - 9;
				log.info(cellName + " == " + newValue);
				publish("zigbee2mqtt/"+ir_name+"/set", JSON.stringify({"learn": {"id": 1,"key_code": number}}));
			}
		}
		
	})
  
	defineVirtualDevice("virtual_cond_"+num, {
		title: "virtual_cond_"+num,
		cells: {
		Toggle: {   
            type : "switch",
            value : false
        }, 
		Speed : {
			type : "range",
			min : 0,
			max : 3,
			value : 0
		}, 
		Mode : {
			type : "range",
			min : 0,
			max : 4,
			value : 0
		}, 
		Setpoint : {
			type : "range",
			min : 16,
			max : 30,
			value : 23
		},
		}
	})
    defineRule ("control_"+num, {
        whenChanged: ["virtual_cond_"+num+"/Toggle", "virtual_cond_"+num+"/Speed", "virtual_cond_"+num+"/Mode", "virtual_cond_"+num+"/Setpoint"],
        then: function (newValue, devName, cellName) {
			log.info(devName+ " " + cellName + " == " + newValue);
			switch (cellName) {
				case "Toggle":
					if (newValue == true) {
						dev["ir_learn_"+num+"/Play_10"] = true; // Включить = 10
					}
					else {
						dev["ir_learn_"+num+"/Play_11"] = true; // Выключить = 11
					}
					break;
				case "Speed":
					dev["ir_learn_"+num+"/Play_"+(newValue+12).toString()] = true; // Скорость 12 = Авто, 13, 14, 15 = Мин средн. Макс
					break;
				case "Mode":
					dev["ir_learn_"+num+"/Play_"+(newValue+31).toString()] = true; // Режим 31 = Авто, 32 = Охлаждение, 33 = Нагрев, 34 = Вентиляция, 35 = Осушение
					break;
				case "Setpoint":
					dev["ir_learn_"+num+"/Play_"+newValue] = true; // Уставка от 16 до 30
					break;
			}
        }
    })
}

IR("1", "0x000d6f00153489ce");

Ну и - не воспроизводится такое потребление ресурсов…