Skip to content

Commit e484901

Browse files
committed
Added support for devices since 2016 (experimental)
1 parent 773354a commit e484901

File tree

7 files changed

+304
-20
lines changed

7 files changed

+304
-20
lines changed

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
[![Build status](https://ci.appveyor.com/api/projects/status/7ggeh5c3b1mcgoe9?svg=true)](https://ci.appveyor.com/project/soef/iobroker-samsung-3vcui)
77
[![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat)](https://github.com/soef/iobroker.samsung/blob/master/LICENSE)
88

9+
<!--[![Node](https://img.shields.io/badge/node-%3E=4.4-red.svg?style=flat-square)](https://www.npmjs.com/packages/soef)-->
10+
911
#### Description
1012

1113
Adapter for Samsung TVs
@@ -26,7 +28,8 @@ npm install iobroker.samsung
2628
```
2729

2830
#### Requirements
29-
Samsung TV before 2014/15
31+
Samsung TV before 2014<br>
32+
Support for devices since 2016 (version 0.2.0, experimental)
3033

3134
After installation, you have to confirm the new connection on your TV
3235

io-package.json

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
{
22
"common": {
33
"name": "samsung",
4-
"version": "0.1.1",
4+
"version": "0.2.1",
5+
"news": {
6+
"0.2.1": {
7+
"en": "Added isOnOff state",
8+
"de": "isOnOff state hinzugefügt."
9+
},
10+
"0.2.0": {
11+
"en": "Added support for devices since 2016 (experimental)",
12+
"de": "Unterstützung für Geräte ab 2016 (nicht getestet)"
13+
}
14+
},
515
"title": "Samsung TV Adapter",
616
"desc": {
717
"en": "Control Samsung TV devices",

keys.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var Keys = {
2222
'KEY_CH_LIST': 'Channel List',
2323
'KEY_PRECH': 'Previous Channel',
2424
'KEY_PICTURE_SIZE': 'Picture Size',
25-
25+
2626
'Input': null,
2727
'KEY_TV': 'TV',
2828
'KEY_HDMI': 'HDMI',

lib/ping.js

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
"use strict";
2+
3+
//taken from iobroker.discovery
4+
5+
var cp = require('child_process');
6+
var p = require('os').platform().toLowerCase();
7+
8+
var isWin = false;
9+
var init;
10+
var xfamily = ['linux', 'sunos', 'unix'];
11+
//var regex = /=.*[<|=]([0-9]*).*TTL|ttl..*=([0-9\.]*)/;
12+
var regex = /=.*[<|=]([0-9]*).*?ttl.*?=([0-9\.]*)/im;
13+
14+
15+
exports.reset = function () {
16+
// if config changed
17+
init = null;
18+
};
19+
20+
function probe (addr, config, callback) {
21+
config = config || {};
22+
23+
var ls = null;
24+
var log = config.log || console.log;
25+
var outstring = '';
26+
27+
if (!init) init = function (ip) {
28+
29+
var args = [];
30+
config = {
31+
numeric: config.numeric === undefined ? true : config.numeric,
32+
timeout: parseInt (config.timeout === undefined ? 2 : config.timeout, 10),
33+
minReply: parseInt (config.minReply === undefined ? 1 : config.minReply, 10),
34+
extra: config.extra || []
35+
};
36+
37+
//var args = [];
38+
39+
if (xfamily.indexOf (p) !== -1) {
40+
//linux
41+
//args = [];
42+
if (config.numeric !== false) args.push ('-n');
43+
44+
if (config.timeout !== false) args.push ('-w ' + config.timeout);
45+
46+
if (config.minReply !== false) args.push ('-c ' + config.minReply);
47+
48+
if (config.extra !== false) args = args.concat (config.extra);
49+
50+
args.push (addr);
51+
//log('System command: /bin/ping ' + args.join(' '));
52+
init = function (ip) {
53+
args[args.length-1] = ip;
54+
return cp.spawn ('/bin/ping', args);
55+
}
56+
} else if (p.match (/^win/)) {
57+
//windows
58+
isWin = true;
59+
var _args = [];
60+
if (config.minReply !== false) _args.push ('-n ' + config.minReply);
61+
62+
if (config.timeout !== false) _args.push ('-w ' + config.timeout * 1/*000*/);
63+
64+
if (config.extra !== false) _args = _args.concat (config.extra);
65+
66+
_args.push (addr);
67+
68+
args = [
69+
'/s', // leave quotes as they are
70+
'/c', // run and exit
71+
// !!! order of c and s is important - c must come last!!!
72+
'"', // enforce starting quote
73+
process.env.SystemRoot + '\\system32\\ping.exe' // command itself. Notice that you'll have to pass it quoted if it contains spaces
74+
].concat (_args)
75+
.concat ('"'); // enforce closing quote
76+
77+
//log('System command: ' + (process.env.comspec || 'cmd.exe') + ' ' + allArgs.join(' '));
78+
// switch the command to cmd shell instead of the original command
79+
init = function (ip) {
80+
args[args.length-2] = ip;
81+
return cp.spawn (process.env.comspec || 'cmd.exe', args, {windowsVerbatimArguments: true});
82+
}
83+
} else if (p === 'darwin' || p === 'freebsd') {
84+
//mac osx or freebsd
85+
//args = [];
86+
if (config.numeric !== false) args.push ('-n');
87+
88+
if (config.timeout !== false) args.push ('-t ' + config.timeout);
89+
90+
if (config.minReply !== false) args.push ('-c ' + config.minReply);
91+
92+
if (config.extra !== false) args = args.concat (config.extra);
93+
94+
args.push (addr);
95+
//log('System command: /sbin/ping ' + args.join(' '));
96+
init = function (ip) {
97+
args[args.length-1] = ip;
98+
return cp.spawn ('/sbin/ping', args);
99+
};
100+
} else {
101+
return callback && callback ('Your platform "' + p + '" is not supported');
102+
}
103+
return init(addr);
104+
};
105+
106+
ls = init(addr);
107+
108+
ls.on('error', function (e) {
109+
callback && callback(new Error('ping.probe: there was an error while executing the ping program. check the path or permissions...'));
110+
callback = null;
111+
});
112+
113+
ls.stdout.on('data', function (data) {
114+
outstring += String(data);
115+
});
116+
117+
ls.on('exit', function (code) {
118+
var ms, m; //, result = 1;
119+
if ((m = regex.exec(outstring)) && m.length >= 2) {
120+
ms = ~~m[1];
121+
//result = 0;
122+
}
123+
124+
// var lines = outstring.split('\n');
125+
// for (var t = 0; t < lines.length; t++) {
126+
// var m = regex.exec(lines[t]) || '';
127+
// if (m !== '') {
128+
// ms = m[1];
129+
// result = 0;
130+
// break;
131+
// }
132+
// }
133+
134+
if (callback) callback (null, {
135+
host: addr,
136+
alive: isWin ? (ms !== undefined) : !code,
137+
ms: ms
138+
});
139+
callback = null;
140+
});
141+
}
142+
143+
exports.probe = probe;

lib/samsung-2016.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"use strict";
2+
3+
//***
4+
// Based on https://github.com/Badisi/samsung-tv-remote
5+
//
6+
7+
const WebSocket = require('ws');
8+
const base64Encode = function(string) {
9+
return new Buffer(string).toString('base64');
10+
};
11+
12+
13+
function SamsungTvRemote(config) {
14+
if( !config.ip ) {
15+
throw new Error('TV IP address is required');
16+
}
17+
config.name = config.name || 'SamsungTvRemote';
18+
config.mac = config.mac || '00:00:00:00';
19+
config.port = config.port || 8001;
20+
config.timeout = config.timeout || 5000;
21+
22+
var self = this;
23+
this.onError = function (error) {
24+
console.log('Eroro Samsung Remote Client: ' + error.code);
25+
};
26+
27+
this.sendKey = this.send = function(key, callback) {
28+
//if(!key) return;
29+
30+
const url = 'http://' + config.ip + ':' + config.port + '/api/v2/channels/samsung.remote.control?name=' + base64Encode(config.name);
31+
var ws = new WebSocket(url, function (error) {
32+
error = error;
33+
});
34+
ws.on('error', function (error) {
35+
self.onError(error);
36+
});
37+
38+
ws.on('message', function (data, flags) {
39+
data = JSON.parse(data);
40+
if( data.event === 'ms.channel.connect' ) {
41+
42+
if (key) { ws.send(
43+
JSON.stringify({
44+
'method': 'ms.remote.control',
45+
'params': {
46+
'Cmd': 'Click',
47+
'DataOfCmd': key,
48+
'Option': 'false',
49+
'TypeOfRemote': 'SendRemoteKey'
50+
}
51+
}),
52+
callback
53+
)} else callback && callback('success');
54+
55+
setTimeout( function () {
56+
ws.close();
57+
}, 1000);
58+
}
59+
});
60+
}
61+
}
62+
63+
module.exports = SamsungTvRemote;

package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "iobroker.samsung",
3-
"version": "0.1.1",
3+
"version": "0.2.1",
44
"description": "Samsung Adapter",
55
"author": {
66
"name": "soef",
@@ -27,7 +27,8 @@
2727
"url": "https://github.com/soef/iobroker.samsung"
2828
},
2929
"dependencies": {
30-
"samsung-remote": "^1.2.5"
30+
"samsung-remote": "^1.2.5",
31+
"ws": "^2.3.1"
3132
},
3233
"devDependencies": {
3334
"grunt": "^0.4.5",
@@ -46,4 +47,4 @@
4647
"scripts": {
4748
"test": "node node_modules/mocha/bin/mocha"
4849
}
49-
}
50+
}

0 commit comments

Comments
 (0)