我在我的Raspberry Pi上构建我的第一个node.js应用程序,我用它来通过LIRC控制空调。当您想要提高AC单元的温度时,会调用以下代码。它每250毫秒发送一次LIRC命令,具体取决于你希望增加多少度。此代码按预期工作。
var iDegrees = 5;
var i = 0;
var delay = 250 // The delay in milliseconds
function increaseTemperatureLoop(){
i++;
//lirc_node.irsend.send_once("ac", "INCREASE", function() {});
console.log(i);
// Call the fucntion/loop again after the delay if we still need to increase the temperature
if (i <= iDegrees){
timer = setTimeout(increaseTemperatureLoop, delay);
}
else {
res.json({"message": "Success"});
}
}
// Start the timer to call the recursive function for the first time
var timer = setTimeout(increaseTemperatureLoop, delay);
我很难处理node.js的异步特性。一旦我的递归函数完成,我将我的json返回到浏览器,如上面的代码所示。根据习惯,我觉得我应该在我的初始函数调用之后在一行代码中返回json,但显然不会等待所有LIRC调用成功 - 将它放在内部似乎很愚蠢功能:
var timer = setTimeout(increaseTemperatureLoop, delay);
res.json({"message": "Success"});
如果我在LIRC发送完成之后但在我想将json发送回浏览器之前还有其他一些事情要做,该怎么办?或者,如果该代码块引发错误...
我的第二个问题是,如何在try / catch中正确包装LIRC调用,然后如果出现错误,请停止递归调用,将错误重新传递回来,然后将其传回浏览器实际的错误信息:
res.json({"message": "Failed"});
答案 0 :(得分:0)
反思的一些例子:
var lircTasks = function __self (){
if (typeof __self.tasks === "undefined") __self.tasks = 0;
__self.func = {
increaseTemperature: function() {
// lirc_node.irsend.send_once("ac", "INCREASE_TEMPERATURE", function() {});
},
increaseFanPower: function() {
// lirc_node.irsend.send_once("ac", "INCREASE_FANPOWER", function() {});
}
}
var fab = function () {
__self.tasks++;
this.i = 0;
this.args = arguments[0];
this.callback = arguments[1];
this.run = function __ref(taskName) {
if (taskName) this.taskName = taskName;
if (this.i<this.args.deg) {
try {
__self.func[this.taskName]();
} catch(e) {
__self.tasks--;
this.callback( {message: "error", error: e, taskName: this.taskName, task: this.args, tasks: __self.tasks} );
}
this.i++;
setTimeout( __ref.bind(this), this.args.delay );
} else {
__self.tasks--;
this.callback({message:"complete", taskName: this.taskName, task: this.args, tasks: __self.tasks});
}
}
}
if ((arguments.length === 2) && (typeof arguments[1] === "function") && arguments[0].deg>0 && arguments[0].delay>=0) {
return new fab(arguments[0], arguments[1]);
}
}
function complete(e) {
console.log(e);
if (e.tasks === 0) console.log({message: "Success"});
}
lircTasks( {deg: 10, delay:100, device: "d1" }, complete ).run("increaseTemperature");
lircTasks( {deg: 20, delay:150, device: "d2" }, complete ).run("increaseTemperature");
lircTasks( {deg: 5, delay:100, device: "d3" }, complete ).run("increaseFanPower");