-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
78 lines (62 loc) · 1.81 KB
/
app.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
'use strict';
const Homey = require('homey');
const Discovery = require('../../lib/discovery.js');
const MediaRenderer = require('../../lib/mediarenderer.js');
class App extends Homey.App {
onInit() {
this.log(`${Homey.manifest.id} running...`);
this.devices = {};
this.registerActions();
let discovery = new Discovery();
for (var i = 0; i < 30; i++) {
setTimeout(function () {
for (var i = 0; i < 5; i++) {
discovery.search('ssdp:all');
// discovery.search('urn:myharmony-com:device:harmony:1');
}
}, Math.random() * 1000);
}
discovery.on('device_discovered', device => {
this.devices[device.UDN + device.deviceType] = device;
this.emit(device.UDN + device.deviceType);
});
}
registerActions() {
let castMediaAction = new Homey.FlowCardAction('cast_media').register();
castMediaAction
.registerRunListener((args, state) => {
let cast_device = args.cast_device;
let cast_device_data = cast_device.getData();
let mediaUrl = args.media_url;
console.log(`Casting ${mediaUrl}`);
let mediaRenderer = new MediaRenderer(cast_device_data);
mediaRenderer.autoPlay(mediaUrl);
});
}
getDevice(deviceId, deviceType){
return this.devices[deviceId + deviceType];
}
getDevices(serviceType) {
var foundDevices = [];
var discoveredDevices = this.devices;
return new Promise((resolve, reject) => {
Object.keys(this.devices).forEach(function (deviceId) {
let device = discoveredDevices[deviceId];
console.log(device);
if (device.getService(serviceType)) {
var foundDevice = {
name: device.friendlyName,
data: {
"id": device.UDN,
"deviceType": device.deviceType,
"iconlist": device.iconList
}
};
foundDevices.push(foundDevice);
}
});
resolve(foundDevices);
});
}
}
module.exports = App;