This commit is contained in:
Gregor_Dutzler 2019-10-11 10:48:04 +02:00
parent fb2b191a73
commit 26fa5aacbb
4 changed files with 50 additions and 37 deletions

View File

@ -1,6 +1,7 @@
package com.wasteinformationserver;
import com.wasteinformationserver.mqtt.*;
public class main {
public static void main(String[] args) {
/*
@ -26,10 +27,8 @@ public class main {
Log.message("thread started");*/
mqtt m = new mqtt();
// m.notifymessage();
m.notifymessage();
m.getDatabasedata();
m.printlist();
// Log.message("mqtt irgentwos");
}
}

View File

@ -2,20 +2,19 @@ package com.wasteinformationserver.mqtt;
import com.wasteinformationserver.db.jdcb;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.GregorianCalendar;
public class mqtt {
private ArrayList<String> mylist = new ArrayList<>();
private int index = 0;
public mqtt() {
}
@ -33,37 +32,40 @@ public class mqtt {
}
public void getDatabasedata() {
int n=0;
String temptime = null;
String tempabfallart = null;
jdcb Database = new jdcb("placeuser", "eaL956R6yFItQVBl", "wasteinformation");
ResultSet result = Database.executeQuery("SELECT*FROM place WHERE Zone=1");
try {
while (result.next()) {
String temp = String.valueOf(result.getString("Abholtag"));
System.out.println(temp);
filllist(temp);
n++;
temptime = String.valueOf(result.getString("Abholtag"));
tempabfallart = String.valueOf(result.getString("Abfallart"));
}
} catch (SQLException e) {
e.printStackTrace();
}
transmitmessagetoESP();
}
private void transmitmessagetoESP() {
mqtttransmitter mt = new mqtttransmitter(mylist);
}
private void filllist(String temp) {
mylist.add(index, temp);
}
public void printlist() {
for (int n = 0; n < index; n++) {
System.out.println(mylist.get(index));
if (temptime != null && tempabfallart != null) {
transmitmessageAbfallart(tempabfallart);
//transmitmessageDate(temptime);
} else {
System.out.println("NO Connection");
}
}
private void transmitmessageAbfallart(String tempabfallart) {
mqtttransmitter mt = new mqtttransmitter();
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

@ -16,6 +16,8 @@ public class mqttreceiver {
String message;
ArrayList<ActionListener> mylisteners = new ArrayList<>();
ArrayList<String> mylist=new ArrayList<>();
int index=0;
public mqttreceiver() {
@ -33,6 +35,7 @@ public class mqttreceiver {
public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
System.out.println(new String(mqttMessage.getPayload()));
notifylisteners(new String(mqttMessage.getPayload()));
addmylist(new String(mqttMessage.getPayload()));
}
@Override
@ -40,7 +43,7 @@ public class mqttreceiver {
}
});
client.subscribe("test/topic");
client.subscribe("TopicIn");
} catch (MqttException e) {
e.printStackTrace();
}
@ -56,4 +59,9 @@ public class mqttreceiver {
public void addMessageReceivedListener(ActionListener l) {
mylisteners.add(l);
}
private void addmylist(String temp){
mylist.add(index,temp);
index++;
}
}

View File

@ -10,19 +10,22 @@ import java.util.ArrayList;
public class mqtttransmitter {
public mqtttransmitter(ArrayList<String> mylist) {
private String messagedatabase;
public mqtttransmitter() {
}
public void sendmessage(ArrayList mylist) {
String topic = "kk";
String content = "Message from MqttPublishSample";
public void sendmessage(String temp) {
String topic = "TopicOut";
String content = temp;
int qos = 2;
String broker = "tcp://192.168.65.15:1883";
String clientId = "JavaSample";
MemoryPersistence persistence = new MemoryPersistence();
try {
MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
@ -37,6 +40,7 @@ public class mqtttransmitter {
sampleClient.disconnect();
System.out.println("Disconnected");
System.exit(0);
} catch (MqttException me) {
System.out.println("reason " + me.getReasonCode());
System.out.println("msg " + me.getMessage());