This repository has been archived by the owner on Jan 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
LGTVbridge.js
199 lines (167 loc) · 4.99 KB
/
LGTVbridge.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
"use strict";
const API = require("./lgtv-api.json");
// Get parameters from environment
const CONFIG = {
"tvMAC": process.env.TV_MAC,
"tvIP": process.env.TV_IP,
"tvNAME": "TV",
"tvAppNAME": "TV"
}
//const CONFIG = require("./config.json");
const START_PORT = 11000;
const URL = 'ws://' + CONFIG.tvIP + ':3000'
var LGTVBridge = function () {
var _self = this;
/*this.initAlexa = function() {
var ip = require("ip");
var FauxMo = require("fauxmojs");
console.log('\nPreparing Amazon Alexa bridge listener...\n')
let fauxMo = new FauxMo({
ipAddress: ip.address(),
devices: _self.getDevicesList()
});
};*/
this.connectToTV = function() {
return require("lgtv2")({
url: URL,
reconnect: false
});
};
this.getDevicesList = function() {
var tv = [_self.initTV()];
var apps = _self.initApps();
var functions = _self.initFunctions();
var devices = tv.concat(apps).concat(functions);
return devices;
};
this.initTV = function() {
var myTV = {
name: CONFIG.tvAppNAME,
port: START_PORT,
handler: (action) => {
if (action == "on") {
console.log("\nTurning on " + CONFIG.tvNAME + ' ...\n');
_self.turnOnTV();
} else {
console.log("\nTurning off " + CONFIG.tvNAME + ' ...\n');
_self.turnOffTV();
}
}
}
return myTV;
};
this.turnOnTV = function(exitProcess) {
var wol = require("node-wol");
wol.wake(CONFIG.tvMAC, function(error) {
if (error) {
console.error("Can't turn on your TV");
} else {
if(exitProcess == 'exitProcess')
process.exit();
}
});
};
this.turnOffTV = function() {
var lgtv = _self.connectToTV();
lgtv.on("connect", function() {
lgtv.request(API.TURN_OFF_TV, {}, function(err, res) {
if(res.returnValue == true)
lgtv.disconnect();
});
}).on("error", function(err) {
if(err.code == 'ECONNREFUSED') {
console.log("TV refused connection, trying again in 2 seconds...");
try {
setTimeout(_self.turnOffTV(), 2000);
} catch(e) {
console.log("Failed to set timer to try again: " + e);
}
} else {
console.error("Error while turning off your TV:", err);
lgtv.disconnect();
}
});
};
this.initApps = function() {
var supportedApps = require("./apps.json");
var applications = [];
supportedApps.forEach(function(app, index) {
applications.push({
name: app.displayName,
port: START_PORT + (index + 1),
handler: (action) => {
if (action == "on") {
console.log("APP: Turning " + app.displayName + ' ON');
_self.execute(API.APP_LAUNCHER, 'request', {id: app.id}, 'sendMagicPkg');
} else {
console.log("APP: Turning " + app.displayName + ' OFF');
_self.execute(API.APP_CLOSER, 'request', {id: app.id});
}
}
});
});
return applications;
};
this.initFunctions = function() {
var supportedFunctions = require("./functions.json");
var functions = [];
supportedFunctions.forEach(function(func, index) {
functions.push({
name: func.displayName,
port: START_PORT + 100 + (index + 1),
handler: (action) => {
if (action == "on") {
_self.execute(func.commandON, 'subscribe', func.paramsON);
} else {
_self.execute(func.commandOFF, 'subscribe', func.paramsOFF);
}
}
});
});
return functions;
};
this.execute = function(command, type, params, sendMagicPkg) {
if (sendMagicPkg === 'sendMagicPkg') {
_self.turnOnTV();
_self.runFunction(command, type, params);
} else {
_self.runFunction(command, type, params);
}
};
this.runFunction = function(command, type, params){
if (type == 'request') {
_self.runRequestFunction(command, params);
} else {
_self.runSubscribeFunction(command, params);
}
};
this.runRequestFunction = function(command, params) {
var lgtv = _self.connectToTV();
lgtv.on("connect", function() {
lgtv.request(command, params, function(err, res) {
console.log('API:', command);
console.log('Results:', res);
lgtv.disconnect();
});
}).on("error", function(err) {
console.error("Error while running", command);
console.error('ERROR:', err);
lgtv.disconnect();
});
};
this.runSubscribeFunction = function(command, params) {
var lgtv = _self.connectToTV();
lgtv.on("connect", function() {
lgtv.subscribe(command, params, function(err, res) {
console.log('API:', command);
console.log('Results:', res);
lgtv.disconnect();
});
}).on("error", function(err) {
console.error("Error while running", command);
console.error('ERROR:', err);
lgtv.disconnect();
});
};
}
module.exports = LGTVBridge;