forked from samuelmatis/viera.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviera.js
191 lines (167 loc) · 5.86 KB
/
viera.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
// Viera.js 0.2.5
// (c) 2014 Samuel Matis
// Viera.js may be freely distributed or modified under the MIT license.
// Modified by g30r93g (George Nick Gorzynski) on 09/05/2019.
//
(function() {
var http = require('http');
/**
* Constructor
*
* @param {String} ipAddress The IP Address of the TV
*/
var Viera = function(ipAddress) {
// Check if ipAddress is valid IP address
var ipRegExp = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/;
if (ipRegExp.test(ipAddress)) {
this.ipAddress = ipAddress;
} else {
throw new TypeError('[viera.js] You entered an invalid IP address');
}
};
/**
* Create and send request to the TV
*
* @param {String} type Type of your request
* @param {String} action The xml action type to perform
* @param {String} command The command from codes.txt you want to perform
* @param {Array} options Options array (mostly for callback)
*/
Viera.prototype.sendRequest = function(type, action, command, options) {
var url, urn;
if (type === 'command') {
url = '/nrc/control_0';
urn = 'panasonic-com:service:p00NetworkControl:1';
} else if (type === 'render') {
url = '/dmr/control_0';
urn = 'schemas-upnp-org:service:RenderingControl:1';
}
var body = '<?xml version="1.0" encoding="utf-8"?> \
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> \
<s:Body> \
<u:'+action+' xmlns:u="urn:'+urn+'"> \
'+command+' \
</u:'+action+'> \
</s:Body> \
</s:Envelope>';
var postRequest = {
host: this.ipAddress,
path: url,
port: 55000,
method: "POST",
headers: {
'Content-Length': body.length,
'Content-Type': 'text/xml; charset="utf-8"',
'SOAPACTION': '"urn:'+urn+'#'+action+'"'
}
};
var self = this;
if (options !== undefined) {
self.callback = options.callback;
} else {
self.callback = function () {};
}
var req = http.request(postRequest, (res) => {
res.setEncoding('utf8');
res.on('data', self.callback);
});
req.on('error', (e) =>{
console.log('error: ' + e.message);
console.log(e);
// self.callback(e, null);
});
req.write(body);
req.end();
return this;
};
/**
* Send a command to the TV
*
* @param {String} command Command from codes.txt
*/
Viera.prototype.sendCommand = function(command) {
this.sendRequest('command', 'X_SendKey', '<X_KeyEvent>NRC_' + command.toUpperCase() + '-ONOFF</X_KeyEvent>');
return this;
};
/**
* Send a change HDMI input to the TV
*
* @param {String} hdmiInput Command from codes.txt
*/
Viera.prototype.sendHDMICommand = function(hdmiInput) {
this.sendRequest('command', 'X_SendKey', '<X_KeyEvent>NRC_HDMI' + (hdmiInput - 1) + '-ONOFF</X_KeyEvent>');
return this;
};
/**
* Send command to open app on the TV
*
* @param {String} appID appId from codes.txt
*/
Viera.prototype.sendAppCommand = function(appID) {
this.sendRequest('command', 'X_LaunchApp', '<X_AppType>vc_app<X_AppType><X_LaunchKeyword>product_id=' + appID + '</X_LaunchKeyword>')
return this;
};
/**
* Get volume from TV
*
* @param {Function} callback
*/
Viera.prototype.getVolume = function(callback) {
var self = this;
self.volumeCallback = callback;
this.sendRequest('render', 'GetVolume', '<InstanceID>0</InstanceID><Channel>Master</Channel>',
{
callback: function(data) {
var match = /<CurrentVolume>(\d*)<\/CurrentVolume>/gm.exec(data);
if (match !== null) {
var volume = match[1];
self.volumeCallback(volume);
}
}
});
};
/**
* Set volume
*
* @param {Int} volume Desired volume in range from 0 to 100
*/
Viera.prototype.setVolume = function(volume) {
if (volume < 0 || volume > 100) {
throw new Error("Volume must be in range from 0 to 100");
}
this.sendRequest('render', 'SetVolume', '<InstanceID>0</InstanceID><Channel>Master</Channel><DesiredVolume>' + volume + '</DesiredVolume>');
return this;
};
/**
* Get the current mute setting
*
* @param {Function} callback
*/
Viera.prototype.getMute = function(callback) {
var self = this;
self.muteCallback = callback;
this.sendRequest('render', 'GetMute', '<InstanceID>0</InstanceID><Channel>Master</Channel>',
{
callback: function(data) {
var regex = /<CurrentMute>([0-1])<\/CurrentMute>/gm;
var match = regex.exec(data);
if (match !== null) {
var mute = (match[1] === '1');
self.muteCallback(mute);
}
}
});
};
/**
* Set mute to on/off
*
* @param {Boolean} enable The value to set mute to
*/
Viera.prototype.setMute = function(enable) {
var mute = (enable)? '1' : '0';
this.sendRequest('render', 'SetMute', "<InstanceID>0</InstanceID><Channel>Master</Channel><DesiredMute>" + mute + "</DesiredMute>");
return this;
};
// Export the constructor
module.exports = Viera;
}).call(this);