all
This commit is contained in:
parent
5d78ff19a5
commit
158cef121e
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
venv/**
|
||||
*.log
|
||||
15
check-connection.sh
Executable file
15
check-connection.sh
Executable file
|
|
@ -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
|
||||
|
||||
74
client/client.py
Normal file
74
client/client.py
Normal file
|
|
@ -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()
|
||||
12
install-on-pi.sh
Executable file
12
install-on-pi.sh
Executable file
|
|
@ -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
|
||||
|
||||
4
requirements.txt
Normal file
4
requirements.txt
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
# pydbus
|
||||
paho-mqtt
|
||||
# Pycairo
|
||||
# PyGObject
|
||||
9
restart-rop.sh
Executable file
9
restart-rop.sh
Executable file
|
|
@ -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 &
|
||||
|
||||
38
run-on-pi.sh
Executable file
38
run-on-pi.sh
Executable file
|
|
@ -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
|
||||
Loading…
Reference in a new issue