mirror of
https://github.com/alexbelgium/hassio-addons.git
synced 2026-06-03 06:14:07 +02:00
Update birdnet_to_mqtt.py
This commit is contained in:
@@ -54,16 +54,22 @@ 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 get_bird_code(scientific_name):
|
||||||
# php_code = f"""
|
with open('/home/pi/BirdNET-Pi/scripts/ebird.php', 'r') as file:
|
||||||
# <?php
|
data = file.read()
|
||||||
# include('/home/pi/BirdNET-Pi/scripts/common.php');
|
|
||||||
# echo get_info_url('{bird_name}');
|
# Extract the array from the PHP file
|
||||||
# ?>
|
array_str = re.search(r'\$ebirds = \[(.*?)\];', data, re.DOTALL).group(1)
|
||||||
# """
|
|
||||||
# process = subprocess.Popen(['php'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
# Convert the PHP array to a Python dictionary
|
||||||
# stdout, _ = process.communicate(php_code.encode())
|
bird_dict = {}
|
||||||
# return stdout.decode().strip()
|
for line in array_str.split('\n'):
|
||||||
|
if '=>' in line:
|
||||||
|
key, value = map(lambda x: x.strip(' "'), line.split('=>'))
|
||||||
|
bird_dict[key] = value
|
||||||
|
|
||||||
|
# Return the corresponding value for the given bird's scientific name
|
||||||
|
return bird_dict.get(scientific_name)
|
||||||
|
|
||||||
# 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
|
||||||
@@ -101,13 +107,13 @@ for row in file_row_generator(syslog):
|
|||||||
# timestamp
|
# timestamp
|
||||||
raw_ts = high_bird_fields[0] + ' ' + high_bird_fields[1]
|
raw_ts = high_bird_fields[0] + ' ' + high_bird_fields[1]
|
||||||
|
|
||||||
bird['ts'] = str(datetime.datetime.timestamp(dateparser.parse(raw_ts)))
|
#bird['ts'] = str(datetime.datetime.timestamp(dateparser.parse(raw_ts)))
|
||||||
bird['Date'] = high_bird_fields[0]
|
bird['Date'] = high_bird_fields[0]
|
||||||
bird['Time'] = high_bird_fields[1]
|
bird['Time'] = high_bird_fields[1]
|
||||||
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'] = get_bird_code(high_bird_fields[2])
|
||||||
bird['ClipName'] = high_bird_fields[11]
|
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
|
||||||
@@ -119,4 +125,3 @@ for row in file_row_generator(syslog):
|
|||||||
print('Posted to MQTT : ok')
|
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