node-red-contrib-samsung-tv-mk
Version:
Node-red node to control Samsung TV
73 lines (53 loc) • 1.89 kB
JavaScript
const Samsung = require('samsung-tv-control').default
const keysProvider = require('./keys.js').provideKeys;
function registerNodes(RED) {
function configNode(config) {
const apiConfig = {
name: 'Node-red',
token: config.token,
ip: config.ip,
mac: '123456789ABC'
}
RED.nodes.createNode(this, config)
this.remote = new Samsung(apiConfig)
this.remote.turnOn()
}
function isAliveNode(config) {
RED.nodes.createNode(this, config)
const node = this
node.on('input', function (msg) {
const device = RED.nodes.getNode(config.device)
device.remote.isAvaliable()
.then(() => node.send([null, msg]))
.catch(e => node.send([msg, null]))
})
}
function sendNode(config) {
RED.nodes.createNode(this, config)
this.on('input', function (msg) {
const device = RED.nodes.getNode(config.device)
var key = config.key ? config.key : msg.payload
if (!key) {
this.error('No key given. Specify either in the config or via msg.payload!')
return;
}
const node = this
device.remote.isAvaliable()
.then(() => device.remote.sendKey(String(key), function (err, res) {
if (err) {
node.warn(err)
} else {
//
}
}))
.catch(err => {
node.warn(err)
})
})
}
RED.nodes.registerType("samsung-tv", configNode)
RED.nodes.registerType("samsung-tv-send", sendNode)
RED.nodes.registerType("samsung-tv-isalive", isAliveNode)
keysProvider(RED)
}
module.exports = registerNodes;