From 26fa5aacbbd08ce337d9d7efa5538737eecd4671 Mon Sep 17 00:00:00 2001 From: Gregor_Dutzler Date: Fri, 11 Oct 2019 10:48:04 +0200 Subject: [PATCH] hi --- src/com/wasteinformationserver/main.java | 5 +- src/com/wasteinformationserver/mqtt/mqtt.java | 58 ++++++++++--------- .../mqtt/mqttreceiver.java | 10 +++- .../mqtt/mqtttransmitter.java | 14 +++-- 4 files changed, 50 insertions(+), 37 deletions(-) diff --git a/src/com/wasteinformationserver/main.java b/src/com/wasteinformationserver/main.java index 2b4d4e4..c98a265 100644 --- a/src/com/wasteinformationserver/main.java +++ b/src/com/wasteinformationserver/main.java @@ -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"); } } diff --git a/src/com/wasteinformationserver/mqtt/mqtt.java b/src/com/wasteinformationserver/mqtt/mqtt.java index 1fee4ab..3104141 100644 --- a/src/com/wasteinformationserver/mqtt/mqtt.java +++ b/src/com/wasteinformationserver/mqtt/mqtt.java @@ -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 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())); + + } +} \ No newline at end of file diff --git a/src/com/wasteinformationserver/mqtt/mqttreceiver.java b/src/com/wasteinformationserver/mqtt/mqttreceiver.java index d515721..309ef4f 100644 --- a/src/com/wasteinformationserver/mqtt/mqttreceiver.java +++ b/src/com/wasteinformationserver/mqtt/mqttreceiver.java @@ -16,6 +16,8 @@ public class mqttreceiver { String message; ArrayList mylisteners = new ArrayList<>(); + ArrayList 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++; + } } diff --git a/src/com/wasteinformationserver/mqtt/mqtttransmitter.java b/src/com/wasteinformationserver/mqtt/mqtttransmitter.java index 1ef5a48..e158bf9 100644 --- a/src/com/wasteinformationserver/mqtt/mqtttransmitter.java +++ b/src/com/wasteinformationserver/mqtt/mqtttransmitter.java @@ -10,19 +10,22 @@ import java.util.ArrayList; public class mqtttransmitter { - public mqtttransmitter(ArrayList 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()); @@ -47,4 +51,4 @@ public class mqtttransmitter { } } -} +} \ No newline at end of file