很难理解 Javascript 中的 "Probably Equals"

Having a hard time understanding "Probably Equals" in Javascript

此项目使用 Apples homekit 和 raspberry pi(https://github.com/KhaosT/HAP-NodeJS) 上的 node.js 服务器来转换电器等 On/off。所以 Light_accessory.js,当 vaule 为 true(1) 时,使用 childprocces 和 wiring pi 打开 lamp(中继)。它还需要在值为 false(0) 时关闭 lamp(gpio write 7 1)。我尝试向其中添加 "probably equals",这样它也会关闭 lamp(继电器)。尝试添加两个值是我和一夜谷歌搜索和语法错误的结果。 我在这个项目上花费的时间比我愿意承认的要多得多。很简单,objective 与我不久前用 php 做的一个项目非常相似。

?php 
    if(isset($_GET['trigger']) && $_GET['trigger'] == 1) {
        error_reporting(E_ALL);
        exec('gpio write 7 0');
    }
    if(isset($_GET['trigger']) && $_GET['trigger'] == 2) {
        error_reporting(E_ALL);
        exec('gpio write 7 1');
    }
?>

................................................ ..................................................... ..................................................... .....................

这是当值为真(1) 时gpio 设置为低(gpio write 7 0) 的地方。

{
            cType: types.POWER_STATE_CTYPE,
            onUpdate: function(value) {
                exec('gpio write 7 0' + value,function(error, stdout, stderr) {}
                );
            },
            perms: ["pw", "pr", "ev"],
            format: "bool",
            initialValue: false,
            supportEvents: false,
            supportBonjour: false,
            manfDescription: "Turn On the Light",
            designedMaxLength: 1
        }, {

如何添加

exec('gpio write 7 1'(error, stdout, stderr)

当 javascript 中的值为 0 时?这样 lamp(继电器)也会关闭。

................................................ ..................................................... ..................................................... .....................

Light_accessory.js 的粗略概述;顶部主要是产品描述而不是实际功能。 "OnUpdate:" 下的 "cType: types.POWER_STATE_CTYPE," 是魔法发生的地方。

................................................ ..................................................... ..................................................... .....................

完整Light_accessory脚本

// 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('gpio write 7 0' + 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: "%"
        }]
    }]
}

Fromm 自己弄乱了这个包我确实注意到值是 'true' 或 'false' 而不是 1 或 0

尝试检查是否为真然后写出 1

value == true ? 1 : 0

根据要求,这里使用您的示例。 { cType: types.POWER_STATE_CTYPE, onUpdate: function(value) { exec('gpio write 7 0 ' + (value == true ? 1 : 0) ,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.POWER_STATE_CTYPE,
            onUpdate: function(value) {
                if(value == 1){
                    exec('gpio write 7 0')
                    setTimeout(function() {exec('gpio write 7 1')},250)

                }
                if(value == 0){
                    exec('gpio write 6 0')
                    setTimeout(function() {exec('gpio write 6 1')},250)
                }
            },
            perms: ["pw", "pr", "ev"],
            format: "bool",
            initialValue: false,
            supportEvents: false,
            supportBonjour: false,
            manfDescription: "Turn On the Light",
            designedMaxLength: 1
        },