-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
131 lines (116 loc) · 2.66 KB
/
app.py
File metadata and controls
131 lines (116 loc) · 2.66 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
from flask import Flask, jsonify, request
from gpiozero import Button
from gpiozero import LED
from gpiozero import Buzzer
from json import dumps
import requests
import threading
import time
import os
app = Flask(__name__)
reedPin = 17
ledPin = 27
soundPin = 22
reed = Button(reedPin, pull_up=True)
led = LED(27)
buzzer = Buzzer(22)
isOpen = False
alarm = False
isAlarm = False
sKey = ""
if not os.path.exists("key.txt"):
open("key.txt", "w")
with open("key.txt", "r") as file:
sKey = file.read()
@app.route('/setup/<key>')
def setup(key):
global sKey
if sKey == "" and key != "":
sKey = key
with open("key.txt", "w") as file:
file.write(sKey)
return "success"
else:
return "already set up - error"
@app.route('/reset/<key>')
def reset(key):
global sKey
if sKey == key and key != "":
sKey = ""
with open("key.txt", "w") as file:
file.write(sKey)
return "success"
else:
return "authentication error"
@app.route('/get')
def send_json():
jsondict = {"IsOpen": isOpen, "IsAlarm": isAlarm, "Alarm": alarm }
data = dumps(jsondict)
print("send")
return data
@app.route('/set/<int:on>/<key>')
def setAlarm(on, key):
global sKey
global alarm
if sKey == key:
if on == 0:
alarm = False
elif on == 1:
alarm = True
else:
return "ArgumentOutOfRangeException"
else:
return "authentication error"
return "success"
@app.route('/mute/<key>')
def muteAlarm(key):
global sKey
global isAlarm
if sKey == key:
isAlarm = False
return "success"
else:
return "authentication error"
@app.route('/test/<key>')
def testAlarm(key):
global sKey
global isAlarm
if sKey == key:
isAlarm = True
time.sleep(1)
isAlarm = False
return "success"
else:
return "authentication error"
def checkIfOpen():
global isAlarm
global isOpen
global alarm
if not reed.is_pressed:
isOpen = True
if alarm:
isAlarm = True
print("offen")
else:
isOpen = False
print("geschlossen")
def doAlarm():
led.toggle()
buzzer.toggle()
def Timer():
global isAlarm
try:
while True:
if isAlarm:
doAlarm()
else:
led.off()
buzzer.off()
checkIfOpen()
time.sleep(0.5) # Jede Sekunde ausführen
except KeyboardInterrupt:
print("beende Programm")
thread = threading.Thread(target=Timer, daemon=True)
thread.start()
if __name__ == '__main__':
app.run()