Contenidos
Ejemplo de ejecución
- https://localhost/api/bombilla?parametro=0&tipo=enchufe&nombre=habitacion
- https://localhost/api/bombilla?parametro=1&tipo=enchufe&nombre=habitacion
Código para encender y apagar la bombilla (o cualquier otro dispositivo)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
import axios from 'axios'; import type { VercelRequest, VercelResponse } from '@vercel/node'; import { login, TplinkDeviceManager, HS100 } from 'tplink-cloud-api'; import { v4 as uuidV4 } from 'uuid'; export default async function handler(req: VercelRequest, res: VercelResponse) { const TPLINK_PASS: string = 'passajsdfljasdflkjasdfsdf'; const TPLINK_TERM: string = 'lkasldfkjasdf-ljalsdfas-ljasdf-asdfasf'; let tplink: TplinkDeviceManager; let myPlug: HS100; try { tplink = await login(TPLINK_USER, TPLINK_PASS, TPLINK_TERM); const dl = await tplink.getDeviceList(); const nombre = req.query.nombre as string; if (!nombre) { return res.status(400).json({ error: 'El parámetro nombre es obligatorio' }); } myPlug = tplink.getHS100(nombre); } catch (error) { console.error('Error:', error); return res.status(500).json({ error: 'Error al conectarse a TP-Link Cloud' }); } const parametro = req.query.parametro as string; const tipo = req.query.tipo as string; if (parametro !== '1' && parametro !== '0') { return res.status(400).json({ error: 'El parámetro debe ser 1 o 0' }); } if (tipo !== 'luz' && tipo !== 'enchufe') { return res.status(400).json({ error: 'El parámetro tipo debe ser luz o enchufe' }); } const Token = tplink.getToken(); const URI = `https://eu-wap.tplinkcloud.com/?token=${Token}`; const Cabeceras = { 'Content-type': 'application/json' }; let Body: any; if (tipo === 'luz') { if (parametro === '1') { Body = { method: "passthrough", params: { deviceId: myPlug.getDeviceId(), requestData: `{"smartlife.iot.smartbulb.lightingservice":{"transition_light_state": {"on_off": 1,"transition_period": 0}}}` } }; } else { Body = { method: "passthrough", params: { deviceId: myPlug.getDeviceId(), requestData: `{"smartlife.iot.smartbulb.lightingservice":{"transition_light_state": {"on_off": 0,"transition_period": 0}}}` } }; } } else if (tipo === 'enchufe') { if (parametro === '1') { Body = { method: "passthrough", params: { deviceId: myPlug.getDeviceId(), requestData: `{"system":{"set_relay_state":{"state":1}}}` } }; } else { Body = { method: "passthrough", params: { deviceId: myPlug.getDeviceId(), requestData: `{"system":{"set_relay_state":{"state":0}}}` } }; } } axios.post(URI, Body, { headers: Cabeceras }) .then(response => { console.log(response.data.result); return res.json({ resultado: parametro }); }) .catch(error => { console.error(error); return res.status(500).json({ error: 'Error al realizar la solicitud al dispositivo' }); }); } |
Package.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
{ "repository": "https://github.com/vercel/examples.git", "license": "MIT", "private": true, "devDependencies": { "@types/node": "^17.0.42", "@vercel/node": "^2.9.6", "typescript": "^4.7.3", "axios": "^0.24.0" }, "dependencies": { "tplink-cloud-api": "^0.1.1", "uuid": "^8.3.2" } } |