
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;


/**
 *
 * @author David Rodriguez A.
 */
public class DataListener implements Runnable, MessageListener {
    private ActiveMQConnectionFactory connectionFactory;
    private ActiveMQConnection connection;
    private Session session;
    private MessageConsumer consumer;
    private String name;
    private ClientFrame clientFrame;
    private int Temp;
    private boolean status;
    private boolean running;
    private float Press;

    public  DataListener(ActiveMQConnectionFactory connectionFactory, String name, ClientFrame frame){
       this.connectionFactory=connectionFactory;
       this.name=name;
       this.clientFrame=frame;
       this.Temp=50;
       this.status=false;
       this.running=false;
    }

    public void run() {
        try {
            connection = (ActiveMQConnection) connectionFactory.createConnection();
            connection.start();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createQueue(name);
            consumer = session.createConsumer(destination);
            consumer.setMessageListener(this);

        } catch (JMSException ex) {
            Logger.getLogger(DataListener.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public int getTemp(){
        return this.Temp;
    }

    public String getName(){
        return this.name;
    }
    public void changeStatus(){
        this.status=!status;
    }

    public boolean isRunning(){
        return this.running;
    }
    public void setRunning(){
        this.running=true;
    }
    

    public void onMessage(Message msg) {
            MapMessage in_msg = (MapMessage) msg;
        try {
        if(status){
                Temp=in_msg.getInt("Temperature");
                Press=in_msg.getFloat("Pressure");
                //System.out.println(Temp);
                clientFrame.getProgressBar1().setValue(Temp);
                clientFrame.getProgressBar2().setValue((int) Press);
        }
        } catch (JMSException ex) {
            
        }
      
    }

}

