-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblinds.js
More file actions
161 lines (140 loc) · 4.46 KB
/
Copy pathblinds.js
File metadata and controls
161 lines (140 loc) · 4.46 KB
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
const DOWN = "down";
const UP = "up";
const DEFAULT_SPEED = 5;
/**
* 0 represents the blinds at the very bottom (fully closed)
* maxSteps represents the blinds at the very top (fully open)
*/
class Blinds {
constructor(motor, initialBlindsPosition, maxSteps) {
if (isNaN(maxSteps) || maxSteps < 0) {
throw "maxSteps needs to be a number above 0, but was " + maxSteps;
}
if (isNaN(initialBlindsPosition) || initialBlindsPosition < 0) {
throw (
"initialBlindsPosition has to be a number above 0, but was " +
initialBlindsPosition
);
}
if (initialBlindsPosition > maxSteps) {
throw "initialBlindsPosition has to be lass or equal to maxSteps";
}
this.motor = motor;
this.currentBlindsPosition = initialBlindsPosition;
this.maxSteps = maxSteps;
this.blindsInMotion = false;
this.blindsStatusObservers = [];
this.blindsPositionObservers = [];
this.blindsResetObservers = [];
}
/**
* 0% represents the blinds at the very bottom (fully closed)
* 100% represents the blinds at the very top (fully open)
*/
async moveToPosition(percentage, speed = DEFAULT_SPEED) {
let steps =
Math.floor((percentage / 100) * this.maxSteps) -
this.currentBlindsPosition;
let dir = steps > 0 ? UP : DOWN;
await this.move(Math.abs(steps), dir, speed);
}
async moveUpToEnd(speed = DEFAULT_SPEED) {
this.moveToPosition(/* percentage = */ 100, speed);
}
async moveDownToEnd(speed = DEFAULT_SPEED) {
this.moveToPosition(/* percentage = */ 0, speed);
}
async moveUp(steps, speed = DEFAULT_SPEED) {
const maxSafeSteps = Math.min(steps, this.maxSteps - this.currentBlindsPosition);
await this.move(maxSafeSteps, UP, speed);
}
async moveDown(steps, speed = DEFAULT_SPEED) {
const maxSafeSteps = Math.min(steps, this.currentBlindsPosition);
await this.move(maxSafeSteps, DOWN, speed);
}
async move(steps, dir, speed) {
if (this.blindsInMotion) {
return;
}
this.setBlindsInMotion(true);
let lastBlindsPosition = this.currentBlindsPosition;
return new Promise((resolve, reject) => this.motor.move(steps, dir, speed, (message) => {
const counter = parseInt(
message.split(/\r?\n/).reverse().find((s) => !isNaN(s) && !isNaN(parseInt(s))))
if (!counter) {
return;
}
let newBlindsPosition = lastBlindsPosition + (dir == UP ? counter : -counter);
if (
newBlindsPosition < 0 ||
newBlindsPosition > this.maxSteps
) {
//this.stopBlinds();
} else {
this.setBlindsPosition(newBlindsPosition);
}
},
(exitCode) => {
this.setBlindsInMotion(false);
resolve();
}));
}
stopBlinds() {
this.motor.stop();
}
setBlindsInMotion(inMotion) {
this.blindsInMotion = inMotion;
this.blindsStatusObservers.forEach((observer) => observer(inMotion));
}
setBlindsPosition(position) {
this.currentBlindsPosition = position;
this.blindsPositionObservers.forEach((observer) =>
observer(position, this.maxSteps)
);
}
getBlindsPosition() {
return this.currentBlindsPosition;
}
resetBlindsUp() {
if (this.blindsInMotion) {
return;
}
this.currentBlindsPosition = this.maxSteps;
this.blindsResetObservers.forEach((observer) => observer(this.currentBlindsPosition));
}
resetBlindsDown() {
if (this.blindsInMotion) {
return;
}
this.currentBlindsPosition = 0;
this.blindsResetObservers.forEach((observer) => observer(this.currentBlindsPosition));
}
registerBlindsPositionObservers(observers) {
this.blindsPositionObservers.push(...observers);
}
unregisterBlindsPositionObservers(observers) {
this.blindsPositionObservers = this.blindsPositionObservers.filter(
(item) => !observers.includes(item)
);
}
registerBlindsStatusObservers(observers) {
this.blindsStatusObservers.push(...observers);
observers.forEach((observer) => observer(this.blindsInMotion));
}
unregisterBlindsStatusObservers(observers) {
this.blindsStatusObservers = this.blindsStatusObservers.filter(
(item) => !observers.includes(item)
);
}
registerBlindsResetObservers(observers) {
this.blindsResetObservers.push(...observers);
}
unregisterBlindsResetObservers(observers) {
this.blindsResetObservers = this.blindsResetObservers.filter(
(item) => !observers.includes(item)
);
}
cleanup() {
}
}
module.exports = { Blinds, DOWN, UP };