120 lines
4.1 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-11-08 10:42:22 +01:00
import com.wasteinformationserver.db.JDCB;
2019-11-15 10:51:50 +01:00
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
2019-10-04 16:52:14 +02:00
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-11-15 10:51:50 +01:00
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
2019-10-11 10:48:04 +02:00
import java.util.GregorianCalendar;
2019-10-04 16:52:14 +02:00
public class mqtt {
2019-11-15 10:51:50 +01:00
MqttClient client = null;
2019-10-11 15:02:55 +02:00
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-11-15 10:51:50 +01:00
try {
client = new MqttClient("tcp://192.168.65.15:1883", "JavaSample");
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
client.connect(connOpts);
} catch (MqttException e) {
e.printStackTrace();
}
mqttreceiver mr = new mqttreceiver(client);
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(",");
2019-11-15 10:51:50 +01:00
getDatabasedata("SELECT pickupdates.pickupdate FROM pickupdates WHERE pickupdates.citywastezoneid=(SELECT cities.zone FROM cities WHERE cities.name='" + split[1] + "' AND cities.wastetype='" + split[2] + "' AND cities.zone=" + split[3] + ")", split[2], Integer.parseInt(split[0]));
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-11-15 10:51:50 +01:00
public void getDatabasedata(String message, String wastetyp, int clientidentify) {
2019-10-11 10:48:04 +02:00
2019-10-11 16:16:28 +02:00
Log.debug(message);
2019-11-08 10:42:22 +01: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 {
2019-11-15 10:51:50 +01:00
if (!result.isBeforeFirst()) {
int abholtag = 0;
transmitmessageAbfallart(clientidentify + "," + wastetyp + "," + abholtag);
} else {
while (result.next()) {
String temptime = String.valueOf(result.getString("pickupdate"));
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] + ".20" + tempyearnew;
String[] partstwo = date.split(" ");
String Datetomorrow=nexDayDate();
int abholtag;
if (partstwo[0].contains(newDate)||partstwo[0].contains(Datetomorrow)) {
abholtag = 1;
transmitmessageAbfallart(clientidentify + "," + wastetyp + "," + abholtag);
}else {
abholtag=0;
transmitmessageAbfallart(clientidentify + "," + wastetyp + "," + abholtag);
}
2019-10-11 15:02:55 +02:00
}
2019-10-11 09:03:14 +02:00
}
} catch (SQLException e) {
2019-11-08 14:13:32 +01:00
System.out.println("Exception");
2019-10-11 09:03:14 +02:00
e.printStackTrace();
2019-10-04 16:52:14 +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-11-15 10:51:50 +01:00
mqtttransmitter mt = new mqtttransmitter(client);
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-11-15 10:51:50 +01:00
private String nexDayDate() {
final DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
Date currentDate = new Date();
Calendar c = Calendar.getInstance();
c.setTime(currentDate);
c.add(Calendar.DATE, 1);
Date currentDatePlusOne = c.getTime();
String temp=dateFormat.format(currentDatePlusOne);
String split[]=temp.split("/");
String newDate=split[2]+"."+split[1]+"."+split[0];
return newDate;
}
2019-10-11 10:48:04 +02:00
}