* mqtt service cleanup
* removed unused methods
This commit is contained in:
parent
4b96ad576d
commit
7c8a0d3018
@ -11,7 +11,7 @@ import java.io.IOException;
|
|||||||
public class main {
|
public class main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
Log.setLevel(Log.INFO);
|
Log.setLevel(Log.DEBUG);
|
||||||
Log.info("startup of WasteInformationServer");
|
Log.info("startup of WasteInformationServer");
|
||||||
|
|
||||||
Log.info("mem: "+Runtime.getRuntime().totalMemory());
|
Log.info("mem: "+Runtime.getRuntime().totalMemory());
|
||||||
@ -52,7 +52,7 @@ public class main {
|
|||||||
//startup mqtt service
|
//startup mqtt service
|
||||||
Log.message("starting mqtt service");
|
Log.message("starting mqtt service");
|
||||||
try {
|
try {
|
||||||
MqttService m = new MqttService();
|
MqttService m = new MqttService("mqtt.heili.eu","1883");
|
||||||
m.startupService();
|
m.startupService();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log.error("An error occured in the class mqtt");
|
Log.error("An error occured in the class mqtt");
|
||||||
|
@ -4,28 +4,25 @@ import com.wasteinformationserver.basicutils.Log;
|
|||||||
import com.wasteinformationserver.db.JDCB;
|
import com.wasteinformationserver.db.JDCB;
|
||||||
import org.eclipse.paho.client.mqttv3.*;
|
import org.eclipse.paho.client.mqttv3.*;
|
||||||
|
|
||||||
import java.awt.event.ActionEvent;
|
|
||||||
import java.awt.event.ActionListener;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.text.DateFormat;
|
import java.text.ParseException;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Calendar;
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.GregorianCalendar;
|
|
||||||
|
|
||||||
public class MqttService {
|
public class MqttService {
|
||||||
MqttClient client = null;
|
private MqttClient client = null;
|
||||||
|
private String serveruri;
|
||||||
public MqttService() {
|
|
||||||
|
|
||||||
|
public MqttService(String serverurl, String port) {
|
||||||
|
serveruri= "tcp://"+serverurl+":"+port;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void startupService() {
|
public void startupService() {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
client = new MqttClient("tcp://192.168.65.15:1883", "JavaSample42");
|
client = new MqttClient(serveruri, "JavaSample42");
|
||||||
MqttConnectOptions connOpts = new MqttConnectOptions();
|
MqttConnectOptions connOpts = new MqttConnectOptions();
|
||||||
connOpts.setCleanSession(true);
|
connOpts.setCleanSession(true);
|
||||||
client.connect(connOpts);
|
client.connect(connOpts);
|
||||||
@ -44,7 +41,7 @@ public class MqttService {
|
|||||||
Log.debug("received message");
|
Log.debug("received message");
|
||||||
String[] split = message.split(",");
|
String[] split = message.split(",");
|
||||||
String wastetyp = getTyp(Integer.parseInt(split[2]));
|
String wastetyp = getTyp(Integer.parseInt(split[2]));
|
||||||
getDatabasedata("SELECT pickupdates.pickupdate FROM pickupdates WHERE pickupdates.citywastezoneid=(SELECT cities.zone FROM cities WHERE cities.name='" + split[1] + "' AND cities.wastetype='" + wastetyp + "' AND cities.zone=" + split[3] + ")", wastetyp, Integer.parseInt(split[0]));
|
getDatabasedata("SELECT pickupdates.pickupdate FROM pickupdates WHERE pickupdates.citywastezoneid=(SELECT cities.id FROM cities WHERE cities.name='" + split[1] + "' AND cities.wastetype='" + wastetyp + "' AND cities.zone=" + split[3] + ")", wastetyp, Integer.parseInt(split[0]));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -75,29 +72,40 @@ public class MqttService {
|
|||||||
ResultSet result = Database.executeQuery(message);
|
ResultSet result = Database.executeQuery(message);
|
||||||
try {
|
try {
|
||||||
result.last();
|
result.last();
|
||||||
if (result.getFetchSize() == 0){
|
if (result.getRow() == 0){
|
||||||
//if not found in db --> send zero
|
//if not found in db --> send zero
|
||||||
transmitmessageAbfallart(clientidentify + "," + wastenumber + "," + 0);
|
Log.debug("not found in db");
|
||||||
}
|
tramsmitMessage(clientidentify + "," + wastenumber + "," + 0);
|
||||||
result.first();
|
|
||||||
while (result.next()) {
|
|
||||||
String newDate = getDateDatabase(String.valueOf(result.getString("pickupdate")));
|
|
||||||
String currentDate = getcurrentDate();
|
|
||||||
String Datetomorrow = nexDayDate();
|
|
||||||
|
|
||||||
if (currentDate.equals(newDate) || currentDate.equals(Datetomorrow)) {
|
|
||||||
transmitmessageAbfallart(clientidentify + "," + wastenumber + "," + 1);
|
|
||||||
}else{
|
}else{
|
||||||
transmitmessageAbfallart(clientidentify + "," + wastenumber + "," + 0);
|
Log.debug(result.getString("pickupdate"));
|
||||||
|
|
||||||
|
result.first();
|
||||||
|
do {
|
||||||
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
|
long timestamp = formatter.parse(result.getString("pickupdate")).getTime();
|
||||||
|
long timestampnow = formatter.parse(formatter.format(new Date())).getTime(); // todo more fancy
|
||||||
|
Log.debug("timestamp is :" + timestamp);
|
||||||
|
|
||||||
|
if(timestamp == timestampnow|| timestamp == timestampnow +86400000){
|
||||||
|
// valid time
|
||||||
|
tramsmitMessage(clientidentify + "," + wastenumber + "," + 1);
|
||||||
|
Log.debug("valid time");
|
||||||
|
return;
|
||||||
|
}else{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}while(result.next());
|
||||||
|
tramsmitMessage(clientidentify + "," + wastenumber + "," + 0); //transmit zero if not returned before
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
Log.error("No data from database");
|
e.printStackTrace();
|
||||||
|
} catch (ParseException e) {
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void transmitmessageAbfallart(String temp) {
|
private void tramsmitMessage(String temp) {
|
||||||
Log.debug("sending message >>>"+temp);
|
Log.debug("sending message >>>"+temp);
|
||||||
MqttMessage message = new MqttMessage(temp.getBytes());
|
MqttMessage message = new MqttMessage(temp.getBytes());
|
||||||
message.setQos(2);
|
message.setQos(2);
|
||||||
@ -108,21 +116,6 @@ public class MqttService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String nexDayDate() {
|
|
||||||
// TODO: 10.01.20 doesnt work
|
|
||||||
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("/");
|
|
||||||
return split[2] + "." + split[1] + "." + split[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getTyp(int number) {
|
private String getTyp(int number) {
|
||||||
if (number == 1) {
|
if (number == 1) {
|
||||||
return "Plastic";
|
return "Plastic";
|
||||||
@ -149,20 +142,4 @@ public class MqttService {
|
|||||||
}
|
}
|
||||||
return number;
|
return number;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getDateDatabase(String temptime) {
|
|
||||||
String[] parts = temptime.split("-");
|
|
||||||
String tempyear = parts[0];
|
|
||||||
String[] yearsplit = tempyear.split("0");
|
|
||||||
String tempyearnew = yearsplit[1];
|
|
||||||
return parts[2] + "." + parts[1] + "." + tempyearnew;
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getcurrentDate() {
|
|
||||||
GregorianCalendar now = new GregorianCalendar();
|
|
||||||
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG);
|
|
||||||
String date = df.format(now.getTime());
|
|
||||||
String[] partstwo = date.split(",");
|
|
||||||
return partstwo[0];
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -53,7 +53,7 @@ public class MainPage implements HttpHandler {
|
|||||||
Log.warning("wrong page sending 404");
|
Log.warning("wrong page sending 404");
|
||||||
sendPage("/404Error.html", t);
|
sendPage("/404Error.html", t);
|
||||||
} else if (fs == null) {
|
} else if (fs == null) {
|
||||||
Log.warning("requested resource doesnt exist");
|
Log.warning("requested resource doesnt exist --> "+path);
|
||||||
} else {
|
} else {
|
||||||
// Object exists and is a file: accept with response code 200.
|
// Object exists and is a file: accept with response code 200.
|
||||||
String mime = "text/html";
|
String mime = "text/html";
|
||||||
|
Loading…
Reference in New Issue
Block a user