forked from iobroker-community-adapters/ioBroker.chromecast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
102 lines (83 loc) · 2.81 KB
/
main.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
/* jshint -W097 */
/* jshint strict: false */
/* jslint node: true */
/* jshint esversion: 6 */
'use strict';
// For profiling: comment out the following block and connect to
// http://c4milo.github.io/node-webkit-agent/26.0.1410.65/inspector.html?host=localhost:19999&page=0
/*
var agent = require('webkit-devtools-agent');
agent.start({
port: 19999,
bind_to: '0.0.0.0',
ipc_port: 13333,
verbose: true
});
*/
// you have to require the utils module and call adapter function
const utils = require('@iobroker/adapter-core');
const adapterName = require('./package.json').name.split('.').pop();
let adapter;
function startAdapter(options) {
options = options || {};
Object.assign(options, {name: adapterName});
adapter = new utils.Adapter(options);
adapter.on('ready', ready);
adapter.on('unload', unload);
return adapter;
}
// const SCAN_INTERVAL = 10000;
let scanner = undefined;
let devices = undefined;
async function ready() {
// Own libraries
const LogWrapper = require('castv2-player').LogWrapper;
const Scanner = require('castv2-player').Scanner(new LogWrapper(adapter.log));
let webPort = 8082;
if (adapter.config.web) {
try {
const webObj = await adapter.getForeignObjectAsync(`system.adapter.${adapter.config.web}`);
webPort = webObj.native.port;
} catch (e) {
adapter.log.error(`Cannot get web port: ${e.toString()}`);
webPort = 8082;
}
}
const ChromecastDevice = await require('./lib/chromecastDevice')(adapter, webPort);
devices = [];
// Create manually added devices (if any)
if (adapter.config.manualDevices) {
for (let i = 0; i < adapter.config.manualDevices.length; i++) {
// Emulate ID
let device = adapter.config.manualDevices[i];
device.id = `${i}-${device.name}`;
// Emulate registerForUpdates
device.registerForUpdates = function () {};
devices.push(new ChromecastDevice(device, true));
}
}
// var chromecastDevices = {};
scanner = new Scanner(connection => {
adapter.log.debug(`New connection: ${JSON.stringify(connection)}`);
devices.push(new ChromecastDevice(connection, false));
});
// in this template all states changes inside the adapters namespace are subscribed
adapter.subscribeStates('*');
}
function unload(callback) {
try {
scanner.destroy();
devices.forEach(device => device.destroy());
devices = undefined;
} catch (error) {
console.error(error);
}
callback();
}
// If started as allInOne/compact mode => return function to create instance
if (typeof module !== undefined && module.parent) {
module.exports = startAdapter;
} else {
// or start the instance directly
startAdapter();
}