-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
137 lines (110 loc) · 4.54 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
from flask import Flask, request, jsonify, render_template, Response
import time # Import time module to use time.sleep()
app = Flask(__name__)
# Store all messages (instead of just the latest one)
messages = [{"response": ""}]
subscribers = []
@app.route('/')
def index():
# Pass the history of messages to the frontend
return render_template('index.html', messages=messages)
@app.route('/echo', methods=['POST'])
def echo():
global messages
data = request.get_json()
new_message = {"response": data.get("text", "")}
# Add the new message to the list
messages.append(new_message)
# Check for specific keywords and set the page to load
page_to_load = None
if "material simulation" in new_message["response"].lower():
page_to_load = "materialsim"
elif "material discovery" in new_message["response"].lower():
page_to_load = "materialdis"
elif "sustainability testing" in new_message["response"].lower():
page_to_load = "sustaintest"
elif "material performance" in new_message["response"].lower():
page_to_load = "materialperf"
elif "lab equipment" in new_message["response"].lower():
page_to_load = "iot" # Direct to IoT page for device-related messages
# Include the page_to_load flag in the response
new_message["page_to_load"] = page_to_load
# Notify subscribers (optional, not required for AJAX)
for subscriber in subscribers:
subscriber.put(new_message["response"])
return jsonify(new_message), 200
@app.route('/sustaintest')
def sustaintest():
return render_template('sustaintest.html')
@app.route('/materialperf')
def materialperf():
return render_template('materialperf.html')
@app.route('/history')
def history():
# Return the full message history as JSON
return jsonify(messages)
# Add route for serving materialdis.html
@app.route('/materialdis')
def materialdis():
return render_template('materialdis.html')
@app.route('/events')
def events():
def generate():
while True:
if messages:
new_message = messages[-1] # Get latest message
yield f"data: {new_message['response']}\n\n"
time.sleep(2) # Update every 2 seconds or based on your own condition
return Response(generate(), content_type='text/event-stream')
@app.route('/materialsim')
def materialsim():
return render_template('materialsim.html')
# Simulated database for devices
devices = {
"oven01": {"status": "off", "performance": {"temperature": 25.0, "usageTime": 0, "errors": []}},
"microscope02": {"status": "off", "performance": {"temperature": None, "usageTime": 10, "errors": []}},
"centrifuge03": {"status": "off", "performance": {"temperature": 20.0, "usageTime": 5, "errors": ["Maintenance required"]}},
}
# Control a specific IoT device
@app.route('/devices/<deviceId>/control', methods=['POST'])
def control_device(deviceId):
action = request.json.get("action")
if not action or action not in ["turn_on", "turn_off"]:
return jsonify({"error": "Invalid action. Use 'turn_on' or 'turn_off'."}), 400
device = devices.get(deviceId)
if not device:
return jsonify({"error": f"Device with ID {deviceId} not found."}), 404
# Update device status based on the action
if action == "turn_on":
device["status"] = "on"
elif action == "turn_off":
device["status"] = "off"
# Send the device status update to all subscribers
for subscriber in subscribers:
subscriber.put(f"Device {deviceId} status: {device['status']}")
return jsonify({
"deviceId": deviceId,
"action": action,
"status": device["status"]
}), 200
# Get the status of a specific IoT device
@app.route('/devices/<deviceId>/status', methods=['GET'])
def get_device_status(deviceId):
device = devices.get(deviceId)
if not device:
return jsonify({"error": f"Device with ID {deviceId} not found."}), 404
return jsonify({
"deviceId": deviceId,
"status": device["status"],
"performance": device["performance"]
}), 200
# Health check endpoint (optional)
@app.route('/health', methods=['GET'])
def health_check():
return jsonify({"status": "healthy"}), 200
# Home route serving an HTML template for IoT
@app.route('/iot')
def iot():
return render_template('iot.html')
if __name__ == '__main__':
app.run(debug=True)