0

该项目使用 Apple 的 homekit 和树莓派上的 node.js 服务器(https://github.com/KhaosT/HAP-NodeJS)打开/关闭电器等。因此 Light_accessory.js,当 vaule 为 true(1) 时,使用 childprocces 和接线 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: "%"
        }]
    }]
}
4

2 回答 2

1

弗洛姆自己弄乱了这个包我确实注意到值是“真”或“假”而不是 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 }

于 2015-01-13T21:11:01.053 回答
0
{
            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
        }, 
于 2015-01-18T22:25:42.757 回答