implemented a watchdog
This commit is contained in:
@@ -49,13 +49,13 @@ def main():
|
|||||||
#create a backup of the data file
|
#create a backup of the data file
|
||||||
shutil.copy('/home/zutritt/Documents/sarah/data.csv', '/home/zutritt/Documents/sarah/logs/' + datetime.today().strftime('%Y%m%d') + '_data_backup.csv')
|
shutil.copy('/home/zutritt/Documents/sarah/data.csv', '/home/zutritt/Documents/sarah/logs/' + datetime.today().strftime('%Y%m%d') + '_data_backup.csv')
|
||||||
|
|
||||||
#read the data file once. it will be re-read, if we don't know the UID of a read card or the card is expired.
|
#read the data file once. it will be re-read, if we don't know the UID of a read card or the card is expired.
|
||||||
#this saves read/write cycles on the SD card
|
#this saves read/write cycles on the SD card
|
||||||
data = pd.read_csv(r'/home/zutritt/Documents/sarah/data.csv') #,names=col_names)
|
data = pd.read_csv(r'/home/zutritt/Documents/sarah/data.csv') #,names=col_names)
|
||||||
|
|
||||||
#mail loop
|
#mail loop
|
||||||
while True:
|
while True:
|
||||||
|
|
||||||
#try reading a tag
|
#try reading a tag
|
||||||
time.sleep(0.5)
|
time.sleep(0.5)
|
||||||
uid = pn532.read_passive_target(timeout=0.5)
|
uid = pn532.read_passive_target(timeout=0.5)
|
||||||
@@ -77,12 +77,22 @@ def main():
|
|||||||
debug_file = open("/home/zutritt/Documents/sarah/logs/"+datetime.today().strftime('%Y%m%d')+"_debug_log.csv", "a")
|
debug_file = open("/home/zutritt/Documents/sarah/logs/"+datetime.today().strftime('%Y%m%d')+"_debug_log.csv", "a")
|
||||||
debug_file.write("\n"+datetime.now().strftime("%d/%m/%Y %H:%M:%S") + ", Manual open via website.")
|
debug_file.write("\n"+datetime.now().strftime("%d/%m/%Y %H:%M:%S") + ", Manual open via website.")
|
||||||
debug_file.close()
|
debug_file.close()
|
||||||
with open("/home/zutritt/Documents/sarah/logs/"+ datetime.today().strftime('%Y%m%d') +"_entrance_log.csv", "a") as x:
|
with open("/home/zutritt/Documents/sarah/logs/"+ datetime.today().strftime('%Y%m%d') +"_entrance_log.csv", "a") as x:
|
||||||
x.write("Manual entry via website, " + datetime.now().strftime("%d/%m/%Y %H:%M:%S") + "\n")
|
x.write("Manual entry via website, " + datetime.now().strftime("%d/%m/%Y %H:%M:%S") + "\n")
|
||||||
x.close()
|
x.close()
|
||||||
|
|
||||||
os.remove('/home/zutritt/Documents/sarah/system_request_commands/manual_door_open_request_set')
|
os.remove('/home/zutritt/Documents/sarah/system_request_commands/manual_door_open_request_set')
|
||||||
|
|
||||||
|
|
||||||
|
#in order to be able to implement a watchdog, the system checks for a file and deletes it is it's there
|
||||||
|
if os.path.isfile('/home/zutritt/Documents/sarah/system_request_commands/watchdog_attention_request_set'):
|
||||||
|
#if this file exists, there was a watchdog request made
|
||||||
|
debug_file = open("/home/zutritt/Documents/sarah/logs/"+datetime.today().strftime('%Y%m%d')+"_debug_log.csv", "a")
|
||||||
|
debug_file.write("\n"+datetime.now().strftime("%d/%m/%Y %H:%M:%S") + ", answered the watchdog.")
|
||||||
|
debug_file.close()
|
||||||
|
os.remove('/home/zutritt/Documents/sarah/system_request_commands/watchdog_attention_request_set')
|
||||||
|
|
||||||
|
|
||||||
#after reading a tag, we check if any numbers have been read
|
#after reading a tag, we check if any numbers have been read
|
||||||
#no numbers mean no tag has been detected
|
#no numbers mean no tag has been detected
|
||||||
if (not isinstance(uid,bytearray)):
|
if (not isinstance(uid,bytearray)):
|
||||||
@@ -103,7 +113,7 @@ def main():
|
|||||||
line = data.query("uid == @uid")
|
line = data.query("uid == @uid")
|
||||||
print(line)
|
print(line)
|
||||||
expiration_date = datetime.strptime(str(list(line["expire"])[0]), "%Y-%m-%d")
|
expiration_date = datetime.strptime(str(list(line["expire"])[0]), "%Y-%m-%d")
|
||||||
|
|
||||||
#check if the card is expired
|
#check if the card is expired
|
||||||
if datetime.now() < expiration_date:
|
if datetime.now() < expiration_date:
|
||||||
|
|
||||||
@@ -122,8 +132,8 @@ def main():
|
|||||||
else: #card is expired
|
else: #card is expired
|
||||||
debug_file.write(", Name:" + str(list(line["name"])[0]) + ", Matrikelnummer:" + str(list(line["matrikelnummer"])[0]) + ", !EXPIRED! no entry!")
|
debug_file.write(", Name:" + str(list(line["name"])[0]) + ", Matrikelnummer:" + str(list(line["matrikelnummer"])[0]) + ", !EXPIRED! no entry!")
|
||||||
try:
|
try:
|
||||||
data = pd.read_csv(r'/home/zutritt/Documents/sarah/data.csv') #read the file again, in case something has changed
|
data = pd.read_csv(r'/home/zutritt/Documents/sarah/data.csv') #read the file again, in case something has changed
|
||||||
debug_file.write(" (data.csv reloaded)")
|
debug_file.write(" (data.csv reloaded)")
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
debug_file.write(", !data.csv reloaded failed!")
|
debug_file.write(", !data.csv reloaded failed!")
|
||||||
|
|
||||||
|
|||||||
71
reader_data/sarah/src/sarahs_watchdog.py
Executable file
71
reader_data/sarah/src/sarahs_watchdog.py
Executable file
@@ -0,0 +1,71 @@
|
|||||||
|
import smtplib, ssl
|
||||||
|
from datetime import datetime
|
||||||
|
import time
|
||||||
|
import os
|
||||||
|
from email.mime.text import MIMEText
|
||||||
|
|
||||||
|
#
|
||||||
|
# This tool was written by Pet (pet@fet.at) in 6.2022
|
||||||
|
#
|
||||||
|
# It is a watchdog for the RFID reader "sarah", in reader.py
|
||||||
|
#
|
||||||
|
|
||||||
|
def main():
|
||||||
|
|
||||||
|
print("Watchdog watches! Started at: " + datetime.now().strftime("%d/%m/%Y %H:%M:%S"))
|
||||||
|
|
||||||
|
#if the file is already there, sarah didn't anwer last time and we don't need to send an additional mail
|
||||||
|
if os.path.isfile('/home/zutritt/Documents/sarah/system_request_commands/watchdog_attention_request_set'):
|
||||||
|
print("Sarah was down the last time we checked, no new check initiated. ")
|
||||||
|
|
||||||
|
#create a file which should be removed by the reader.py
|
||||||
|
with open("/home/zutritt/Documents/sarah/system_request_commands/watchdog_attention_request_set", "a") as x:
|
||||||
|
x.write(datetime.now().strftime("%d/%m/%Y %H:%M:%S") + ", Watchdog request set. \n")
|
||||||
|
x.close()
|
||||||
|
print("Watchdog file written.")
|
||||||
|
|
||||||
|
#wait for the read to delete the file
|
||||||
|
time.sleep(10)
|
||||||
|
|
||||||
|
#if the file is still there, sarah didn't anwer
|
||||||
|
if os.path.isfile('/home/zutritt/Documents/sarah/system_request_commands/watchdog_attention_request_set'):
|
||||||
|
print("Sarah didn't answer, we need to send a mail to the lab people. ")
|
||||||
|
send_mail()
|
||||||
|
else:
|
||||||
|
print("Sarah answered, everything is good.")
|
||||||
|
|
||||||
|
def send_mail():
|
||||||
|
|
||||||
|
debug_file_text = open("/home/zutritt/Documents/sarah/logs/"+datetime.today().strftime('%Y%m%d')+"_debug_log.csv", "r").read()
|
||||||
|
|
||||||
|
port = 587 # For starttls
|
||||||
|
smtp_server = "buran.htu.tuwien.ac.at"
|
||||||
|
sender_email = "doorknob@fet.at"
|
||||||
|
sender_username = "doorknob"
|
||||||
|
receiver_email = "lab@fet.at"
|
||||||
|
password = "JYgPXDWuX47N6ef"
|
||||||
|
|
||||||
|
|
||||||
|
message = "Hallo, \nich bin Sarahs Watchdog. Sarah, die Türöffnerin im FET Lab, reagiert nicht. \n" +\
|
||||||
|
"Hier ein paar Logs zum debuggen:\n" + \
|
||||||
|
"Es ist jetzt: " + datetime.now().strftime("%d/%m/%Y %H:%M:%S") + "\nDas Debug file beinhaltet: \n" + debug_file_text
|
||||||
|
|
||||||
|
|
||||||
|
msg = MIMEText(message)
|
||||||
|
|
||||||
|
msg['Subject'] = 'FET Doorknob issues'
|
||||||
|
msg['From'] = sender_email
|
||||||
|
msg['To'] = receiver_email
|
||||||
|
|
||||||
|
context = ssl.create_default_context()
|
||||||
|
with smtplib.SMTP(smtp_server, port) as server:
|
||||||
|
server.ehlo() # Can be omitted
|
||||||
|
server.starttls(context=context)
|
||||||
|
server.ehlo() # Can be omitted
|
||||||
|
server.login(sender_username, password)
|
||||||
|
server.sendmail(sender_email, receiver_email, msg.as_string())
|
||||||
|
|
||||||
|
print("Mail sent.")
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user