-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
212 lines (184 loc) · 6.8 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
'use strict';
const bluebird = require('bluebird');
const request = require('request-promise');
const moment = require('moment');
class kankun {
constructor() {
this.name = 'kankun-plug';
this.displayname = 'Kankun Plug';
this.description = 'Control your Kankun plugs';
}
init() {
this.plugs = {};
this.listenBroadcast('kankun-plug', (message) => {
let tags;
if (message.uuid) {
if (message.group) {
tags = message.group.split(',')
} else {
tags = message.tags.split(',');
}
if (tags instanceof Array === false) {
tags = [tags];
}
tags = tags.map((value) => {
return value.trim().toLowerCase();
});
this.plugs[message.uuid] = {name: message.name.toLowerCase(), tags: tags, ip: message.ip};
}
});
this.listen({
id: 'on',
listener: '(kankun|small k) on (:<group or item>.+?)',
role: 'standard',
command: (from, interfaceName, params) => {
return this.checkGroups(params[1], (obj) => {
return this.turnOn(obj, interfaceName, from);
});
}
});
this.listen({
id: 'off',
listener: '(kankun|small k) off (:<group or item>.+?)',
role: 'standard',
command: (from, interfaceName, params) => {
return this.checkGroups(params[1], (obj) => {
return this.turnOff(obj, interfaceName, from);
});
}
});
this.listen({
id: 'status',
listener: '(kankun|small k) status (:<group or item>.+?)',
role: 'standard',
command: (from, interfaceName, params) => {
return this.checkGroups(params[1], (obj) => {
return this.checkStatus(obj, interfaceName, from);
});
}
});
this.listen({
id: 'timer',
listener: '(kankun|small k) timer (:<unit>.+?) (:<seconds, minutes, hours or days>second|minute|hour|day|seconds|minutes|hours|days) (:<state>off|on) (:<group or item>.+?)',
role: 'standard',
command: (from, interfaceName, params) => {
const crontime = moment().add(params[1], params[2]).toDate();
return this.addCronJob(crontime, 'timer', {
plug: params[4],
status: params[3],
interfaceName: interfaceName,
from: from
}).then((id) => {
return `Timer added with ID ${id}`;
});
}
});
this.listen({
id: 'cancel-timer',
listener: '(kankun|small k) cancel timer (:<timer id>.+?)',
role: 'standard',
command: (from, interfaceName, params) => {
return this.removeCronJob(params[1]).then(() => {
return `Timer with ID ${params[1]} cancelled`;
});
}
});
this.listen({
id: 'list',
listener: '(kankun|small k) list',
role: 'standard',
command: (from, interfaceName, params) => {
let tags = {};
let names = [];
let message = '';
for (let key in this.plugs) {
names.push(`${this.plugs[key].name} (IP: ${this.plugs[key].ip})`);
this.plugs[key].tags.forEach((value) => {
tags[value] = true;
});
}
if (names.length > 0) {
names = ` - ${names.join('\n - ')}`;
message += `Plug names:\n${names}\n`;
}
if (Object.keys(tags).length > 0) {
tags = Object.keys(tags);
tags = ` - ${tags.join('\n - ')}`;
message += `Tags:\n${tags}`;
}
message = message.trim();
if (message.length === 0) {
message = 'No plugs detected yet'
}
return message;
}
});
this.registerCronHandler('timer', (params) => {
this.checkGroups(params.plug, (obj) => {
if (params.status === 'on') {
this.turnOn(obj, params.interfaceName, params.from);
} else {
this.turnOff(obj, params.interfaceName, params.from);
}
});
});
}
checkGroups(name, callback) {
const promises = [];
for (let key in this.plugs) {
if (this.plugs[key].name === name.toLowerCase() || this.plugs[key].tags.indexOf(name.toLowerCase()) > -1) {
promises.push(callback(this.plugs[key]));
}
}
if (promises.length > 0) {
return bluebird.all(promises).then((messages) => {
return messages.join(`\n`);
});
} else {
return `No plugs matched "${name}"`;
}
}
turnOn(obj, interfaceName, from){
return request({
uri: `http://${obj.ip}/cgi-bin/relay.cgi?on`,
method: `GET`,
headers: {
'User-Agent': 'Woodhouse Bot - https://github.com/Woodhouse/core'
}
}).then((res) => {
return `${obj.name} has been turned on`;
}).catch((e) => {
return `${obj.name} could not be turned on: ${e.message}`;
});
}
turnOff(obj, interfaceName, from) {
return request({
uri: `http://${obj.ip}/cgi-bin/relay.cgi?off`,
method: `GET`,
headers: {
'User-Agent': 'Woodhouse Bot - https://github.com/Woodhouse/core'
}
}).then((res) => {
return `${obj.name} has been turned off`;
}).catch((e) => {
return `${obj.name} could not be turned off: ${e.message}`;
});
}
checkStatus(obj, interfaceName, from) {
return request({
uri: `http://${obj.ip}/cgi-bin/relay.cgi?state`,
method: `GET`,
headers: {
'User-Agent': 'Woodhouse Bot - https://github.com/Woodhouse/core'
}
}).then((data) => {
if (data.trim() === 'OFF') {
return `${obj.name} is off`;
} else if (data.trim() === 'ON') {
return `${obj.name} is on`;
}
}).catch((e) => {
return `${obj.name} could not be turned reached: ${e.message}`;
});
}}
module.exports = kankun;