Created
October 27, 2020 12:23
-
-
Save IT-Berater/dc7695fdb406d6f93d4a47553d309ec9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.wenzlaff; | |
import java.time.LocalDateTime; | |
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 org.json.JSONArray; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
/** | |
* Paho MQTT-CO2 Client. | |
* | |
* Beispiele siehe | |
* | |
* http://www.wenzlaff.info | |
* | |
* @author Thomas Wenzlaff | |
* | |
*/ | |
public class MqttCo2Client { | |
/** Der Server mit dem MQTT Broker. */ | |
private static final String DEFAULT_SERVER = "localhost"; | |
/** Der abonnierte Topic. */ | |
private static final String TOPIC = "co2"; | |
/** ID Prefix für den Client. */ | |
private static final String CLIENT_ID = "id-"; | |
private static int co2WertInPpm; | |
public enum LÜFTUNGSAMPEL { | |
/** Kohlendioxid-Leitwert als Lüftungsampel. */ | |
GRÜN, GELB, ROT | |
} | |
public static int getCo2WertInPpm() { | |
return co2WertInPpm; | |
} | |
/** | |
* Startet eine MQTT Client der an den Topic co2 abonniert ist. Im Hintergurnd | |
* werden die Werte mit Bewertung auf System.out ausgegeben. | |
* | |
* @param args der Server | |
* @throws MqttException bei verbindungs Problemen | |
* @throws Exception bei Fehler | |
*/ | |
public static void start() throws Exception { | |
MqttClient client = new MqttClient("tcp://" + DEFAULT_SERVER, generateClientId()); | |
System.out.println("Starte den CO2 MQTT-Client mit Server " + DEFAULT_SERVER + " und ID: " + client.getClientId() + " ..."); | |
client.setCallback(new MqttCallback() { | |
@Override | |
public void connectionLost(Throwable throwable) { | |
} | |
@Override | |
public void messageArrived(String topic, MqttMessage nachricht) throws Exception { | |
leseCo2(nachricht); | |
} | |
private void leseCo2(MqttMessage nachricht) { | |
JSONObject jsonNachricht = new JSONObject(nachricht.toString()); | |
try { | |
JSONArray nachrichten = jsonNachricht.getJSONObject("messung").getJSONArray("satz"); | |
JSONObject satz = (JSONObject) nachrichten.get(1); | |
int co2 = satz.getInt("co2"); | |
System.out.println("CO2: " + co2 + " ppm"); | |
co2WertInPpm = co2; | |
bewertungUBAausgeben(co2); | |
} catch (JSONException e) { | |
System.err.println(e); | |
} | |
} | |
@Override | |
public void deliveryComplete(IMqttDeliveryToken t) { | |
} | |
}); | |
client.connect(); | |
client.subscribe(TOPIC); | |
} | |
/** | |
* Quelle: | |
* https://www.hamburg.de/contentblob/1016516/7073fd694816ce7e8edd0eb82a2593e6/data/gesundheitliche-bewertung-co2.pdf | |
* | |
* https://www.dguv.de/medien/ifa/de/pub/rep/pdf/reports2013/innenraumarbeitsplaetze/kapitel_12_4_1.pdf | |
* | |
* @param co2 Wert in ppm | |
*/ | |
static LÜFTUNGSAMPEL bewertungUBAausgeben(int co2) { | |
if (co2 < 1000) { | |
System.out.println("Laut UBA GRÜN: Hygienisch unbedenklich, keine weiteren Maßnahmen"); | |
return LÜFTUNGSAMPEL.GRÜN; | |
} else if (co2 >= 1000 && co2 < 2000) { | |
System.out.println("Laut UBA GELB: Hygienisch auffällig, Lüftungsmaßnahmen erhöhen, Lüftungsverhalten überprüfen und verbessern"); | |
return LÜFTUNGSAMPEL.GELB; | |
} else { | |
System.out.println("Laut UBA ROT: Hygienisch inakzeptabel, Belüftbarkeit des Raumes prüfen ggf. weitergehende Maßnahmen prüfen"); | |
return LÜFTUNGSAMPEL.ROT; | |
} | |
} | |
private static String generateClientId() { | |
return CLIENT_ID + LocalDateTime.now(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment