mirror of
https://github.com/alexbelgium/hassio-addons.git
synced 2026-06-03 22:34:11 +02:00
New logic
This commit is contained in:
@@ -3,12 +3,10 @@
|
|||||||
|
|
||||||
import time
|
import time
|
||||||
import re
|
import re
|
||||||
import dateparser
|
|
||||||
import datetime
|
import datetime
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import paho.mqtt.client as mqtt
|
import paho.mqtt.client as mqtt
|
||||||
import subprocess
|
|
||||||
import requests
|
import requests
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
@@ -16,6 +14,9 @@ import os
|
|||||||
sys.path.append('/home/pi/BirdNET-Pi/scripts/utils')
|
sys.path.append('/home/pi/BirdNET-Pi/scripts/utils')
|
||||||
from helpers import get_settings
|
from helpers import get_settings
|
||||||
|
|
||||||
|
# Setup logging
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
# Used in flickrimage
|
# Used in flickrimage
|
||||||
flickr_images = {}
|
flickr_images = {}
|
||||||
conf = get_settings()
|
conf = get_settings()
|
||||||
@@ -25,7 +26,7 @@ settings_dict = dict(conf)
|
|||||||
mqtt_server = "%%mqtt_server%%"
|
mqtt_server = "%%mqtt_server%%"
|
||||||
mqtt_user = "%%mqtt_user%%"
|
mqtt_user = "%%mqtt_user%%"
|
||||||
mqtt_pass = "%%mqtt_pass%%"
|
mqtt_pass = "%%mqtt_pass%%"
|
||||||
mqtt_port = %%mqtt_port%%
|
mqtt_port = "%%mqtt_port%%"
|
||||||
|
|
||||||
mqtt_topic_confident_birds = 'birdnet'
|
mqtt_topic_confident_birds = 'birdnet'
|
||||||
bird_lookup_url_base = 'http://en.wikipedia.org/wiki/'
|
bird_lookup_url_base = 'http://en.wikipedia.org/wiki/'
|
||||||
@@ -33,9 +34,9 @@ bird_lookup_url_base = 'http://en.wikipedia.org/wiki/'
|
|||||||
def on_connect(client, userdata, flags, rc, properties=None):
|
def on_connect(client, userdata, flags, rc, properties=None):
|
||||||
""" Callback for when the client receives a CONNACK response from the server. """
|
""" Callback for when the client receives a CONNACK response from the server. """
|
||||||
if rc == 0:
|
if rc == 0:
|
||||||
logging.info("Connected to MQTT Broker!")
|
log.info("Connected to MQTT Broker!")
|
||||||
else:
|
else:
|
||||||
logging.error(f"Failed to connect, return code {rc}\n")
|
log.error(f"Failed to connect, return code {rc}\n")
|
||||||
|
|
||||||
def get_bird_code(scientific_name):
|
def get_bird_code(scientific_name):
|
||||||
with open('/home/pi/BirdNET-Pi/scripts/ebird.php', 'r') as file:
|
with open('/home/pi/BirdNET-Pi/scripts/ebird.php', 'r') as file:
|
||||||
@@ -48,16 +49,6 @@ def get_bird_code(scientific_name):
|
|||||||
|
|
||||||
return bird_dict.get(scientific_name)
|
return bird_dict.get(scientific_name)
|
||||||
|
|
||||||
ts_noise = 0.0
|
|
||||||
|
|
||||||
mqttc = mqtt.Client('birdnet_mqtt')
|
|
||||||
mqttc.username_pw_set(mqtt_user, mqtt_pass)
|
|
||||||
mqttc.on_connect = on_connect
|
|
||||||
|
|
||||||
try:
|
|
||||||
mqttc.connect(mqtt_server, mqtt_port)
|
|
||||||
mqttc.loop_start()
|
|
||||||
|
|
||||||
def automatic_mqtt_publish(file, detections):
|
def automatic_mqtt_publish(file, detections):
|
||||||
for detection in detections:
|
for detection in detections:
|
||||||
bird = {}
|
bird = {}
|
||||||
@@ -87,7 +78,7 @@ try:
|
|||||||
image_url = 'https://farm'+str(data["farm"])+'.static.flickr.com/'+str(data["server"])+'/'+str(data["id"])+'_'+str(data["secret"])+'_n.jpg'
|
image_url = 'https://farm'+str(data["farm"])+'.static.flickr.com/'+str(data["server"])+'/'+str(data["id"])+'_'+str(data["secret"])+'_n.jpg'
|
||||||
flickr_images[common_name] = image_url
|
flickr_images[common_name] = image_url
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error("FLICKR API ERROR: " + str(e))
|
log.error("FLICKR API ERROR: " + str(e))
|
||||||
image_url = ""
|
image_url = ""
|
||||||
else:
|
else:
|
||||||
image_url = flickr_images[common_name]
|
image_url = flickr_images[common_name]
|
||||||
@@ -95,10 +86,21 @@ try:
|
|||||||
|
|
||||||
json_bird = json.dumps(bird)
|
json_bird = json.dumps(bird)
|
||||||
mqttc.publish(mqtt_topic_confident_birds, json_bird, 1)
|
mqttc.publish(mqtt_topic_confident_birds, json_bird, 1)
|
||||||
logging.info('Posted to MQTT: ok')
|
log.info("Posted to MQTT: ok")
|
||||||
|
|
||||||
|
mqttc = mqtt.Client('birdnet_mqtt')
|
||||||
|
mqttc.username_pw_set(mqtt_user, mqtt_pass)
|
||||||
|
mqttc.on_connect = on_connect
|
||||||
|
|
||||||
|
try:
|
||||||
|
mqttc.connect(mqtt_server, mqtt_port)
|
||||||
|
mqttc.loop_start()
|
||||||
|
|
||||||
|
# Assuming `file` and `detections` are provided from somewhere
|
||||||
|
# automatic_mqtt_publish(file, detections)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"An error occurred: {e}")
|
log.error("Cannot post mqtt: %s", e)
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
mqttc.loop_stop()
|
mqttc.loop_stop()
|
||||||
|
|||||||
Reference in New Issue
Block a user