117 lines
3.2 KiB
Java
Raw Normal View History

2019-10-04 16:52:14 +02:00
package com.wasteinformationserver.mqtt;
2019-10-11 16:16:28 +02:00
import com.wasteinformationserver.basicutils.Log;
2019-10-04 16:52:14 +02:00
import com.wasteinformationserver.db.jdcb;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
2019-10-11 10:48:04 +02:00
import java.text.DateFormat;
2019-10-11 15:02:55 +02:00
import java.util.ArrayList;
2019-10-11 10:48:04 +02:00
import java.util.GregorianCalendar;
2019-10-04 16:52:14 +02:00
public class mqtt {
2019-10-11 15:02:55 +02:00
ArrayList<String> mylist = new ArrayList<>();
int index = 0;
2019-10-11 09:03:14 +02:00
public mqtt() {
2019-10-04 16:52:14 +02:00
}
2019-10-11 09:03:14 +02:00
public void notifymessage() {
2019-10-04 16:52:14 +02:00
2019-10-11 15:02:55 +02:00
GregorianCalendar now = new GregorianCalendar();
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG);
String date = df.format(now.getTime());
2019-10-11 16:16:28 +02:00
Log.debug(date);
2019-10-11 15:02:55 +02:00
String[] parts = date.split(" ");
String temp2=parts[0];
2019-10-11 16:16:28 +02:00
Log.debug(temp2);
2019-10-11 15:02:55 +02:00
String[] partstwo=temp2.split("\\.");
String newDate="20"+partstwo[2]+"-"+partstwo[1]+"-"+partstwo[0];
2019-10-11 09:03:14 +02:00
mqttreceiver mr = new mqttreceiver();
2019-10-04 16:52:14 +02:00
2019-10-11 16:16:28 +02:00
// Log.debug(message);
2019-10-11 15:02:55 +02:00
2019-10-04 16:52:14 +02:00
mr.addMessageReceivedListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
2019-10-11 15:02:55 +02:00
String temp = e.getActionCommand();
String[] split = temp.split(",");
String message = "SELECT*FROM place WHERE Ort='" + split[0] + "' AND Abfallart='" + split[1] + "' AND Zone='" + split[2] + "' AND ABholtag='"+newDate+"'";
getDatabasedata(message);
2019-10-04 16:52:14 +02:00
}
});
2019-10-11 15:02:55 +02:00
mr.getmessage();
2019-10-04 16:52:14 +02:00
}
2019-10-11 15:02:55 +02:00
public void getDatabasedata(String message) {
String temp;
2019-10-11 10:48:04 +02:00
2019-10-11 09:03:14 +02:00
2019-10-11 16:16:28 +02:00
Log.debug(message);
2019-10-11 09:03:14 +02:00
jdcb Database = new jdcb("placeuser", "eaL956R6yFItQVBl", "wasteinformation");
2019-10-11 15:02:55 +02:00
ResultSet result = Database.executeQuery(message);
2019-10-11 09:03:14 +02:00
try {
while (result.next()) {
2019-10-11 15:02:55 +02:00
String temptime = String.valueOf(result.getString("Abholtag"));
String tempabfallart = String.valueOf(result.getString("Abfallart"));
GregorianCalendar now = new GregorianCalendar();
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG);
String date = df.format(now.getTime());
String[] parts = temptime.split("-");
String tempyear = parts[0];
String[] yearsplit = tempyear.split("0");
String tempyearnew = yearsplit[1];
String newDate = parts[2] + "." + parts[1] + "." + tempyearnew;
String[] partstwo = date.split(" ");
boolean abholtag;
if (partstwo[0].contains(newDate)) {
abholtag = true;
} else {
abholtag = false;
}
temp = tempabfallart + "," + abholtag;
if (temp != null) {
transmitmessageAbfallart(temp);
} else {
2019-10-11 16:16:28 +02:00
Log.debug("NO Connection");
2019-10-11 15:02:55 +02:00
}
2019-10-11 09:03:14 +02:00
}
} catch (SQLException e) {
e.printStackTrace();
2019-10-04 16:52:14 +02:00
}
2019-10-11 15:02:55 +02:00
2019-10-11 09:03:14 +02:00
}
2019-10-04 16:52:14 +02:00
2019-10-11 16:14:05 +02:00
private void transmitmessageAbfallart(String temp) {
2019-10-04 16:52:14 +02:00
2019-10-11 10:48:04 +02:00
mqtttransmitter mt = new mqtttransmitter();
2019-10-11 16:16:28 +02:00
Log.debug(temp);
2019-10-11 16:14:05 +02:00
mt.sendmessage(temp);
2019-10-04 16:52:14 +02:00
}
2019-10-11 10:48:04 +02:00
}