Skip to content

Commit a042fd8

Browse files
committed
create cli version
1 parent f6b483b commit a042fd8

File tree

4 files changed

+78
-41
lines changed

4 files changed

+78
-41
lines changed

Diff for: cli.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env node
2+
3+
const { resolve } = require('path');
4+
const { connect, sendCommand, disconnect } = require(resolve(__dirname, './client'));
5+
6+
connect()
7+
.then(() => sendCommand(process.argv[2]))
8+
.then(() => disconnect());

Diff for: client.js

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
const { resolve } = require('path');
2+
const WebSocket = require('ws');
3+
const readline = require('readline');
4+
5+
const mappings = require(resolve(__dirname, './mappings'));
6+
7+
const IP = '192.168.1.100';
8+
const PORT = 8001;
9+
10+
const stdin = process.openStdin();
11+
const readLine = readline.createInterface({
12+
input: process.stdin,
13+
output: process.stdout,
14+
terminal: false
15+
});
16+
17+
function getCommand(keyPress) {
18+
const command = {
19+
method: 'ms.remote.control',
20+
params: {
21+
Cmd: 'Click',
22+
DataOfCmd: keyPress,
23+
Option: 'false',
24+
TypeOfRemote: 'SendRemoteKey'
25+
}
26+
};
27+
28+
return JSON.stringify(command);
29+
}
30+
31+
function sendCommand(input) {
32+
const keyPress = mappings[input]
33+
if (keyPress) {
34+
connection.send(getCommand(keyPress));
35+
}
36+
}
37+
38+
function listenForCommands() {
39+
readLine.on('line', sendCommand);
40+
}
41+
42+
let connection;
43+
function connect() {
44+
return new Promise((resolve) => {
45+
connection = new WebSocket(`ws://${IP}:${PORT}/api/v2/channels/samsung.remote.control`);
46+
connection.on('open', () => {
47+
listenForCommands();
48+
resolve();
49+
});
50+
});
51+
}
52+
53+
function disconnect() {
54+
if (connection) {
55+
connection.close();
56+
readLine.close();
57+
connection = undefined;
58+
}
59+
}
60+
61+
module.exports = {
62+
connect,
63+
disconnect,
64+
sendCommand
65+
};

Diff for: index.js

+2-41
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,3 @@
1-
const mappings = require('./mappings');
2-
const WebSocket = require('ws');
3-
const readline = require('readline');
4-
5-
const IP = '192.168.1.100';
6-
const PORT = 8001;
7-
const ws = new WebSocket(`ws://${IP}:${PORT}/api/v2/channels/samsung.remote.control`);
8-
9-
const stdin = process.openStdin();
10-
const readLine = readline.createInterface({
11-
input: process.stdin,
12-
output: process.stdout,
13-
terminal: false
14-
});
15-
16-
function getCommand(keyPress) {
17-
const command = {
18-
method: 'ms.remote.control',
19-
params: {
20-
Cmd: 'Click',
21-
DataOfCmd: keyPress,
22-
Option: 'false',
23-
TypeOfRemote: 'SendRemoteKey'
24-
}
25-
};
26-
27-
return JSON.stringify(command);
28-
}
29-
30-
function processLineEntry(input) {
31-
const keyPress = mappings[input]
32-
if (keyPress) {
33-
ws.send(getCommand(keyPress));
34-
}
35-
}
36-
37-
function listenForCommands() {
38-
readLine.on('line', processLineEntry);
39-
}
40-
41-
ws.on('open', listenForCommands);
1+
const { connect } = require("./client");
422

3+
connect();

Diff for: package.json

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
"start": "node index.js",
88
"debug": "nodemon index.js"
99
},
10+
"bin": {
11+
"tv": "./cli.js"
12+
},
1013
"author": "Nathan Mahdavi",
1114
"license": "ISC",
1215
"dependencies": {

0 commit comments

Comments
 (0)