mqtt automatic poster works

https://github.com/alexbelgium/hassio-addons/issues/1464
This commit is contained in:
Alexandre
2024-07-10 13:13:10 +02:00
committed by GitHub
parent 72a978205d
commit 4d3aa94db1

View File

@@ -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"""
<?php
include('/home/pi/BirdNET-Pi/scripts/common.php');
echo get_info_url('{bird_name}');
?>
"""
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"""
# <?php
# include('/home/pi/BirdNET-Pi/scripts/common.php');
# echo get_info_url('{bird_name}');
# ?>
# """
# 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)