-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
170 lines (119 loc) · 4.53 KB
/
index.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
163
164
165
166
167
168
169
170
'use strict';
const path = require('path');
const { execSync, exec } = require('child_process');
let Service;
let Characteristic;
/**
* Platform "KFX210"
*/
function KFX210Platform(log, config) {
this.log = log;
this.config = config;
}
KFX210Platform.prototype.accessories = function(callback) {
this.kFX210Accessory = new KFX210Accessory(this.log, this.config);
callback( [ this.kFX210Accessory ] );
};
/**
* Accessory "KFX210"
*/
function KFX210Accessory(log, config) {
this.log = log;
this.name = 'Velux KFX210';
this.statePollInterval = config.state_poll_interval || 3;
this.comfortSwitchTime = config.comfort_switch_time || 0.5;
this.pythonPath = config.python_path || '/usr/bin/python';
this.alarm = false;
this.error = false;
this.comfort = false;
this.alarmService = new Service.ContactSensor('Alarm', 'alarm');
this.alarmService.getCharacteristic(Characteristic.ContactSensorState).on('get', this.getAlarm.bind(this));
this.errorService = new Service.ContactSensor('Error', 'error');
this.errorService.getCharacteristic(Characteristic.ContactSensorState).on('get', this.getError.bind(this));
this.comfortService = new Service.Switch('Comfort', 'comfort');
this.comfortService.getCharacteristic(Characteristic.On)
.on('get', this.getComfort.bind(this))
.on('set', this.setComfort.bind(this));
this.accessoryInformationService = new Service.AccessoryInformation();
this.accessoryInformationService.setCharacteristic(Characteristic.Manufacturer, "vectronic");
this.accessoryInformationService.setCharacteristic(Characteristic.Model, "Velux KFX210");
this.inputScript = path.join(__dirname, 'input.py');
this.log(`inputScript: ${this.inputScript}`);
this.relayScript = path.join(__dirname, 'relay.py');
this.log(`relayScript: ${this.relayScript}`);
this.log(`pythonPath: ${this.pythonPath}`);
this.startStateTimeout();
}
KFX210Accessory.prototype.startStateTimeout = function() {
const that = this;
this.stateTimeout = setTimeout((function() {
try {
const alarmState = execSync(`${this.pythonPath} ${that.inputScript} 1`);
that.log(`alarmState result: ${alarmState.toString()}`);
that.alarm = alarmState.toString().startsWith('1');
const errorState = execSync(`${this.pythonPath} ${that.inputScript} 2`);
that.log(`errorState result: ${errorState.toString()}`);
that.error = errorState.toString().startsWith('1');
}
catch (err) {
that.log(`stateTimeout error: ${err}`);
}
this.startStateTimeout();
}).bind(this), this.statePollInterval * 1000);
this.stateTimeout.unref();
};
KFX210Accessory.prototype.getComfort = function(callback) {
this.log(`Getting current value of Comfort: ${this.comfort}`);
callback(null, this.comfort);
};
KFX210Accessory.prototype.execRelay = function(relayNumber, callback) {
const that = this;
exec(`${this.pythonPath} ${this.relayScript} ${relayNumber} ${this.comfortSwitchTime}`, (error, stdout, stderr) => {
if (error) {
that.log(`setComfort exec error: ${error}`);
callback(error);
return;
}
that.log(`setComfort stdout: ${stdout}`);
that.log(`setComfort stderr: ${stderr}`);
callback();
});
};
KFX210Accessory.prototype.setComfort = function(comfort, callback) {
this.log(`Request to set current value of Comfort to: ${comfort}`);
this.comfort = comfort;
try {
if (this.comfort) {
this.execRelay(1, callback);
}
else {
this.execRelay(2, callback);
}
}
catch (err) {
this.log(`setComfort error: ${err}`);
callback(err);
}
};
KFX210Accessory.prototype.getAlarm = function(callback) {
this.log(`Getting current value of Alarm: ${this.error}`);
callback(null, this.alarm);
};
KFX210Accessory.prototype.getError = function(callback) {
this.log(`Getting current value of Error: ${this.error}`);
callback(null, this.error);
};
KFX210Accessory.prototype.getServices = function() {
this.log('getServices');
return [
this.accessoryInformationService,
this.comfortService,
this.alarmService,
this.errorService
];
};
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerPlatform("homebridge-plugin-velux-KFX210", "KFX210", KFX210Platform);
};