mirror of
https://github.com/alexbelgium/hassio-addons.git
synced 2026-06-06 23:55:56 +02:00
mqtt_hook branch
This commit is contained in:
@@ -2,6 +2,17 @@
|
|||||||
# shellcheck shell=bash
|
# shellcheck shell=bash
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
|
common_steps () {
|
||||||
|
|
||||||
|
# Copy script
|
||||||
|
cp /helpers/birdnet_to_mqtt.py "$HOME"/BirdNET-Pi/scripts/birdnet_to_mqtt.py
|
||||||
|
chmod +x "$HOME"/BirdNET-Pi/scripts/birdnet_to_mqtt.py
|
||||||
|
|
||||||
|
# Add hook
|
||||||
|
sed -i "/import sendAppriseNotifications/a from .birdnet_to_mqtt import automatic_mqtt_publish" "$HOME"/BirdNET-Pi/scripts/utils/reporting.py
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
if bashio::services.available 'mqtt' && ! bashio::config.true 'MQTT_DISABLED' ; then
|
if bashio::services.available 'mqtt' && ! bashio::config.true 'MQTT_DISABLED' ; then
|
||||||
bashio::log.green "---"
|
bashio::log.green "---"
|
||||||
bashio::log.blue "MQTT addon is active on your system! Birdnet-pi is now automatically configured to send its ouptut to MQTT"
|
bashio::log.blue "MQTT addon is active on your system! Birdnet-pi is now automatically configured to send its ouptut to MQTT"
|
||||||
@@ -19,11 +30,9 @@ if bashio::services.available 'mqtt' && ! bashio::config.true 'MQTT_DISABLED' ;
|
|||||||
sed -i "s|%%mqtt_user%%|$(bashio::services "mqtt" "username")|g" /helpers/birdnet_to_mqtt.py
|
sed -i "s|%%mqtt_user%%|$(bashio::services "mqtt" "username")|g" /helpers/birdnet_to_mqtt.py
|
||||||
sed -i "s|%%mqtt_pass%%|$(bashio::services "mqtt" "password")|g" /helpers/birdnet_to_mqtt.py
|
sed -i "s|%%mqtt_pass%%|$(bashio::services "mqtt" "password")|g" /helpers/birdnet_to_mqtt.py
|
||||||
|
|
||||||
# Copy script
|
# Common steps
|
||||||
cp /helpers/birdnet_to_mqtt.py /usr/bin/birdnet_to_mqtt.py
|
common_steps
|
||||||
cp /helpers/birdnet_to_mqtt.sh /custom-services.d
|
|
||||||
chmod 777 /usr/bin/birdnet_to_mqtt.py
|
|
||||||
chmod 777 /custom-services.d/birdnet_to_mqtt.sh
|
|
||||||
elif bashio::config.has_value "MQTT_HOST_manual" && bashio::config.has_value "MQTT_PORT_manual"; then
|
elif bashio::config.has_value "MQTT_HOST_manual" && bashio::config.has_value "MQTT_PORT_manual"; then
|
||||||
bashio::log.green "---"
|
bashio::log.green "---"
|
||||||
bashio::log.blue "MQTT is manually configured in the addon options"
|
bashio::log.blue "MQTT is manually configured in the addon options"
|
||||||
@@ -39,9 +48,7 @@ elif bashio::config.has_value "MQTT_HOST_manual" && bashio::config.has_value "MQ
|
|||||||
sed -i "s|%%mqtt_user%%|$(bashio::config "MQTT_USER_manual")|g" /helpers/birdnet_to_mqtt.py
|
sed -i "s|%%mqtt_user%%|$(bashio::config "MQTT_USER_manual")|g" /helpers/birdnet_to_mqtt.py
|
||||||
sed -i "s|%%mqtt_pass%%|$(bashio::config "MQTT_PASSWORD_manual")|g" /helpers/birdnet_to_mqtt.py
|
sed -i "s|%%mqtt_pass%%|$(bashio::config "MQTT_PASSWORD_manual")|g" /helpers/birdnet_to_mqtt.py
|
||||||
|
|
||||||
# Copy script
|
# Common steps
|
||||||
cp /helpers/birdnet_to_mqtt.py /usr/bin/birdnet_to_mqtt.py
|
common_steps
|
||||||
cp /helpers/birdnet_to_mqtt.sh /custom-services.d
|
|
||||||
chmod +x /usr/bin/birdnet_to_mqtt.py
|
|
||||||
chmod +x /custom-services.d/birdnet_to_mqtt.sh
|
|
||||||
fi
|
fi
|
||||||
|
|||||||
@@ -4,9 +4,6 @@
|
|||||||
# Adapted from : https://gist.github.com/deepcoder/c309087c456fc733435b47d83f4113ff
|
# Adapted from : https://gist.github.com/deepcoder/c309087c456fc733435b47d83f4113ff
|
||||||
# Adapted from : https://gist.github.com/JuanMeeske/08b839246a62ff38778f701fc1da5554
|
# Adapted from : https://gist.github.com/JuanMeeske/08b839246a62ff38778f701fc1da5554
|
||||||
#
|
#
|
||||||
# monitor the records in the syslog file for info from the birdnet system on birds that it detects
|
|
||||||
# publish this data to mqtt
|
|
||||||
#
|
|
||||||
|
|
||||||
import time
|
import time
|
||||||
import re
|
import re
|
||||||
@@ -26,19 +23,6 @@ flickr_images = {}
|
|||||||
conf = get_settings()
|
conf = get_settings()
|
||||||
settings_dict = dict(conf)
|
settings_dict = dict(conf)
|
||||||
|
|
||||||
# Setup basic configuration for logging
|
|
||||||
logging.basicConfig(level=logging.INFO)
|
|
||||||
|
|
||||||
# this generator function monitors the requested file handle for new lines added at its end
|
|
||||||
# the newly added line is returned by the function
|
|
||||||
def file_row_generator(s):
|
|
||||||
while True :
|
|
||||||
line = s.readline()
|
|
||||||
if not line:
|
|
||||||
time.sleep(0.1)
|
|
||||||
continue
|
|
||||||
yield line
|
|
||||||
|
|
||||||
# mqtt server
|
# mqtt server
|
||||||
mqtt_server = "%%mqtt_server%%" # server for mqtt
|
mqtt_server = "%%mqtt_server%%" # server for mqtt
|
||||||
mqtt_user = "%%mqtt_user%%" # Replace with your MQTT username
|
mqtt_user = "%%mqtt_user%%" # Replace with your MQTT username
|
||||||
@@ -51,11 +35,6 @@ mqtt_topic_confident_birds = 'birdnet'
|
|||||||
# url base for website that will be used to look up info about bird
|
# url base for website that will be used to look up info about bird
|
||||||
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
|
|
||||||
re_high_clean = re.compile(r'(?<=^\[birdnet_analysis\]\[INFO\] ).*?(?=\.mp3$)')
|
|
||||||
|
|
||||||
syslog = open('/proc/1/fd/1', 'r')
|
|
||||||
|
|
||||||
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:
|
||||||
@@ -91,16 +70,11 @@ mqttc.connect(mqtt_server, mqtt_port) # Connect to (broker, port, keepalive-tim
|
|||||||
mqttc.on_connect = on_connect
|
mqttc.on_connect = on_connect
|
||||||
mqttc.loop_start()
|
mqttc.loop_start()
|
||||||
|
|
||||||
# call the generator function and process each line that is returned
|
|
||||||
for row in file_row_generator(syslog):
|
|
||||||
# bird found above confidence level found, process it
|
|
||||||
if re_high_clean.search(row) :
|
|
||||||
|
|
||||||
# this slacker regular expression work, extracts the data about the bird found from the log line
|
# Call function
|
||||||
# I do the parse in two passes, because I did not know the re to do it in one!
|
def automatic_mqtt_publish(species, confidence, confidencepct, path,
|
||||||
|
date, time, week, latitude, longitude, cutoff,
|
||||||
raw_high_bird = re.search(re_high_clean, row)
|
sens, overlap, settings_dict, db_path=DB_PATH):
|
||||||
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
|
||||||
high_bird_fields = raw_high_bird.split(';')
|
high_bird_fields = raw_high_bird.split(';')
|
||||||
@@ -108,22 +82,17 @@ for row in file_row_generator(syslog):
|
|||||||
# build a structure in python that will be converted to json
|
# build a structure in python that will be converted to json
|
||||||
bird = {}
|
bird = {}
|
||||||
|
|
||||||
# human time in this record is in two fields, date and time. They are human format
|
|
||||||
# combine them together separated by a space and they turn the human data into a python
|
|
||||||
# timestamp
|
|
||||||
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'] = date
|
||||||
bird['Time'] = high_bird_fields[1]
|
bird['Time'] = time
|
||||||
bird['ScientificName'] = high_bird_fields[2]
|
bird['ScientificName'] = sciName
|
||||||
bird['CommonName'] = high_bird_fields[3]
|
bird['CommonName'] = comName
|
||||||
bird['Confidence'] = high_bird_fields[4]
|
bird['Confidence'] = confidence
|
||||||
bird['SpeciesCode'] = get_bird_code(high_bird_fields[2])
|
bird['SpeciesCode'] = get_bird_code(sciName)
|
||||||
bird['ClipName'] = high_bird_fields[11]
|
bird['ClipName'] = path
|
||||||
|
|
||||||
# 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 + sciName.replace(' ', '_')
|
||||||
|
|
||||||
# Flickimage
|
# Flickimage
|
||||||
image_url = ""
|
image_url = ""
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
#!/usr/bin/with-contenv bashio
|
|
||||||
# shellcheck shell=bash
|
|
||||||
|
|
||||||
echo "Starting service: mqtt automated publish"
|
|
||||||
"$PYTHON_VIRTUAL_ENV" /usr/bin/birdnet_to_mqtt.py &>/proc/1/fd/1
|
|
||||||
Reference in New Issue
Block a user