-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathev3auto.py
More file actions
122 lines (107 loc) · 4.03 KB
/
ev3auto.py
File metadata and controls
122 lines (107 loc) · 4.03 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#! /usr/bin/env python
#
# This script depends on ev3pi.py to be running, and if it is then it will automatically
# arm and disarm the security system based on a daily schedule.
#
# The basic assumption behind this script is you want the security system on at night when
# you are asleep and it shut offs when you are awake
#
# If on vacation or schedule is off, then don't make any changes.
# When on vacation, system should be in leave mode, but this script won't make any changes.
#
# The day of week is: Sunday, Monday, ... Friday, Saturday (0-6)
# To simplify the logic, arm time must be after earliest arm time (22:00:00)
#
# run this script as a crontab every 15 moinutes - arm/disarm doesn't need to be super accurate
# sudo crontab -e
import socket
import sys
import time
import datetime
import string
import re
import select
import sqlite3
import smtplib
class Ev3auto:
def __init__(self):
self.db_file = '/var/www/db/security.db'
self.db_con = sqlite3.connect(self.db_file)
self.db_cmd = self.db_con.cursor()
self.weekday = {0 : 'Monday', 1 : 'Tuesday', 2 : 'Wednesday', 3 : 'Thursday', 4 : 'Friday', 5 : 'Saturday', 6 : 'Sunday'}
#
# CHANGE THE ITEMS IN THE ANGLE BRACKETS BELOW - DO NOT PUBLISH
#
self.gmail_password = <your gmail password> # your gmail password
self.gmail_address = <your gmail address> # your gmail email address
self.cellphone = <your cell#>@<carrier sms> # your carrier's way to send texts via email to your phone
#
# CHANGE THE ITEMS ABOVE - DO NOT PUBLISH
#
def sendAlert(self):
subject = "Security system not armed"
message = 'Subject: %s' % (subject)
mail = smtplib.SMTP("smtp.gmail.com",587)
mail.ehlo()
mail.starttls()
mail.login(self.gmail_address, self.gmail_password)
mail.sendmail("cell", self.cellphone, message)
mail.close()
if __name__ == '__main__':
try:
e = Ev3auto()
# e.dbInit() is performed by ev3pi.py
# read vacation from database, but don't make any changes if vacation == yes
v = e.db_cmd.execute('SELECT * FROM status WHERE name = "vacation";')
for row in v:
vacation = row[3]
# read schedule from database, but don't make any changes if vacation == yes
s = e.db_cmd.execute('SELECT * FROM status WHERE name = "schedule";')
for row in s:
schedule = row[3]
# read system from database
v = e.db_cmd.execute('SELECT * FROM status WHERE name = "system";')
for row in v:
system = row[3]
# vacation must be yes or no - don't use else
if vacation == 'yes':
if system == 'sleep' or system == 'disarm':
# send alert that home security is not set
e.sendAlert()
# only make changes if schedule is on
if schedule == "on":
if vacation == 'no':
d = datetime.datetime.today().weekday()
day = e.weekday[d]
# read schedule from database
t = e.db_cmd.execute('SELECT * from schedule where day = "' + day + '";')
for row in t:
armTime = row[3]
disarmTime = row[4]
current_time = time.strftime("%H:%M:%S")
ct = time.strptime(current_time, "%H:%M:%S")
at = time.strptime(armTime, "%H:%M:%S")
dt = time.strptime(disarmTime, "%H:%M:%S")
eat = time.strptime("22:00:00", "%H:%M:%S")
mt = time.strptime("23:59:59", "%H:%M:%S")
zt = time.strptime("00:00:00", "%H:%M:%S")
# Should it arm between arm time and midnight?
if at >= eat and at <= mt and ct >= eat and ct <= mt:
if system == 'disarm':
e.db_cmd.execute('UPDATE status SET value = "sleep" WHERE name = "command";')
# Should it arm between midnight and disarm time?
if at >= zt and at <= dt and ct >= zt and ct <= dt:
if system == 'disarm':
e.db_cmd.execute('UPDATE status SET value = "sleep" WHERE name = "command";')
# Should it disarm between disarm and arm time
if at == zt:
if ct >= dt and ct < mt:
if system == 'armed':
e.db_cmd.execute('UPDATE status SET value = "disarm" WHERE name = "command";')
else:
if ct >= dt and ct < mt:
if system == 'armed':
e.db_cmd.execute('UPDATE status SET value = "disarm" WHERE name = "command";')
finally:
e.db_con.commit();
e.db_con.close()