Adding text input popup window with self destroy windows when done

This commit is contained in:
raven
2022-12-21 21:20:20 -05:00
parent bc65cc0cd9
commit e2122a1715
5 changed files with 26 additions and 0 deletions

View File

@ -0,0 +1,25 @@
<html>
<head>
<style>
body {
background-color: #101010;
color: #ffffff;
}
h1 {
color: #ffffff;
}
nav a {
color: #ffffff;
}
footer {
color: #ffffff;
}
</style>
</head><CENTER> The Input was Sent!<BR><BR>
<body><input type="button" id="close" value="Close" onclick="window.close()" />
</body></CENTER>
</body>
</html>

9
apiserver/package.json Normal file
View File

@ -0,0 +1,9 @@
{
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.2",
"jsonfile": "^6.1.0",
"samsung-tv-control": "^1.13.0"
}
}

205
apiserver/tv_api.js Normal file
View File

@ -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/cert.key', 'utf8');
var certificate = fs.readFileSync('/cert/cert.crt', '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}`)
})
})