-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommonFunctions.js
More file actions
590 lines (548 loc) · 15.1 KB
/
commonFunctions.js
File metadata and controls
590 lines (548 loc) · 15.1 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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
import constants from './constants'
import moment from 'moment'
import _http from 'http'
import { Client } from 'ssh2'
import URL from 'url'
/**
* This function activates a given plugin
* @param plugin_name
* @returns current state of given plugin
*/
export const pluginActivate = function(plugin_name) {
return this.$thunder.api.Controller.activate({ callsign: plugin_name })
.then(() =>
this.$thunder.api.call(constants.controllerPlugin, 'status').then(
result =>
result.filter(p => {
return p.callsign === plugin_name
})[0].state
)
)
.catch(err => err)
}
/**
* This function deactivates a given plugin
* @param plugin_name
* @returns current state of given plugin
*/
export const pluginDeactivate = function(plugin_name) {
return this.$thunder.api.Controller.deactivate({ callsign: plugin_name })
.then(() =>
this.$thunder.api.call(constants.controllerPlugin, 'status').then(
result =>
result.filter(p => {
return p.callsign === plugin_name
})[0].state
)
)
.catch(err => err)
}
/**
* This function is used to get Plugin information
* @param plugin_name
* @returns {!ManagedPromise<R>|PromiseLike<any>|Promise<any>}
*/
export const getPluginState = function(plugin_name) {
return this.$thunder.api.call(constants.controllerPlugin, 'status').then(result => {
let value = result.filter(p => {
if (p.callsign === plugin_name) {
return true
}
})
return value[0].state
})
}
/**
* This function resumes or suspends WebKitBrowser plugin
* @param action
*/
export const webKitBrowserActions = function(action) {
return this.$thunder.api.WebKitBrowser.state(action)
.then(() => {
return this.$thunder.api.call(constants.controllerPlugin, 'status').then(
result =>
result.filter(p => {
return p.callsign === 'WebKitBrowser'
})[0].state
)
})
.catch(err => err)
}
/**
* This function sets the URL
* @param URL
* @returns URL
*/
export const setWebKitUrl = function(URL) {
return this.$thunder.api.WebKitBrowser.url(URL)
.then(() => this.$thunder.api.WebKitBrowser.url().then(url => url))
.catch(err => err)
}
/**
* This function resumes or suspends Youtube plugin
* @param action
*/
export const youtubeActions = function(action) {
return this.$thunder.api.Cobalt.state(action)
.then(() => {
return this.$thunder.api.call(constants.controllerPlugin, 'status').then(
result =>
result.filter(p => {
return p.callsign === 'Cobalt'
})[0].state
)
})
.catch(err => err)
}
/**
* This function gets the URL
* @param URL
* @returns URL
*/
export const getYoutubeUrl = function(URL) {
return this.$thunder.api.Cobalt.url(URL)
.then(() =>
this.$thunder.api.Cobalt.url().then(url => {
this.$log('URL is', url)
return url
})
)
.catch(err => err)
}
/**
* This function checks if the process is running by getting the process id and comparing it to the number.
* - If the process id is a number, then false is returned
* - If the process is not a number, then true is returned
* @param process
*/
export const checkIfProcessIsRunning = function(process) {
let opts = {
cmd: `ps w | grep ${process} | grep -v grep | awk ` + "'{printf(" + '"%i \\n\\r", $1)}' + "'",
}
exec(opts, res => {
//parse response as integer
var processId
try {
processId = parseInt(res)
} catch (e) {
return e
}
//only return true if valid number
return isNaN(processId) === false ? true : false
})
}
/**
* This function is used to connect RbPI
* @param opts
* @param cb
*/
export const exec = async function(opts) {
var conn = new Client()
let execCmd = new Promise((resolve, reject) => {
conn
.on('ready', function() {
return conn.exec(opts.cmd, function(err, stream) {
var res = ''
if (err) reject(err)
if (opts.cbWhenStarted === true) resolve(true)
stream
.on('close', function(code, signal) {
stream.end()
conn.end()
if (opts.cbWhenStarted !== true) resolve(res)
})
.on('data', function(data) {
res += data
})
.stderr.on('data', function(data) {})
})
})
.connect({
host: constants.host,
port: 22,
username: 'root',
password: 'root',
})
conn.on('timeout', function(e) {
throw new Error(`{opts.cmd}: Timeout while connecting to ${constants.host}`)
})
conn.on('error', function(err) {
throw new Error(`${opts.cmd}: ${err}`)
})
})
let result = await execCmd
return result
}
/**
* This function calculates the average of FPS samples collected in fetchFPS function
* @returns {results}
*/
export const calcAvgFPS = function() {
let sum = 0
let average
let samples = this.$data.read('samples')
for (let i = 0; i < samples.length; i++) {
sum += samples[i]
}
average = sum / samples.length
average = average.toFixed(2)
this.$data.write('average', average)
}
/**
* This function is used to get screenshot
* @returns {Promise<void>}
*/
export const screenshot = async function() {
let url = `http://${constants.host}:80/Service/Snapshot/Capture?${moment().valueOf()}`
// create a new promise inside of the async function
let bufferData = new Promise((resolve, reject) => {
_http
.get(url, function(res) {
if (res.headers['content-length'] === undefined)
this.$log(
'Framework did not return a content-length! This will slow down the screenshot module.'
)
var buffers = []
var imageSize = res.headers['content-length']
res.on('data', function(chunk) {
buffers.push(Buffer.from(chunk))
})
res.on('end', function() {
return resolve(Buffer.concat(buffers, parseInt(imageSize)))
})
})
.on('error', function(e) {
return reject(e)
})
})
// wait for the promise to resolve
let result = await bufferData
this.$data.write('screenshotResult', result)
return result
}
/**
* This function is used to kill the process
* @param process
*/
export const killProcess = function(process) {
exec({ cmd: `killall -9 ${process}` }, err => {
return true
})
}
/**
* This function is used to stop the process
* @param process
*/
export const stopProcess = function(process) {
return exec({ cmd: `killall ${process}` }, () => {
return true
})
}
export const startFramework = function() {
return exec({ cmd: 'nohup WPEFramework WPEProcess -b &', cbWhenStarted: true })
}
/**
* This function is used to restart the framework
*/
export const restartFramework = function() {
return this.$sequence([
() => killProcess('WPEFramework'),
() => killProcess('WPEProcess'),
() => startFramework(),
])
}
/**
* This function performs below operations on WebKitBrowser Plugin
* - Deactivate
* - Activate
* - Resume
*/
export const webKitBrowserStartAndResume = function() {
return this.$sequence([
() => pluginDeactivate.call(this, constants.webKitBrowserPlugin),
() => pluginActivate.call(this, constants.webKitBrowserPlugin),
() => webKitBrowserActions.call(this, constants.resume),
])
}
/**
* This function performs below operations on Youtube Plugin
* - Deactivate
* - Activate
* - Resume
*/
export const youtubeStartAndResume = function() {
return this.$sequence([
() => pluginDeactivate.call(this, constants.youTubePlugin),
() => pluginActivate.call(this, constants.youTubePlugin),
() => youtubeActions().call(this, constants.resume),
])
}
/**
* This function is used to get Plugin Info
* @param plugin
* @returns {Promise<AxiosResponse<any>>}
*/
export const getPluginInfo = function(plugin) {
return this.$http
.get(`http://${constants.host}:80/Service/${plugin}`)
.then(result => result)
.catch(err => err)
}
/**
* This function is used to Suspend or Resume a plugin
* @param plugin
* @param action
* @returns {Promise<unknown>}
*/
export const suspendOrResumePlugin = function(plugin, action) {
return this.$http
.post(`http://${constants.host}:80/Service/${plugin}/${action}`)
.then(result => result)
.catch(err => err)
}
/**
* This function is used to get System Info of Device Ingo Plugin
* @param plugin
* @returns {Promise<AxiosResponse<any>>}
*/
export const getDeviceInfo = function() {
return this.$thunder.api.DeviceInfo.systeminfo()
.then(result => {
this.$data.write('systeminfo', result)
})
.catch(err => err)
}
/**
* This function is used to get esn Info of Netflix Plugin
* @returns {Promise<unknown>}
*/
export const getNetflixPluginEsnInfo = function() {
return this.$thunder.api.Netflix.esn()
.then(result => result)
.catch(err => err)
}
/**
* This function is used to suspend or resume Info of Netflix Plugin
* @param state
* @returns {Promise<unknown>}
*/
export const suspendOrResumeNetflixPlugin = function(state) {
return this.$thunder.api.Netflix.state(state)
.then(result => result)
.catch(err => err)
}
/**
* This function is used to get Addresses Info of Device Info Plugin
* @param plugin
* @returns {Promise<AxiosResponse<any>>}
*/
export const getAddressesInfo = function() {
return this.$thunder.api.DeviceInfo.addresses()
.then(result => {
this.$data.write('addressinfo', result)
return result
})
.catch(err => err)
}
/**
* This function is used to get Monitor Info
* @param plugin
* @returns {Promise<AxiosResponse<any>>}
*/
export const getMonitorInfo = function() {
return this.$thunder.api.Monitor.status()
.then(result => {
this.$data.write('monitorinfo', result)
return result
})
.catch(err => err)
}
/**
* This function is used to get the controller UI
* @returns {Promise<AxiosResponse<any>>}
*/
export const getControllerUI = function() {
return this.$http
.get(`http://${constants.host}:80/Service/Controller/UI`)
.then(result => result)
.catch(err => err)
}
/**
* This function is used to get CpuLoad
* @returns {Promise<T>}
*/
export const getCpuLoad = function() {
this.$thunder.api.DeviceInfo.systeminfo()
.then(result => {
this.$data.write('cpuload', result.cpuload)
return result
})
.catch(err => err)
}
/**
* This function is used to get Controller plugin data
* @returns {Promise<any> | Thenable<any> | PromiseLike<any>}
*/
export const getControllerPluginData = function() {
return this.$thunder.api.Controller.status()
.then(result => result)
.catch(err => err)
}
/**
* This function is used to get Provisioning plugin data
* @returns {Promise<any> | Thenable<any> | PromiseLike<any>}
*/
export const getProvisioningPluginData = function() {
return this.$thunder.api.Provisioning.state()
.then(result => result)
.catch(err => err)
}
/**
* This function is used to start Provisioning plugin
* @returns {Promise<any> | Thenable<any> | PromiseLike<any>}
*/
export const startProvisioning = function() {
return this.$thunder.api.Provisioning.provision()
.then(result => result)
.catch(err => err)
}
/**
* This function is used to put request
* @param plugin
* @returns {Promise<AxiosResponse<any>>}
*/
export const putRequestForPlugin = function() {
return this.$http
.put(`http://${constants.host}:80/Service/Provisioning`)
.then(result => result)
.catch(err => err)
}
/**
* This function is used to perform GET request operation on the URL
* @returns {Promise<AxiosResponse<any>>}
*/
export const getReqURL = function(URL) {
return this.$http
.get(URL)
.then(result => result)
.catch(err => err)
}
/**
* This function is used to get response from URL based on the Method type
* @param methodType
* @param url
* @returns {Promise<AxiosResponse<any>>}
*/
export const getResponseForURLRequest = function(methodType, url) {
if (methodType === 'GET') {
return getReqURL.call(this, url)
} else if (methodType === 'PUT') {
return putReqURL.call(this, url)
}
}
/**
* This function is used to perform {PUT} request operation on the URL
* @returns {Promise<AxiosResponse<any>>}
*/
export const putReqURL = function(URL) {
return this.$http
.put(URL)
.then(result => result)
.catch(err => err)
}
/**
* This function stops the WPE Framework Process
* @returns {boolean}
*/
export const stopWPEFramework = function() {
stopProcess('WPEFramework')
return true
}
/**
* This function is used to scan Info of Bluetooth Control Plugin
* @param type , timeout
* @returns {Promise<AxiosResponse<any>>}
*/
export const scanDevices = function(type, timeout) {
return this.$thunder.api.BluetoothControl.scan(type, timeout)
.then(result => result)
.catch(err => err)
}
/**
* This function is used to get available Bluetooth devices
* @returns {Promise<AxiosResponse<any>>}
*/
export const getBluetoothDevices = function() {
return this.$thunder.api.BluetoothControl.devices()
.then(result => result)
.catch(err => err)
}
/**
* This function is used to pair with the available Bluetooth devices
* @param address
* @returns {Promise<AxiosResponse<any>>}
*/
export const pairBTDevice = function(address) {
return this.$thunder.api.BluetoothControl.pair(address)
.then(result => result)
.catch(err => err)
}
/**
* This function is used to connect with the available Bluetooth devices
* @param address
* @returns {Promise<AxiosResponse<any>>}
*/
export const connectBTDevice = function(address) {
return this.$thunder.api.BluetoothControl.connect(address)
.then(result => result)
.catch(err => err)
}
/**
* This function is used to disconnect with the available Bluetooth devices
* @param address
* @returns {Promise<AxiosResponse<any>>}
*/
export const disconnectBTDevice = function(address) {
return this.$thunder.api.BluetoothControl.disconnect(address)
.then(result => result)
.catch(err => err)
}
/**
* This function is used to disconnect with the available Bluetooth devices
* @param address
* @returns {Promise<AxiosResponse<any>>}
*/
export const unpairBTDevice = function(address) {
return this.$thunder.api.BluetoothControl.unpair(address)
.then(result => result)
.catch(err => err)
}
/**
* This function is used to get available Bluetooth adapters
* @returns {Promise<AxiosResponse<any>>}
*/
export const getBluetoothAdapters = function() {
return this.$thunder.api.BluetoothControl.adapters()
.then(result => result)
.catch(err => err)
}
/**
* This function is used to get info of particular Bluetooth adapters
* @param adaptername
* @returns {Promise<AxiosResponse<any>>}
*/
export const getBluetoothAdapterInfo = function(adaptername) {
return this.$thunder.api.BluetoothControl.adapter(adaptername)
.then(result => result)
.catch(err => err)
}
/**
* This function is used to get info of particular Bluetooth device
* @param devicemac
* @returns {Promise<AxiosResponse<any>>}
*/
export const getBluetoothDeviceInfo = function(devicemac) {
return this.$thunder.api.BluetoothControl.adapter(devicemac)
.then(result => result)
.catch(err => err)
}