-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli1.js
140 lines (120 loc) · 4.1 KB
/
cli1.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
var qmotion = require('./qmotion');
var client = qmotion.search();
client.on("timeout", function(device) {
console.log("No QSync devices found");
});
client.on("found", function(device) {
device.on("initialized", function(items) {
var blind;
var blinds = Object.keys(items).map(
function(k){
return items[k]
}
);
blinds.sort(compare);
var stdin = process.openStdin();
process.stdin.setRawMode(true);
process.stdin.resume();
function selectBlindMsg() {
var str = "";
console.log();
console.log("Select blind to control:");
for (i = 0; i < blinds.length; i++) {
str += (i + 1) + ": " + blinds[i].name + ", ";
}
console.log(str);
process.stdin.resume();
}
function blindSelectionMessage(blind) {
console.log();
console.log("*** " + blind.name + " Selected ***");
selectBlindPositionMsg();
}
function blindPositionMsg(blind, position) {
console.log();
console.log("*** Move " + blind.name + " to " + position + "% ***");
}
function selectBlindPositionMsg() {
console.log();
console.log("Select blind position:");
console.log("1: 0%, 2: 12.5%, 3: 25%, 4: 37.5%, 5: 50%, 6: 62.5%, 7: 75%, 8: 87.5:, 9: 100%");
}
function processExit() {
console.log("Closing...");
process.stdin.pause();
}
selectBlindMsg();
stdin.on('data', function (key) {
var index = parseInt(String.fromCharCode(key[0])) - 1;
if (blind == null) {
switch (key[0]) {
case 0x31: // 1
case 0x32: // 2
case 0x33: // 3
case 0x34: // 4
case 0x35: // 5
case 0x36: // 6
case 0x37: // 7
case 0x38: // 8
case 0x39: // 9
if (blinds[index]) {
blind = blinds[index];
blind.on(
"currentPosition",
function(blind) {
console.log("currentPosition " + blind.state.currentPosition)
}
);
blind.on(
"positionState",
function(blind) {
console.log("positionState");
}
);
blindSelectionMessage(blind);
}
break;
case 0x03: // ctrl-c
processExit();
break;
default:
blind = null;
}
}
else {
switch (key[0]) {
case 0x1b: // esc
blind.removeAllListeners();
blind = null;
selectBlindMsg();
break;
case 0x31: // 1
case 0x32: // 2
case 0x33: // 3
case 0x34: // 4
case 0x35: // 5
case 0x36: // 6
case 0x37: // 7
case 0x38: // 8
case 0x39: // 9
var position = index * 12.5;
blindPositionMsg(blind, position);
blind.move(position);
break;
case 0x03: // ctrl-c
processExit();
break;
}
}
})
})
});
function compare(a,b) {
if (a.name < b.name) {
return -1;
}
if (a.name > b.name) {
return 1;
}
return 0;
}