I want to share my solution to evaluate and dispatch danger alarms provides by the BUND (a german instance).
Before I post some parts of code I want describe the solution a little bit:
The german instance BUND hosts a website to provide danger alarms and disaster messages via JSON (https://warnung.bund.de/bbk.mowas/gefahrendurchsagen.json). The content has information about the kind of the disaster, the region, instructions for the population and contacts. So I have to know the region code, for which region I want to have the messages and I have to parse the JSON and forward the information to OH.
At this site https://tableaumapping.wordpress.com/2014/04/15/germany-kreise you can find an overview about all regions and assigned geo codes of germany. The column “RS alt” contains the right value.
I wrote a php-Script which load the JSON, parse the JSON and provide the information splittet into area, headline, description, instruction and contact via MQTT messages. Additional I generate a human readable text message with all of this. The script is called from a cron job every 30 minutes.
In OH I have some items which are filled from the MQTT messages above and a rule, if the human readable message is changed. In the rule I post the message to the smartphone of me and my wife using Telegram.
OK. Here comes the code. First, the php-Script. You should change the values for your MQTT broker and at the end the geo code with your own values.
<?php
$MOSQUITTO_IP = "192.168.1.48";
$MOSQUITTO_PORT = 1883;
$MOSQUITTO_USER = "Username";
$MOSQUITTO_PASS = "xxxxxx";
$MOSQUITTO_PATH = "state/gefahrendurchsagen/";
function MQTTsend ($mqttSubName, $value) {
$strPath = $GLOBALS['MOSQUITTO_PATH'];
$strCmd = 'mosquitto_pub -h ' . $GLOBALS['MOSQUITTO_IP'] . ' -p ' . $GLOBALS['MOSQUITTO_PORT'] . ' -u ' . $GLOBALS['MOSQUITTO_USER'] . ' -P ' . $GLOBALS['MOSQUITTO_PASS'] . ' -t ' . $strPath;
shell_exec ($strCmd . $mqttSubName . ' -m "' . $value . '"');
}
function ParseRegion ($r, $AreaName) {
MQTTsend ('area', $AreaName);
MQTTsend ('headline', $r['headline']);
MQTTsend ('description', $r['description']);
MQTTsend ('instruction', $r['instruction']);
MQTTsend ('web', $r['web']);
MQTTsend ('contact', $r['contact']);
$sMessage = $r['headline'] . " [" . $AreaName . "]";
$sMessage .= "\r\n\r\nDescription:\r\n";
$sMessage .= $r['description'];
$sMessage .= "\r\n\r\nInstructions:\r\n";
$sMessage .= $r['instruction'];
$sMessage .= "\r\n\r\nFurther information on: ";
$sMessage .= $r['web'];
$sMessage .= "\r\nContact: ";
$sMessage .= $r['contact'];
MQTTsend ('message', $sMessage);
}
$strMessages = file_get_contents ("https://warnung.bund.de/bbk.mowas/gefahrendurchsagen.json");
$arrJSON = json_decode ($strMessages, TRUE);
foreach ($arrJSON as $i) {
foreach ($i as $ki => $vi) {
if ($ki == 'info') {
if ($vi[0]['area'][0]['geocode'][0]['value'] == '145220000000') {
ParseRegion ($vi[0], 'Mittelsachsen');
}
if ($vi[0]['area'][0]['geocode'][0]['value'] == '146120000000') {
ParseRegion ($vi[0], 'Dresden');
}
if ($vi[0]['area'][0]['geocode'][0]['value'] == '146270000000') {
ParseRegion ($vi[0], 'Meissen');
}
sleep (10);
}
}
}
?>
The line inside the cron tab:
0,30 * * * * php /var/www/html/BUNDGefahrenmeldung/getgefahrenmeldung.php
The content of the items file:
String MLD_area "County [%s]" { mqtt="<[mosquitto:state/gefahrendurchsagen/area:state:default]" }
String MLD_headline "Headline [%s]" { mqtt="<[mosquitto:state/gefahrendurchsagen/headline:state:default]" }
String MLD_description "Description [%s]" { mqtt="<[mosquitto:state/gefahrendurchsagen/description:state:default]" }
String MLD_instruction "Instruction [%s]" { mqtt="<[mosquitto:state/gefahrendurchsagen/instruction:state:default]" }
String MLD_web "Web [%s]" { mqtt="<[mosquitto:state/gefahrendurchsagen/web:state:default]" }
String MLD_contact "Contact [%s]" { mqtt="<[mosquitto:state/gefahrendurchsagen/contact:state:default]" }
String MLD_message "[%s]" { mqtt="<[mosquitto:state/gefahrendurchsagen/message:state:default]" }
and the content of the rule which send the message to the smartphones. You have to edit the receiver of the message!
rule "GFM"
when
Item MLD_message changed
then
TelegramSender.sendCommand ("@User1 @User2 " + MLD_message.state)
end
At last a screenshot from my smartphone with a test message inside Telegram.
Have fun
Stephan Pilz