1
0
Fork 0
A Python based project for controlling a set of lights via relay switches -- based on the light meter readings made available via the "Midpoint" project.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

51 lines
1.7 KiB

#!/bin/bash
# Startup Script
# ====================================================================
# I wrote this script with the intention of using it to start a
# cron-job -- when the Raspberry Pi is turned on. Before you set the
# cron-job up, you must make sure you have either ran the
# "make-log-files.sh" script or created the appropriate log file and
# folder at the location specified at "logFile" below.
# ====
# Please note: This script calls another (Python) script at the end of
# it. The code in the Python script (relay.py) is an infinite-loop
# so you will need to kill it manually or turn-off the Raspberry Pi.
# ====
# I put the "sleep 60" call at the start to reduce any errors
# occurring because a part of the system (I.E. curl) has not finished
# loading. 60 seconds is a little excessive but I wrote this script
# with the expectation of it running on an unmanned Raspberry Pi. So,
# reliable/consistent behaviour is my preference over "fast start-up
# times".
remove 60 sec. delay at startup and add add new (cli) exception message. The removal of the delay is in preparation of moving the program to a Systemd service -- instead of a '@reboot' cronjob. By doing that, the need to wait for the network stack on the Raspberry Pi to start is a built-in feature. The change to Systemd is becuase the program would terminate when it hits the 'max. requests exceeded' exception in the 'requests' module (Python). Having spoke to Nic (lead artist), she has stated she has a preference for the program to be requesting the latest light readings or nothing at all. The 'backoff' features which come with the 'requests' module will stop the program from crashing but at the possibility of the having the Relays be out-of-sync -- whilst still looking like it's working -- with the latest light readings produced by the Light Meters in the factory and what's been delivered to the server (Midpoint). To be fair, this problem only arose when I ran the two gallery Relays, one Light Meter and one instance of Eyes and Ears from the same location (my flat). This wouldn't be the case in the when the exhibiton is live. Either way, Systemd it is. The natural process of waiting for the program to restart (with added delay in Systemd service file if wanted) naturally allows the system to 'backoff' but it keeps the all-or-nothing aspect Nic prefers. The exception message is a minor change. It's there just to make it easier to determine if the program is terminating because of the 'max. requests exceeded' exception. I added it to the generic exception for ease more than anything else. I can catch other exceptions whilst expecting the 'max. requests' one. This is not the best approach to programming but the trade-off between project goals and budget make me think this is ok (for now). It is what it is.
3 years ago
#sleep 60
logDate=$(date '+%Y-%m-%dT%TZ')
logFile="/home/rtrp/logs/startup-logs.txt"
mainURL="http://ritherdon.abbether.net/api/status/update" # Make sure this is valid.
getApiUrl () {
case $HOSTNAME in
(factory1) apiURL="${mainURL}/1";;
(factory2) apiURL="${mainURL}/2";;
(factory3) apiURL="${mainURL}/3";;
(gallery1) apiURL="${mainURL}/4";;
(gallery2) apiURL="${mainURL}/5";;
(gallery3) apiURL="${mainURL}/6";;
esac
}
logStatusChange () {
cat << EOF >> $logFile
$logDate
EOF
}
logStatusChange
getApiUrl
curl -S -X POST --header 'Content-Type: application/json' --header 'Accept: text/html' -d '{"status": "on", "time": "'${logDate}'", "token": "QWERTYuiopasdfghjklzxcvbnm_1234567890"}' "${apiURL}"
python3 /home/rtrp/repos/relay/relay.py