aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/re/jag/mcqtt/MQTTHandler.java
blob: 78033c11e41196d528b6683f733f8cc88d541d27 (plain)
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
package re.jag.mcqtt;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;

public class MQTTHandler {
    private String server;
    private String user;
    private String password;

    private MqttClient client;
    private Thread client_thread;
    private boolean should_stop = false;

    MQTTHandler(String _server, String _user, String _password){
        try {
            client = new MqttClient(_server, _user);
        } catch (MqttException e) {
            Mcqtt.LOG.error(e.getMessage());
            return;
        }

        client_thread = new Thread(this::run);
        client_thread.run();
    }

    private void run() {
        while ( ! should_stop ) {
            // TODO wait for publish interval
            if ( ! client.isConnected() ) {
                try { client.connect(); } catch (MqttException e) {
                    Mcqtt.LOG.error( e.getMessage() );
                    continue; // TODO wait for reconnect
                }
            }
        }
    }
}