mirror of
https://github.com/alexbelgium/hassio-addons.git
synced 2026-06-02 05:44:03 +02:00
mqtt automatic poster works
https://github.com/alexbelgium/hassio-addons/issues/1464
This commit is contained in:
@@ -43,13 +43,7 @@ mqtt_topic_confident_birds = 'birdnet'
|
|||||||
bird_lookup_url_base = 'http://en.wikipedia.org/wiki/'
|
bird_lookup_url_base = 'http://en.wikipedia.org/wiki/'
|
||||||
|
|
||||||
# regular expression patters used to decode the records from birdnet
|
# regular expression patters used to decode the records from birdnet
|
||||||
|
re_high_clean = re.compile(r'(?<=^\[birdnet_analysis\]\[INFO\] ).*?(?=\.mp3$)')
|
||||||
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$')
|
|
||||||
|
|
||||||
syslog = open('/proc/1/fd/1', 'r')
|
syslog = open('/proc/1/fd/1', 'r')
|
||||||
|
|
||||||
@@ -60,16 +54,16 @@ def on_connect(client, userdata, flags, rc, properties=None):
|
|||||||
else:
|
else:
|
||||||
logging.error(f"Failed to connect, return code {rc}\n")
|
logging.error(f"Failed to connect, return code {rc}\n")
|
||||||
|
|
||||||
def call_php_function(bird_name):
|
#def call_php_function(bird_name):
|
||||||
php_code = f"""
|
# php_code = f"""
|
||||||
<?php
|
# <?php
|
||||||
include('/home/pi/BirdNET-Pi/scripts/common.php');
|
# include('/home/pi/BirdNET-Pi/scripts/common.php');
|
||||||
echo get_info_url('{bird_name}');
|
# echo get_info_url('{bird_name}');
|
||||||
?>
|
# ?>
|
||||||
"""
|
# """
|
||||||
process = subprocess.Popen(['php'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
# process = subprocess.Popen(['php'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
||||||
stdout, _ = process.communicate(php_code.encode())
|
# stdout, _ = process.communicate(php_code.encode())
|
||||||
return stdout.decode().strip()
|
# return stdout.decode().strip()
|
||||||
|
|
||||||
# this little hack is to make each received record for the all birds section unique
|
# 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
|
# 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
|
# call the generator function and process each line that is returned
|
||||||
for row in file_row_generator(syslog):
|
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
|
# bird found above confidence level found, process it
|
||||||
if re_high_found.search(row) :
|
if re_high_clean.search(row) :
|
||||||
print('yes')
|
|
||||||
|
|
||||||
# this slacker regular expression work, extracts the data about the bird found from the log line
|
# 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!
|
# 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 = re.search(re_high_clean, row)
|
||||||
raw_high_bird = raw_high_bird.group(0)
|
|
||||||
raw_high_bird = re.search(re_high_clean, raw_high_bird)
|
|
||||||
raw_high_bird = raw_high_bird.group(0)
|
raw_high_bird = raw_high_bird.group(0)
|
||||||
|
|
||||||
# the fields we want are separated by semicolons, so split
|
# 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['ScientificName'] = high_bird_fields[2]
|
||||||
bird['CommonName'] = high_bird_fields[3]
|
bird['CommonName'] = high_bird_fields[3]
|
||||||
bird['Confidence'] = high_bird_fields[4]
|
bird['Confidence'] = high_bird_fields[4]
|
||||||
bird['SpeciesCode'] = call_php_function(high_bird_fields[2])
|
# bird['SpeciesCode'] = call_php_function(high_bird_fields[2])
|
||||||
bird['ClipName'] = high_bird_fields[12]
|
bird['ClipName'] = high_bird_fields[11]
|
||||||
|
|
||||||
# build a url from scientific name of bird that can be used to lookup info about bird
|
# 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 + 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
|
# convert to json string we can sent to mqtt
|
||||||
json_bird = json.dumps(bird)
|
json_bird = json.dumps(bird)
|
||||||
|
|
||||||
print(json_bird)
|
print('Posted to MQTT : ok')
|
||||||
|
|
||||||
mqttc.publish(mqtt_topic_confident_birds, json_bird, 1)
|
mqttc.publish(mqtt_topic_confident_birds, json_bird, 1)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user