// Path: dashboard/src/main/java/nl/crowndov/displaydirect/dashboard/mqtt/MonitoringHandler.java
// public class MonitoringHandler implements Callback<DisplayDirectMessage.Monitoring> {
// 
//     private static final Logger LOGGER = LoggerFactory.getLogger(MonitoringHandler.class);
// 
//     @Override
//     public void onSuccess(DisplayDirectMessage.Monitoring value) {
//         if (value.getLogsList().size() > 0) {
//             value.getLogsList().forEach(m -> LogStore.add(DisplayDirectMessageParser.toLogMessage(m)));
//         } else if (value.getMetricsList().size() > 0) {
//             value.getMetricsList().forEach(m -> MetricStore.update(DisplayDirectMessageParser.toMetric(m)));
//         } else {
//             LOGGER.error("Got empty log message");
//         }
//     }
// 
//     @Override
//     public void onFailure(Throwable value) {
//         LOGGER.info("Failed with error", value);
//     }
// }
// 
// Path: dashboard/src/main/java/nl/crowndov/displaydirect/dashboard/mqtt/MqttClient.java
// public class MqttClient {
// 
//     private static final Logger LOGGER = LoggerFactory.getLogger(MqttClient.class);
// 
//     private MQTT mqtt;
//     private final CallbackConnection connection;
// 
//     private Callback<byte[]> subscriptionCompleteCallback = new Callback<byte[]>() {
//         @Override
//         public void onSuccess(byte[] value) {
//             LOGGER.info("Subscribed succesfully");
//         }
// 
//         @Override
//         public void onFailure(Throwable value) {
//             LOGGER.info("Error during subscribing", value);
//         }
//     };
// 
//     public MqttClient(Callback<DisplayDirectMessage.Monitoring> msg) {
//         mqtt = new MQTT();
//         try {
//             mqtt.setHost(Configuration.getHostname());
//         } catch (URISyntaxException e) {
//             LOGGER.error("Error setting host");
//         }
//         mqtt.setClientId(Configuration.getClientId());
//         mqtt.setCleanSession(false);
// 
//         mqtt.setKeepAlive((short) 60);
// //            mqtt.setReconnectBackOffMultiplier(3);
// //            mqtt.setConnectAttemptsMax(10000);
// //            mqtt.setReconnectDelay(20);
// //            mqtt.setReconnectDelayMax(10000);
//         mqtt.setVersion("3.1.1");
// 
//         connection = mqtt.callbackConnection();
//         LOGGER.info("Setting up connection");
//         connection.listener(new Listener() {
//             @Override
//             public void onConnected() {
//                 LOGGER.info("Connected");
//             }
// 
//             @Override
//             public void onDisconnected() {
//                 LOGGER.info("Disconnected");
//             }
// 
//             @Override
//             public void onPublish(UTF8Buffer topic, Buffer body, Runnable ack) {
//                 try {
//                     DisplayDirectMessage.Monitoring mon = DisplayDirectMessage.Monitoring.parseFrom(body.toByteArray());
//                     msg.onSuccess(mon);
//                 } catch (InvalidProtocolBufferException e) {
//                     LOGGER.error("Failed to parse message", e);
//                     msg.onFailure(e);
//                 }
//                 ack.run();
//             }
// 
//             @Override
//             public void onFailure(Throwable value) {
//                 LOGGER.info("MQTT Client failed", value);
//             }
//         });
//         connection.connect(new Callback<Void>() {
//             @Override
//             public void onSuccess(Void value) {
//                 LOGGER.info("Subscribing to topics");
//                 Topic[] topics = { new Topic (TopicFactory.monitoring(), QoS.AT_LEAST_ONCE) };
//                 connection.subscribe(topics, subscriptionCompleteCallback);
//                 // TODO: Figure this out
//                 Topic[] topics2 = { new Topic (TopicFactory.monitoring("+"), QoS.AT_LEAST_ONCE) };
//                 connection.subscribe(topics2, subscriptionCompleteCallback);
//             }
// 
//             @Override
//             public void onFailure(Throwable value) {
//                 LOGGER.info("Connection failure", value);
//             }
//         });
// 
//     }
// 
//     public void stop() {
//         connection.disconnect(new Callback<Void>() {
//             @Override
//             public void onSuccess(Void value) {
//                 LOGGER.info("Disconnect success");
//             }
// 
//             @Override
//             public void onFailure(Throwable value) {
//                 LOGGER.info("Disconnect failure");
//             }
//         });
//     }
// 
//     public void publish(String queue, byte[] msg, QoS qos, Callback<Void> callback) {
//         if (qos == null) {
//             qos = QoS.AT_LEAST_ONCE;
//         }
//         if (callback == null) {
//             callback = new Callback<Void>() {
//                 @Override
//                 public void onSuccess(Void value) {
// 
//                 }
// 
//                 @Override
//                 public void onFailure(Throwable value) {
//                     LOGGER.info("Error sending message");
//                 }
//             };
//         }
//         connection.publish(queue, msg, qos, false, callback);
//     }
// }


