-
Notifications
You must be signed in to change notification settings - Fork 0
/
lisa.js
318 lines (287 loc) · 8.9 KB
/
lisa.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
'use strict';
import bonjourFactory from 'bonjour';
import EventEmitter from 'events';
import mdns from 'mdns-js';
import {isEqual, differenceWith, differenceBy, difference} from 'lodash-es';
import ChatBot from './lib/chatbots/models/chatbot.js';
import Preference from './lib/common/models/preference.js';
import services from './lib/common/services/manager.js';
import {DEVICE_TYPE, NOTIFICATION_TYPE} from './lib/common/utils/enums.js';
import logger from './lib/common/utils/logger.js';
import Device from './lib/devices/models/device.js';
import Room from './lib/rooms/models/room.js';
const bonjour = bonjourFactory();
const LISA = (function() {
/**
* Get native stack
* @return array of javascript calls
*/
const getStack = function() {
// Save original Error.prepareStackTrace
const origPrepareStackTrace = Error.prepareStackTrace;
// Override with function that just returns `stack`
Error.prepareStackTrace = (_, stack) => stack;
// Create a new `Error`, which automatically gets `stack`
const err = new Error();
// Evaluate `err.stack`, which calls our new `Error.prepareStackTrace`
const stack = err.stack;
// Restore original `Error.prepareStackTrace`
Error.prepareStackTrace = origPrepareStackTrace;
// Remove superfluous function call on stack
stack.shift(); // getStack --> Error
return stack;
};
/**
* Get path of the current plugin
* @return path
*/
const getCaller = () => {
const stack = getStack();
stack.shift();
let obj;
for (let i = 0; i < stack.length; i++) {
obj = stack[i];
if (obj.getFileName().toLowerCase().indexOf('plugin-') !== -1) {
break;
}
}
// Return caller's caller
return obj.getFileName();
};
/**
* Get name of the current plugin
* @return string name
*/
const getCurrentPlugin = () => {
const pathString = getCaller();
const parts = pathString.split('/');
let part = parts.find((part) => part.indexOf('lisa-plugin') !== -1);
if (!part) {
part = 'unknown';
}
const name = part.replace(/lisa\-/, '').replace(/plugin\-/, '').toCamelCase();
return services.pluginService.plugins[name] ? services.pluginService.plugins[name].fullName : 'unknown';
};
return class LISA extends EventEmitter {
get NOTIFICATION_TYPE() {
return NOTIFICATION_TYPE;
}
get DEVICE_TYPE() {
return DEVICE_TYPE;
}
constructor() {
super();
}
toCamelCase(input) {
return input.toCamelCase();
}
getRooms() {
return Room.findAll().then((rooms) => {
return Promise.resolve(rooms.map((room) => room.toJSON()));
});
}
createRoom(name) {
return Room.create({name: name}).then((room) => {
return Promise.resolve(room.toJSON());
});
}
async createOrUpdateDevices(device, criteria) {
const plugin = getCurrentPlugin();
// logger.debug(plugin)
let promise;
if (Array.isArray(device)) {
const toCreate = device.filter((element) => !element.id).map((device) => {
device.pluginName = plugin;
return device;
});
const toUpdate = device.filter((element) => element.id);
const todo = [];
if (toCreate.length > 0) {
todo.push(Device.bulkCreate(toCreate));
}
if (toUpdate.length > 0) {
for (const deviceToUpdate of toUpdate) {
todo.push(this.createOrUpdateDevices(deviceToUpdate));
}
}
promise = Promise.all(todo);
}
else {
device.pluginName = plugin;
if (device.id) {
const existingDevice = (await Device.findByPk(device.id)).toRawData();
delete existingDevice.createdAt;
delete existingDevice.updatedAt;
device.data = {...device.data}; // transform class instance like Light to plain object to compare later
//update only if there is different with what's on database to avoid unnecessary websocket events
if (isEqual(existingDevice, device)) {
promise = Promise.resolve(existingDevice);
} else{
promise = Device.update(device, {
where: {
id: device.id,
},
individualHooks: true,
});
}
}
else if (criteria) {
criteria.pluginName = plugin;
promise = Device.update(device, {
where: criteria,
individualHooks: true,
});
}
else {
promise = Device.create(device);
}
}
return promise.then((device) => {
if (Array.isArray(device)) {
return device.map((item) => {
if (item.toRawData) {
return item.toRawData();
}
return item;
});
}
else {
if (device.toRawData) {
return device.toRawData();
}
return device;
}
});
}
/**
*
* @param criteria to retrieve specific devices
* @return Promise
*/
findDevices(criteria) {
criteria = criteria || {};
// logger.debug(plugin)
criteria.pluginName = getCurrentPlugin();
const promise = criteria.id ? Device.find({
where: criteria,
}) : Device.findAll({
where: criteria,
});
return promise.then((devices) => {
if (Array.isArray(devices)) {
return devices.map((device) => device.toRawData());
} else {
return devices.toRawData();
}
});
}
/**
* Send notification to the user(s)
* @param to @optional user id to send the notif to
* @param title of the notif
* @param type
* @param desc of the notif
* @param image of the notif
* @param defaultAction of the notif
* @param action of the notif
* @param lang of the notif
* @return Promise - notif data
*/
sendNotification(to, title, type, desc, image, defaultAction, action, lang) {
const plugin = getCurrentPlugin();
return services.notificationService.sendNotification(to, plugin, title, type, desc, image, defaultAction,
action, lang, 'default').then((notification) => notification.toJSON());
}
/**
* Retrieve plugin preferences
* @return {Promise} preferences or error
*/
getPreferences() {
const plugin = getCurrentPlugin();
logger.debug(plugin);
return Preference.findByPk(plugin + '_prefs').then((preferences) => {
return preferences ? preferences.value : {};
});
}
/**
* Set plugin preferences
* @param preferences to save
* @return {Promise} saved preferences or error
*/
setPreferences(preferences) {
const plugin = getCurrentPlugin();
logger.debug(plugin);
return Preference.upsert({
key: plugin + '_prefs',
value: preferences,
}).then(() => {
return preferences;
});
}
addChatBot(botId, botData) {
botData.pluginName = getCurrentPlugin();
return services.chatBotService.addBot(botId, botData).then((chatBot) => Promise.resolve(chatBot.toJSON()));
}
getChatBot(botId = null) {
const plugin = getCurrentPlugin();
const where = {
pluginName: plugin,
};
if (botId) {
where.name = botId;
}
return ChatBot.findAll({
where: where,
}).then((chatBots) => Promise.resolve(chatBots.map((bot) => bot.toJSON())));
}
updateChatBot(botId, botData) {
botData.pluginName = getCurrentPlugin();
return services.chatBotService.updateBot(botId, botData).then((_) => Promise.resolve());
}
deleteChatBot(botId) {
// const plugin = getCurrentPlugin()
return services.chatBotService.deleteBot(botId).then((_) => Promise.resolve());
}
get log() {
const getArguments = (args) => {
const plugin = getCurrentPlugin();
const mainArguments = Array.prototype.slice.call(args);
return [plugin + ':'].concat(mainArguments);
};
return {
debug: function() {
logger.debug(JSON.stringify(getArguments(arguments)));
},
info: function() {
logger.info(JSON.stringify(getArguments(arguments)));
},
error: function() {
logger.error(JSON.stringify(getArguments(arguments)));
},
silly: function() {
logger.silly(JSON.stringify(getArguments(arguments)));
},
verbose: function() {
logger.verbose(JSON.stringify(getArguments(arguments)));
},
warn: function() {
logger.warn(JSON.stringify(getArguments(arguments)));
},
};
}
get bonjour() {
return bonjour;
}
get mdns() {
return mdns;
}
get ir() {
return {
send: (remote, action) => {
return services.irService.send(remote, action);
},
};
}
};
})();
export default new LISA();