// Import Libraries
require('dotenv').config()
const Ably = require("ably");
const logger = require("./logger");
const express = require('express');
const axios = require("axios");
let ablyConn = new Ably.Realtime(process.env.ABLY_CONNECTION_KEY || "");
let channel = ablyConn.channels.get(process.env.ABLY_BROADCASTING_CHANNEL || "default");

// Create Express instance
let app = express();
//  Initialize the body-parser option
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

// Subscribe to messages on channel
channel.subscribe('device-trigger', function(message) {
  console.log("Event Received: " + message.data.triggername);
  axios.get(`${process.env.NTB_CONTROLLER_SERVICE}/api/v1/goto/${message.data.triggername}`)
});

const SERVER_PORT = process.env.NODE_PORT || 8980;
app.listen(SERVER_PORT, () => {
  logger.info(`Server Listening on Port : ${SERVER_PORT}`);
});
