const express = require('express'); const mqtt = require('mqtt'); const router = express.Router(); const {MQTT_SERVER_IP, PUBLISH_TOPIC, SUBSCRIBE_TOPIC} = require('../config'); const client = mqtt.connect(`mqtt://${MQTT_SERVER_IP}`);
router.post('/', function (req, res) {
let command = req.body.header.name;
switch (command) { case "DiscoverAppliancesRequest": DiscoverAppliancesRequest(req, res); break; case "HealthCheckRequest": HealthCheckRequest(req, res); break; case "TurnOnRequest": TurnOnRequest(req, res); break; case "TurnOffRequest": TurnOffRequest(req, res); break; default: res.sendStatus(403); break; } });
router.get('/login', function (req, res) { console.log(req.query); let url = decodeURIComponent(req.query.redirect_uri)+"?state="+req.query.state+"&code="+"FakeToken"+"&token_type=Bearer"; res.redirect(url); });
router.post('/token',function(req,res){ res.send(` { "access_token":"fakeAccess", "refresh_token":"fakeRefresh" } `); });
function DiscoverAppliancesRequest(req, res) {
let messageId = req.body.header.messageId;
let resultObject = new Object(); resultObject.header = new Object(); resultObject.header.messageId = messageId; resultObject.header.name = "DiscoverAppliancesResponse "; resultObject.header.namespace = "HomeIOT"; resultObject.header.payloadVersion = "1.0"; resultObject.payload = new Object(); resultObject.payload.discoveredAppliances = new Array();
let switchbot = new Object(); switchbot.applianceId = "ESP01-001"; switchbot.manufacturerName = " Ai-Thinker"; switchbot.modelName = "ESP-01"; switchbot.friendlyName = "전등"; switchbot.version = "9.5.0" switchbot.isIr = false; switchbot.actions = ["HealthCheck", "TurnOn", "TurnOff"]; switchbot.applianceTypes = ["SWITCH"]; resultObject.payload.discoveredAppliances.push(switchbot);
res.send(resultObject); }
function HealthCheckRequest(req, res) {
const client = mqtt.connect(`mqtt://${MQTT_SERVER_IP}`);
client.on('connect', function () {
client.subscribe(SUBSCRIBE_TOPIC, {qos: 1});
let messageId = req.body.header.messageId;
let resultObject = new Object(); resultObject.header = new Object(); resultObject.header.messageId = messageId; resultObject.header.name = "HealthCheckResponse "; resultObject.header.namespace = "HomeIOT"; resultObject.header.payloadVersion = "1.0"; resultObject.payload = new Object(); resultObject.payload.isReachable = true;
client.on('message', (topic, msg) => { if (msg.toString().toLowerCase() == "on") { resultObject.payload.isTurnOn = true; } else { resultObject.payload.isTurnOn = false; } res.send(resultObject); client.end(); });
client.publish(PUBLISH_TOPIC, "", {qos: 1}); }); }
function TurnOnRequest(req, res) {
client.publish(PUBLISH_TOPIC, "ON");
let applianceId = req.body.payload.appliance.applianceId; let messageId = req.body.header.messageId;
let resultObject = new Object(); resultObject.header = new Object(); resultObject.header.messageId = req.body.header.messageId; resultObject.header.name = "TurnOnConfirmation"; resultObject.header.payloadVersion = "1.0"; resultObject.payload = new Object(); res.send(resultObject); }
function TurnOffRequest(req, res) {
client.publish(PUBLISH_TOPIC, "OFF");
let applianceId = req.body.payload.appliance.applianceId; let messageId = req.body.header.messageId;
let resultObject = new Object(); resultObject.header = new Object(); resultObject.header.messageId = req.body.header.messageId; resultObject.header.name = "TurnOffConfirmation"; resultObject.header.payloadVersion = "1.0"; resultObject.payload = new Object(); res.send(resultObject); }
module.exports = router; |