This commit is contained in:
Gregor_Dutzler 2019-10-11 09:03:14 +02:00
parent 032d3a9b9c
commit fb2b191a73
7 changed files with 88 additions and 79 deletions

View File

@ -17,43 +17,43 @@ public class Log {
private static final String ANSI_CYAN = "\u001B[36m"; private static final String ANSI_CYAN = "\u001B[36m";
private static final String ANSI_WHITE = "\u001B[37m"; private static final String ANSI_WHITE = "\u001B[37m";
public static final int CRITICAL_ERROR=6; public static final int CRITICAL_ERROR = 6;
public static final int ERROR=5; public static final int ERROR = 5;
public static final int WARNING=4; public static final int WARNING = 4;
public static final int INFO=3; public static final int INFO = 3;
public static final int MESSAGE=2; public static final int MESSAGE = 2;
public static final int DEBUG=1; public static final int DEBUG = 1;
private static ArrayList<String> colors= new ArrayList<String>(Arrays.asList("","DEBUG","MESSAGE","INFO","WARNING","ERROR","CRITICAL_ERROR")); private static ArrayList<String> colors = new ArrayList<String>(Arrays.asList("", "DEBUG", "MESSAGE", "INFO", "WARNING", "ERROR", "CRITICAL_ERROR"));
public static void criticalerror(String msg){ public static void criticalerror(String msg) {
log(msg,CRITICAL_ERROR); log(msg, CRITICAL_ERROR);
} }
public static void error(String msg){ public static void error(String msg) {
log(msg,ERROR); log(msg, ERROR);
} }
public static void warning(String msg){ public static void warning(String msg) {
log(msg,WARNING); log(msg, WARNING);
} }
public static void info(String msg){ public static void info(String msg) {
log(msg,INFO); log(msg, INFO);
} }
public static void message(String msg){ public static void message(String msg) {
log(msg,MESSAGE); log(msg, MESSAGE);
} }
public static void debug(String msg){ public static void debug(String msg) {
log(msg,DEBUG); log(msg, DEBUG);
} }
public static void log(String msg, int level){ public static void log(String msg, int level) {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
switch (level){ switch (level) {
case INFO: case INFO:
builder.append(ANSI_CYAN); builder.append(ANSI_CYAN);
break; break;

View File

@ -52,7 +52,7 @@ Database {
System.out.println(row); System.out.println(row);
} }
} catch(SQLException e) { } catch (SQLException e) {
} }
} }
@ -68,7 +68,7 @@ Database {
Vector<Vector<Object>> data = new Vector<>(); Vector<Vector<Object>> data = new Vector<>();
while(res.next()) { while (res.next()) {
Vector<Object> row = new Vector(); Vector<Object> row = new Vector();
for (int i = 1; i <= columnCount; ++i) { for (int i = 1; i <= columnCount; ++i) {
@ -79,7 +79,7 @@ Database {
} }
return new DefaultTableModel(data, columnNames); return new DefaultTableModel(data, columnNames);
} catch(SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -10,9 +10,9 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
public class jdcb { public class jdcb {
Connection conn; Connection conn;
public jdcb(String username, String password,String dbname) { public jdcb(String username, String password, String dbname) {
Database db = new MySQLConnector( Database db = new MySQLConnector(
username, username,
password, password,
@ -29,7 +29,6 @@ Connection conn;
} }
public ResultSet executeQuery(String sql) { public ResultSet executeQuery(String sql) {
try { try {
PreparedStatement stmt = conn.prepareStatement(sql); PreparedStatement stmt = conn.prepareStatement(sql);
return stmt.executeQuery(); return stmt.executeQuery();
@ -39,7 +38,7 @@ Connection conn;
return null; return null;
} }
public int executeUpdate(String sql){ public int executeUpdate(String sql) {
try { try {
PreparedStatement stmt = conn.prepareStatement(sql); PreparedStatement stmt = conn.prepareStatement(sql);

View File

@ -1,11 +1,6 @@
package com.wasteinformationserver; package com.wasteinformationserver;
import com.wasteinformationserver.mqtt.*; import com.wasteinformationserver.mqtt.*;
import org.eclipse.paho.client.mqttv3.MqttException;
import java.net.URISyntaxException;
import java.sql.SQLException;
public class main { public class main {
public static void main(String[] args) { public static void main(String[] args) {
/* /*
@ -30,13 +25,11 @@ public class main {
Log.message("thread started");*/ Log.message("thread started");*/
mqtt m=new mqtt(); mqtt m = new mqtt();
// m.notifymessage(); // m.notifymessage();
try { m.getDatabasedata();
m.getDatabasedata(); m.printlist();
} catch (SQLException e) { // Log.message("mqtt irgentwos");
e.printStackTrace();
}
} }
} }

View File

@ -2,24 +2,27 @@ package com.wasteinformationserver.mqtt;
import com.wasteinformationserver.db.jdcb; import com.wasteinformationserver.db.jdcb;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
public class mqtt { public class mqtt {
private ArrayList<String> mylist=new ArrayList<>(); private ArrayList<String> mylist = new ArrayList<>();
private int index=0; private int index = 0;
public mqtt(){ public mqtt() {
} }
public void notifymessage(){ public void notifymessage() {
mqttreceiver mr=new mqttreceiver(); mqttreceiver mr = new mqttreceiver();
mr.addMessageReceivedListener(new ActionListener() { mr.addMessageReceivedListener(new ActionListener() {
@Override @Override
@ -29,30 +32,38 @@ public class mqtt {
}); });
} }
public void getDatabasedata() throws SQLException { public void getDatabasedata() {
jdcb database=new jdcb("placeuser","eaL956R6yFItQVBl","wasteinformation"); int n=0;
ResultSet result = database.executeQuery("SELECT*FROM place");
while(result.next()) jdcb Database = new jdcb("placeuser", "eaL956R6yFItQVBl", "wasteinformation");
{ ResultSet result = Database.executeQuery("SELECT*FROM place WHERE Zone=1");
String temp= String.valueOf(result.getStatement()); try {
filllist(temp); while (result.next()) {
String temp = String.valueOf(result.getString("Abholtag"));
System.out.println(temp);
filllist(temp);
n++;
}
} catch (SQLException e) {
e.printStackTrace();
} }
transmitmessagetoESP();
//transmitmessagetoESP();
} }
private void transmitmessagetoESP()
{
mqtttransmitter mt=new mqtttransmitter(); private void transmitmessagetoESP() {
mqtttransmitter mt = new mqtttransmitter(mylist);
} }
private void filllist(String temp) private void filllist(String temp) {
{ mylist.add(index, temp);
mylist.add(index,temp); }
public void printlist() {
for (int n = 0; n < index; n++) {
System.out.println(mylist.get(index));
}
} }
} }

View File

@ -47,13 +47,13 @@ public class mqttreceiver {
} }
private void notifylisteners(String message){ private void notifylisteners(String message) {
for (ActionListener ac:mylisteners) { for (ActionListener ac : mylisteners) {
new ActionEvent(this,0,message); new ActionEvent(this, 0, message);
} }
} }
public void addMessageReceivedListener(ActionListener l){ public void addMessageReceivedListener(ActionListener l) {
mylisteners.add(l); mylisteners.add(l);
} }
} }

View File

@ -6,25 +6,30 @@ import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage; import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import java.util.ArrayList;
public class mqtttransmitter { public class mqtttransmitter {
public mqtttransmitter() { public mqtttransmitter(ArrayList<String> mylist) {
String topic = "kk"; }
String content = "Message from MqttPublishSample";
int qos = 2; public void sendmessage(ArrayList mylist) {
String broker = "tcp://192.168.65.15:1883"; String topic = "kk";
String clientId = "JavaSample"; String content = "Message from MqttPublishSample";
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); MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
MqttConnectOptions connOpts = new MqttConnectOptions(); MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true); connOpts.setCleanSession(true);
System.out.println("Connecting to broker: "+broker); System.out.println("Connecting to broker: " + broker);
sampleClient.connect(connOpts); sampleClient.connect(connOpts);
System.out.println("Connected"); System.out.println("Connected");
System.out.println("Publishing message: "+content); System.out.println("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); sampleClient.publish(topic, message);
@ -32,13 +37,14 @@ public class mqtttransmitter {
sampleClient.disconnect(); sampleClient.disconnect();
System.out.println("Disconnected"); System.out.println("Disconnected");
System.exit(0); System.exit(0);
} catch(MqttException me) { } catch (MqttException me) {
System.out.println("reason "+me.getReasonCode()); System.out.println("reason " + me.getReasonCode());
System.out.println("msg "+me.getMessage()); System.out.println("msg " + me.getMessage());
System.out.println("loc "+me.getLocalizedMessage()); System.out.println("loc " + me.getLocalizedMessage());
System.out.println("cause "+me.getCause()); System.out.println("cause " + me.getCause());
System.out.println("excep "+me); System.out.println("excep " + me);
me.printStackTrace(); me.printStackTrace();
} }
} }
} }