From 4d3aa94db1d76c730450633e0b154def15ab54aa Mon Sep 17 00:00:00 2001 From: Alexandre <44178713+alexbelgium@users.noreply.github.com> Date: Wed, 10 Jul 2024 13:13:10 +0200 Subject: [PATCH] mqtt automatic poster works https://github.com/alexbelgium/hassio-addons/issues/1464 --- birdnet-pi/rootfs/helpers/birdnet_to_mqtt.py | 44 +++++++------------- 1 file changed, 16 insertions(+), 28 deletions(-) diff --git a/birdnet-pi/rootfs/helpers/birdnet_to_mqtt.py b/birdnet-pi/rootfs/helpers/birdnet_to_mqtt.py index 4f722cee1..3a4385e12 100644 --- a/birdnet-pi/rootfs/helpers/birdnet_to_mqtt.py +++ b/birdnet-pi/rootfs/helpers/birdnet_to_mqtt.py @@ -43,13 +43,7 @@ mqtt_topic_confident_birds = 'birdnet' bird_lookup_url_base = 'http://en.wikipedia.org/wiki/' # regular expression patters used to decode the records from birdnet - -re_all_found = re.compile(r'birdnet_analysis\.sh.*\(.*\)') -re_found_bird = re.compile(r'\(([^)]+)\)') -re_log_timestamp = re.compile(r'.+?(?= birdnet-)') - -re_high_found = re.compile(r'.*\.mp3$') -re_high_clean = re.compile(r'(?<=\]:).*\.mp3$') +re_high_clean = re.compile(r'(?<=^\[birdnet_analysis\]\[INFO\] ).*?(?=\.mp3$)') syslog = open('/proc/1/fd/1', 'r') @@ -60,16 +54,16 @@ def on_connect(client, userdata, flags, rc, properties=None): else: logging.error(f"Failed to connect, return code {rc}\n") -def call_php_function(bird_name): - php_code = f""" - - """ - process = subprocess.Popen(['php'], stdin=subprocess.PIPE, stdout=subprocess.PIPE) - stdout, _ = process.communicate(php_code.encode()) - return stdout.decode().strip() +#def call_php_function(bird_name): +# php_code = f""" +# +# """ +# process = subprocess.Popen(['php'], stdin=subprocess.PIPE, stdout=subprocess.PIPE) +# stdout, _ = process.communicate(php_code.encode()) +# return stdout.decode().strip() # this little hack is to make each received record for the all birds section unique # the date and time that the log returns is only down to the 1 second accuracy, do @@ -87,19 +81,13 @@ mqttc.loop_start() # call the generator function and process each line that is returned for row in file_row_generator(syslog): - # if line in log is from 'birdnet_analysis.sh' routine, then operate on it - print(row) - # bird found above confidence level found, process it - if re_high_found.search(row) : - print('yes') + 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_found, row) - raw_high_bird = raw_high_bird.group(0) - raw_high_bird = re.search(re_high_clean, raw_high_bird) + raw_high_bird = re.search(re_high_clean, row) raw_high_bird = raw_high_bird.group(0) # the fields we want are separated by semicolons, so split @@ -119,8 +107,8 @@ for row in file_row_generator(syslog): bird['ScientificName'] = high_bird_fields[2] bird['CommonName'] = high_bird_fields[3] bird['Confidence'] = high_bird_fields[4] - bird['SpeciesCode'] = call_php_function(high_bird_fields[2]) - bird['ClipName'] = high_bird_fields[12] + # bird['SpeciesCode'] = call_php_function(high_bird_fields[2]) + bird['ClipName'] = high_bird_fields[11] # 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(' ', '_') @@ -128,7 +116,7 @@ for row in file_row_generator(syslog): # convert to json string we can sent to mqtt json_bird = json.dumps(bird) - print(json_bird) + print('Posted to MQTT : ok') mqttc.publish(mqtt_topic_confident_birds, json_bird, 1)