javascript - 很难理解 Javascript 中的 "Probably Equals"

标签 javascript node.js raspberry-pi gpio homekit

该项目使用 Apple 的 homekit 和树莓派 ( https://github.com/KhaosT/HAP-NodeJS ) 上的 node.js 服务器来打开/关闭设备等。所以 Light_accessory.js,当 vaule 为 true(1) 时,使用子进程和接线 pi 打开灯(继电器)。当值为 false(0)时,还需要关闭灯(gpio write 7 1)。 Iv 尝试添加“可能等于”,这样它也会关闭灯(继电器)。尝试添加两个值是我和一夜的谷歌搜索和语法错误的结果。 我在这个项目上花费的时间比我愿意承认的要多。很简单,这个目标与我不久前用 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');
    }
?>

...................................................... ...................................................... ...................................................... …………

这是当值为 true(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?这样灯(继电器)也会关闭。

...................................................... ...................................................... ...................................................... …………

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

尝试检查 true 然后写出 1

值 == true ? 1:0

根据要求,这里使用您的示例。 { cType:类型.POWER_STATE_CTYPE, onUpdate: 函数(值) { exec('gpio write 7 0 ' + (value == true ? 1 : 0) ,function(error, stdout, stderr) {} ); }, 权限:[“pw”,“pr”,“ev”], 格式:“ bool ”, 初始值:假, 支持事件:假, 支持 Bonjour: false, manf描述:“打开灯”, 设计最大长度:1 }

关于javascript - 很难理解 Javascript 中的 "Probably Equals",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27930473/

相关文章:

java - Raspberry PI GPIO Java 代码错误

javascript - 切换元素

javascript - 在单一值上使用 Lodash 链

javascript - GWT:如何从纯 js 访问它的 web 服务?

angularjs - ExpressJS AngularJS POST

node.js - 在nodejs中将错误消息发送回浏览器

Node.js异步编程——复制二进制文件流

python - 从ALSA Raspberry Pi获取音频振幅

python - Image Magick 错误/blob.c/OpenBlob/2638

javascript - jqplot转换为图像不显示y轴html