This repository has been archived by the owner on Aug 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlisa.js
315 lines (283 loc) · 8.17 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
'use strict'
const EventEmitter = require('events')
const NOTIFICATION_TYPE = require('./api/utils/enums').NOTIFICATION_TYPE
const DEVICE_TYPE = require('./api/utils/enums').DEVICE_TYPE
module.exports = (function () {
//private
let app
/**
* Get native stack
* @returns 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
* @returns 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
* @returns 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 app.packs.pluginsManager.plugins[name] ? app.packs.pluginsManager.plugins[name].fullName : 'unknown'
}
return class LISA extends EventEmitter {
get NOTIFICATION_TYPE() {
return NOTIFICATION_TYPE
}
get DEVICE_TYPE() {
return DEVICE_TYPE
}
constructor(currentApp) {
super()
app = currentApp
}
toCamelCase(input) {
return input.toCamelCase()
}
getRooms() {
return app.orm.Room.findAll().then(rooms => {
return Promise.resolve(rooms.map(room => room.toJSON()))
})
}
createRoom(name) {
return app.orm.Room.create({name: name}).then(room => {
return Promise.resolve(room.toJSON())
})
}
createOrUpdateDevices(device, criteria) {
const plugin = getCurrentPlugin()
//app.log.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(app.orm.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) {
promise = app.orm.Device.update(device, {
where: {
id: device.id
}
})
}
else if (criteria) {
criteria.pluginName = plugin
promise = app.orm.Device.update(device, {
where: criteria
})
}
else {
promise = app.orm.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
* @returns Promise
*/
findDevices(criteria) {
criteria = criteria || {}
const plugin = getCurrentPlugin()
//app.log.debug(plugin)
criteria.pluginName = plugin
const promise = criteria.id ? app.orm.Device.find({
where: criteria
}) : app.orm.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
* @returns Promise - notif data
*/
sendNotification(to, title, type, desc, image, defaultAction, action, lang) {
const plugin = getCurrentPlugin()
return app.services.NotificationService.sendNotification(to, plugin, title, type, desc, image, defaultAction,
action, lang, 'default').then(notification => notification.toJSON())
}
/**
* Retrieve plugin preferences
* @returns {Promise} preferences or error
*/
getPreferences() {
const plugin = getCurrentPlugin()
app.log.debug(plugin)
return app.orm.Preference.findById(plugin + '_prefs').then(preferences => {
return preferences ? preferences.value : {}
})
}
/**
* Set plugin preferences
* @param preferences to save
* @returns {Promise} saved preferences or error
*/
setPreferences(preferences) {
const plugin = getCurrentPlugin()
app.log.debug(plugin)
return app.orm.Preference.upsert({
key: plugin + '_prefs',
value: preferences
}).then(() => {
return preferences
})
}
addChatBot(botId, botData) {
const plugin = getCurrentPlugin()
botData.pluginName = plugin
return app.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 app.orm.ChatBot.findAll({
where: where
}).then(chatBots => Promise.resolve(chatBots.map(bot => bot.toJSON())))
}
updateChatBot(botId, botData) {
const plugin = getCurrentPlugin()
botData.pluginName = plugin
return app.services.ChatBotService.updateBot(botId, botData).then(_ => Promise.resolve())
}
deleteChatBot(botId) {
//const plugin = getCurrentPlugin()
return app.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)
}
const logger = app.config.log.pluginLogger
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 _() {
return app._
}
get i18n() {
return app._
}
get bonjour() {
return app.bonjour
}
get mdns() {
return app.mdns
}
get serialPort() {
return app.serialPort
}
get ir() {
return {
send: (remote, action) => {
return app.services.IRService.send(remote, action)
}
}
}
}
})()