SamsungTVController/tv_api.js

206 lines
4.4 KiB
JavaScript
Raw Normal View History

2022-12-21 20:23:42 -05:00
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');
2022-12-21 20:25:52 -05:00
var privateKey = fs.readFileSync('/cert/cert.key', 'utf8');
var certificate = fs.readFileSync('/cert/cert.crt', 'utf8');
2022-12-21 20:23:42 -05:00
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}`)
})
})