0

This project turns appliances and such On/off using Apple's homekit and a node.js server on raspberry pi (https://github.com/KhaosT/HAP-NodeJS). So Light_accessory.js, when vaule is true(1), turns a lamp(relay) on using a child process and wiring pi. It also needs to turn the lamp off (gpio write 7 1) when value is false(0). I have tried adding IF statements to it (near the end), but I keep getting syntax errors from the if statements.. Don't know why. //frustrated.

// 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",
    username: "1A:2B:4A: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 0",
            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:

                if ((function(value)) == 1) {
                    exec('gpio write 7 0' + value,function(error, stdout, stderr) {}
                },
                if ((function(value)) == 0) {
                    exec('gpio write 7 1' + value,function(error, stdout, stderr) {}
                },
                );

            },
            perms: ["pw", "pr", "ev"],
            format: "bool",
            initialValue: false,
            supportEvents: false,
            supportBonjour: false,
            manfDescription: "Turn On the Light",
            designedMaxLength: 1
            },
        }]
    }]
}
Benjamin
  • 33
  • 1
  • 7
  • 2
    It seems what you need is a **function**: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions – Felix Kling Jan 14 '15 at 05:01
  • Could you give an example? – Benjamin Jan 14 '15 at 05:05
  • I wouldn't call myself a n00b, but I'm a n00b.. I can fumble through most languages by looking at other programs and putting the pieces together, but I could never sit down and write a program like this. A bit more educational handholding would be much appreciated if you can spare the time. – Benjamin Jan 14 '15 at 05:09
  • Well, you have to assign a function to `onUpdate` (I assume). `onUpdate: function() { if (...) .... },`. `(function(value))` is also invalid, I don't know what that's supposed to be. Maybe you mean the value passed to `onUpdate`, in that case: `onUpdate: function(value) { if (value == 1) .... },` – Felix Kling Jan 14 '15 at 05:10

1 Answers1

1

The first thing I noticed is you are mixing up an object and a code block. You can read about JSON which will give you a familiarity with javascript objects (not the same and here is a brief explanation) but hopefully this helps.

You can't put a code block directly into an object. an object has a key then a value. Notices your code here

onUpdate:
  if ((function(value)) == 1) { // ...

onUpdate would be your key, then you start directly into a code block.

So the way you get a block of code into your object is by using a function. if you do something like..

onUpdate: function () {
  if (value == 1) {
    // do something
  } else {
    // do something else
  }

That would make your onUpdate now be a function you could call.

I haven't read through everything in your code but this is for sure a syntactical problem that wont let you compile.

Also looking at your if ((function(value)) == 1) logic seems like a code smell to me (i.e. I am not sure that it is wrong but it seems funny). I would think you need a function name there instead of the keyword function but again I am not 100% sure on that.

Hope this gets you going in the right direction.

EDIT:

Now that I am at a desk I was re-reading your code and it looks like you have another problem with your export object. Your export object looks like this

export.accessory = {
  displayName : 'string-value',
  username : 'string-value',
  pincode : 'string-value',
  services : [{array: 'of-objects'}],
  }, { // LOOKIE HERE
   // fill in the rest
  }
}

Notice at the LOOKIE HERE part it actually is closing the export.accessory object. The , and continuation of the object is erronious. You could take that line out and it would fix that problem.

Remember that an object needs a {'key':'value'}, so you can't just set an object within an object it needs a name.

A good technique you could use to help manage this is to create the values before hand then just export them, look at this

var displayName = 'name';
var username = 'user-name';
var characteristics = [{},{},{}];
var someObject = {};
var someFunction = function () {};

export.accessory = {
  displayName : displayName,
  username : username,
  characteristics : characteristics,
  someObject : someObject,
  someFunction : someFunction
}

Notice in the object.accessory for each key/value pair the first word (to the left of the :) is the name of the key, the second word (to the right of the :) is the reference to the variable you created above.

I hope this helps.

Community
  • 1
  • 1
ChewOnThis_Trident
  • 2,105
  • 2
  • 18
  • 22