-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
156 lines (133 loc) · 5.22 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
var request = require('request');
var Service, Characteristic;
module.exports = function(homebridge){
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory('homebridge-tasmota-motor', 'TasmotaMotor', TasmotaMotor);
}
function TasmotaMotor(log, config){
this.log = log; // log file
this.name = config["name"];
this.hostname = config["hostname"];
this.durationUp = config["secondsUp"];
this.durationDown = config["secondsDown"];
this.lastPosition = 0; // Last known position, (0-100%)
this.currentPositionState = 2; // 2 = Stopped , 1=Moving Up , 0=Moving Down.
this.currentTargetPosition = 0; // Target Position, (0-100%)
this.infoService = new Service.AccessoryInformation();
this.infoService
.setCharacteristic(Characteristic.Manufacturer, "Sonoff")
.setCharacteristic(Characteristic.Model, "Sonoff T1 Motor")
.setCharacteristic(Characteristic.SerialNumber, "Version 1.0.0");
this.service = new Service.WindowCovering(this.name);
this.service
.getCharacteristic(Characteristic.CurrentPosition)
.on('get', this.getCurrentPosition.bind(this));
this.service
.getCharacteristic(Characteristic.PositionState)
.on('get', this.getPositionState.bind(this));
this.service
.getCharacteristic(Characteristic.TargetPosition)
.on('get', this.getTargetPosition.bind(this))
.on('set', this.setTargetPosition.bind(this));
}
TasmotaMotor.prototype.getCurrentPosition = function(callback) {
this.log("Requested CurrentPosition: %s", this.lastPosition);
callback(null, this.lastPosition);
}
TasmotaMotor.prototype.getPositionState = function(callback) {
this.log("Requested PositionState: %s", this.currentPositionState);
callback(null, this.currentPositionState);
}
TasmotaMotor.prototype.getTargetPosition = function(callback) {
this.log("Requested TargetPosition: %s", this.currentTargetPosition);
callback(null, this.currentTargetPosition);
}
TasmotaMotor.prototype.setTargetPosition = function(pos, callback) {
this.log("Setting target position to %s", pos);
if (this.currentPositionState != 2) {
this.log("Blinds are moving. You need to wait. I will do nothing.");
callback();
return false;
}
if (pos == 0 && this.lastPosition == 0) {
this.lastPosition = 100
}
if (pos == 100 && this.lastPosition == 100) {
this.lastPosition = 0
}
if (this.lastPosition == pos) {
this.log("Current position already matches target position. There is nothing to do.");
callback();
return true;
}
this.currentTargetPosition = pos;
var move = (this.currentTargetPosition > this.lastPosition);
var duration;
if (move) {
if(this.lastPosition==0){
duration = (this.currentTargetPosition - this.lastPosition) / 100 ;
} else
{
duration = (this.currentTargetPosition - this.lastPosition) / 100 ;
}
duration = duration * this.durationUp
} else {
if(this.currentTargetPosition==0){
duration = (this.lastPosition-this.currentTargetPosition) / 100;
} else
{
duration = (this.lastPosition-this.currentTargetPosition) / 100;
}
duration = duration * this.durationDown
}
this.log("Duration: %s s", duration.toFixed(1));
var that = this
var setFinalBlindsState = function() {
that.currentPositionState = 2;
that.service.setCharacteristic(Characteristic.PositionState, 2);
that.service.setCharacteristic(Characteristic.CurrentPosition, that.currentTargetPosition);
that.lastPosition = that.currentTargetPosition;
that.log("Successfully moved to target position: %s", that.currentTargetPosition);
callback();
}
this.httpRequest(move, duration, function(err) {
if(err) return callback(err);
that.log(move ? "Moving up" : "Moving down");
that.service.setCharacteristic(Characteristic.PositionState, (move ? 1 : 0));
that.currentPositionState = (move ? 1 : 0)
setTimeout(setFinalBlindsState, duration*1000);
});
return true;
}
TasmotaMotor.prototype.httpRequest = function(move, duration, callback){
var url, pulsetime, delay = 2
if (duration < 12) {
duration = duration * 10;
if (duration > 2) delay = duration
}
else {
delay = duration * 10
duration = duration + 100
}
if (move) {
if (this.durationUp < 12) pulsetime = this.durationUp * 10; else pulsetime = this.durationUp + 100
}
else {
if (this.durationDown < 12) pulsetime = this.durationDown * 10; else pulsetime = this.durationDown + 100
}
var m = move ? 2 : 1
url = 'http://' + this.hostname + '/cm?cmnd=backlog%20pulsetime'+m+'%20'+duration.toFixed(0)+';power'+m+'%20on;delay%20'+delay.toFixed(0)+';pulsetime'+m+'%20'+pulsetime.toFixed(0)
this.log("Sonoff link for moving blinds: " + url);
request.get({ url: url, }, function(err, response, body) {
if (!err && response && response.statusCode == 200) {
return callback(null);
} else {
this.log("Error communicating to: " + url, err);
return callback(err);
}
}.bind(this));
}
TasmotaMotor.prototype.getServices = function() {
return [this.infoService, this.service];
}