运行 python 脚本在 node.js SyntaxError

Running python script in node.js SyntaxError

正在尝试 运行 python 脚本抛出 child_process。收到语法错误.. 简而言之 shell 我正在尝试为 homekit 创建一个 "accessory",它在值为 true 时执行 Python 脚本,而一个单独的 python 脚本在值为 false 时执行 运行s价值。对 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: "%"
            }]
        }]
    }

让我重新缩进有问题的部分,这样你就可以更容易地看到哪里出了问题。标记的行包含两个不在您的代码中的字符。

{
  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当作一个表达式(也是一个语句),然后你找到一个永远不应该跟在语句后面的: ,但只能是对象内部的键,或者语句之前的标签; neither 不能是一个表达式。因此语法错误。