-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
82 lines (61 loc) · 2.18 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
# app.py
# -*- coding: utf-8 -*-
from flask import Flask, render_template, request
import paho.mqtt.client as mqtt
import os
import threading
import sound_detector
from time import sleep # time 모듈에서 sleep 함수 import
app = Flask(__name__)
client = mqtt.Client()
@app.route('/')
def index():
return render_template('index.html')
@app.route('/mqtt', methods=['GET', 'POST'])
def mqtt():
# MQTT 브로커에 연결합니다.
client.connect("localhost", 1883, 60)
# 'test' 토픽에 메시지를 발행합니다.
client.publish("test", "Hello MQTT")
return "MQTT route"
def on_connect(client, userdata, flag, rc):
client.subscribe("led", qos=0) # "led" 토픽으로 구독 신청
def on_message(client, userdata, msg):
on_off = int(msg.payload) # on_off는 0 또는 1의 정수
sound_detector.controlLED(on_off) # LED를 켜거나 끔
client.on_connect = on_connect
client.on_message = on_message
def play_alarm(total_seconds):
sleep(total_seconds)
os.system('cvlc /home/pi/static/alarm.mp3 --play-and-exit')
def start_mqtt():
client.connect("localhost", 1883, 60)
client.loop_start()
def stop_mqtt():
client.loop_stop()
@app.route('/set_alarm', methods=['POST'])
def set_alarm():
hours = request.form.get('hours')
minutes = request.form.get('minutes')
seconds = request.form.get('seconds')
total_seconds = int(hours) * 3600 + int(minutes) * 60 + int(seconds)
client.publish("alarm/topic", total_seconds)
threading.Thread(target=play_alarm, args=(total_seconds,), daemon=True).start()
return 'Alarm set for ' + hours + ' hours ' + minutes + ' minutes ' + seconds + ' seconds later.'
@app.route('/stop', methods=['POST'])
def stop():
os.system('pkill vlc')
return 'Alarm stopped.'
@app.route('/start_sound_measurement', methods=['GET'])
def start_sound_measurement():
start_mqtt()
return 'Sound measurement started.'
@app.route('/stop_sound_measurement', methods=['GET'])
def stop_sound_measurement():
stop_mqtt()
return str(sound_data)
@app.route('/get_sound_data', methods=['GET'])
def get_sound_data():
return str(sound_data)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)