forked from tedstriker/node-red-contrib-homekit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
homekit.js
162 lines (137 loc) · 5.03 KB
/
homekit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
module.exports = function (RED) {
'use strict'
var API = require('./lib/api.js')(RED)
var HapNodeJS = require('hap-nodejs')
var Accessory = HapNodeJS.Accessory
var Service = HapNodeJS.Service
var Characteristic = HapNodeJS.Characteristic
var uuid = HapNodeJS.uuid
Service.prototype.setCharacteristicWithContext = function(name, value, context) {
this.getCharacteristic(name).setValue(value, null, context);
return this; // for chaining
}
// Initialize our storage system
if (RED.settings.available()) {
var userDir = RED.settings.userDir
HapNodeJS.init(userDir + '/homekit-persist')
} else {
HapNodeJS.init()
}
// Initialize API
API.init()
function HAPAccessoryNode (n) {
RED.nodes.createNode(this, n)
// config node properties
this.name = n.accessoryName
this.pinCode = n.pinCode
this.port = n.port
this.manufacturer = n.manufacturer
this.serialNo = n.serialNo
this.model = n.model
this.accessoryType = n.accessoryType
// generate UUID and username (MAC-address) from node id
var accessoryUUID = uuid.generate(this.id)
var accessoryUsername = macify(this.id)
// create accessory object
var accessory = new Accessory(this.name, accessoryUUID)
accessory.getService(Service.AccessoryInformation)
.setCharacteristic(Characteristic.Manufacturer, this.manufacturer)
.setCharacteristic(Characteristic.SerialNumber, this.serialNo)
.setCharacteristic(Characteristic.Model, this.model)
// publish accessory
accessory.publish({
username: accessoryUsername,
pincode: this.pinCode,
port: this.port || 0,
category: this.accessoryType
}, true)
this.on('close', function () {
accessory.destroy()
})
this.accessory = accessory
}
RED.nodes.registerType('homekit-accessory', HAPAccessoryNode)
function HAPServiceNode (n) {
RED.nodes.createNode(this, n)
// service node properties
this.name = n.name
this.serviceName = n.serviceName
this.configNode = RED.nodes.getNode(n.accessory)
// generate UUID from node id
var subtypeUUID = uuid.generate(this.id)
// add service
var accessory = this.configNode.accessory
var service = accessory.addService(Service[this.serviceName], this.name, subtypeUUID)
this.service = service
var node = this
// the pinCode should be shown to the user until interaction with
// iOS client starts
node.status({fill: 'yellow', shape: 'ring', text: node.configNode.pinCode})
// emit message when value changes
service.on('characteristic-change', function (info) {
var msg = { payload: {}, hap: info, name: node.name }
var key = info.characteristic.displayName.replace(/ /g, '')
msg.payload[key] = info.newValue
node.status({fill: 'yellow', shape: 'dot', text: key + ': ' + info.newValue})
setTimeout(function () { node.status({}) }, 3000)
node.send(msg)
})
// which characteristics are supported?
var supported = { read: [], write: []}
var allCharacteristics = service.characteristics.concat(service.optionalCharacteristics)
allCharacteristics.map(function (characteristic, index) {
var cKey = characteristic.displayName.replace(/ /g, '')
if (characteristic.props.perms.indexOf('pw') > -1) {
supported.read.push(cKey)
}
if ((characteristic.props.perms.indexOf('pr') + characteristic.props.perms.indexOf('ev')) > -2) {
supported.write.push(cKey)
}
})
// respond to inputs
this.on('input', function (msg) {
if (msg.hasOwnProperty('payload')) {
// payload must be an object
var type = typeof msg.payload
if (type != 'object') {
node.warn('Invalid payload type: ' + type)
return
}
} else {
node.warn('Invalid message (payload missing)')
return
}
var context = null;
if (msg.payload.hasOwnProperty('Context')) {
context = msg.payload.Context;
delete msg.payload.Context;
}
// iterate over characteristics to be written
Object.keys(msg.payload).map(function (key, index) {
if (supported.write.indexOf(key) < 0) {
// characteristic is not supported
node.warn('Characteristic ' + key + ' cannot be written.\nTry one of these: ' + supported.write.join(', '))
} else {
if (context !== null) {
service.setCharacteristicWithContext(Characteristic[key], (msg.payload[key]), context)
} else {
service.setCharacteristic(Characteristic[key], (msg.payload[key]))
}
}
})
})
this.on('close', function () {
accessory.removeService(service)
})
}
RED.nodes.registerType('homekit-service', HAPServiceNode)
}
function pad (str, length) {
return (str.length < length) ? pad('0' + str, length) : str
}
function macify (nodeId) {
var noDecimalStr = nodeId.replace('.', '')
var paddedStr = pad(noDecimalStr, 16)
var macifiedStr = paddedStr.match(/.{1,2}/g).join(':')
return macifiedStr
}