From 158cef121ea6a5bf13897d264f1ebcbaef18a259 Mon Sep 17 00:00:00 2001 From: pi Date: Fri, 21 Feb 2025 16:58:18 +0100 Subject: [PATCH] all --- .gitignore | 2 ++ check-connection.sh | 15 +++++++++ client/client.py | 74 +++++++++++++++++++++++++++++++++++++++++++++ install-on-pi.sh | 12 ++++++++ nohup.out | 0 requirements.txt | 4 +++ restart-rop.sh | 9 ++++++ run-on-pi.sh | 38 +++++++++++++++++++++++ 8 files changed, 154 insertions(+) create mode 100644 .gitignore create mode 100755 check-connection.sh create mode 100644 client/client.py create mode 100755 install-on-pi.sh create mode 100644 nohup.out create mode 100644 requirements.txt create mode 100755 restart-rop.sh create mode 100755 run-on-pi.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..712f6ed --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +venv/** +*.log diff --git a/check-connection.sh b/check-connection.sh new file mode 100755 index 0000000..2d759a1 --- /dev/null +++ b/check-connection.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +cd ~/mqtt-halt-pack || exit + + +while [ 0 -eq 0 ]; do + + if [ $(ping -q -w 10 -c 1 192.168.31.5 > /dev/null && echo 1 || echo 0) -eq 0 ]; then + echo "$(date '+%Y%m%d%H%M%S'): 0" + else + echo "$(date '+%Y%m%d%H%M%S'): 1" + fi + sleep 10 +done + diff --git a/client/client.py b/client/client.py new file mode 100644 index 0000000..468fc66 --- /dev/null +++ b/client/client.py @@ -0,0 +1,74 @@ +#!/usr/bin/python3 + +import paho.mqtt.client as mqtt +import paho.mqtt.publish as publish +import traceback +import os +import datetime + +monitor_state = 1 + +def on_connect(mqttc, obj, flags, rc): + now = datetime.datetime.now() + print(str(now) + ": " +"Connected to %s:%s" % (mqttc._host, mqttc._port)) + +def on_message(mqttc, obj, msg): + global monitor_state + + now = datetime.datetime.now() + print(str(now) + ": " +msg.topic+" "+str(msg.qos)+" "+str(msg.payload)) + + if msg.payload.decode('UTF-8') == 'halt': + print('powering off') + try: + publish.single("dom/pimirror-ack", "went-halt", hostname="192.168.31.5") + print("ack sent") + except Exception as e: + print("Exception when sending:" + str(e)) + except: + print("Exception when sending:" + traceback.format_exc()) + + os.system("sudo shutdown now -h") + + elif msg.payload.decode('UTF-8') == 'display_on': + if monitor_state == 0: + monitor_state = 1 + os.system("/home/pi/display_on.sh") + print("display on") + + elif msg.payload.decode('UTF-8') == 'display_off': + if monitor_state == 1: + monitor_state = 0 + os.system("/home/pi/display_off.sh") + print("display off") + +def on_publish(mqttc, obj, mid): + now = datetime.datetime.now() + print(str(now) + ": " +"mid: "+str(mid)) + +def on_subscribe(mqttc, obj, mid, granted_qos): + now = datetime.datetime.now() + print(str(now) + ": " +"Subscribed: "+str(mid)+" "+str(granted_qos)) + + +def on_log(mqttc, obj, level, string): + now = datetime.datetime.now() + print(str(now) + ": " +string) + +# If you want to use a specific client id, use +# mqttc = mqtt.Client("client-id") +# but note that the client id must be unique on the broker. Leaving the client +# id parameter empty will generate a random id for you. + +mqttc = mqtt.Client() +mqttc.on_message = on_message +mqttc.on_connect = on_connect +mqttc.on_publish = on_publish +mqttc.on_subscribe = on_subscribe + +# Uncomment to enable debug messages +mqttc.on_log = on_log +mqttc.connect("192.168.31.5") +mqttc.subscribe("dom/pimirror", 0) + +mqttc.loop_forever() diff --git a/install-on-pi.sh b/install-on-pi.sh new file mode 100755 index 0000000..96d79ee --- /dev/null +++ b/install-on-pi.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +cd ~/mqtt-halt-pack || exit + +python3 -m venv venv + +source venv/bin/activate + +pip install --upgrade pip + +pip install -r requirements.txt --no-cache-dir --disable-pip-version-check --index-url https://www.piwheels.org/simple + diff --git a/nohup.out b/nohup.out new file mode 100644 index 0000000..e69de29 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..4554ec7 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +# pydbus +paho-mqtt +# Pycairo +# PyGObject diff --git a/restart-rop.sh b/restart-rop.sh new file mode 100755 index 0000000..19cd680 --- /dev/null +++ b/restart-rop.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +echo +echo "$(date '+%Y%m%d%H%M%S'): restarting run-on-pi" >> /home/pi/mqtt-halt-pack/app.log + +killall run-on-pi.sh + +nohup /home/pi/mqtt-halt-pack/run-on-pi.sh >> /home/pi/mqtt-halt-pack/app.log 2>&1 & + diff --git a/run-on-pi.sh b/run-on-pi.sh new file mode 100755 index 0000000..437cbd8 --- /dev/null +++ b/run-on-pi.sh @@ -0,0 +1,38 @@ +#!/bin/bash + +cd ~/mqtt-halt-pack || exit + +# wait until network is ready +counter=0 +max_counter=120 +max_restart_counter=20 + +echo "$(date '+%Y%m%d%H%M%S'): checking connectivity" + +while [ $(ping -q -w 3 -c 1 192.168.31.5 > /dev/null && echo 1 || echo 0) -eq 0 ]; do + if [ $counter -eq $max_counter ]; then + echo "$(date '+%Y%m%d%H%M%S') give up..." + exit 1 + fi + let counter++ + sleep 1 +done + +echo "$(date '+%Y%m%d%H%M%S'): starting client app" + +source venv/bin/activate + +restart_counter=0 + +while [ 1 -eq 1 ]; do + python client/client.py + + if [ $restart_counter -eq $max_restart_counter ]; then + echo "$(date '+%Y%m%d%H%M%S') give up restarts..." + exit 1 + fi + let restart_counter++ + + echo "$(date '+%Y%m%d%H%M%S'): restarting client app in 5 seconds..." + sleep 5 +done