import com.netflix.config.ConfigurationManager;
import nl.crowndov.displaydirect.dashboard.mqtt.MonitoringHandler;
import nl.crowndov.displaydirect.dashboard.mqtt.MqttClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.io.IOException;
package nl.crowndov.displaydirect.dashboard;



/**
 * Copyright 2017 CROW-NDOV
 *
 * This file is subject to the terms and conditions defined in file 'LICENSE.txt', which is part of this source code package.
 */
@WebListener
public class BootListener implements ServletContextListener {

    private static final Logger LOGGER = LoggerFactory.getLogger(BootListener.class);
    private MqttClient mqtt;

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        try {
            ConfigurationManager.loadCascadedPropertiesFromResources("configuration");
        } catch (IOException e) {
            LOGGER.error("Failed to load properties", e);
        }

// Path: dashboard/src/main/java/nl/crowndov/displaydirect/dashboard/mqtt/MonitoringHandler.java
// public class MonitoringHandler implements Callback<DisplayDirectMessage.Monitoring> {
// 
//     private static final Logger LOGGER = LoggerFactory.getLogger(MonitoringHandler.class);
// 
//     @Override
//     public void onSuccess(DisplayDirectMessage.Monitoring value) {
//         if (value.getLogsList().size() > 0) {
//             value.getLogsList().forEach(m -> LogStore.add(DisplayDirectMessageParser.toLogMessage(m)));
//         } else if (value.getMetricsList().size() > 0) {
//             value.getMetricsList().forEach(m -> MetricStore.update(DisplayDirectMessageParser.toMetric(m)));
//         } else {
//             LOGGER.error("Got empty log message");
//         }
//     }
// 
//     @Override
//     public void onFailure(Throwable value) {
//         LOGGER.info("Failed with error", value);
//     }
// }
// 
// Path: dashboard/src/main/java/nl/crowndov/displaydirect/dashboard/mqtt/MqttClient.java
// public class MqttClient {
// 
//     private static final Logger LOGGER = LoggerFactory.getLogger(MqttClient.class);
// 
//     private MQTT mqtt;
//     private final CallbackConnection connection;
// 
//     private Callback<byte[]> subscriptionCompleteCallback = new Callback<byte[]>() {
//         @Override
//         public void onSuccess(byte[] value) {
//             LOGGER.info("Subscribed succesfully");
//         }
// 
//         @Override
//         public void onFailure(Throwable value) {
//             LOGGER.info("Error during subscribing", value);
//         }
//     };
// 
//     public MqttClient(Callback<DisplayDirectMessage.Monitoring> msg) {
//         mqtt = new MQTT();
//         try {
//             mqtt.setHost(Configuration.getHostname());
//         } catch (URISyntaxException e) {
//             LOGGER.error("Error setting host");
//         }
//         mqtt.setClientId(Configuration.getClientId());
//         mqtt.setCleanSession(false);
// 
//         mqtt.setKeepAlive((short) 60);
// //            mqtt.setReconnectBackOffMultiplier(3);
// //            mqtt.setConnectAttemptsMax(10000);
// //            mqtt.setReconnectDelay(20);
// //            mqtt.setReconnectDelayMax(10000);
//         mqtt.setVersion("3.1.1");
// 
//         connection = mqtt.callbackConnection();
//         LOGGER.info("Setting up connection");
//         connection.listener(new Listener() {
//             @Override
//             public void onConnected() {
//                 LOGGER.info("Connected");
//             }
// 
//             @Override
//             public void onDisconnected() {
//                 LOGGER.info("Disconnected");
//             }
// 
//             @Override
//             public void onPublish(UTF8Buffer topic, Buffer body, Runnable ack) {
//                 try {
//                     DisplayDirectMessage.Monitoring mon = DisplayDirectMessage.Monitoring.parseFrom(body.toByteArray());
//                     msg.onSuccess(mon);
//                 } catch (InvalidProtocolBufferException e) {
//                     LOGGER.error("Failed to parse message", e);
//                     msg.onFailure(e);
//                 }
//                 ack.run();
//             }
// 
//             @Override
//             public void onFailure(Throwable value) {
//                 LOGGER.info("MQTT Client failed", value);
//             }
//         });
//         connection.connect(new Callback<Void>() {
//             @Override
//             public void onSuccess(Void value) {
//                 LOGGER.info("Subscribing to topics");
//                 Topic[] topics = { new Topic (TopicFactory.monitoring(), QoS.AT_LEAST_ONCE) };
//                 connection.subscribe(topics, subscriptionCompleteCallback);
//                 // TODO: Figure this out
//                 Topic[] topics2 = { new Topic (TopicFactory.monitoring("+"), QoS.AT_LEAST_ONCE) };
//                 connection.subscribe(topics2, subscriptionCompleteCallback);
//             }
// 
//             @Override
//             public void onFailure(Throwable value) {
//                 LOGGER.info("Connection failure", value);
//             }
//         });
// 
//     }
// 
//     public void stop() {
//         connection.disconnect(new Callback<Void>() {
//             @Override
//             public void onSuccess(Void value) {
//                 LOGGER.info("Disconnect success");
//             }
// 
//             @Override
//             public void onFailure(Throwable value) {
//                 LOGGER.info("Disconnect failure");
//             }
//         });
//     }
// 
//     public void publish(String queue, byte[] msg, QoS qos, Callback<Void> callback) {
//         if (qos == null) {
//             qos = QoS.AT_LEAST_ONCE;
//         }
//         if (callback == null) {
//             callback = new Callback<Void>() {
//                 @Override
//                 public void onSuccess(Void value) {
// 
//                 }
// 
//                 @Override
//                 public void onFailure(Throwable value) {
//                     LOGGER.info("Error sending message");
//                 }
//             };
//         }
//         connection.publish(queue, msg, qos, false, callback);
//     }
// }


// Path: dashboard/src/main/java/nl/crowndov/displaydirect/dashboard/BootListener.java
import com.netflix.config.ConfigurationManager;
import nl.crowndov.displaydirect.dashboard.mqtt.MonitoringHandler;
import nl.crowndov.displaydirect.dashboard.mqtt.MqttClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.io.IOException;

package nl.crowndov.displaydirect.dashboard;



/**
 * Copyright 2017 CROW-NDOV
 *
 * This file is subject to the terms and conditions defined in file 'LICENSE.txt', which is part of this source code package.
 */
@WebListener
public class BootListener implements ServletContextListener {

    private static final Logger LOGGER = LoggerFactory.getLogger(BootListener.class);
    private MqttClient mqtt;

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        try {
            ConfigurationManager.loadCascadedPropertiesFromResources("configuration");
        } catch (IOException e) {
            LOGGER.error("Failed to load properties", e);
        }

        mqtt = new MqttClient(new MonitoringHandler());