/
server.py
executable file
·319 lines (267 loc) · 10.8 KB
/
server.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/python
# all the imports
import sqlite3
import os.path
from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash
from homevolution.db import init_db
from flask_cors import CORS
from contextlib import closing
from homevolution.device import action, get_devices, list_devices
import homevolution.zoneminder as zoneminder
import homevolution.kodi as kodi
import homevolution.schedule as schedule
#import homevolution.schedules as schedules
#import homevolution.kodiapi as kodiapi
#import homevolution.modules as modules
# configuration
DATABASE = 'homevolution.db'
DEBUG = True
SECRET_KEY = 'DeeXPDogs5svAQ7giGwaGgpYA154jFfxoqRw'
USERNAME = 'admin'
PASSWORD = 'admin'
# create our little application :)
app = Flask(__name__, static_url_path='')
app.config.from_object(__name__)
def connect_db():
return sqlite3.connect(app.config['DATABASE'])
def init_db():
with closing(connect_db()) as db:
with app.open_resource('database.sql', mode='r') as f:
db.cursor().executescript(f.read())
db.commit()
"""
Check to see if there is a database else create on
"""
if not os.path.exists("homevolution.db"):
init_db()
@app.before_request
def before_request():
g.db = connect_db()
@app.teardown_request
def teardown_request(exception):
db = getattr(g, 'db', None)
if db is not None:
db.close()
@app.route('/')
def index():
if not session.get('logged_in'):
return render_template('login.html')
else:
templateData={}
return render_template('index.html', **templateData)
@app.route('/kodi')
def kodis():
# Pass the template data into the template dashboard.html and return it to the user
if not session.get('logged_in'):
flash('Need to login')
return render_template('login.html')
else:
updated_nowplaying = kodi.nowplaying(True)
templateData = {
'kodi' : kodi.list(),
'now_playing' : updated_nowplaying['now_playing'],
'now_playing_image' : updated_nowplaying['now_playing_image'],
}
return render_template('kodi.html', **templateData)
@app.route('/schedules')
def schedules():
# Pass the template data into the template dashboard.html and return it to the user
update_schedule = schedule.gettime("sname")
update_run = schedule.getrun("sname")
#SCHEDULE_TIME = config['SCHEDULE']
templateData = {
'schedule' : schedule.SCHEDULE,
'schedules' : update_schedule,
'schedulerun' : update_run,
}
return render_template('schedules.html', **templateData)
@app.route('/devices')
def show_devices():
if not session.get('logged_in'):
flash('Need to login')
return render_template('login.html')
else:
get_device = get_devices()
for result in get_device.itervalues():
if "error" in str(result):
error = get_devices()
else:
error = None
templateData={
'error' : error,
'devices' : get_devices()
}
return render_template('devices.html', **templateData)
@app.route('/<action>/<module>',methods=['POST'])
def add(action, module):
if not session.get('logged_in'):
abort(401)
if action == "add":
if module == "device":
g.db.execute('insert into slaves (node, key) values (?, ?)',
[request.form['node'], request.form['key']])
g.db.commit()
flash('New device was successfully added')
if module == "kodi":
g.db.execute('insert into kodi (name, port) values (?,?)',
[request.form['name'], request.form['port']])
g.db.commit()
flash('New server was successfully added')
if module == "zoneminder":
g.db.execute('insert into zoneminder (name, url, port) values (?, ?, ?)',
[request.form['name'], request.form['url'], request.form['port']])
g.db.commit()
flash('New server was successfully added')
if module == "schedule":
print request.form['name'],request.form['month'],request.form['dayofweek'],request.form['hour'],request.form['minute']
g.db.execute('insert into schedule (name, month, dayofweek, hour, minute) values (?, ?, ?, ?, ?)', [request.form['name'], request.form['month'], request.form['dayofweek'], request.form['hour'], request.form['minute']])
g.db.commit()
flash('New schedule was successfully added')
if module == "schedules":
print request.form['name'],request.form['host'],request.form['device'],request.form['action']
g.db.execute('insert into schedules (name, host, device, action) values (?, ?, ?, ?)', [request.form['name'], request.form['host'], request.form['device'], request.form['action']])
g.db.commit()
flash('New schedule was successfully added')
elif action == "del":
if module == "device":
g.db.execute('delete from slaves where node=?',
[request.form['node']])
g.db.commit()
flash(request.form['node']+' was deleted')
if module == "kodi":
g.db.execute('delete from kodi where name=?',
[request.form['name']])
g.db.commit()
flash(request.form['name']+' was deleted')
if module == "zoneminder":
g.db.execute('delete from zoneminder where name=?',
[request.form['name']])
g.db.commit()
flash(request.form['name']+' was deleted')
if module == "schedule":
g.db.execute('delete from schedule where name=?',
[request.form['name']])
g.db.commit()
flash(request.form['name']+' was deleted')
if module == "schedules":
g.db.execute('delete from schedules where name=?',
[request.form['name']])
g.db.commit()
flash(request.form['name']+' was deleted')
return redirect(url_for('settings'))
@app.route('/dashboard')
def dashboards():
templateData={ }
return render_template('dashboard.html', **templateData)
#@app.route('/add/modules', methods=['POST'])
#def add_module():
# if not session.get('logged_in'):
# abort(401)
# g.db.execute('insert into modules (name, url, enabled) values (?, ?, ?)',
# [request.form['name'], request.form['url'], request.form['enabled']])
# g.db.commit()
# flash('New module was successfully added')
# return redirect(url_for('settings'))
@app.route('/show')
def show_entries():
cur = g.db.execute('select title, text from entries order by id desc')
entries = [dict(title=row[0], text=row[1]) for row in cur.fetchall()]
return render_template('show_entries.html', entries=entries)
# Settings Stuff add edit and delete
@app.route('/add', methods=['POST'])
def add_entry():
if not session.get('logged_in'):
abort(401)
g.db.execute('insert into slaves (node, key) values (?, ?)',
[request.form['title'], request.form['text']])
g.db.commit()
flash('New entry was successfully posted')
return redirect(url_for('show_entries'))
@app.route('/cameras')
def cameras():
# Pass the template data into the template dashboard.html and return it to the user
if not session.get('logged_in'):
flash('Need to login')
return render_template('login.html')
else:
if "error" in str(zoneminder.get_cameras()):
error = zoneminder.get_cameras()
else:
error = None
templateData = {
'error' : error,
'zmhost' : zoneminder.ZMHOST,
'zoneminder' : zoneminder.get_cameras(),
}
return render_template('cameras.html', **templateData)
@app.route('/settings',methods=['GET','POST'])
def settings():
# Pass the template data into the template dashboard.html and return it to the user
if not session.get('logged_in'):
flash('Need to login')
return render_template('login.html')
else:
templateData = {
'nodes' : list_devices(),
'kodi' : kodi.list(),
'zoneminder' : zoneminder.list(),
'devices' : get_devices(),
'schedule' : schedule.list(),
'schedules' : schedule.lists()
}
return render_template('settings.html', **templateData)
@app.route('/login', methods=['GET', 'POST'])
def login():
error = None
if request.method == 'POST':
if request.form['username'] != app.config['USERNAME']:
error = 'Invalid username'
elif request.form['password'] != app.config['PASSWORD']:
error = 'Invalid password'
else:
session['logged_in'] = True
flash('You were logged in')
return redirect(url_for('index'))
return render_template('login.html', error=error)
@app.route('/logout')
def logout():
session.pop('logged_in', None)
flash('You were logged out')
return redirect(url_for('index'))
#API stuff
@app.route('/api/<plugin>/<action>', methods = ['GET'])
def service(plugin, action):
if plugin == "kodi":
if action == "options":
return jsonify(KODI_API_OPT)
if action == "playing":
updated_nowplaying = thing(KODI)
return jsonify( { 'now_playing': updated_nowplaying['now_playing'] } )
if action == "play":
return "play"
if action == "stop":
return "stop"
if action == "rewind":
return "rewind"
if action == "begining":
return "begining"
if action == "fastforward":
return "fastforward"
if action == "end":
return "end"
if action == "volup":
return "volup"
if action == "voldwn":
return "voldwn"
if action == "info":
return "info"
else:
return abort(404)
else:
return abort(404)
if __name__ == '__main__':
app.run(
debug=True,
host="0.0.0.0",
port=int("80")
)