cleanup in mqtt methods
deleted unneccessary classes
This commit is contained in:
parent
f7383474ac
commit
4b96ad576d
@ -3,7 +3,7 @@ package com.wasteinformationserver;
|
||||
import com.wasteinformationserver.basicutils.Info;
|
||||
import com.wasteinformationserver.basicutils.Log;
|
||||
import com.wasteinformationserver.db.JDCB;
|
||||
import com.wasteinformationserver.mqtt.mqtt;
|
||||
import com.wasteinformationserver.mqtt.MqttService;
|
||||
import com.wasteinformationserver.website.Webserver;
|
||||
|
||||
import java.io.IOException;
|
||||
@ -35,8 +35,8 @@ public class main {
|
||||
Log.message("initial login to db");
|
||||
new Thread(() -> {
|
||||
try {
|
||||
//JDCB.init("users", "admin0", "wasteinformation", "192.168.65.15", 1883);
|
||||
JDCB.init("users", "kOpaIJUjkgb9ur6S", "wasteinformation", "192.168.65.15", 3306);
|
||||
JDCB.init("ingproject", "Kb9Dxklumt76ieq6", "ingproject", "db.power4future.at", 3306);
|
||||
//JDCB.init("users", "kOpaIJUjkgb9ur6S", "wasteinformation", "192.168.65.15", 3306);
|
||||
} catch (IOException e) {
|
||||
//e.printStackTrace();
|
||||
Log.error("no connection to db");
|
||||
@ -52,8 +52,8 @@ public class main {
|
||||
//startup mqtt service
|
||||
Log.message("starting mqtt service");
|
||||
try {
|
||||
mqtt m = new mqtt();
|
||||
m.notifymessage();
|
||||
MqttService m = new MqttService();
|
||||
m.startupService();
|
||||
} catch (Exception e) {
|
||||
Log.error("An error occured in the class mqtt");
|
||||
}
|
||||
|
@ -2,9 +2,7 @@ package com.wasteinformationserver.mqtt;
|
||||
|
||||
import com.wasteinformationserver.basicutils.Log;
|
||||
import com.wasteinformationserver.db.JDCB;
|
||||
import org.eclipse.paho.client.mqttv3.MqttClient;
|
||||
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
|
||||
import org.eclipse.paho.client.mqttv3.MqttException;
|
||||
import org.eclipse.paho.client.mqttv3.*;
|
||||
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
@ -17,35 +15,47 @@ import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
public class mqtt {
|
||||
public class MqttService {
|
||||
MqttClient client = null;
|
||||
|
||||
public mqtt() {
|
||||
public MqttService() {
|
||||
|
||||
}
|
||||
|
||||
public void notifymessage() {
|
||||
public void startupService() {
|
||||
|
||||
try {
|
||||
client = new MqttClient("tcp://192.168.65.15:1883", "JavaSample42");
|
||||
MqttConnectOptions connOpts = new MqttConnectOptions();
|
||||
connOpts.setCleanSession(true);
|
||||
client.connect(connOpts);
|
||||
} catch (MqttException e) {
|
||||
Log.error("Connection to the ESB was failed");
|
||||
|
||||
client.setCallback(new MqttCallback() {
|
||||
@Override
|
||||
public void connectionLost(Throwable throwable) {
|
||||
Log.error("connection lost");
|
||||
}
|
||||
|
||||
mqttreceiver mr = new mqttreceiver(client);
|
||||
mr.addMessageReceivedListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
|
||||
String message = new String(mqttMessage.getPayload());
|
||||
Log.info("received Request from PCB");
|
||||
|
||||
Log.debug("received message");
|
||||
String[] split = e.getActionCommand().split(",");
|
||||
String[] split = message.split(",");
|
||||
String wastetyp = getTyp(Integer.parseInt(split[2]));
|
||||
getDatabasedata("SELECT pickupdates.pickupdate FROM pickupdates WHERE pickupdates.citywastezoneid=(SELECT cities.zone FROM cities WHERE cities.name='" + split[1] + "' AND cities.wastetype='" + wastetyp + "' AND cities.zone=" + split[3] + ")", wastetyp, Integer.parseInt(split[0]));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
|
||||
|
||||
}
|
||||
});
|
||||
mr.getmessage();
|
||||
client.subscribe("TopicIn");
|
||||
} catch (MqttException e) {
|
||||
Log.error("Connection to the Broker failed");
|
||||
}
|
||||
}
|
||||
|
||||
public void getDatabasedata(String message, String wastetyp, int clientidentify) {
|
||||
@ -89,13 +99,17 @@ public class mqtt {
|
||||
|
||||
private void transmitmessageAbfallart(String temp) {
|
||||
Log.debug("sending message >>>"+temp);
|
||||
mqtttransmitter mt = new mqtttransmitter(client);
|
||||
Log.debug(temp);
|
||||
mt.sendmessage(temp);
|
||||
MqttMessage message = new MqttMessage(temp.getBytes());
|
||||
message.setQos(2);
|
||||
try {
|
||||
client.publish("TopicOut", message);
|
||||
} catch (MqttException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private String nexDayDate() {
|
||||
|
||||
// TODO: 10.01.20 doesnt work
|
||||
final DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
|
||||
Date currentDate = new Date();
|
||||
|
@ -1,62 +0,0 @@
|
||||
package com.wasteinformationserver.mqtt;
|
||||
|
||||
import com.wasteinformationserver.basicutils.Log;
|
||||
import org.eclipse.paho.client.mqttv3.*;
|
||||
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class mqttreceiver {
|
||||
|
||||
private MqttClient client;
|
||||
public ArrayList<ActionListener> mylisteners = new ArrayList<>();
|
||||
public String message;
|
||||
|
||||
public mqttreceiver(MqttClient mqtt) {
|
||||
this.client = mqtt;
|
||||
}
|
||||
|
||||
public String getmessage() {
|
||||
|
||||
try {
|
||||
client.setCallback(new MqttCallback() {
|
||||
@Override
|
||||
public void connectionLost(Throwable throwable) {
|
||||
Log.error("connection lost");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
|
||||
message = new String(mqttMessage.getPayload());
|
||||
Log.info("received Request from PCB");
|
||||
notifylisteners(message);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
|
||||
|
||||
}
|
||||
});
|
||||
client.subscribe("TopicIn");
|
||||
Log.debug("subscribed topic");
|
||||
} catch (MqttException e) {
|
||||
Log.error("Connection to the ESB failed");
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
|
||||
private void notifylisteners(String message) {
|
||||
for (ActionListener ac : mylisteners) {
|
||||
ac.actionPerformed(new ActionEvent(this, 0, message));
|
||||
}
|
||||
}
|
||||
|
||||
public void addMessageReceivedListener(ActionListener l) {
|
||||
mylisteners.add(l);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,40 +0,0 @@
|
||||
package com.wasteinformationserver.mqtt;
|
||||
|
||||
import com.wasteinformationserver.basicutils.Log;
|
||||
import org.eclipse.paho.client.mqttv3.MqttClient;
|
||||
import org.eclipse.paho.client.mqttv3.MqttException;
|
||||
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
||||
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
|
||||
|
||||
|
||||
public class mqtttransmitter {
|
||||
MqttClient client;
|
||||
|
||||
public mqtttransmitter(MqttClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
public void sendmessage(String temp) {
|
||||
String topic = "TopicOut";
|
||||
String content = temp;
|
||||
int qos = 2;
|
||||
MemoryPersistence persistence = new MemoryPersistence();
|
||||
|
||||
try {
|
||||
Log.debug("Connected");
|
||||
Log.debug("Publishing message: " + content);
|
||||
MqttMessage message = new MqttMessage(content.getBytes());
|
||||
message.setQos(qos);
|
||||
client.publish(topic, message);
|
||||
Log.debug("Message published");
|
||||
|
||||
|
||||
} catch (MqttException me) {
|
||||
Log.debug("reason " + me.getReasonCode());
|
||||
Log.debug("msg " + me.getMessage());
|
||||
Log.debug("loc " + me.getLocalizedMessage());
|
||||
Log.debug("cause " + me.getCause());
|
||||
Log.debug("excep " + me);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user