尝试运行 python 脚本会抛出 child_process。收到一个 SyntaxError .. 简而言之,我试图为 homekit 创建一个“附件”,当值为 true 时执行 Python 脚本,并为 false 值运行单独的 python 脚本。对 JavaScript 菜鸟的建议?
……
语法错误
/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: "%"
}]
}]
}