-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
157 lines (129 loc) · 5.98 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
const rpio = require('rpio');
let Service, Characteristic;
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory('homebridge-somfy-hotwired', 'Homebridge-somfy-hotwired', Somfy);
};
function Somfy(log, config) {
this.service = new Service.WindowCovering(this.name);
this.log = log;
if (config.default_position === "up") {
this.currentPosition = 100;
this.targetPosition = 100;
} else {
this.currentPosition = 0;
this.targetPosition = 0;
}
this.buttonPressDuration = 500;
this.positionState = Characteristic.PositionState.STOPPED;
this.pinUp = config['pin_up'];
this.pinDown = config['pin_down'];
this.pinMyPosition = config['pin_my_position'];
this.movementDuration = config['movement_duration'];
rpio.open(this.pinUp, rpio.OUTPUT, rpio.HIGH);
rpio.open(this.pinDown, rpio.OUTPUT, rpio.HIGH);
rpio.open(this.pinMyPosition, rpio.OUTPUT, rpio.HIGH);
}
Somfy.prototype = {
getCurrentPosition: function (callback) {
callback(null, this.currentPosition);
},
getTargetPosition: function (callback) {
callback(null, this.targetPosition);
},
setTargetPosition: function (position, callback) {
setTimeout(() => {
clearInterval(this.interval);
this.targetPosition = position;
if (this.targetPosition === 100) {
this.log('Opening shutters');
rpio.write(this.pinUp, rpio.LOW);
rpio.msleep(this.buttonPressDuration);
rpio.write(this.pinUp, rpio.HIGH);
this.intermediatePosition = false;
this.positionState = Characteristic.PositionState.DECREASING;
} else if (this.targetPosition === 10) {
this.log('Going to MySomfy position');
rpio.write(this.pinMyPosition, rpio.LOW);
rpio.msleep(this.buttonPressDuration);
rpio.write(this.pinMyPosition, rpio.HIGH);
this.intermediatePosition = false;
if (this.targetPosition > this.currentPosition) {
this.positionState = Characteristic.PositionState.INCREASING;
} else {
this.positionState = Characteristic.PositionState.DECREASING;
}
} else if (this.targetPosition === 0) {
this.log('Closing shutters');
rpio.write(this.pinDown, rpio.LOW);
rpio.msleep(this.buttonPressDuration);
rpio.write(this.pinDown, rpio.HIGH);
this.intermediatePosition = false;
this.positionState = Characteristic.PositionState.INCREASING;
} else {
this.log('Opening shutters to %i percent', this.targetPosition);
let sleepTime = this.movementDuration / 90 * this.targetPosition;
this.log('Operation will be stopped after %i seconds', sleepTime);
let pin = null;
if (this.targetPosition > this.currentPosition) {
pin = this.pinUp;
} else {
pin = this.pinDown
}
rpio.write(pin, rpio.LOW);
rpio.msleep(this.buttonPressDuration);
rpio.write(pin, rpio.HIGH);
this.intermediatePosition = true;
this.positionState = Characteristic.PositionState.INCREASING;
}
this.interval = setInterval(() => {
if (this.currentPosition !== this.targetPosition) {
if (this.targetPosition > this.currentPosition) {
this.currentPosition += 10;
} else {
this.currentPosition -= 10;
}
this.service.getCharacteristic(Characteristic.CurrentPosition).updateValue(this.currentPosition);
} else {
if (this.intermediatePosition) {
rpio.write(this.pinMyPosition, rpio.LOW);
rpio.msleep(this.buttonPressDuration);
rpio.write(this.pinMyPosition, rpio.HIGH);
}
this.log('Operation completed!');
this.positionState = Characteristic.PositionState.STOPPED;
this.service.getCharacteristic(Characteristic.PositionState).updateValue(this.positionState);
clearInterval(this.interval);
}
}, this.movementDuration * 100);
}, 0);
callback(null);
},
getPositionState: function (callback) {
callback(null, this.positionState);
},
getServices: function () {
const informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, "Somfy")
.setCharacteristic(Characteristic.Model, "Telis 1 RTS")
.setCharacteristic(Characteristic.SerialNumber, "1337");
const currentPositionChar = this.service.getCharacteristic(Characteristic.CurrentPosition);
currentPositionChar.on('get', this.getCurrentPosition.bind(this));
const targetPositionChar = this.service.getCharacteristic(Characteristic.TargetPosition);
targetPositionChar.setProps({
format: Characteristic.Formats.UINT8,
unit: Characteristic.Units.PERCENTAGE,
maxValue: 100,
minValue: 0,
minStep: 10,
perms: [Characteristic.Perms.READ, Characteristic.Perms.WRITE, Characteristic.Perms.NOTIFY]
});
targetPositionChar.on('get', this.getTargetPosition.bind(this));
targetPositionChar.on('set', this.setTargetPosition.bind(this));
this.service.getCharacteristic(Characteristic.PositionState)
.on('get', this.getPositionState.bind(this));
return [informationService, this.service];
}
};