This commit is contained in:
Gregor_Dutzler 2019-11-15 10:51:50 +01:00
parent 954eb8d61c
commit cfddff4d8d
5 changed files with 74 additions and 57 deletions

View File

@ -1,5 +1,5 @@
<component name="ArtifactManager"> <component name="ArtifactManager">
<artifact type="jar" build-on-make="true" name="WasteinformationServer"> <artifact type="jar" name="WasteinformationServer">
<output-path>$PROJECT_DIR$/out/artifacts/WasteinformationServer</output-path> <output-path>$PROJECT_DIR$/out/artifacts/WasteinformationServer</output-path>
<root id="archive" name="WasteinformationServer.jar"> <root id="archive" name="WasteinformationServer.jar">
<element id="module-output" name="WasteInformationServer" /> <element id="module-output" name="WasteInformationServer" />

View File

@ -21,7 +21,7 @@ public class main {
})); }));
Thread mythread = new Thread(() -> new Webserver().startserver()); Thread mythread = new Thread(() -> new Webserver().startserver());
mythread.start(); //mythread.start();
Log.message("thread started"); Log.message("thread started");

View File

@ -2,16 +2,22 @@ package com.wasteinformationserver.mqtt;
import com.wasteinformationserver.basicutils.Log; import com.wasteinformationserver.basicutils.Log;
import com.wasteinformationserver.db.JDCB; 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 java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
public class mqtt { public class mqtt {
MqttClient client = null;
public mqtt() { public mqtt() {
@ -19,60 +25,63 @@ public class mqtt {
public void notifymessage() { public void notifymessage() {
mqttreceiver mr = new mqttreceiver(); try {
client = new MqttClient("tcp://192.168.65.15:1883", "JavaSample");
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
client.connect(connOpts);
} catch (MqttException e) {
e.printStackTrace();
}
mqttreceiver mr = new mqttreceiver(client);
mr.addMessageReceivedListener(new ActionListener() { mr.addMessageReceivedListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
String temp = e.getActionCommand(); String temp = e.getActionCommand();
System.out.println(temp);
String[] split = temp.split(","); String[] split = temp.split(",");
getDatabasedata("SELECT pickupdates.pickupdate FROM pickupdates WHERE pickupdates.citywastezoneid=(SELECT cities.zone FROM cities WHERE cities.name='" + split[1] + "' AND cities.wastetype='" + split[2] + "' AND cities.zone=" + split[3] + ")",split[2], Integer.parseInt(split[0])); getDatabasedata("SELECT pickupdates.pickupdate FROM pickupdates WHERE pickupdates.citywastezoneid=(SELECT cities.zone FROM cities WHERE cities.name='" + split[1] + "' AND cities.wastetype='" + split[2] + "' AND cities.zone=" + split[3] + ")", split[2], Integer.parseInt(split[0]));
} }
}); });
mr.getmessage(); mr.getmessage();
} }
public void getDatabasedata(String message,String wastetyp, int clientidentify) { public void getDatabasedata(String message, String wastetyp, int clientidentify) {
String temp;
Log.debug(message); Log.debug(message);
JDCB Database = new JDCB("placeuser", "eaL956R6yFItQVBl", "wasteinformation"); JDCB Database = new JDCB("placeuser", "eaL956R6yFItQVBl", "wasteinformation");
ResultSet result = Database.executeQuery(message); ResultSet result = Database.executeQuery(message);
try { try {
while (result.next()) { if (!result.isBeforeFirst()) {
String temptime = String.valueOf(result.getString("pickupdate")); int abholtag = 0;
transmitmessageAbfallart(clientidentify + "," + wastetyp + "," + abholtag);
} else {
while (result.next()) {
String temptime = String.valueOf(result.getString("pickupdate"));
GregorianCalendar now = new GregorianCalendar(); GregorianCalendar now = new GregorianCalendar();
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG); DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG);
String date = df.format(now.getTime()); String date = df.format(now.getTime());
String[] parts = temptime.split("-");
String[] parts = temptime.split("-"); String tempyear = parts[0];
String tempyear = parts[0]; String[] yearsplit = tempyear.split("0");
String tempyearnew = yearsplit[1];
String[] yearsplit = tempyear.split("0"); String newDate = parts[2] + "." + parts[1] + ".20" + tempyearnew;
String tempyearnew = yearsplit[1]; String[] partstwo = date.split(" ");
String Datetomorrow=nexDayDate();
String newDate = parts[2] + "." + parts[1] + "." + tempyearnew;
String[] partstwo = date.split(" ");
int abholtag; int abholtag;
if (partstwo[0].contains(newDate)) { if (partstwo[0].contains(newDate)||partstwo[0].contains(Datetomorrow)) {
abholtag = 1; abholtag = 1;
} else { transmitmessageAbfallart(clientidentify + "," + wastetyp + "," + abholtag);
abholtag = 0; }else {
} abholtag=0;
temp = clientidentify+","+ wastetyp + "," + abholtag; transmitmessageAbfallart(clientidentify + "," + wastetyp + "," + abholtag);
System.out.println(temp); }
if (temp != null) {
transmitmessageAbfallart(temp);
} else {
Log.debug("NO Connection");
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
@ -83,11 +92,29 @@ public class mqtt {
} }
private void transmitmessageAbfallart(String temp) { private void transmitmessageAbfallart(String temp) {
mqtttransmitter mt = new mqtttransmitter(); mqtttransmitter mt = new mqtttransmitter(client);
Log.debug(temp); Log.debug(temp);
mt.sendmessage(temp); mt.sendmessage(temp);
} }
private String nexDayDate() {
final DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
Date currentDate = new Date();
Calendar c = Calendar.getInstance();
c.setTime(currentDate);
c.add(Calendar.DATE, 1);
Date currentDatePlusOne = c.getTime();
String temp=dateFormat.format(currentDatePlusOne);
String split[]=temp.split("/");
String newDate=split[2]+"."+split[1]+"."+split[0];
return newDate;
}
} }

View File

@ -17,20 +17,17 @@ public class mqttreceiver {
public ArrayList<ActionListener> mylisteners = new ArrayList<>(); public ArrayList<ActionListener> mylisteners = new ArrayList<>();
public String message; public String message;
public mqttreceiver() { public mqttreceiver(MqttClient mqtt) {
this.client = mqtt;
} }
public String getmessage() { public String getmessage() {
try { try {
client = new MqttClient("tcp://192.168.65.15:1883", "JavaSample"); client.setCallback(new MqttCallback() {
client.connect();
client.setCallback(new MqttCallback() {
@Override @Override
public void connectionLost(Throwable throwable) { public void connectionLost(Throwable throwable) {
Log.error("connection lost");
} }
@Override @Override

View File

@ -9,32 +9,25 @@ import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
public class mqtttransmitter { public class mqtttransmitter {
MqttClient client;
public mqtttransmitter() { public mqtttransmitter(MqttClient client) {
this.client=client;
} }
public void sendmessage(String temp) { public void sendmessage(String temp) {
String topic = "TopicOut"; String topic = "TopicOut";
String content = temp; String content = temp;
int qos = 2; int qos = 2;
String broker = "tcp://192.168.65.15:1883";
String clientId = "JavaSample";
MemoryPersistence persistence = new MemoryPersistence(); MemoryPersistence persistence = new MemoryPersistence();
try { try {
MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
Log.debug("Connecting to broker: " + broker);
sampleClient.connect(connOpts);
Log.debug("Connected"); Log.debug("Connected");
Log.debug("Publishing message: " + content); Log.debug("Publishing message: " + content);
MqttMessage message = new MqttMessage(content.getBytes()); MqttMessage message = new MqttMessage(content.getBytes());
message.setQos(qos); message.setQos(qos);
sampleClient.publish(topic, message); client.publish(topic, message);
Log.debug("Message published"); Log.debug("Message published");
sampleClient.disconnect();
Log.debug("Disconnected");
} catch (MqttException me) { } catch (MqttException me) {