尝试运行python脚本时抛出child_process。接收语法错误。
简而言之,我试图为homekit创建一个“附件”,该附件在value为true时执行Python脚本,并为false值运行一个单独的python脚本。对javascript noob的建议?
....
语法错误
/root/HAP-NodeJS-master/accessories/Light_accessory.js:85
format: "bool",
^
SyntaxError: Unexpected token :
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at accessories (/root/HAP-NodeJS-master/Core.js:21:24)
at Array.forEach (native)
at Object.<anonymous> (/root/HAP-NodeJS-master/Core.js:19:53)
at Module._compile (module.js:456:26)
root@raspberrypi:~/HAP-NodeJS-master#
........
码
// HomeKit types required
var types = require("./types.js")
var exports = module.exports = {};
var exec = require('child_process').exec;
var execute = function (accessory, characteristic, value) {
console.log("executed accessory: " + accessory + ", and characteristic: " + characteristic + ", with value: " + value + ".");
}
exports.accessory = {
displayName: "Light 1",
username: "1A:2B:3C:4D:5E:FF",
pincode: "031-45-154",
services: [{
sType: types.ACCESSORY_INFORMATION_STYPE,
characteristics: [{
cType: types.NAME_CTYPE,
onUpdate: null,
perms: ["pr"],
format: "string",
initialValue: "Light 1",
supportEvents: false,
supportBonjour: false,
manfDescription: "Bla",
designedMaxLength: 255
}, {
cType: types.MANUFACTURER_CTYPE,
onUpdate: null,
perms: ["pr"],
format: "string",
initialValue: "Oltica",
supportEvents: false,
supportBonjour: false,
manfDescription: "Bla",
designedMaxLength: 255
}, {
cType: types.MODEL_CTYPE,
onUpdate: null,
perms: ["pr"],
format: "string",
initialValue: "Rev-1",
supportEvents: false,
supportBonjour: false,
manfDescription: "Bla",
designedMaxLength: 255
}, {
cType: types.SERIAL_NUMBER_CTYPE,
onUpdate: null,
perms: ["pr"],
format: "string",
initialValue: "A1S2NASF88EW",
supportEvents: false,
supportBonjour: false,
manfDescription: "Bla",
designedMaxLength: 255
}, {
cType: types.IDENTIFY_CTYPE,
onUpdate: null,
perms: ["pw"],
format: "bool",
initialValue: false,
supportEvents: false,
supportBonjour: false,
manfDescription: "Identify Accessory",
designedMaxLength: 1
}]
}, {
sType: types.LIGHTBULB_STYPE,
characteristics: [{
cType: types.NAME_CTYPE,
onUpdate: null,
perms: ["pr"],
format: "string",
initialValue: "Light 1",
supportEvents: false,
supportBonjour: false,
manfDescription: "Bla",
designedMaxLength: 255
}, {
cType: types.POWER_STATE_CTYPE,
onUpdate: function (value) {
exec('python /home/pi/Desktop/Projects/Lights/on.py' + value, function (error, stdout, stderr) {
});
perms: ["pw", "pr", "ev"],
format: "bool",
initialValue: false,
supportEvents: false,
supportBonjour: false,
manfDescription: "Turn On the Light",
designedMaxLength: 1
},
{
cType: types.HUE_CTYPE,
onUpdate: function (value) {
console.log("Change:", value);
execute("Test Accessory 1", "Light - Hue", value);
},
perms: ["pw", "pr", "ev"],
format: "int",
initialValue: 0,
supportEvents: false,
supportBonjour: false,
manfDescription: "Adjust Hue of Light",
designedMinValue: 0,
designedMaxValue: 360,
designedMinStep: 1,
unit: "arcdegrees"
},
{
cType: types.BRIGHTNESS_CTYPE,
onUpdate: function (value) {
console.log("Change:", value);
execute("Test Accessory 1", "Light - Brightness", value);
},
perms: ["pw", "pr", "ev"],
format: "int",
initialValue: 0,
supportEvents: false,
supportBonjour: false,
manfDescription: "Adjust Brightness of Light",
designedMinValue: 0,
designedMaxValue: 100,
designedMinStep: 1,
unit: "%"
},
{
cType: types.SATURATION_CTYPE,
onUpdate: function (value) {
console.log("Change:", value);
execute("Test Accessory 1", "Light - Saturation", value);
},
perms: ["pw", "pr", "ev"],
format: "int",
initialValue: 0,
supportEvents: false,
supportBonjour: false,
manfDescription: "Adjust Saturation of Light",
designedMinValue: 0,
designedMaxValue: 100,
designedMinStep: 1,
unit: "%"
}]
}]
}
最佳答案
让我重新指出有问题的部分,以便您可以更轻松地了解出了什么问题。标记的行包含两个不在您的代码中的字符。
{
cType: types.POWER_STATE_CTYPE,
onUpdate:
function (value) {
exec('python /home/pi/Desktop/Projects/Lights/on.py' + value,
function (error, stdout, stderr) {
}
);
}, // <---- MISSING
perms: ["pw", "pr", "ev"],
format: "bool",
....
没有大括号和逗号,该对象在您的代码中解释为:
{
cType: types.POWER_STATE_CTYPE,
onUpdate:
function (value) {
exec('python /home/pi/Desktop/Projects/Lights/on.py' + value,
function (error, stdout, stderr) {
}
);
perms: ["pw", "pr", "ev"],
format: "bool",
....
在这里,将
perms:
用作标签,将["pw", "pr", "ev"], format
用作表达式(也是语句),然后您将找到一个:
,该永远不应跟随语句,而只能跟随对象内部的键或声明前的标签;而且都不是表达式。因此,语法错误。关于javascript - 在node.js SyntaxError中运行python脚本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27916510/