forked from anthonywebb/homebridge-cbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
accessory.js
executable file
·93 lines (74 loc) · 2.62 KB
/
accessory.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
'use strict';
let Service;
let Characteristic;
let Accessory;
let uuid;
require('../hot-debug.js');
const log = require('debug')('cbus:accessory');
const chalk = require('chalk'); // does not alter string prototype
const cbusUtils = require('../lib/cbus-utils.js');
const CBusNetId = require('../lib/cbus-netid.js');
module.exports = function (_service, _characteristic, _accessory, _uuid) {
Service = _service;
Characteristic = _characteristic;
Accessory = _accessory;
uuid = _uuid;
return CBusAccessory;
};
function CBusAccessory(platform, accessoryData) {
// type is absolutely required
console.assert(typeof accessoryData.type !== `undefined`, `accessoryData.type must not be undefined`);
this.client = platform.client;
this.accessoryData = accessoryData;
this.type = accessoryData.type;
// ensure we have a name
if (typeof accessoryData.name !== `string`) {
throw new Error(`missing required 'name' field`);
}
this.name = accessoryData.name;
// ensure we have a valid group address
if (typeof accessoryData.id === `undefined`) {
throw new Error(`accessory '${this.name} missing required 'id' (group address) field`);
}
let groupAddress;
try {
groupAddress = cbusUtils.integerise(accessoryData.id);
} catch (err) {
throw new Error(`id '${accessoryData.id}' for accessory '${this.name} is not an integer`);
}
if (typeof accessoryData.action !== `undefined`) {
let actionValue;
try {
actionValue = cbusUtils.integerise(accessoryData.action);
} catch (err) {
throw new Error(`action value '${accessoryData.action}' for accessory '${this.name} is not an integer`);
}
}
// build netId
this.netId = new CBusNetId(
platform.project,
accessoryData.network || platform.client.network,
accessoryData.application || platform.client.application,
groupAddress
);
// fire our parent
const ourUUID = uuid.generate(this.netId.getHash());
Accessory.call(this, this.name, ourUUID);
// setup service
const service = this.getService(Service.AccessoryInformation);
// configure service
service.setCharacteristic(Characteristic.Manufacturer, `Clipsal C-Bus`);
service.setCharacteristic(Characteristic.SerialNumber, this.netId.toString());
service.setCharacteristic(Characteristic.Model, this.type);
}
CBusAccessory.prototype.getNetIds = function () {
return [this.netId];
};
CBusAccessory.prototype.getServices = function () {
return this.services;
};
CBusAccessory.prototype._log = function (file, action, message) {
const fileName = file.split(`-`)[0];
const accessoryLabel = cbusUtils.formatTag(this.name, this.netId);
log(`${chalk.gray.bold(fileName)} ${accessoryLabel} ${chalk.red(action)} ${message}`);
};