diff --git a/apiserver/tv_api.js b/apiserver/tv_api.js index 1e0438a..b6ebea4 100644 --- a/apiserver/tv_api.js +++ b/apiserver/tv_api.js @@ -2,34 +2,45 @@ var fs = require('fs'); const express = require('express') const { Samsung, KEYS, APPS } = require('samsung-tv-control') const jsonfile = require('jsonfile') +// Location of File Cache const file = '/home/token.json' var https = require('https'); -var privateKey = fs.readFileSync('/cert/cert.key', 'utf8'); -var certificate = fs.readFileSync('/cert/cert.crt', 'utf8'); + +// Set up SSL +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}; +// Keep BASE URL in case its needed +let baseURL = "https://tv.grwh.work" + +// Enable CORS for Express var cors = require('cors') +// Use These options to allow ALL const corsOptions ={ origin:'*', credentials:true, optionSuccessStatus:200, } +// Use JSON NPM to read the token from cache jsonfile.readFile(file, function (err, obj) { if (err) console.error(err) + // The token was read, here it is console.log("token: " + obj.data) + // Lets add that to the configuration const config = { debug: false, // Default: false ip: '192.168.0.2', mac: 'A4:30:7A:08:57:6A', - nameApp: 'OurControlApp', // Default: NodeJS + nameApp: 'WebControl', // Default: NodeJS port: 8002, // Default: 8002 token: obj.data, } - + // Now we have a config, lets set it const control = new Samsung(config) - // Get all installed apps from TV + // Get all installed apps from TV - FUNCTION control.getAppsFromTV((err, res) => { console.log(err) if (!err) { @@ -37,6 +48,7 @@ jsonfile.readFile(file, function (err, obj) { } }) + // Function to send KEY! async function sendKey(key) { // Send key to TV control.sendKey(key, function (err, res) { @@ -45,18 +57,31 @@ jsonfile.readFile(file, function (err, obj) { } }) } - + // Function to send INPUT! + async function sendInput(input){ + // Send text to focused input on TV + control.sendText(input, function (err, res) { + if (!err) { + console.log("Sending: " + input) + console.log('# Response sendText', res) + } + }) + } + // Defube Express and https serer const app = express() var httpsServer = https.createServer(credentials, app); - + // Set port const port = 3031 + // USe CORS app.use(cors(corsOptions)) + // Lets handle BLANK Traffic! app.get('/', (req, res) => { res.end('Welcome to the TV API!') }) - + + // Handling home app.get('/home', (req, res) => { (async () => { await sendKey(KEYS.KEY_HOME) @@ -64,6 +89,7 @@ jsonfile.readFile(file, function (err, obj) { })(); }) + // Handling right app.get('/right', (req, res) => { (async () => { await sendKey(KEYS.KEY_RIGHT) @@ -71,6 +97,7 @@ jsonfile.readFile(file, function (err, obj) { })(); }) + // Handling left app.get('/left', (req, res) => { (async () => { await sendKey(KEYS.KEY_LEFT) @@ -78,6 +105,7 @@ jsonfile.readFile(file, function (err, obj) { })(); }) + // up app.get('/up', (req, res) => { (async () => { await sendKey(KEYS.KEY_UP) @@ -85,6 +113,7 @@ jsonfile.readFile(file, function (err, obj) { })(); }) + // Handling down app.get('/down', (req, res) => { (async () => { await sendKey(KEYS.KEY_DOWN) @@ -92,6 +121,7 @@ jsonfile.readFile(file, function (err, obj) { })(); }) + // Handling enter app.get('/enter', (req, res) => { (async () => { await sendKey(KEYS.KEY_ENTER) @@ -99,6 +129,7 @@ jsonfile.readFile(file, function (err, obj) { })(); }) + // Handling back app.get('/back', (req, res) => { (async () => { await sendKey(KEYS.KEY_RETURN) @@ -106,6 +137,7 @@ jsonfile.readFile(file, function (err, obj) { })(); }) + // Handling volume down app.get('/volumeDown', (req, res) => { (async () => { await sendKey(KEYS.KEY_VOLDOWN) @@ -113,6 +145,7 @@ jsonfile.readFile(file, function (err, obj) { })(); }) + // Handling volume up app.get('/volumeUp', (req, res) => { (async () => { await sendKey(KEYS.KEY_VOLUP) @@ -120,6 +153,7 @@ jsonfile.readFile(file, function (err, obj) { })(); }) + // Handling mute app.get('/mute', (req, res) => { (async () => { await sendKey(KEYS.KEY_MUTE) @@ -127,6 +161,7 @@ jsonfile.readFile(file, function (err, obj) { })(); }) + // Handling picture mode app.get('/pmode', (req, res) => { (async () => { await sendKey(KEYS.KEY_PMODE) @@ -134,6 +169,7 @@ jsonfile.readFile(file, function (err, obj) { })(); }) + // Handling off app.get('/off', (req, res) => { (async () => { await sendKey(KEYS.KEY_POWEROFF) @@ -141,6 +177,19 @@ jsonfile.readFile(file, function (err, obj) { })(); }) + + // Handling input + app.get('/input', (req, res) => { + const path = require('path'); + (async () => { + let sent = req.query.data + await sendInput(sent) + res.sendFile(path.join(__dirname, '/finish_input.html')); + })(); + }) + + + // Generating a new token app.get('/newToken', (req, res) => { (async () => { control @@ -150,7 +199,7 @@ jsonfile.readFile(file, function (err, obj) { control.getToken((token) => { console.info('# Response getToken:', token) const obj = { data: token } - if (token == "null") return + if (token == null) return jsonfile.writeFile(file, obj, function (err) { if (err) console.error(err) console.log("new key, exiting to refresh") @@ -162,33 +211,10 @@ jsonfile.readFile(file, function (err, obj) { })(); }) - 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') - })(); - }) - + // Hadling checking password app.get('/checkPass', (req, res) => { (async () => { - let pass = "YourPassHere" + let pass = "scott" let sent = req.query.pass if (pass == sent){ console.log("User Authed Properly!") @@ -199,6 +225,7 @@ jsonfile.readFile(file, function (err, obj) { })(); }) + // Listen httpsServer.listen(port, () => { console.log(`TV Control API listening on port ${port}`) }) diff --git a/htdocs/index.html b/htdocs/index.html index 43fe7ec..b01f1ea 100644 --- a/htdocs/index.html +++ b/htdocs/index.html @@ -7,6 +7,7 @@ Samsung TVController ++ + + + + + + + +
-
-
+ +