From cddc7096a56dfdb212ff29cc644aa9c52db6a0e4 Mon Sep 17 00:00:00 2001 From: raven Date: Wed, 21 Dec 2022 20:23:42 -0500 Subject: [PATCH] First Commit --- README.md | 30 ++++++++ client.html | 201 ++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 9 +++ tv_api.js | 205 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 445 insertions(+) create mode 100644 README.md create mode 100644 client.html create mode 100644 package.json create mode 100644 tv_api.js diff --git a/README.md b/README.md new file mode 100644 index 0000000..bb55dc4 --- /dev/null +++ b/README.md @@ -0,0 +1,30 @@ + +# Samsung TV Web Browser Controller + +Currently this project comes with an API to control your Samsung Smart TV which has the means to generate authenication tokens and the following functions: + +CURRENT COMMAND SHORTCUTS: + +HOME + +UP + +LEFT + +DOWN + +RIGHT + +Back + +Volume Up + +Volume Down + +MUTE + +Enter + +Picture Mode + +New Updates will also provide a GUI for the web app, currently its only using keyboard events. diff --git a/client.html b/client.html new file mode 100644 index 0000000..662c723 --- /dev/null +++ b/client.html @@ -0,0 +1,201 @@ + + + + + + +Scott Household TV Controller + +
+
+
+ + + + + + + + + diff --git a/package.json b/package.json new file mode 100644 index 0000000..3b8a4d3 --- /dev/null +++ b/package.json @@ -0,0 +1,9 @@ +{ + "dependencies": { + "cors": "^2.8.5", + "express": "^4.18.2", + "jsonfile": "^6.1.0", + "samsung-tv-control": "^1.13.0" + } +} + diff --git a/tv_api.js b/tv_api.js new file mode 100644 index 0000000..fcaeb76 --- /dev/null +++ b/tv_api.js @@ -0,0 +1,205 @@ +var fs = require('fs'); +const express = require('express') +const { Samsung, KEYS, APPS } = require('samsung-tv-control') +const jsonfile = require('jsonfile') +const file = '/home/token.json' +var https = require('https'); +var privateKey = fs.readFileSync('/cert/grwh.work.key', 'utf8'); +var certificate = fs.readFileSync('/cert/grwh.work.cert.combined', 'utf8'); +var credentials = {key: privateKey, cert: certificate}; + +var cors = require('cors') +const corsOptions ={ + origin:'*', + credentials:true, + optionSuccessStatus:200, +} + +jsonfile.readFile(file, function (err, obj) { + if (err) console.error(err) + console.log("token: " + obj.data) + const config = { + debug: false, // Default: false + ip: '192.168.0.2', + mac: 'A4:30:7A:08:57:6A', + nameApp: 'OurControlApp', // Default: NodeJS + port: 8002, // Default: 8002 + token: obj.data, + } + + + const control = new Samsung(config) + // Get all installed apps from TV + control.getAppsFromTV((err, res) => { + console.log(err) + if (!err) { + console.log('# Response getAppsFromTV', res) + } + }) + + async function sendKey(key) { + // Send key to TV + control.sendKey(key, function (err, res) { + if (!err) { + console.log("Sending: " + key) + } + }) + } + + const app = express() + var httpsServer = https.createServer(credentials, app); + + const port = 3031 + app.use(cors(corsOptions)) + + app.get('/', (req, res) => { + res.end('Welcome to the TV API!') + + }) + + app.get('/home', (req, res) => { + (async () => { + await sendKey(KEYS.KEY_HOME) + res.end('1') + })(); + }) + + app.get('/right', (req, res) => { + (async () => { + await sendKey(KEYS.KEY_RIGHT) + res.end('1') + })(); + }) + + app.get('/left', (req, res) => { + (async () => { + await sendKey(KEYS.KEY_LEFT) + res.end('1') + })(); + }) + + app.get('/up', (req, res) => { + (async () => { + await sendKey(KEYS.KEY_UP) + res.end('1') + })(); + }) + + app.get('/down', (req, res) => { + (async () => { + await sendKey(KEYS.KEY_DOWN) + res.end('1') + })(); + }) + + app.get('/enter', (req, res) => { + (async () => { + await sendKey(KEYS.KEY_ENTER) + res.end('1') + })(); + }) + + app.get('/back', (req, res) => { + (async () => { + await sendKey(KEYS.KEY_RETURN) + res.end('1') + })(); + }) + + app.get('/volumeDown', (req, res) => { + (async () => { + await sendKey(KEYS.KEY_VOLDOWN) + res.end('1') + })(); + }) + + app.get('/volumeUp', (req, res) => { + (async () => { + await sendKey(KEYS.KEY_VOLUP) + res.end('1') + })(); + }) + + app.get('/mute', (req, res) => { + (async () => { + await sendKey(KEYS.KEY_MUTE) + res.end('1') + })(); + }) + + app.get('/pmode', (req, res) => { + (async () => { + await sendKey(KEYS.KEY_PMODE) + res.end('1') + })(); + }) + + app.get('/off', (req, res) => { + (async () => { + await sendKey(KEYS.KEY_POWEROFF) + res.end('1') + })(); + }) + + app.get('/newToken', (req, res) => { + (async () => { + control + .isAvailable() + .then(() => { + // Get token for API + control.getToken((token) => { + console.info('# Response getToken:', token) + const obj = { data: token } + if (token == "null") return + jsonfile.writeFile(file, obj, function (err) { + if (err) console.error(err) + console.log("new key, exiting to refresh") + process.exit(0); + }) + }) + }) + res.end('1') + })(); + }) + + app.get('/openNetFlix', (req, res) => { + (async () => { + // Open app by appId which you can get from getAppsFromTV + control.openApp(APPS.Netflix, (err, res) => { + if (!err) { + console.log('# Response openApp', res) + } + }) + res.end('1') + })(); + }) + + app.get('/OpenYouTube', (req, res) => { + (async () => { + // Open app by appId which you can get from getAppsFromTV + control.openApp(APPS.Netflix, (err, res) => { + if (!err) { + console.log('# Response openApp', res) + } + }) + res.end('1') + })(); + }) + + app.get('/checkPass', (req, res) => { + (async () => { + let pass = "YourPassHere" + let sent = req.query.pass + if (pass == sent){ + console.log("User Authed Properly!") + res.end('1') + } else { + res.end('0') + } + })(); + }) + + httpsServer.listen(port, () => { + console.log(`TV Control API listening on port ${port}`) + }) +})