forked from ileniaficili/AIoT-Greenhouse-Health-Assessment
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessing_node.py
More file actions
42 lines (36 loc) · 1.31 KB
/
processing_node.py
File metadata and controls
42 lines (36 loc) · 1.31 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
from flask import Flask, request, jsonify
import requests
app = Flask(__name__)
ADMIN_ENDPOINT = "http://localhost:5000/admin/alert"
# Thresholds acting as a proxy for the LSTM model decision boundary
THRESHOLDS = {"temp_max": 32.0, "hum_min": 45.0}
def analyze_data(data):
"""
Analyzes incoming telemetry.
Returns a list of anomalies if thresholds are breached.
"""
anomalies = []
if data['temperature'] > THRESHOLDS['temp_max']:
anomalies.append(f"Critical Temp: {data['temperature']}C")
if data['humidity'] < THRESHOLDS['hum_min']:
anomalies.append(f"Low Humidity: {data['humidity']}%")
return anomalies
@app.route('/api/telemetry', methods=['POST'])
def receive_telemetry():
content = request.json
# Execute Anomaly Detection
detected_issues = analyze_data(content)
if detected_issues:
# Trigger Alert to Administrator
alert_payload = {
"severity": "HIGH",
"device": content['device_id'],
"issues": detected_issues
}
try:
# Unencrypted communication as per PoC requirements
requests.post(ADMIN_ENDPOINT, json=alert_payload)
except:
pass
return jsonify({"status": "alert_triggered"}), 200
return jsonify({"status": "nominal"}), 200