cleaned project structure
This commit is contained in:
4
src/java/com/wasteinformationserver/mqtt/Database.java
Normal file
4
src/java/com/wasteinformationserver/mqtt/Database.java
Normal file
@ -0,0 +1,4 @@
|
||||
package com.wasteinformationserver.mqtt;
|
||||
|
||||
public class Database {
|
||||
}
|
127
src/java/com/wasteinformationserver/mqtt/mqtt.java
Normal file
127
src/java/com/wasteinformationserver/mqtt/mqtt.java
Normal file
@ -0,0 +1,127 @@
|
||||
package com.wasteinformationserver.mqtt;
|
||||
|
||||
import com.wasteinformationserver.basicutils.Log;
|
||||
import com.wasteinformationserver.db.JDCB;
|
||||
import org.eclipse.paho.client.mqttv3.MqttClient;
|
||||
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
|
||||
import org.eclipse.paho.client.mqttv3.MqttException;
|
||||
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.io.IOException;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
public class mqtt {
|
||||
MqttClient client = null;
|
||||
|
||||
public mqtt() {
|
||||
|
||||
}
|
||||
|
||||
public void notifymessage() {
|
||||
|
||||
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);
|
||||
mr.addMessageReceivedListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
String temp = e.getActionCommand();
|
||||
|
||||
String[] split = temp.split(",");
|
||||
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]));
|
||||
}
|
||||
});
|
||||
mr.getmessage();
|
||||
}
|
||||
|
||||
public void getDatabasedata(String message, String wastetyp, int clientidentify) {
|
||||
|
||||
Log.debug(message);
|
||||
JDCB Database = null;
|
||||
try {
|
||||
Database = JDCB.getInstance();
|
||||
} catch (IOException e) {
|
||||
//e.printStackTrace();
|
||||
}
|
||||
//new JDCB("placeuser", "eaL956R6yFItQVBl", "wasteinformation");
|
||||
ResultSet result = Database.executeQuery(message);
|
||||
try {
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
System.out.println("Exception");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void transmitmessageAbfallart(String temp) {
|
||||
|
||||
mqtttransmitter mt = new mqtttransmitter(client);
|
||||
Log.debug(temp);
|
||||
mt.sendmessage(temp);
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
65
src/java/com/wasteinformationserver/mqtt/mqttreceiver.java
Normal file
65
src/java/com/wasteinformationserver/mqtt/mqttreceiver.java
Normal file
@ -0,0 +1,65 @@
|
||||
package com.wasteinformationserver.mqtt;
|
||||
|
||||
import com.wasteinformationserver.basicutils.Log;
|
||||
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
|
||||
import org.eclipse.paho.client.mqttv3.MqttCallback;
|
||||
import org.eclipse.paho.client.mqttv3.MqttClient;
|
||||
import org.eclipse.paho.client.mqttv3.MqttException;
|
||||
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
||||
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class mqttreceiver {
|
||||
|
||||
private MqttClient client;
|
||||
public ArrayList<ActionListener> mylisteners = new ArrayList<>();
|
||||
public String message;
|
||||
|
||||
public mqttreceiver(MqttClient mqtt) {
|
||||
this.client = mqtt;
|
||||
}
|
||||
|
||||
public String getmessage() {
|
||||
|
||||
try {
|
||||
client.setCallback(new MqttCallback() {
|
||||
@Override
|
||||
public void connectionLost(Throwable throwable) {
|
||||
Log.error("connection lost");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
|
||||
message =new String(mqttMessage.getPayload());
|
||||
notifylisteners(message);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
|
||||
|
||||
}
|
||||
});
|
||||
client.subscribe("TopicIn");
|
||||
Log.debug("subscribed topic");
|
||||
} catch (MqttException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
|
||||
private void notifylisteners(String message) {
|
||||
for (ActionListener ac : mylisteners) {
|
||||
ac.actionPerformed(new ActionEvent(this, 0, message));
|
||||
}
|
||||
}
|
||||
|
||||
public void addMessageReceivedListener(ActionListener l) {
|
||||
mylisteners.add(l);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,42 @@
|
||||
package com.wasteinformationserver.mqtt;
|
||||
|
||||
import com.wasteinformationserver.basicutils.Log;
|
||||
import org.eclipse.paho.client.mqttv3.MqttClient;
|
||||
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
|
||||
import org.eclipse.paho.client.mqttv3.MqttException;
|
||||
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
||||
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
|
||||
|
||||
|
||||
public class mqtttransmitter {
|
||||
MqttClient client;
|
||||
|
||||
public mqtttransmitter(MqttClient client) {
|
||||
this.client=client;
|
||||
}
|
||||
|
||||
public void sendmessage(String temp) {
|
||||
String topic = "TopicOut";
|
||||
String content = temp;
|
||||
int qos = 2;
|
||||
MemoryPersistence persistence = new MemoryPersistence();
|
||||
|
||||
try {
|
||||
Log.debug("Connected");
|
||||
Log.debug("Publishing message: " + content);
|
||||
MqttMessage message = new MqttMessage(content.getBytes());
|
||||
message.setQos(qos);
|
||||
client.publish(topic, message);
|
||||
Log.debug("Message published");
|
||||
|
||||
|
||||
} catch (MqttException me) {
|
||||
Log.debug("reason " + me.getReasonCode());
|
||||
Log.debug("msg " + me.getMessage());
|
||||
Log.debug("loc " + me.getLocalizedMessage());
|
||||
Log.debug("cause " + me.getCause());
|
||||
Log.debug("excep " + me);
|
||||
me.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user