-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroutes.py
More file actions
156 lines (138 loc) · 4.47 KB
/
routes.py
File metadata and controls
156 lines (138 loc) · 4.47 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from flask import Flask, render_template, flash, request, jsonify
from flask_mail import Mail, Message
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
import sys
import time
import zmq
app = Flask(__name__)
app.config.from_pyfile('webHomeControl.cfg')
app.config.update(
#EMAIL SETTINGS
MAIL_SERVER='smtp.gmail.com',
MAIL_PORT=465,
MAIL_USE_SSL=True,
MAIL_USERNAME = 'webhomecontrol@gmail.com',
MAIL_PASSWORD = 'towsongraduateproject'
)
mail=Mail(app)
# set the secret key. keep this really secret:
app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'
db = SQLAlchemy(app)
class Status(db.Model):
# Setting the table name and
# creating columns for various fields
__tablename__ = 'status'
id = db.Column(db.Integer, primary_key = True)
hardware=db.Column(db.String, index = True, unique = False)
time=db.Column(db.DateTime, index = True, unique = False)
status=db.Column(db.String, index = True, unique = False)
__mapper_args__ = {
'polymorphic_on':hardware,
'polymorphic_identity':'none'
}
def __init__(self, time, status):
# Initializes the fields with entered data
self.time = datetime.now()
self.status = status
class DoorStatus(Status):
__mapper_args__ = {
'polymorphic_identity':'door'
}
class MotionStatus(Status):
__mapper_args__ = {
'polymorphic_identity':'motionSensor'
}
class MotorStatus(Status):
duration = db.Column(db.Integer, index = False, unique = False)
def __init__(self, time, status, duration):
super(MotorStatus, self).__init__(hardware, time, status)
self.duration = duration
# The default route for the app
@app.route('/')
def home():
return render_template('home.html')
@app.route('/doorSensor/status/<status>', methods = ['POST'])
def doorStatus(status):
msg = Message(
'WebHomeControl',
sender='webhomecontrol@gmail.com',
recipients=
['marbaugh@gmail.com'])
if request.method == 'POST':
insert_door = None
if status == 'opened':
insert_door = DoorStatus(datetime.now(), status)
msg.body = "Door Opened"
elif status == 'closed':
insert_door = DoorStatus(datetime.now(), status)
msg.body = "Door Closed"
else:
pass
db.session.add(insert_door)
db.session.commit()
mail.send(msg)
return 'success'
@app.route('/motionSensor/status/<status>', methods = ['POST'])
def motionStatus(status):
msg = Message(
'WebHomeControl',
sender='webhomecontrol@gmail.com',
recipients=['marbaugh@gmail.com'])
if request.method == 'POST':
insert_motion = None
if status == 'motion':
insert_motion = MotionStatus(datetime.now(), status)
msg.body = "Motion Detected"
else:
pass
db.session.add(insert_motion)
db.session.commit()
mail.send(msg)
return 'success'
@app.route('/doorSensor/history')
def doorSensor_history():
aadata = dict()
rowData= list()
results = DoorStatus.query.order_by(db.desc(DoorStatus.time)).all()
for row in results:
rowData.append([row.time, row.status])
aadata['aaData'] = rowData
return jsonify(aadata)
@app.route('/motionSensor/history')
def motionSensor_history():
aadata = dict()
rowData= list()
results = MotionStatus.query.order_by(db.desc(MotionStatus.time)).all()
for row in results:
rowData.append([row.time, row.status])
aadata['aaData'] = rowData
return jsonify(aadata)
@app.route('/motor/forward', methods = ['POST'])
def motor_forward():
port = "5556"
topic = "motor"
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:{0}".format(port))
time.sleep(.5)
messagedata = 'forward'
print "{0} {1}".format(topic, messagedata)
socket.send("{0} {1}".format(topic, messagedata))
print "after socket"
return 'success'
@app.route('/motor/reverse', methods = ['POST'])
def motor_reverse():
port = "5556"
topic = "motor"
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:{0}".format(port))
time.sleep(.5)
messagedata = 'reverse'
print "{0} {1}".format(topic, messagedata)
socket.send("{0} {1}".format(topic, messagedata))
print "after socket"
return 'success'
if __name__ == '__main__':
app.run(host='192.168.3.107', debug=True)