[英]How to structure an express app to run a light strip on a Raspberry Pi3
I've set up a node server on a raspberry pi to display a sequence of colors on an Adafruit Dotstar light strip. 我已经在树莓派上设置了一个节点服务器,以在Adafruit Dotstar灯条上显示一系列颜色。 The functionality works as follows: I make an HTTP request to
localhost:8000/fade
, and the server responds by running the fade.js file, which is an infinite loop that fades through different colors on the light strip. 该功能的工作方式如下:我向
localhost:8000/fade
发出HTTP请求,服务器通过运行fade.js文件进行响应,该文件是一个无限循环,通过光带上的不同颜色淡入淡出。 Unfortunately, I'd like to be able to exit this command and shut off the light strip with another request to localhost:8000/off
. 不幸的是,我希望能够退出此命令,并通过另一个对
localhost:8000/off
请求来关闭灯带。
I've experimented with the child_process
package in order to run the "fade" code, while also listening to new requests. 我已经尝试过
child_process
包,以便运行“淡入淡出”代码,同时还监听新的请求。 However, I'm unable to kill the "fade" process. 但是,我无法终止“淡入淡出”过程。
Posted below is my app.js code. 下面发布的是我的app.js代码。 Any suggestions on how to kill the child_process, or perhaps restructure the code in some other way to accomplish the same goal?
关于如何终止child_process或以其他方式重组代码以实现相同目标的任何建议? I really just need to be able to run the "fade" code continuously, while also responding to new requests.
我真的只需要能够连续运行“淡入淡出”代码,同时还要响应新的请求。
ps This is my first JS project so go easy! ps这是我的第一个JS项目,请轻松! Any help is appreciated.
任何帮助表示赞赏。
app.js: app.js:
var express = require('express'),
app = express();
app.get('/', function (req, res) {
res.send('App is responding to requests');
});
app.get('/fade', function (req, res) {
var fork = require('child_process').fork;
child = fork('./sequences/fade.js');
});
app.get('/off', function (req, res) {
var fork = require('child_process').fork;
child = fork('./sequences/off.js');
});
app.listen(8000, function () {
console.log('Example app listening on port 8000!')
})
fade.js: fade.js:
console.log("running fade.js");
var dotstar = require('dotstar'),
SPI = require('pi-spi'),
sleep = require('sleep');
spi = SPI.initialize('/dev/spidev0.0');
const ledStripLength = 30;
const ledStrip = new dotstar.Dotstar(spi, {
length: ledStripLength
});
while(1) {
fade(); //where fade is a long sequence of colors
};
var child; // Outer scope, available in both functions.
app.get('/fade', function (req, res) {
var fork = require('child_process').fork;
child = fork('./sequences/fade.js');
});
app.get('/off', function (req, res) {
// (might be a good idea to check if child exists and is running first...)
child.kill('SIGHUP');
});
Also http://programmergamer.blogspot.com/2013/05/clarification-on-sigint-sigterm-sigkill.html 也是http://programmergamer.blogspot.com/2013/05/clarification-on-sigint-sigterm-sigkill.html
SIGHUP: ... A process has to explicitly handle this signal for it to work.
SIGHUP:...一个进程必须显式处理此信号才能起作用。 ...
...
So SIGINT
if you want to close without handling it on the fork side, or SIGKILL
to just kill that mofo. 因此,如果要关闭而不在前叉侧对其进行操作,则请使用
SIGINT
,或者SIGKILL
以杀死该Mofo。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.