Skip to content

Commit

Permalink
Merge pull request #64 from hjelev/dev
Browse files Browse the repository at this point in the history
Separate update checks and have separate interval for them
  • Loading branch information
hjelev authored Feb 1, 2024
2 parents 72b5895 + 48efff4 commit 560c8af
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/config.py.example
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ qos = 0

# this is the time between executiuons if the script is used as service (--service option)
service_sleep_time = 120

update_check_interval = 3600 # 1 hour
cpu_load = True
cpu_temp = True
used_space = True
Expand Down
44 changes: 31 additions & 13 deletions src/rpi-cpu2mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import update
import config


# get device host name - used in mqtt topic
hostname = socket.gethostname()

Expand Down Expand Up @@ -146,28 +147,28 @@ def get_manufacturer():


def check_git_update(script_dir):
if config.version == update.check_git_version_remote(script_dir):
remote_version = update.check_git_version_remote(script_dir)
if config.version == remote_version:
git_update = {
"installed_ver": config.version,
"new_ver": config.version,
}
else:
git_update = {
"installed_ver": config.version,
"new_ver": update.check_git_version_remote(script_dir),
"new_ver": remote_version,
}

return(json.dumps(git_update))


def check_git_version(script_dir):
full_cmd = "git -C {} describe --tags `git -C {} rev-list --tags --max-count=1`".format(script_dir, script_dir)
git_version = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].decode("utf-8").replace('\n', '')

return(git_version)




def get_network_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
Expand All @@ -180,6 +181,7 @@ def get_network_ip():
s.close()
return IP


def print_measured_values( cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_clock_speed=0, swap=0, memory=0,
uptime_days=0, uptime_seconds = 0, wifi_signal=0, wifi_signal_dbm=0, rpi5_fan_speed=0, git_update=False):
print(":: rpi-mqtt-monitor")
Expand Down Expand Up @@ -210,6 +212,7 @@ def print_measured_values( cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_
print(" Update Available: " + str(git_update))
print("")


def config_json(what_config):
model_name = check_model_name()
manufacturer = get_manufacturer()
Expand Down Expand Up @@ -323,6 +326,7 @@ def on_log(client, userdata, level, buf):
if level == paho.MQTT_LOG_ERR:
print("MQTT error: ", buf)


def on_connect(client, userdata, flags, rc):
if rc != 0:
print("Error: Unable to connect to MQTT broker, return code:", rc)
Expand Down Expand Up @@ -478,6 +482,7 @@ def parse_arguments():
exit()
return args


def gather_and_send_info():
while True:
# set all monitored values to False in case they are turned off in the config
Expand Down Expand Up @@ -514,27 +519,32 @@ def gather_and_send_info():
wifi_signal_dbm = check_wifi_signal('dbm')
if config.rpi5_fan_speed:
rpi5_fan_speed = check_rpi5_fan_speed()
if config.git_update:
git_update = check_git_update(script_dir)
if config.update:
update = check_git_update(script_dir)

# Display collected values on screen if --display option is used
if args.display:
if config.git_update: git_update = check_git_update(script_dir)
print_measured_values(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, uptime_seconds, wifi_signal, wifi_signal_dbm, rpi5_fan_speed, git_update)

# Publish messages to MQTT
if hasattr(config, 'group_messages') and config.group_messages:
bulk_publish_to_mqtt(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, uptime_seconds, wifi_signal, wifi_signal_dbm, rpi5_fan_speed, git_update)
bulk_publish_to_mqtt(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, uptime_seconds, wifi_signal, wifi_signal_dbm, rpi5_fan_speed)
else:
publish_to_mqtt(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, uptime_seconds, wifi_signal, wifi_signal_dbm, rpi5_fan_speed, git_update, update)
publish_to_mqtt(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, uptime_seconds, wifi_signal, wifi_signal_dbm, rpi5_fan_speed)

# if not running as a service, break the loop after one iteration
if not args.service:
break
# if running as a service, sleep before the next iteration
time.sleep(config.service_sleep_time)


def update_status():
while True:
git_update = update = check_git_update(script_dir)
publish_to_mqtt(update, git_update)
time.sleep(config.update_check_interval)


def on_message(client, userdata, msg):
global exit_flag
print("Received message: ", msg.payload.decode())
Expand All @@ -546,6 +556,7 @@ def on_message(client, userdata, msg):

exit_flag = False


if __name__ == '__main__':
script_dir = os.path.dirname(os.path.realpath(__file__))
args = parse_arguments();
Expand All @@ -563,10 +574,17 @@ def on_message(client, userdata, msg):

client.subscribe("homeassistant/update/" + hostname + "/command") # Replace with your MQTT topic
print("Listening to topic : " + "homeassistant/update/" + hostname + "/command")

# Start the gather_and_send_info function in a new thread
thread = threading.Thread(target=gather_and_send_info)
thread.daemon = True # Set the daemon attribute to True
thread.start()
thread1 = threading.Thread(target=gather_and_send_info)
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.start()

client.loop_start() # Start the MQTT client loop in a new thread

Expand Down
7 changes: 6 additions & 1 deletion src/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ def display_config_differences(current_config, example_config, display=True):

def check_git_version_remote(script_dir):
full_cmd = "git -C {} ls-remote --tags origin | awk -F'/' '{{print $3}}' | sort -V | tail -n 1".format(script_dir)
result = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].decode("utf-8")
try:
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 None

latest_tag = result.strip()
return latest_tag if latest_tag else None

Expand Down

0 comments on commit 560c8af

Please sign in to comment.