diff --git a/birdnet-pi/rootfs/etc/cont-init.d/33-mqtt.sh b/birdnet-pi/rootfs/etc/cont-init.d/33-mqtt.sh index 8f6f41342..4a8cffb0b 100755 --- a/birdnet-pi/rootfs/etc/cont-init.d/33-mqtt.sh +++ b/birdnet-pi/rootfs/etc/cont-init.d/33-mqtt.sh @@ -2,6 +2,17 @@ # shellcheck shell=bash set -e +common_steps () { + + # Copy script + cp /helpers/birdnet_to_mqtt.py "$HOME"/BirdNET-Pi/scripts/birdnet_to_mqtt.py + chmod +x "$HOME"/BirdNET-Pi/scripts/birdnet_to_mqtt.py + + # Add hook + sed -i "/import sendAppriseNotifications/a from .birdnet_to_mqtt import automatic_mqtt_publish" "$HOME"/BirdNET-Pi/scripts/utils/reporting.py + +} + if bashio::services.available 'mqtt' && ! bashio::config.true 'MQTT_DISABLED' ; then bashio::log.green "---" bashio::log.blue "MQTT addon is active on your system! Birdnet-pi is now automatically configured to send its ouptut to MQTT" @@ -19,11 +30,9 @@ if bashio::services.available 'mqtt' && ! bashio::config.true 'MQTT_DISABLED' ; sed -i "s|%%mqtt_user%%|$(bashio::services "mqtt" "username")|g" /helpers/birdnet_to_mqtt.py sed -i "s|%%mqtt_pass%%|$(bashio::services "mqtt" "password")|g" /helpers/birdnet_to_mqtt.py - # Copy script - cp /helpers/birdnet_to_mqtt.py /usr/bin/birdnet_to_mqtt.py - cp /helpers/birdnet_to_mqtt.sh /custom-services.d - chmod 777 /usr/bin/birdnet_to_mqtt.py - chmod 777 /custom-services.d/birdnet_to_mqtt.sh + # Common steps + common_steps + elif bashio::config.has_value "MQTT_HOST_manual" && bashio::config.has_value "MQTT_PORT_manual"; then bashio::log.green "---" bashio::log.blue "MQTT is manually configured in the addon options" @@ -39,9 +48,7 @@ elif bashio::config.has_value "MQTT_HOST_manual" && bashio::config.has_value "MQ sed -i "s|%%mqtt_user%%|$(bashio::config "MQTT_USER_manual")|g" /helpers/birdnet_to_mqtt.py sed -i "s|%%mqtt_pass%%|$(bashio::config "MQTT_PASSWORD_manual")|g" /helpers/birdnet_to_mqtt.py - # Copy script - cp /helpers/birdnet_to_mqtt.py /usr/bin/birdnet_to_mqtt.py - cp /helpers/birdnet_to_mqtt.sh /custom-services.d - chmod +x /usr/bin/birdnet_to_mqtt.py - chmod +x /custom-services.d/birdnet_to_mqtt.sh + # Common steps + common_steps + fi diff --git a/birdnet-pi/rootfs/helpers/birdnet_to_mqtt.py b/birdnet-pi/rootfs/helpers/birdnet_to_mqtt.py index a57c9ad21..cbeb36392 100644 --- a/birdnet-pi/rootfs/helpers/birdnet_to_mqtt.py +++ b/birdnet-pi/rootfs/helpers/birdnet_to_mqtt.py @@ -4,9 +4,6 @@ # Adapted from : https://gist.github.com/deepcoder/c309087c456fc733435b47d83f4113ff # Adapted from : https://gist.github.com/JuanMeeske/08b839246a62ff38778f701fc1da5554 # -# monitor the records in the syslog file for info from the birdnet system on birds that it detects -# publish this data to mqtt -# import time import re @@ -26,19 +23,6 @@ flickr_images = {} conf = get_settings() settings_dict = dict(conf) -# Setup basic configuration for logging -logging.basicConfig(level=logging.INFO) - -# this generator function monitors the requested file handle for new lines added at its end -# the newly added line is returned by the function -def file_row_generator(s): - while True : - line = s.readline() - if not line: - time.sleep(0.1) - continue - yield line - # mqtt server mqtt_server = "%%mqtt_server%%" # server for mqtt mqtt_user = "%%mqtt_user%%" # Replace with your MQTT username @@ -51,11 +35,6 @@ mqtt_topic_confident_birds = 'birdnet' # url base for website that will be used to look up info about bird bird_lookup_url_base = 'http://en.wikipedia.org/wiki/' -# regular expression patters used to decode the records from birdnet -re_high_clean = re.compile(r'(?<=^\[birdnet_analysis\]\[INFO\] ).*?(?=\.mp3$)') - -syslog = open('/proc/1/fd/1', 'r') - def on_connect(client, userdata, flags, rc, properties=None): """ Callback for when the client receives a CONNACK response from the server. """ if rc == 0: @@ -91,16 +70,11 @@ mqttc.connect(mqtt_server, mqtt_port) # Connect to (broker, port, keepalive-tim mqttc.on_connect = on_connect mqttc.loop_start() -# call the generator function and process each line that is returned -for row in file_row_generator(syslog): - # bird found above confidence level found, process it - if re_high_clean.search(row) : - # this slacker regular expression work, extracts the data about the bird found from the log line - # I do the parse in two passes, because I did not know the re to do it in one! - - raw_high_bird = re.search(re_high_clean, row) - raw_high_bird = raw_high_bird.group(0) +# Call function +def automatic_mqtt_publish(species, confidence, confidencepct, path, + date, time, week, latitude, longitude, cutoff, + sens, overlap, settings_dict, db_path=DB_PATH): # the fields we want are separated by semicolons, so split high_bird_fields = raw_high_bird.split(';') @@ -108,22 +82,17 @@ for row in file_row_generator(syslog): # build a structure in python that will be converted to json bird = {} - # human time in this record is in two fields, date and time. They are human format - # combine them together separated by a space and they turn the human data into a python - # timestamp - raw_ts = high_bird_fields[0] + ' ' + high_bird_fields[1] - #bird['ts'] = str(datetime.datetime.timestamp(dateparser.parse(raw_ts))) - bird['Date'] = high_bird_fields[0] - bird['Time'] = high_bird_fields[1] - bird['ScientificName'] = high_bird_fields[2] - bird['CommonName'] = high_bird_fields[3] - bird['Confidence'] = high_bird_fields[4] - bird['SpeciesCode'] = get_bird_code(high_bird_fields[2]) - bird['ClipName'] = high_bird_fields[11] + bird['Date'] = date + bird['Time'] = time + bird['ScientificName'] = sciName + bird['CommonName'] = comName + bird['Confidence'] = confidence + bird['SpeciesCode'] = get_bird_code(sciName) + bird['ClipName'] = path # build a url from scientific name of bird that can be used to lookup info about bird - bird['url'] = bird_lookup_url_base + high_bird_fields[2].replace(' ', '_') + bird['url'] = bird_lookup_url_base + sciName.replace(' ', '_') # Flickimage image_url = "" diff --git a/birdnet-pi/rootfs/helpers/birdnet_to_mqtt.sh b/birdnet-pi/rootfs/helpers/birdnet_to_mqtt.sh deleted file mode 100755 index 54045d508..000000000 --- a/birdnet-pi/rootfs/helpers/birdnet_to_mqtt.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/usr/bin/with-contenv bashio -# shellcheck shell=bash - -echo "Starting service: mqtt automated publish" -"$PYTHON_VIRTUAL_ENV" /usr/bin/birdnet_to_mqtt.py &>/proc/1/fd/1