diff --git a/install.sh b/install.sh index e649a14..7697b0b 100755 --- a/install.sh +++ b/install.sh @@ -73,7 +73,11 @@ create_venv(){ # Activate the virtual environment source rpi_mon_env/bin/activate - python=$(which python) + if [[ $(python3 --version) ]]; then + python=$(which python3) + else + python=$(which python) + fi print_green "+ Activated virtual environment" } @@ -173,6 +177,8 @@ set_service(){ sudo sed -i "s|WorkingDirectory=.*|WorkingDirectory=${cwd}|" /etc/systemd/system/rpi-mqtt-monitor.service sudo sed -i "s|User=YOUR_USER|User=root|" /etc/systemd/system/rpi-mqtt-monitor.service sudo sed -i "s|ExecStart=.*|ExecStart=${exec_start}|" /etc/systemd/system/rpi-mqtt-monitor.service + home_dir=$(eval echo ~$user) + sudo sed -i "s|Environment=\"HOME=/home/username\"|Environment=\"HOME=${home_dir}\"|" /etc/systemd/system/rpi-mqtt-monitor.service sudo systemctl daemon-reload sudo systemctl enable rpi-mqtt-monitor.service sudo systemctl start rpi-mqtt-monitor.service diff --git a/rpi-mqtt-monitor.service b/rpi-mqtt-monitor.service index 67c1fc7..dbc35e1 100644 --- a/rpi-mqtt-monitor.service +++ b/rpi-mqtt-monitor.service @@ -5,6 +5,7 @@ Wants=network-online.target [Service] ExecStart=/home/username/git/rpi-mqtt-monitor/rpi_mon_env/bin/python /home/username/git/rpi-mqtt-monitor/src/rpi-cpu2mqtt.py --service +Environment="HOME=/home/username" WorkingDirectory=/home/username/git/rpi-mqtt-monitor/ StandardOutput=inherit StandardError=inherit diff --git a/src/rpi-cpu2mqtt.py b/src/rpi-cpu2mqtt.py index 02dd562..82a53fb 100644 --- a/src/rpi-cpu2mqtt.py +++ b/src/rpi-cpu2mqtt.py @@ -16,7 +16,7 @@ import argparse import threading import update import config - +import logging # get device host name - used in mqtt topic hostname = socket.gethostname() @@ -148,6 +148,7 @@ def get_manufacturer(): def check_git_update(script_dir): remote_version = update.check_git_version_remote(script_dir) + logging.info("git_update value:" + remote_version) if config.version == remote_version: git_update = { "installed_ver": config.version, @@ -352,17 +353,18 @@ def create_mqtt_client(): def publish_update_status_to_mqtt(git_update): - + client = create_mqtt_client() if client is None: print("Error: Unable to connect to MQTT broker") - + return + client.loop_start() if config.git_update: if config.discovery_messages: client.publish("homeassistant/binary_sensor/" + config.mqtt_topic_prefix + "/" + hostname + "_git_update/config", config_json('git_update'), qos=config.qos) - client.publish(config.mqtt_topic_prefix + "/" + hostname + "/git_update", git_update, qos=config.qos, retain=config.retain) + client.publish(config.mqtt_topic_prefix + "/" + hostname + "/git_update", git_update, qos=1, retain=config.retain) if config.update: if config.discovery_messages: @@ -610,7 +612,7 @@ exit_flag = False stop_event = threading.Event() script_dir = os.path.dirname(os.path.realpath(__file__)) if __name__ == '__main__': - + logging.basicConfig(level=logging.INFO, stream=sys.stdout) args = parse_arguments(); if args.service: @@ -629,14 +631,14 @@ if __name__ == '__main__': client.loop_start() # Start the MQTT client loop in a new thread # Start the gather_and_send_info function in a new thread thread1 = threading.Thread(target=gather_and_send_info) - thread1.daemon = True # Set the daemon attribute to True + # thread1.daemon = True # Set the daemon attribute to True thread1.start() if config.update: # Start the update_status function in a new thread thread2 = threading.Thread(target=update_status) - thread2.daemon = True # Set the daemon attribute to True + # thread2.daemon = True # Set the daemon attribute to True thread2.start() # Check the exit flag in the main thread diff --git a/src/update.py b/src/update.py index cbedf78..8a7b437 100644 --- a/src/update.py +++ b/src/update.py @@ -2,6 +2,7 @@ import ast import os import subprocess import config +import logging def get_assignments(filename): with open(filename) as f: @@ -54,9 +55,10 @@ def check_git_version_remote(script_dir): result = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].decode("utf-8") except subprocess.CalledProcessError as e: print("Error: {}".format(e)) - return "0" + return config.version latest_tag = result.strip() + logging.info("git update:" + result + script_dir) return latest_tag if latest_tag else "0"