-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
1338 lines (1172 loc) · 50.2 KB
/
app.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from flask import Flask, request, render_template, make_response, redirect, jsonify, session, abort
from flask_wtf import FlaskForm
from flask_bcrypt import Bcrypt # Re-enabling bcrypt ror Raspberry Pi 6/24
from wtforms import StringField, PasswordField, IntegerField, BooleanField, HiddenField, Form, FormField, FieldList, TextAreaField, SelectField, SelectMultipleField
from wtforms.fields.simple import SubmitField
from wtforms.validators import InputRequired, NumberRange
import os
from common import *
from gzlogging import *
import redis
import secrets
"""
Globals
"""
app = Flask(__name__)
bcrypt = Bcrypt(app) # Re-enabling bcrypt ror Raspberry Pi 6/24
app.config['SECRET_KEY'] = 'ADD_YOUR_SECRET_HERE'
settings = read_settings()
cmdsts = redis.Redis()
INPIN_TYPES = ['limitsensoropen', 'limitsensorclosed']
OUTPIN_TYPES = ['doorbutton']
temp_door = {}
"""
Local Class Definitions
"""
class NotifyServiceForm(FlaskForm):
sname = StringField('Name')
id = HiddenField('ID')
ntype = HiddenField('Notify Type')
class EmailForm(NotifyServiceForm):
to_email = StringField('To E-mail', validators=[InputRequired()])
from_email = StringField('From E-mail', validators=[InputRequired()])
username = StringField('Username', validators=[InputRequired()])
password = PasswordField('Password')
smtpserver = StringField('SMTP Server', validators=[InputRequired()])
smtpport = IntegerField('SMTP Port', validators=[InputRequired(), NumberRange(min=1, max=65535)])
tls = BooleanField('TLS')
class IftttForm(NotifyServiceForm):
apikey = StringField('API Key', validators=[InputRequired()])
iftttevent = StringField('IFTTT Event', validators=[InputRequired()])
class PushbulletForm(NotifyServiceForm):
apikey = StringField('API Key', validators=[InputRequired()])
class PushoverForm(NotifyServiceForm):
apikey = StringField('API Key', validators=[InputRequired()])
userkeys = StringField('User Key(s)', validators=[InputRequired()])
class ProtoNotifyForm(NotifyServiceForm):
pass
class InPinFieldForm(Form):
inpin = SelectField('Sensor', choices=[('limitsensoropen', 'Limit Sensor - OPEN'), ('limitsensorclosed', 'Limit Sensor - CLOSED')])
gpio_pin = SelectField('GPIO Pin', choices=[0,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])
class OutPinFieldForm(Form):
outpin = SelectField('Output', choices=[('doorbutton', 'Door Button')])
gpio_pin = SelectField('GPIO Pin', choices=[0,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])
class DoorForm(FlaskForm):
dname = StringField('Door Name')
id = HiddenField('ID')
inpins = FieldList(FormField(InPinFieldForm), min_entries=0)
outpins = FieldList(FormField(OutPinFieldForm), min_entries=0)
triggerlevel = BooleanField('Button/Relay Trigger Level High')
sensorlevel = BooleanField('Sensor Trigger Level High')
class EventForm(FlaskForm):
id = HiddenField('ID')
event_type = SelectField('Event Type', choices=[('on_open', 'On Open'), ('on_open_time', 'Open for Time'), ('on_open_time_remind', 'Open Periodic Reminder'), ('on_close', 'On Close'), ('on_closed_time', 'Closed for Time'), ('closed_time_remind', 'Closed Periodic Reminder')])
title = StringField('Title', validators=[InputRequired()])
message = TextAreaField('Message', validators=[InputRequired()], render_kw={"rows": 3})
sensor = SelectField('Watch Sensor', choices=[('limitsensoropen', 'Limit Sensor - OPEN'), ('limitsensorclosed', 'Limit Sensor - CLOSED')])
sensorlevel = SelectField('Sensor Trigger Level', choices=[('True', 'HIGH'), ('False', 'LOW')])
time = IntegerField('Time Delay Before Notification(m)', validators=[NumberRange(min=0, max=1440)])
starttimer = BooleanField('Start the Timer') # True for simple time events
timerendevent = SelectField('Timer End on Event', choices=[('', 'None'), ('on_open', 'On Open'), ('on_open_time', 'Open for Time'), ('on_open_time_remind', 'Open Periodic Reminder'), ('on_close', 'On Close'), ('on_closed_time', 'Closed for Time'), ('closed_time_remind', 'Closed Periodic Reminder')])
remindevent = SelectField('Reminder on Event', choices=[('', 'None'), ('on_open', 'On Open'), ('on_open_time', 'Open for Time'), ('on_open_time_remind', 'Open Periodic Reminder'), ('on_close', 'On Close'), ('on_closed_time', 'Closed for Time'), ('closed_time_remind', 'Closed Periodic Reminder')])
logtype = SelectField('Log Type (Displayed in Log)', choices=['OPENED', 'OPEN', 'REMIND', 'CLOSED'])
notifylist = SelectMultipleField('Select Notification Services', choices=[], validate_choice=False)
"""
App Route Functions Begin
"""
@app.route('/login', methods=['POST', 'GET'])
def loginpage():
global settings
# Create Alert Structure for Alert Notification
alert = {
'type' : '',
'text' : ''
}
if request.method == 'POST':
if('password' in request.form):
password = request.form['password']
secrets = read_secrets()
for secret in secrets:
if bcrypt.check_password_hash(secret['pc_hash'], password):
session.permanent = True
session['active'] = secret['username']
event = f'{secret["username"]} Logged In.'
write_log(event, logtype='LOGIN')
return redirect('/')
alert['type'] = 'error'
alert['text'] = 'Incorrect Passcode.'
event = f'Login Denied: Passcode Incorrect.'
write_log(event, logtype='DENIED')
elif('logout' in request.form):
username = session['active']
session.pop('active', None) # Logout
alert['type'] = 'success'
alert['text'] = f'User {username} logged out.'
event = f'{username} Logged In.'
write_log(event, logtype='LOGOUT')
return render_template('login.html', alert=alert, settings=settings)
@app.route('/', methods=['POST','GET'])
def index():
global settings
# Check if password protected AND not logged in
if (settings['misc']['password'] == True) and ('active' not in session):
return redirect('/login')
button_list, errorlevel = build_button_list() # This structure is used to build the initial buttons to render on the dash
# Create Alert Structure for Alert Notification
alert = {
'type' : '',
'text' : ''
}
if(errorlevel > 0):
alert['type'] = 'error'
alert['text'] = 'Unable to find door definitions in redis, is "control.py" running?'
write_log(alert['text'], logtype='ERROR')
return render_template('settings.html', alert=alert, settings=settings)
if (request.method == 'POST'):
response = request.form
if('listorder' in response):
if(response['listorder'] == 'topdown'):
settings['misc']['listorder'] = 'topdown'
else:
settings['misc']['listorder'] = 'bottomup'
write_settings(settings)
if('24htime' in response):
if(response['24htime'] == 'true'):
settings['misc']['24htime'] = True
else:
settings['misc']['24htime'] = False
write_settings(settings)
return render_template('index.html', settings=settings, button_list=button_list)
@app.route('/test', methods=['POST','GET'])
def testpage():
global settings
# Check if password protected AND not logged in
if (settings['misc']['password'] == True) and ('active' not in session):
return redirect('/login')
button_list, errorlevel = build_button_list() # This structure is used to build the initial buttons to render on the dash
# Create Alert Structure for Alert Notification
alert = {
'type' : '',
'text' : ''
}
if(errorlevel > 0):
alert['type'] = 'error'
alert['text'] = 'Unable to find door definitions in redis, is "control.py" running?'
write_log(alert['text'], logtype='ERROR')
return render_template('settings.html', alert=alert, settings=settings)
if (request.method == 'POST'):
response = request.form
if(('keyname' in response) and ('button' in response)):
cmdsts.hset(response['keyname'], response['button'], 1)
if(('keyname' in response) and ('status' in response)):
tempval = 1 if int(cmdsts.hget(response['keyname'], response['status'])) == 0 else 0
cmdsts.hset(response['keyname'], response['status'], tempval)
return jsonify({ 'result' : 'success' })
if (request.method == 'GET'):
return render_template('testpage.html', settings=settings, button_list=button_list)
return redirect('/')
@app.route('/status')
def doorstatus():
global settings
# Check if password protected AND not logged in
if (settings['misc']['password'] == True) and ('active' not in session):
return redirect('/login')
button_list, errorlevel = build_button_list() # This structure is used to build the initial buttons to render on the dash
# Create Alert Structure for Alert Notification
alert = {
'type' : '',
'text' : ''
}
if(errorlevel > 0):
alert['type'] = 'error'
alert['text'] = 'Unable to find door definitions in redis, is "control.py" running?'
write_log(alert['text'], logtype='ERROR')
return render_template('settings.html', alert=alert, settings=settings)
return jsonify(button_list)
@app.route('/button', methods=['POST'])
def button():
global settings
# Check if password protected AND not logged in
if (settings['misc']['password'] == True) and ('active' not in session):
return redirect('/login')
if (request.method == 'POST'):
global cmdsts
response = request.form
if(('keyname' in response) and ('button' in response)):
cmdsts.hset(response['keyname'], response['button'], 1)
return jsonify({ 'result' : 'success' })
return jsonify({ 'result' : 'failed' })
@app.route('/shortlog')
def shortlog():
global settings
# Check if password protected AND not logged in
if (settings['misc']['password'] == True) and ('active' not in session):
return redirect('/login')
door_history, events = read_log(10, twentyfourhtime=settings['misc']['24htime'])
return render_template('shortlog.html', door_history=door_history, events=events, settings=settings)
@app.route('/history', methods=['POST','GET'])
def history():
global settings
# Check if password protected AND not logged in
if (settings['misc']['password'] == True) and ('active' not in session):
return redirect('/login')
if (request.method == 'POST'):
response = request.form
if('listorder' in response):
if(response['listorder'] == 'topdown'):
settings['misc']['listorder'] = 'topdown'
else:
settings['misc']['listorder'] = 'bottomup'
write_settings(settings)
if('24htime' in response):
if(response['24htime'] == 'true'):
settings['misc']['24htime'] = True
else:
settings['misc']['24htime'] = False
write_settings(settings)
door_history, events = read_log(twentyfourhtime=settings['misc']['24htime'])
return render_template('history.html', door_history=door_history, events=events, pagetheme=settings['misc']['theme'], settings=settings)
@app.route('/settings', methods=['POST','GET'])
def settings_base(action=None):
global settings
# Check if password protected AND not logged in
if (settings['misc']['password'] == True) and ('active' not in session):
return redirect('/login')
global temp_door
settings = read_settings()
# Create Alert Structure for Alert Notification
alert = {
'type' : '',
'text' : ''
}
# Update the system theme
if('theme' in request.form):
for theme in settings['misc']['themelist']:
if theme['name'] == request.form['theme']:
settings['misc']['theme'] = theme['filename']
write_settings(settings)
alert['type'] = 'success'
alert['text'] = 'Theme updated to ' + theme['name'] + "."
# Delete a notification service
if('del_service' in request.form):
delete_service_id = request.form['del_service']
alert['type'] = 'error'
alert['text'] = 'Could not delete service. Service ID not found. ' + delete_service_id + "."
for index in range(len(settings['notify'])):
if(settings['notify'][index]['id'] == delete_service_id):
service_name = settings['notify'][index]['name']
# If referenced in notify events of doors, delete from those
for door in settings['doors']:
for notify_event in door['notify_events']:
if delete_service_id in door['notify_events'][notify_event]['notifylist']:
door['notify_events'][notify_event]['notifylist'].remove(delete_service_id)
# Delete notification service from structure
del settings['notify'][index]
write_settings(settings)
cmdsts.set('settings_update', 'true')
alert['type'] = 'success'
alert['text'] = 'Deleted "' + service_name + '" notification service.'
return render_template('settings.html', alert=alert, settings=settings)
# Notify Service Settings
if('edit_service' in request.form):
notify_forms_list = build_notify_forms()
notify_form_id = request.form['edit_service']
if(notify_form_id in notify_forms_list):
return render_template('settings_notify_service.html', alert=alert, settings=settings, form=notify_forms_list[notify_form_id])
else:
alert['type'] = 'error'
alert['text'] = 'Edit Notification Service Failed. Unrecognized service ID: ' + request.form['edit_service']
return render_template('settings.html', alert=alert, settings=settings)
if('add_service' in request.form):
if(request.form['add_service'] == 'email'):
notify_service_form = EmailForm()
notify_service_form.ntype.data = 'email'
notify_service_form.id.data = '' # Indicate that this is a new service
elif(request.form['add_service'] == 'ifttt'):
notify_service_form = IftttForm()
notify_service_form.ntype.data = 'ifttt'
notify_service_form.id.data = '' # Indicate that this is a new service
elif(request.form['add_service'] == 'pushbullet'):
notify_service_form = PushbulletForm()
notify_service_form.ntype.data = 'pushbullet'
notify_service_form.id.data = '' # Indicate that this is a new service
elif(request.form['add_service'] == 'pushover'):
notify_service_form = PushoverForm()
notify_service_form.ntype.data = 'pushover'
notify_service_form.id.data = '' # Indicate that this is a new service
elif(request.form['add_service'] == 'proto'):
notify_service_form = ProtoNotifyForm()
notify_service_form.ntype.data = 'proto'
notify_service_form.id.data = '' # Indicate that this is a new service
elif(request.form['add_service'] == 'none'):
return render_template('settings.html', alert=alert, settings=settings)
else:
notify_service_form = NotifyServiceForm()
notify_service_form.ntype.data = 'error'
alert['type'] = 'warning'
alert['text'] = 'Add service failed. Unrecognized service type: ' + request.form['add_service']
return render_template('settings_notify_service.html', alert=alert, settings=settings, form=notify_service_form)
# Update the public / local URL
if('public_url' in request.form):
settings['misc']['public_url'] = request.form['public_url']
write_settings(settings)
cmdsts.set('settings_update', 'true')
newurl = settings['misc']['public_url'] if settings['misc']['public_url'] != '' else 'BLANK'
alert['type'] = 'success'
alert['text'] = 'Public / Local URL updated to ' + newurl + '.'
if('del_door' in request.form):
delete_door_id = request.form['del_door']
alert['type'] = 'error'
alert['text'] = 'Could not delete door. Door ID not found. ' + delete_door_id + "."
for index in range(len(settings['doors'])):
if(settings['doors'][index]['id'] == delete_door_id):
door_name = settings['doors'][index]['name']
# Delete Door from settings
del settings['doors'][index]
write_settings(settings)
cmdsts.set('settings_update', 'true')
alert['type'] = 'success'
alert['text'] = 'Deleted "' + door_name + '" profile.'
return render_template('settings.html', alert=alert, settings=settings)
if('add_door' in request.form):
temp_door = default_door_obj_data()
temp_door['id'] = 'new'
door_form = build_door_form(temp_door)
event_list = build_event_list(temp_door)
return render_template('settings_door.html', alert=alert, settings=settings, dform=door_form, elist=event_list)
if('edit_door' in request.form):
door_id = request.form["edit_door"]
for door in settings['doors']:
if door_id == door['id']:
door_form = build_door_form(door)
event_list = build_event_list(door)
return render_template('settings_door.html', alert=alert, settings=settings, dform=door_form, elist=event_list)
return render_template('settings.html', alert=alert, settings=settings)
# Route for Adding / Editing Notification Service
@app.route('/addeditnotify', methods=['POST'])
def addeditnotify(action=None):
global settings
# Check if password protected AND not logged in
if (settings['misc']['password'] == True) and ('active' not in session):
return redirect('/login')
settings = read_settings()
# Create Alert Structure for Alert Notification
alert = {
'type' : '',
'text' : ''
}
if('add_service' in request.form):
print(f'Add Service: {request.form["add_service"]}')
if(request.form['add_service'] == 'email'):
add_notify_form = EmailForm()
add_notify_form.ntype.data = 'email'
add_notify_form.id.data = '' # Indicate that this is a new service
elif(request.form['add_service'] == 'ifttt'):
add_notify_form = IftttForm()
add_notify_form.ntype.data = 'ifttt'
add_notify_form.id.data = '' # Indicate that this is a new service
elif(request.form['add_service'] == 'pushbullet'):
add_notify_form = PushbulletForm()
add_notify_form.ntype.data = 'pushbullet'
add_notify_form.id.data = '' # Indicate that this is a new service
elif(request.form['add_service'] == 'pushover'):
add_notify_form = PushoverForm()
add_notify_form.ntype.data = 'pushover'
add_notify_form.id.data = '' # Indicate that this is a new service
else:
add_notify_form = ProtoNotifyForm()
add_notify_form.ntype.data = 'proto'
add_notify_form.id.data = '' # Indicate that this is a new service
if add_notify_form.validate():
if(add_notify_form.ntype.data == 'email'):
add_notify_service = default_email_notify_obj_data()
add_notify_service['to_email'] = add_notify_form.to_email.data
add_notify_service['from_email'] = add_notify_form.from_email.data
add_notify_service['username'] = add_notify_form.username.data
add_notify_service['password'] = add_notify_form.password.data
add_notify_service['smtpserver'] = add_notify_form.smtpserver.data
add_notify_service['smtpport'] = add_notify_form.smtpport.data
add_notify_service['tls'] = add_notify_form.tls.data
elif(add_notify_form.ntype.data =='ifttt'):
add_notify_service = default_ifttt_notify_obj_data()
add_notify_service['apikey'] = add_notify_form.apikey.data
add_notify_service['iftttevent'] = add_notify_form.iftttevent.data
elif(add_notify_form.ntype.data =='pushover'):
add_notify_service = default_pushover_notify_obj_data()
add_notify_service['apikey'] = add_notify_form.apikey.data
add_notify_service['userkeys'] = add_notify_form.userkeys.data
elif(add_notify_form.ntype.data =='pushbullet'):
add_notify_service = default_pushbullet_notify_obj_data()
add_notify_service['apikey'] = add_notify_form.apikey.data
else:
add_notify_service = default_proto_notify_obj_data()
# Global Settings Apply
#add_notify_service['id'] = add_notify_form.id.data
add_notify_service['name'] = add_notify_form.sname.data
# Write udpate to settings.json
settings['notify'].append(add_notify_service)
write_settings(settings)
cmdsts.set('settings_update', 'true')
alert['type'] = 'success'
alert['text'] = 'Added notification service "' + add_notify_service['name'] + '".'
return render_template('settings.html', alert=alert, settings=settings)
else:
# Something failed, so get the error message and notify user
for fieldName, errorMessages in add_notify_form.errors.items():
for err in errorMessages:
print(f'Error: {fieldName} {err} {errorMessages}')
message = err
alert['type'] = 'warning'
alert['text'] = 'Add notificiation service failed: ' + message
return render_template('settings_notify_service.html', alert=alert, settings=settings, form=add_notify_form)
if('edit_service' in request.form):
# If EDIT Service is requested, check the form and apply changes
notifyforms = {}
notifyforms = build_notify_forms()
for notifyservice in notifyforms:
# Look for selected service ID in current list of forms
if notifyservice == request.form['edit_service']:
# Found the ID, now check ntype and build form
if(notifyforms[notifyservice].ntype.data == 'email'):
edit_notify_form = EmailForm()
edit_notify_form.ntype.data = 'email'
elif(notifyforms[notifyservice].ntype.data == 'ifttt'):
edit_notify_form = IftttForm()
edit_notify_form.ntype.data = 'ifttt'
elif(notifyforms[notifyservice].ntype.data == 'pushbullet'):
edit_notify_form = PushbulletForm()
edit_notify_form.ntype.data = 'pushbullet'
elif(notifyforms[notifyservice].ntype.data == 'pushover'):
edit_notify_form = PushoverForm()
edit_notify_form.ntype.data = 'pushover'
else:
edit_notify_form = ProtoNotifyForm()
edit_notify_form.ntype.data = 'proto'
if edit_notify_form.validate():
for service in settings['notify']:
if service['id'] == edit_notify_form.id.data:
if(edit_notify_form.ntype.data == 'email'):
service['to_email'] = edit_notify_form.to_email.data
service['from_email'] = edit_notify_form.from_email.data
service['username'] = edit_notify_form.username.data
if edit_notify_form.password.data != '':
service['password'] = edit_notify_form.password.data
service['smtpserver'] = edit_notify_form.smtpserver.data
service['smtpport'] = edit_notify_form.smtpport.data
service['tls'] = edit_notify_form.tls.data
elif(edit_notify_form.ntype.data =='ifttt'):
service['apikey'] = edit_notify_form.apikey.data
service['iftttevent'] = edit_notify_form.iftttevent.data
elif(edit_notify_form.ntype.data =='pushover'):
service['apikey'] = edit_notify_form.apikey.data
service['userkeys'] = edit_notify_form.userkeys.data
elif(edit_notify_form.ntype.data =='pushbullet'):
service['apikey'] = edit_notify_form.apikey.data
# Global Settings Apply
service['name'] = edit_notify_form.sname.data
write_settings(settings)
cmdsts.set('settings_update', 'true')
break
alert['type'] = 'success'
alert['text'] = 'Edited notification service "' + edit_notify_form.sname.data + '".'
# Reload notify forms
notifyforms = build_notify_forms()
return render_template('settings.html', alert=alert, settings=settings)
else:
# Something failed, so get the error message and notify user
for fieldName, errorMessages in edit_notify_form.errors.items():
for err in errorMessages:
print(f'Error: {fieldName} {err} {errorMessages}')
message = err
alert['type'] = 'error'
alert['text'] = 'Add notificiation service failed: ' + message
return render_template('settings_notify_service.html', alert=alert, settings=settings, form=edit_notify_form)
return render_template('settings.html', alert=alert, settings=settings)
# Route for Adding / Editing Notification Service
@app.route('/addeditdoor', methods=['POST'])
def addeditdoor(action=None):
global settings
# Check if password protected AND not logged in
if (settings['misc']['password'] == True) and ('active' not in session):
return redirect('/login')
global temp_door
settings = read_settings()
# Create Alert Structure for Alert Notification
alert = {
'type' : '',
'text' : ''
}
if('edit_door' in request.form):
door_id = request.form["edit_door"]
for door in settings['doors']:
if door['id'] == door_id:
edit_door_form = DoorForm()
if(edit_door_form.validate()):
door['name'] = edit_door_form.dname.data
door['triggerlevel'] = edit_door_form.triggerlevel.data
door['sensorlevel'] = edit_door_form.sensorlevel.data
del door['inpins']
door['inpins'] = {}
for field in edit_door_form.inpins:
pin_type = field.inpin.data
door['inpins'][pin_type] = int(field.gpio_pin.data)
del door['outpins']
door['outpins'] = {}
for field in edit_door_form.outpins:
pin_type = field.outpin.data
door['outpins'][pin_type] = int(field.gpio_pin.data)
write_settings(settings)
cmdsts.set('settings_update', 'true')
alert['type'] = 'success'
alert['text'] = 'Edited door "' + door['name'] + '".'
return render_template('settings.html', alert=alert, settings=settings)
else:
# Something failed, so get the error message and notify user
for fieldName, errorMessages in edit_door_form.errors.items():
for err in errorMessages:
print(f'Error: {fieldName} {err} {errorMessages}')
message = err
alert['type'] = 'error'
alert['text'] = 'Edit door failed: ' + message
door_form = build_door_form(door)
event_list = build_event_list(door)
return render_template('settings_door.html', alert=alert, settings=settings, dform=door_form, elist=event_list)
# Door ID not found so return fail
alert['type'] = 'error'
alert['text'] = 'Edit door failed: Door ID not found'
return render_template('settings.html', alert=alert, settings=settings)
if('add_door' in request.form):
edit_door_form = DoorForm()
door = temp_door.copy()
if(edit_door_form.validate()):
door['name'] = edit_door_form.dname.data
door['id'] = get_unique_id()
door['triggerlevel'] = edit_door_form.triggerlevel.data
door['sensorlevel'] = edit_door_form.sensorlevel.data
door['inpins'] = {}
for field in edit_door_form.inpins:
pin_type = field.inpin.data
door['inpins'][pin_type] = int(field.gpio_pin.data)
del door['outpins']
door['outpins'] = {}
for field in edit_door_form.outpins:
pin_type = field.outpin.data
door['outpins'][pin_type] = int(field.gpio_pin.data)
settings['doors'].append(door)
write_settings(settings)
cmdsts.set('settings_update', 'true')
alert['type'] = 'success'
alert['text'] = 'Added door "' + door['name'] + '".'
return render_template('settings.html', alert=alert, settings=settings)
else:
# Something failed, so get the error message and notify user
for fieldName, errorMessages in edit_door_form.errors.items():
for err in errorMessages:
print(f'Error: {fieldName} {err} {errorMessages}')
message = err
alert['type'] = 'error'
alert['text'] = 'Edit door failed: ' + message
event_list = build_event_list(door)
return render_template('settings_door.html', alert=alert, settings=settings, dform=edit_door_form, elist=event_list)
if('add_event' in request.form):
# First build notify services list
choices = []
for notify_service in settings['notify']:
choices.append((notify_service['id'], notify_service['name']))
# Create Empty Event Form
event_form = EventForm()
event_form.notifylist.choices = choices
event_form.process()
event_form.id.data = request.form['add_event']
event_form.time.data = 0
action = 'add_event'
return render_template('settings_event.html', alert=alert, settings=settings, eform=event_form, action=action)
if('edit_event' in request.form):
event_type, door_id = request.form['edit_event'].split(':', 2)
event_form = build_event_form(door_id, event_type)
return render_template('settings_event.html', alert=alert, settings=settings, eform=event_form)
if('del_event' in request.form):
event_type, door_id = request.form['del_event'].split(':', 2)
if door_id == 'new':
temp_door['notify_events'].pop(event_type)
alert['type'] = 'success'
alert['text'] = 'Deleted Event "' + event_type + '".'
door_form = build_door_form(temp_door)
event_list = build_event_list(temp_door)
return render_template('settings_door.html', alert=alert, settings=settings, dform=door_form, elist=event_list)
else:
for door in settings['doors']:
if door_id == door['id']:
# Remove dict 'event_type' from door['notify_events']
door['notify_events'].pop(event_type)
write_settings(settings)
cmdsts.set('settings_update', 'true')
alert['type'] = 'success'
alert['text'] = 'Deleted Event "' + event_type + '".'
door_form = build_door_form(door)
event_list = build_event_list(door)
return render_template('settings_door.html', alert=alert, settings=settings, dform=door_form, elist=event_list)
return render_template('settings.html', alert=alert, settings=settings)
@app.route('/addeditevent', methods=['POST'])
def addeditevent(action=None):
global settings
# Check if password protected AND not logged in
if (settings['misc']['password'] == True) and ('active' not in session):
return redirect('/login')
global temp_door
settings = read_settings()
# Create Alert Structure for Alert Notification
alert = {
'type' : '',
'text' : ''
}
if('add_event' in request.form):
# First build notify services list
choices = []
for notify_service in settings['notify']:
choices.append((notify_service['id'], notify_service['name']))
# Create Empty Event Form
event_form = EventForm()
event_form.notifylist.choices = choices
if event_form.validate():
event_data = {}
event_data['title'] = event_form.title.data
event_data['message'] = event_form.message.data
event_data['sensor'] = event_form.sensor.data
event_data['sensorlevel'] = True if event_form.sensorlevel.data == 'True' else False
event_data['time'] = event_form.time.data
event_data['starttimer'] = event_form.starttimer.data
event_data['timerendevent'] = event_form.timerendevent.data
event_data['remindevent'] = event_form.remindevent.data
event_data['logtype'] = event_form.logtype.data
event_data['notifylist'] = event_form.notifylist.data
event_type_name = event_form.event_type.data
door_id = event_form.id.data
if door_id == 'new':
temp_door['notify_events'][event_type_name] = event_data
door_form = build_door_form(temp_door)
event_list = build_event_list(temp_door)
return render_template('settings_door.html', alert=alert, settings=settings, dform=door_form, elist=event_list)
else:
for door in settings['doors']:
if door['id'] == door_id:
door['notify_events'][event_type_name] = event_data
write_settings(settings)
cmdsts.set('settings_update', 'true')
door_form = build_door_form(door)
event_list = build_event_list(door)
return render_template('settings_door.html', alert=alert, settings=settings, dform=door_form, elist=event_list)
else:
# Something failed, so get the error message and notify user
for fieldName, errorMessages in event_form.errors.items():
for err in errorMessages:
message = err
alert['type'] = 'error'
alert['text'] = 'Add event failed: ' + message
return render_template('settings_event.html', alert=alert, settings=settings, eform=event_form, action='add_event')
if('edit_event' in request.form):
event_type, door_id = request.form['edit_event'].split(':', 2)
# First build notify services list
choices = []
for notify_service in settings['notify']:
choices.append((notify_service['id'], notify_service['name']))
# Create Empty Event Form
event_form = EventForm()
event_form.notifylist.choices = choices
#event_form.process()
if event_form.validate():
if door_id == 'new':
for event in temp_door['notify_events']:
if event == event_type:
temp_door['notify_events'][event]['title'] = event_form.title.data
temp_door['notify_events'][event]['message'] = event_form.message.data
temp_door['notify_events'][event]['sensor'] = event_form.sensor.data
temp_door['notify_events'][event]['sensorlevel'] = True if event_form.sensorlevel.data == 'True' else False
temp_door['notify_events'][event]['time'] = event_form.time.data
temp_door['notify_events'][event]['starttimer'] = event_form.starttimer.data
temp_door['notify_events'][event]['timerendevent'] = event_form.timerendevent.data
temp_door['notify_events'][event]['remindevent'] = event_form.remindevent.data
temp_door['notify_events'][event]['logtype'] = event_form.logtype.data
temp_door['notify_events'][event]['notifylist'] = event_form.notifylist.data
door_form = build_door_form(temp_door)
event_list = build_event_list(temp_door)
return render_template('settings_door.html', alert=alert, settings=settings, dform=door_form, elist=event_list)
else:
for door in settings['doors']:
if door['id'] == door_id:
for event in door['notify_events']:
if event == event_type:
door['notify_events'][event]['title'] = event_form.title.data
door['notify_events'][event]['message'] = event_form.message.data
door['notify_events'][event]['sensor'] = event_form.sensor.data
door['notify_events'][event]['sensorlevel'] = True if event_form.sensorlevel.data == 'True' else False
door['notify_events'][event]['time'] = event_form.time.data
door['notify_events'][event]['starttimer'] = event_form.starttimer.data
door['notify_events'][event]['timerendevent'] = event_form.timerendevent.data
door['notify_events'][event]['remindevent'] = event_form.remindevent.data
door['notify_events'][event]['logtype'] = event_form.logtype.data
door['notify_events'][event]['notifylist'] = event_form.notifylist.data
write_settings(settings)
cmdsts.set('settings_update', 'true')
door_form = build_door_form(door)
event_list = build_event_list(door)
return render_template('settings_door.html', alert=alert, settings=settings, dform=door_form, elist=event_list)
alert['type'] = 'error'
alert['text'] = 'Edit event failed: Door ID or Event ID not found'
else:
# Something failed, so get the error message and notify user
for fieldName, errorMessages in event_form.errors.items():
for err in errorMessages:
print(f'Error: {fieldName} {err} {errorMessages}')
message = err
alert['type'] = 'error'
alert['text'] = 'Edit event failed: ' + message
return render_template('settings_event.html', alert=alert, settings=settings, eform=event_form)
return render_template('settings.html', alert=alert, settings=settings)
@app.route('/pinsdata', methods=['POST','GET'])
def pinsdata(action=None):
global settings
# Check if password protected AND not logged in
if (settings['misc']['password'] == True) and ('active' not in session):
return redirect('/login')
global temp_door
if('action' in request.form):
door_id = request.form['id']
door = {}
if door_id == 'new':
door = temp_door
else:
for door_obj in settings['doors']:
if door_obj['id'] == door_id:
door = door_obj
break
if(request.form['action'] == 'get_inpins'):
door_form = build_door_form(door)
return render_template('settings_inpins.html', dform=door_form)
if(request.form['action'] == 'get_outpins'):
door_form = build_door_form(door)
return render_template('settings_outpins.html', dform=door_form)
if(request.form['action'] == 'get_avail_inpins'):
avail_inpins = INPIN_TYPES.copy()
for inpin in door['inpins']:
if inpin in avail_inpins:
avail_inpins.remove(inpin)
if avail_inpins == []:
htmlout='<option value="none">No Sensor Types Available</option>'
else:
htmlout=''
for inpin in avail_inpins:
htmlout += f'<option value="{inpin}">{inpin}</option>'
return(htmlout)
if(request.form['action'] == 'get_avail_outpins'):
avail_outpins = OUTPIN_TYPES.copy()
for outpin in door['outpins']:
if outpin in avail_outpins:
avail_outpins.remove(outpin)
if avail_outpins == []:
htmlout='<option value="none">No Button Types Available</option>'
else:
htmlout=''
for outpin in avail_outpins:
htmlout += f'<option value="{outpin}">{outpin}</option>'
return(htmlout)
if(request.form['action'] == 'add_inpin'):
pintype = request.form['pintype']
if (pintype != 'none') and (pintype != ''):
door['inpins'][pintype] = 0
write_settings(settings)
cmdsts.set('settings_update', 'true')
door_form = build_door_form(door)
return render_template('settings_inpins.html', dform=door_form)
if(request.form['action'] == 'add_outpin'):
pintype = request.form['pintype']
if (pintype != 'none') and (pintype != ''):
door['outpins'][pintype] = 0
write_settings(settings)
cmdsts.set('settings_update', 'true')
door_form = build_door_form(door)
return render_template('settings_outpins.html', dform=door_form)
if(request.form['action'] == 'del_inpin'):
pintype = request.form['pintype']
del door['inpins'][pintype]
write_settings(settings)
cmdsts.set('settings_update', 'true')
door_form = build_door_form(door)
return render_template('settings_inpins.html', dform=door_form)
if(request.form['action'] == 'del_outpin'):
pintype = request.form['pintype']
del door['outpins'][pintype]
write_settings(settings)
cmdsts.set('settings_update', 'true')
door_form = build_door_form(door)
return render_template('settings_outpins.html', dform=door_form)
return ('Error Retrieving Table Data')
@app.route('/admin/<action>', methods=['POST','GET'])
@app.route('/admin', methods=['POST','GET'])
def admin(action=None):
global settings
# Check if password protected AND not logged in
if (settings['misc']['password'] == True) and ('active' not in session):
return redirect('/login')
settings = read_settings()
secrets = read_secrets()
# Create Alert Structure for Alert Notification
alert = {
'type' : '',
'text' : ''
}
if action == 'reboot':
event = "Reboot Requested."
write_log(event, logtype="REBOOT")
os.system("sleep 3 && sudo reboot &")
#Show Reboot Splash
return render_template('shutdown.html', action=action, settings=settings)
if action == 'shutdown':
event = "Shutdown Requested."
write_log(event, logtype="SHUTDOWN")
os.system("sleep 3 && sudo shutdown -h now &")
#Show Shutdown Splash
return render_template('shutdown.html', action=action, settings=settings)
if (request.method == 'POST'):
response = request.form
if('enable_security' in response):
pass
'''
# Bcrypt Issue on Raspberry Pi, enabling login security is disabled (2/2023)
if response['enable_security'] == 'true':
if len(secrets) > 0:
settings['misc']['password'] = True
write_settings(settings)
event = 'Enabled security / password access.'
write_log(event, logtype='SETTINGS')
alert['type'] = 'success'
alert['text'] = event
else:
event = 'No users/passcodes defined. You must have at least one user/passcode to enable security.'
write_log(event, logtype='SETTINGS')
alert['type'] = 'warning'
alert['text'] = event
else:
settings['misc']['password'] = False
write_settings(settings)
event = 'Disabled security / password access.'
write_log(event, logtype='SETTINGS')
alert['type'] = 'success'
alert['text'] = event
'''
if('del_user' in response):
for index in range(len(secrets)):
if(secrets[index]['id'] == response['del_user']):
del secrets[index]
write_secrets(secrets)
event = 'Deleted User. '
write_log(event, logtype='SETTINGS')
alert['type'] = 'success'
alert['text'] = event
# If there are no more users in the secrets file, then disable password security to prevent lockout
if len(secrets) == 0:
settings['misc']['password'] = False
write_settings(settings)
event = 'Disabled security / password access since there are no users.'
write_log(event, logtype='SETTINGS')
alert['text'] += event
if('add_user' in response):
password = response['password']
if(password.isnumeric()) and (len(password) > 3) and (response['username'] != ''):
newuser = {}
newuser['id'] = get_unique_id()
newuser['username'] = response['username']
newuser['pc_hash'] = bcrypt.generate_password_hash(response['password']).decode('UTF-8')
secrets.append(newuser)
write_secrets(secrets)
event = f'Added User {response["username"]}'
write_log(event, logtype='SETTINGS')
alert['type'] = 'success'
alert['text'] = event
else:
alert['type'] = 'error'
alert['text'] = 'Passcode must be numeric only and at least four digits long. Please try again.'
uptime = os.popen('uptime').readline()
cpuinfo = os.popen('cat /proc/cpuinfo').readlines()
return render_template('admin.html', alert=alert, uptime=uptime, cpuinfo=cpuinfo, settings=settings, secrets=secrets)
@app.route('/sec', methods=['POST','GET'])
def sec_check():
global settings
# Check if password protected AND not logged in
if (settings['misc']['password'] == True) and ('active' not in session):