adding comments

This commit is contained in:
raven 2022-12-22 04:19:27 -05:00
parent 1a9b8532d5
commit 65c7735804
2 changed files with 100 additions and 48 deletions

View File

@ -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}`)
})

View File

@ -7,6 +7,7 @@
</head>
<title>Samsung TVController</title>
<!--Styles-->+
<style>
body {
background-color: #101010;
@ -25,16 +26,22 @@
color: #ffffff;
}
</style>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"
integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
crossorigin="anonymous"></script>
<!--END Styles-->
<!--MAIN JS-->
<script>
// Set API URL
let rootURL = "https://grwh.work:3031"
// Init timer VAR
var timer;
// Youtube Shorts Timer -
// Youtube Shorts Timer - Skip Every 55 Seconds
function startTimer() {
console.log("Shorts timer started!")
showSuccessAlert("Started Shorts Timer")
@ -56,11 +63,13 @@
}, 55000);
}
// Kill the Timer
function stopTimer() {
showSuccessAlert("Shorts Timer Disabled!")
clearInterval(timer);
}
// Settng up success alerts
function showSuccessAlert(message) {
document.getElementById("view2").innerHTML = "<div class=\"alert alert-success\" role=\"alert\"><CENTER>" + message + "</CENTER></div>";
@ -70,6 +79,7 @@
}, 15000);
}
// Setting up Failed Alerts
function showFailAlert(message) {
document.getElementById("view2").innerHTML = "<div class=\"alert alert-danger\" role=\"alert\"><CENTER>" + message + "</CENTER></div>";
@ -81,9 +91,8 @@
}
showSuccessAlert("You have logged in successfully!")
// This function is only used for the Refresh Button
function generateNewToken() {
fetch(rootURL + "/newToken")
.then(function (response) {
@ -100,23 +109,26 @@
})
}
// Ask the user for the password.
let pass = prompt("Enter the password!", "");
let text;
// Ask the API if the password is correct
fetch(rootURL + "/checkPass?pass=" + pass)
.then(function (response) {
return response.json();
})
}) // Check for a positive 1 response back
.then(function (data) {
if (data == "1") {
// If they do not submit a password OR hit cancel, fail the login!
if (pass == null || pass == "") {
document.getElementById("view").innerHTML = "WOO"
} else {
// If we got this far, we have logged in, lets tell the user that
showSuccessAlert("You have logged in successfully!")
document.getElementById("view").innerHTML = "<CENTER><IMG SRC=\"../images/icon.png\"></CENTER><a href=\"javascript:window.open('help.html','Click for Key ShortCuts!','width=600,height=400')\">Click for Key ShortCuts!</a><BR><BR><a href=\"javascript:window.open('input.html','Click to Send Text Imput!','width=600,height=400')\"><button type=\"button\" class=\"btn btn-primary\">Open Text Input</button></a><BR><BR><button type=\"button\" class=\"btn btn-primary\" onclick=\"startTimer()\">Enable AutoScroll</button> / <button type=\"button\" class=\"btn btn-primary\" onclick=\"stopTimer()\"> Disable</button> <BR><BR>Commands Not Working? Issue a new Token using: <BR> <button type=\"button\" class=\"btn btn-primary\" onclick=\"generateNewToken()\">Request new Token</button>";
// Adding the event Listening Function
function addEvent(element, eventName, callback) {
if (element.addEventListener) {
element.addEventListener(eventName, callback, false);
@ -127,9 +139,11 @@
}
}
// Lets now listen for events
addEvent(document, "keypress", function (e) {
e = e || window.event;
// Command UP
if (e.key == "w" || e.code == "Numpad8") {
fetch(rootURL + "/up")
.then(function (response) {
@ -146,6 +160,7 @@
})
}
// Command LEFT
if (e.key == "a" || e.code == "Numpad4") {
fetch(rootURL + "/left")
.then(function (response) {
@ -162,6 +177,7 @@
})
}
// Command DOWN
if (e.key == "s" || e.key == "2") {
fetch(rootURL + "/down")
.then(function (response) {
@ -178,6 +194,7 @@
})
}
// Command RIGHT
if (e.key == "d" || e.code == "Numpad6") {
fetch(rootURL + "/right")
.then(function (response) {
@ -194,6 +211,7 @@
})
}
// Command ENTER
if (e.key == "Enter" || e.code == "Numpad5") {
fetch(rootURL + "/enter")
.then(function (response) {
@ -210,6 +228,7 @@
})
}
// Command BACK
if (e.key == "x" || e.code == "NumpadDecimal") {
fetch(rootURL + "/back")
.then(function (response) {
@ -226,6 +245,7 @@
})
}
// Command VOL UP
if (e.key == "p" || e.code == "NumpadAdd") {
fetch(rootURL + "/volumeUp")
.then(function (response) {
@ -242,6 +262,7 @@
})
}
// Command VOL DOWN
if (e.key == "o" || e.code == "NumpadSubtract") {
fetch(rootURL + "/volumeDown")
.then(function (response) {
@ -258,6 +279,7 @@
})
}
// Request new token using the R shortcut
if (e.key == "r") {
fetch(rootURL + "/newToken")
.then(function (response) {
@ -274,7 +296,7 @@
})
}
// Command MUTE
if (e.key == "m" || e.code == "Numpad0") {
fetch(rootURL + "/mute")
.then(function (response) {
@ -291,6 +313,7 @@
})
}
// Command Picture Mode
if (e.key == "l") {
fetch(rootURL + "/pmode")
.then(function (response) {
@ -307,6 +330,7 @@
})
}
// Command HOME
if (e.key == "h") {
fetch(rootURL + "/home")
.then(function (response) {
@ -323,26 +347,27 @@
})
}
});
}
} else {
// Login Failed!
showFailAlert("The password was incorrect!")
document.getElementById("view").innerHTML = "<meta http-equiv=\"refresh\" content=3;>"
}
})
</script>
<!--END MAIN JS-->
</head>
<body>
<center>
<!--MAIN VIEWPORT-->
<center>
<div id="view"></div>
<center>
<!--END MAIN VIEWPORT-->
</body>
</html>