UNPKG

vistamare-smarthome

Version:

service to manage actions via REST

156 lines (148 loc) 5.39 kB
var express = require('express'); const connection = require( './util/connection' ); const devices = require( './util/devices' ); const delay = require( 'delay' ); const https = require('https'); const fs = require('fs'); const port = 3000; const adress = process.argv[2]; // IKEA Gateway const tvkeys = require('./util/tv-keys.json'); const TuyAPI = require('tuyapi'); var format = require('date-format'); // --------------------------------------------------------------------------- TV /* const SamsungRemote = require('samsung-remote'); const remote = new SamsungRemote({ ip: process.argv[3] // required: IP address of your Samsung Smart TV }); */ // --------------------------------------------------------------------------- TV // var key = fs.readFileSync(__dirname + '\\security\\selfsigned.key'); // for win // var cert = fs.readFileSync(__dirname + '\\security\\selfsigned.crt'); // for win var key = fs.readFileSync(__dirname + '/security/selfsigned.key'); // for termux var cert = fs.readFileSync(__dirname + '/security/selfsigned.crt'); // for termux var options = { key: key, cert: cert }; app = express() app.use(express.json()); (async () => { const tradfri = await connection.getConnection(); tradfri.observeDevices(); await delay( 1000 ) let currentDevice = null; let accessory = null; app.get('/status', (req, res) => { const b64auth = (req.headers.authorization || '').split(' ')[1] || '' const [login, password] = Buffer.from(b64auth, 'base64').toString().split(':') if (login === process.argv[5] && password === process.argv[6]){ // var result = eval('device.lightList[0].' + req.query.attribute); if (req.query.id === 'all'){ res.send(tradfri.devices); } else if (req.query.id === 'tvkeys') { res.send(tvkeys); } else { const device = tradfri.devices[req.query.id]; res.send(device.lightList[0]); } // res.send("{ 'value' : " + result + "}"); } else { console.log(format('yyyy-MM-dd hh:mm:ss: ',new Date()),"unauthorized GET",req.query); // console.log(process.argv[5], login, process.argv[6], password); res.status(401).send("Authorization not correctly ... tonto"); } }); app.post('/change', function(request, response){ var command = request.body; var resp_status = 200; var resp_message = command.action + "-on-ID: " + command.ID + ", 'with Value':" + command.value; const b64auth = (request.headers.authorization || '').split(' ')[1] || '' const [login, password] = Buffer.from(b64auth, 'base64').toString().split(':') if (login === process.argv[5] && password === process.argv[6]){ // wenn es ein direktes KOmmando für ein Gerät ist if(command.system === 'IKEA'){ console.log(format('yyyy-MM-dd hh:mm:ss: ',new Date()),"IKEA GET",command); if(command.ID > 0) { currentDevice = devices.findDevice( tradfri, command.ID ); accessory = currentDevice.lightList[0]; accessory.client = tradfri; switch( command.action ) { case 'on': accessory.turnOn();break; case 'off': accessory.turnOff();break; case 'toggle': accessory.toggle();break; case 'color': accessory.setColor(command.value);break; case 'brightness': accessory.setBrightness( command.value );break; default: resp_status = 501; resp_message = 'IKEA command not allowed'; break; } } else { resp_status = 501; resp_message = 'IKEA command-ID missing'; } } /* if(command.system === 'TV'){ // check if TV is alive (ping) remote.isAlive((err) => { if (err) { // throw new Error('TV is offline'); resp_status = 501; resp_message = 'TV is offline'; } else { remote.send(command.action, (err) => { if (err) { // throw new Error(err); console.log(format('yyyy-MM-dd hh:mm:ss: ',new Date()),'key has sended but with error'); resp_status = 501; resp_message = 'TV command error'; } else { // command has been successfully transmitted to your tv } }); } }); } */ if(command.system === 'TUYA'){ if(command.ID != '') { const tuyadevice = new TuyAPI({ id: command.ID, ip: command.IP, key: command.key, version: '3.3', issueRefreshOnConnect: false}); switch( command.action ) { case 'toggle': (async () => { let status = await tuyadevice.get(); await tuyadevice.set({set: !status}); status = await tuyadevice.get(); tuyadevice.disconnect(); })(); break; default: resp_status = 501; resp_message = 'TUYA command not allowed'; break; } } else { resp_status = 501; resp_message = 'TUYA Device-ID missing'; } } } else { console.log(format('yyyy-MM-dd hh:mm:ss: ',new Date()),"unauthorized POST",request.body); resp_status = 401; resp_message = "Authorization not correctly"; } response.status(resp_status).send(resp_message); // echo the result back }); })() var server = https.createServer(options, app); server.listen(port, () => { console.log(format('yyyy-MM-dd hh:mm:ss: ',new Date()),"server starting, REQ is expecting on https:// " + adress + ":" + port + "/status") }); /* this is for http only app.listen(8081, () => { console.log("Server running on port 8081"); }); */ // await delay(1000); // await tradfri.destroy(); // process.exit(0);