This commit is contained in:
Gregor_Dutzler 2019-10-11 15:02:55 +02:00
parent 26fa5aacbb
commit bd89db2c29
4 changed files with 85 additions and 40 deletions

View File

@ -28,7 +28,6 @@ public class main {
mqtt m = new mqtt(); mqtt m = new mqtt();
m.notifymessage(); m.notifymessage();
m.getDatabasedata();
} }
} }

View File

@ -7,52 +7,103 @@ 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.ParseException; import java.util.ArrayList;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
public class mqtt { public class mqtt {
ArrayList<String> mylist = new ArrayList<>();
int index = 0;
public mqtt() { public mqtt() {
} }
public void notifymessage() { public void notifymessage() {
GregorianCalendar now = new GregorianCalendar();
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG);
String date = df.format(now.getTime());
System.out.println(date);
String[] parts = date.split(" ");
String temp2=parts[0];
System.out.println(temp2);
String[] partstwo=temp2.split("\\.");
String newDate="20"+partstwo[2]+"-"+partstwo[1]+"-"+partstwo[0];
mqttreceiver mr = new mqttreceiver(); mqttreceiver mr = new mqttreceiver();
// System.out.println(message);
mr.addMessageReceivedListener(new ActionListener() { mr.addMessageReceivedListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand()); 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);
} }
}); });
mr.getmessage();
} }
public void getDatabasedata() { public void getDatabasedata(String message) {
String temptime = null; String temp;
String tempabfallart = null;
System.out.println(message);
jdcb Database = new jdcb("placeuser", "eaL956R6yFItQVBl", "wasteinformation"); jdcb Database = new jdcb("placeuser", "eaL956R6yFItQVBl", "wasteinformation");
ResultSet result = Database.executeQuery("SELECT*FROM place WHERE Zone=1"); ResultSet result = Database.executeQuery(message);
try { try {
while (result.next()) { while (result.next()) {
temptime = String.valueOf(result.getString("Abholtag")); String temptime = String.valueOf(result.getString("Abholtag"));
tempabfallart = String.valueOf(result.getString("Abfallart")); 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 {
System.out.println("NO Connection");
}
} }
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
if (temptime != null && tempabfallart != null) {
transmitmessageAbfallart(tempabfallart);
//transmitmessageDate(temptime);
} else {
System.out.println("NO Connection");
}
} }
@ -61,11 +112,4 @@ public class mqtt {
mqtttransmitter mt = new mqtttransmitter(); mqtttransmitter mt = new mqtttransmitter();
mt.sendmessage(tempabfallart); mt.sendmessage(tempabfallart);
} }
private void transmitmessageDate(String temptime) {
GregorianCalendar now = new GregorianCalendar();
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG); // 14.04.12 21:34:07 MESZ
System.out.println(df.format(now.getTime()));
}
} }

View File

@ -12,15 +12,18 @@ import java.util.ArrayList;
public class mqttreceiver { public class mqttreceiver {
MqttClient client; private MqttClient client;
String message; public ArrayList<ActionListener> mylisteners = new ArrayList<>();
public String message;
ArrayList<ActionListener> mylisteners = new ArrayList<>();
ArrayList<String> mylist=new ArrayList<>();
int index=0;
public mqttreceiver() { public mqttreceiver() {
}
public String getmessage() {
String temp;
try { try {
client = new MqttClient("tcp://192.168.65.15:1883", "JavaSample"); client = new MqttClient("tcp://192.168.65.15:1883", "JavaSample");
client.connect(); client.connect();
@ -33,9 +36,9 @@ public class mqttreceiver {
@Override @Override
public void messageArrived(String s, MqttMessage mqttMessage) throws Exception { public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
System.out.println(new String(mqttMessage.getPayload())); message =new String(mqttMessage.getPayload());
notifylisteners(new String(mqttMessage.getPayload())); notifylisteners(message);
addmylist(new String(mqttMessage.getPayload()));
} }
@Override @Override
@ -44,15 +47,17 @@ public class mqttreceiver {
} }
}); });
client.subscribe("TopicIn"); client.subscribe("TopicIn");
System.out.println("subscribed topic");
} catch (MqttException e) { } catch (MqttException e) {
e.printStackTrace(); e.printStackTrace();
} }
return message;
} }
private void notifylisteners(String message) { private void notifylisteners(String message) {
for (ActionListener ac : mylisteners) { for (ActionListener ac : mylisteners) {
new ActionEvent(this, 0, message); ac.actionPerformed(new ActionEvent(this, 0, message));
} }
} }
@ -60,8 +65,5 @@ public class mqttreceiver {
mylisteners.add(l); mylisteners.add(l);
} }
private void addmylist(String temp){
mylist.add(index,temp);
index++;
}
} }