-
Notifications
You must be signed in to change notification settings - Fork 0
/
Previsto_Power_Monitor_Gen.py
executable file
·100 lines (91 loc) · 5.43 KB
/
Previsto_Power_Monitor_Gen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python3
# Previsto Power Monitor Monitor Gen Version 1.0 - First Release
import RPi.GPIO as GPIO
import time
from datetime import datetime
import http.client
import urllib
import requests
class PrevistoPowerMonitor(object): # The class for the main power monitor program
def __init__(self, pushover_token, pushover_user, power_off_duration_alert_time, power_off_reminder_time):
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
# Set up the GPIO ports
self.power_fail = 24
GPIO.setup(self.power_fail, GPIO.IN, pull_up_down=GPIO.PUD_UP)
self.power_on = False
self.power_off_duration_alert_time = power_off_duration_alert_time * 3600
self.power_off_reminder_time = power_off_reminder_time * 3600
self.long_duration_printed = False
self.power_off_duration = 0
# Set up pushover
self.pushover_token = pushover_token
self.pushover_user = pushover_user
def print_status(self, print_message):
today = datetime.now()
print(print_message + today.strftime('%A %d %B %Y @ %H:%M:%S'))
def send_pushover_message(self, token, user, pushed_message, alert_sound):
conn = http.client.HTTPSConnection("api.pushover.net:443")
conn.request("POST", "/1/messages.json",
urllib.parse.urlencode({
"token": token,
"user": user,
"html": "1",
"title": "Previsto",
"message": pushed_message,
"sound": alert_sound,
}), { "Content-type": "application/x-www-form-urlencoded" })
def shutdown_cleanup(self):
GPIO.cleanup()
self.print_status("Previsto Power Monitor Stopped on ")
def run(self):
startup_state = GPIO.input(self.power_fail)
if startup_state == True:
self.print_status("Previsto Power Monitor Started with Power Off on ")
startup_message = "Previsto Power Monitor Started with Power Off"
self.power_on = False
power_off_start = time.time()
else:
self.print_status("Previsto Power Monitor Started with Power On on ")
startup_message = "Previsto Power Monitor Started with Power On"
self.power_on = True
power_on_start = time.time()
self.send_pushover_message(self.pushover_token, self.pushover_user, startup_message, "updown")
try:
while True: # Run Power Monitor in continuous loop
if GPIO.input(self.power_fail) == True: # Check if the power has failed
if self.power_on == True: # Send pushover message if the previous power state was on
self.print_status("Previsto Power Failed on ")
self.send_pushover_message(self.pushover_token, self.pushover_user, "Previsto Power Failed", "updown")
self.power_on = False
power_off_start = time.time()
self.power_off_duration = time.time() - power_off_start
if self.power_off_duration > self.power_off_duration_alert_time:
if self.long_duration_printed == False:
long_power_failure_message = "Previsto Power has been off for " + str(round(self.power_off_duration/3600, 1)) + " hours"
self.print_status(long_power_failure_message + " on ")
self.send_pushover_message(self.pushover_token, self.pushover_user, long_power_failure_message, "updown")
self.long_duration_printed = True
self.previous_power_off_reminder_time = time.time()
else:
if (time.time() - self.previous_power_off_reminder_time >= self.power_off_reminder_time):
long_power_failure_message = "Previsto Power has been off for " + str(round(self.power_off_duration/3600, 1)) + " hours"
self.print_status(long_power_failure_message + " on ")
self.send_pushover_message(self.pushover_token, self.pushover_user, long_power_failure_message, "updown")
self.previous_power_off_reminder_time = time.time()
else:
if self.power_on == False:# Send pushover message if the previous power state was off
power_on_message = "Previsto Power Restored after being off for " +str(round(self.power_off_duration/60, 0)) + " minutes"
self.print_status(power_on_message + " on ")
self.send_pushover_message(self.pushover_token, self.pushover_user, power_on_message, "magic")
self.power_on = True
power_on_start = time.time()
self.long_duration_printed = False
time.sleep(5)
except KeyboardInterrupt: # Shutdown on ctrl C
# Shutdown main program
self.shutdown_cleanup()
if __name__ == '__main__': # This is where to overall code kicks off
monitor = PrevistoPowerMonitor(pushover_token = "<your pushover token>", pushover_user = "<your pushover user>",
power_off_duration_alert_time = 10, power_off_reminder_time = 1)
monitor.run()