* changed city zone handling to server instead of pcb

This commit is contained in:
lukas-heiligenbrunner 2020-01-12 13:22:31 +01:00
parent 05b15a1ea2
commit c3fb92d6eb
2 changed files with 48 additions and 40 deletions

View File

@ -33,7 +33,6 @@ public class main {
//initial connect to db //initial connect to db
Log.message("initial login to db"); Log.message("initial login to db");
new Thread(() -> {
try { try {
JDCB.init("ingproject", "Kb9Dxklumt76ieq6", "ingproject", "db.power4future.at", 3306); JDCB.init("ingproject", "Kb9Dxklumt76ieq6", "ingproject", "db.power4future.at", 3306);
//JDCB.init("users", "kOpaIJUjkgb9ur6S", "wasteinformation", "192.168.65.15", 3306); //JDCB.init("users", "kOpaIJUjkgb9ur6S", "wasteinformation", "192.168.65.15", 3306);
@ -41,7 +40,6 @@ public class main {
//e.printStackTrace(); //e.printStackTrace();
Log.error("no connection to db"); Log.error("no connection to db");
} }
}).start();
//startup web server //startup web server

View File

@ -14,9 +14,15 @@ import java.util.Date;
public class MqttService { public class MqttService {
private MqttClient client = null; private MqttClient client = null;
private String serveruri; private String serveruri;
JDCB db;
public MqttService(String serverurl, String port) { public MqttService(String serverurl, String port) {
serveruri = "tcp://" + serverurl + ":" + port; serveruri = "tcp://" + serverurl + ":" + port;
try {
db = JDCB.getInstance();
} catch (IOException e) {
e.printStackTrace();
}
} }
public void startupService() { public void startupService() {
@ -31,6 +37,7 @@ public class MqttService {
@Override @Override
public void connectionLost(Throwable throwable) { public void connectionLost(Throwable throwable) {
Log.error("connection lost"); Log.error("connection lost");
// TODO: 12.01.20 reconnect
} }
@Override @Override
@ -38,11 +45,27 @@ public class MqttService {
String message = new String(mqttMessage.getPayload()); String message = new String(mqttMessage.getPayload());
Log.info("received Request from PCB"); Log.info("received Request from PCB");
Log.debug("received message"); ResultSet res = db.executeQuery("SELECT * from devices WHERE DeviceID=" + message);
String[] split = message.split(","); try {
String wastetyp = getTyp(Integer.parseInt(split[2])); res.last();
// TODO: 12.01.20 check if id is in db -- save when not if (res.getRow() != 0) {
checkDatabase(wastetyp, Integer.parseInt(split[0]), split[1], Integer.parseInt(split[3])); //existing device
res.first();
int cityid = res.getInt("CityID");
if (cityid == -1) {
//device not configured yet
tramsmitMessage(message + ",-1");
} else {
checkDatabase(Integer.parseInt(message));
}
} else {
//new device
db.executeUpdate("INSERT INTO devices (DeviceID) VALUES (" + message + ")");
tramsmitMessage(message + ",-1");
}
} catch (SQLException e) {
e.printStackTrace();
}
} }
@Override @Override
@ -56,25 +79,14 @@ public class MqttService {
} }
} }
public void checkDatabase(String wastetyp, int clientidentify, String cityname, int zone) { public void checkDatabase(int deviceid) {
Log.debug(wastetyp); ResultSet result = db.executeQuery("SELECT pickupdates.pickupdate FROM pickupdates WHERE pickupdates.citywastezoneid=" + deviceid);
Log.debug(clientidentify);
JDCB Database = null;
try {
Database = JDCB.getInstance();
} catch (IOException e) {
Log.error("No Connection to the databank");
}
int wastenumber = getIntTyp(wastetyp);
ResultSet result = Database.executeQuery("SELECT pickupdates.pickupdate FROM pickupdates WHERE pickupdates.citywastezoneid=(SELECT cities.id FROM cities WHERE cities.name='" + cityname + "' AND cities.wastetype='" + wastetyp + "' AND cities.zone=" + zone + ")");
try { try {
result.last(); result.last();
if (result.getRow() == 0) { if (result.getRow() == 0) {
//if not found in db --> send zero //if not found in db --> send zero
Log.debug("not found in db"); Log.debug("not found in db");
tramsmitMessage(clientidentify + "," + wastenumber + "," + 0); tramsmitMessage(deviceid + "," + "Plastic" + "," + 0);
} else { } else {
Log.debug(result.getString("pickupdate")); Log.debug(result.getString("pickupdate"));
@ -87,16 +99,14 @@ public class MqttService {
if (timestamp == timestampnow || timestamp == timestampnow + 86400000) { // 86400000 == one day if (timestamp == timestampnow || timestamp == timestampnow + 86400000) { // 86400000 == one day
// valid time // valid time
tramsmitMessage(clientidentify + "," + wastenumber + "," + 1); tramsmitMessage(deviceid + "," + "Plastic" + "," + 1);
Log.debug("valid time"); Log.debug("valid time");
return; return;
} }
} while (result.next()); } while (result.next());
tramsmitMessage(clientidentify + "," + wastenumber + "," + 0); //transmit zero if not returned before tramsmitMessage(deviceid + "," + "Plastic" + "," + 0); //transmit zero if not returned before
} }
} catch (SQLException e) { } catch (SQLException | ParseException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }