From ae459a06f58e0bfc1cdf5506ce481990362b56e4 Mon Sep 17 00:00:00 2001 From: pocketjawa Date: Mon, 16 Apr 2018 01:20:13 -0400 Subject: [PATCH] Message owner when UPS goes on and off battery. --- pocketbot.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/pocketbot.py b/pocketbot.py index 0831e13..b6d9154 100644 --- a/pocketbot.py +++ b/pocketbot.py @@ -45,9 +45,32 @@ client = discord.Client() #Subroutine to get UPS Status @asyncio.coroutine def ups_check(): + yield from client.wait_until_ready() while not client.is_closed: yield from asyncio.create_subprocess_exec("/bin/bash", cwd + "/ups_status.sh") - yield from asyncio.sleep(60) + yield from asyncio.sleep(5) + #Make temp status file if it doesn't exist + if not os.path.isfile(cwd + "/tmp/ups_status.txt"): + ups_temp = open(cwd + "/tmp/ups_status.txt", "w") + ups_temp.write("ONLINE") + ups_temp.close() + #Message and set status if on battery + if 'ONBATT' in open('/tmp/ups_status.txt').read() and 'ONLINE' in open(cwd + '/tmp/ups_status.txt').read(): + yield from client.send_message(bot_channel_id, "<@"+owner_id.id+"> UPS is on battery!") + ups_status = open("/tmp/ups_status.txt","r") + yield from client.send_message(bot_channel_id, ups_status.read()) + ups_temp = open(cwd + "/tmp/ups_status.txt", "w") + ups_temp.write("ONBATT") + ups_temp.close() + #Message and set status back to normal when power comes back on + elif 'ONLINE' in open('/tmp/ups_status.txt').read() and 'ONBATT' in open(cwd + '/tmp/ups_status.txt').read(): + yield from client.send_message(bot_channel_id, "<@"+owner_id.id+"> UPS is back online!") + ups_status = open("/tmp/ups_status.txt","r") + yield from client.send_message(bot_channel_id, ups_status.read()) + ups_temp = open(cwd + "/tmp/ups_status.txt", "w") + ups_temp.write("ONLINE") + ups_temp.close() + yield from asyncio.sleep(55) @client.event