mqtt improvements

new device page
This commit is contained in:
lukas-heiligenbrunner
2020-01-10 16:38:18 +01:00
parent c44a0dd9ec
commit 0518c1c809
9 changed files with 381 additions and 69 deletions

View File

@ -14,6 +14,7 @@ public class main {
Log.setLevel(Log.INFO);
Log.info("startup of WasteInformationServer");
Log.info("mem: "+Runtime.getRuntime().totalMemory());
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
@ -56,5 +57,7 @@ public class main {
} catch (Exception e) {
Log.error("An error occured in the class mqtt");
}
Log.info("mem: "+Runtime.getRuntime().totalMemory());
}
}

View File

@ -27,7 +27,7 @@ public class mqtt {
public void notifymessage() {
try {
client = new MqttClient("tcp://192.168.65.15:1883", "JavaSample");
client = new MqttClient("tcp://192.168.65.15:1883", "JavaSample42");
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
client.connect(connOpts);
@ -39,6 +39,7 @@ public class mqtt {
mr.addMessageReceivedListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Log.debug("received message");
String[] split = e.getActionCommand().split(",");
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]));
@ -63,6 +64,12 @@ public class mqtt {
ResultSet result = Database.executeQuery(message);
try {
result.last();
if (result.getFetchSize() == 0){
//if not found in db --> send zero
transmitmessageAbfallart(clientidentify + "," + wastenumber + "," + 0);
}
result.first();
while (result.next()) {
String newDate = getDateDatabase(String.valueOf(result.getString("pickupdate")));
String currentDate = getcurrentDate();
@ -81,7 +88,7 @@ public class mqtt {
private void transmitmessageAbfallart(String temp) {
Log.debug("sending message >>>"+temp);
mqtttransmitter mt = new mqtttransmitter(client);
Log.debug(temp);
mt.sendmessage(temp);

View File

@ -29,6 +29,7 @@ public class mqttreceiver {
@Override
public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
message = new String(mqttMessage.getPayload());
Log.info("received Request from PCB");
notifylisteners(message);
}

View File

@ -17,9 +17,9 @@ import java.net.InetSocketAddress;
public class Webserver {
public void startserver() {
Log.info("starting Webserver");
HttpServer server = null;
try {
server = HttpServer.create(new InetSocketAddress(8000), 0);
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
server.createContext("/", new MainPage());
@ -33,72 +33,10 @@ public class Webserver {
server.setExecutor(null); // creates a default executor
server.start();
Log.info("Server available at http://127.0.0.1:8000 now");
}catch (BindException e){
} catch (BindException e) {
Log.criticalerror("The Port 8000 is already in use!");
} catch (IOException e) {
e.printStackTrace();
}
/*
try {
server = HttpsServer.create(new InetSocketAddress(8000), 0);
// initialise the HTTPS server
SSLContext sslContext = SSLContext.getInstance("TLS");
// initialise the keystore
char[] password = "password".toCharArray();
KeyStore ks = KeyStore.getInstance("JKS");
FileInputStream fis = new FileInputStream("testkey.jks");
ks.load(fis, password);
// setup the key manager factory
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, password);
// setup the trust manager factory
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(ks);
// setup the HTTPS context and parameters
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
server.setHttpsConfigurator(new HttpsConfigurator(sslContext) {
public void configure(HttpsParameters params) {
try {
// initialise the SSL context
SSLContext context = getSSLContext();
SSLEngine engine = context.createSSLEngine();
params.setNeedClientAuth(false);
params.setCipherSuites(engine.getEnabledCipherSuites());
params.setProtocols(engine.getEnabledProtocols());
// Set the SSL parameters
SSLParameters sslParameters = context.getSupportedSSLParameters();
params.setSSLParameters(sslParameters);
} catch (Exception ex) {
System.out.println("Failed to create HTTPS port");
}
}
});
} catch (IOException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnrecoverableKeyException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
*/
}
}