-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathovercurrent_simulator.py
More file actions
40 lines (34 loc) · 1.38 KB
/
overcurrent_simulator.py
File metadata and controls
40 lines (34 loc) · 1.38 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
import requests
import time
import random
# URL of the Flask app's alert endpoint
ALERT_URL = "http://localhost:5000/api/system/alert"
def simulate_overcurrent_detection():
"""
Simulates a microcontroller detecting an overcurrent and reporting it.
"""
print("Simulating overcurrent detection event...")
# Simulate a random chance of an overcurrent event
if random.random() < 0.1: # 10% chance of overcurrent
payload = {
"source": "micropython_controller",
"event_type": "overcurrent_detected",
"details": {
"component": "storage_array_power_bus",
"current_amps": random.uniform(10.5, 15.0), # Simulate high current
"threshold_amps": 10.0
}
}
try:
response = requests.post(ALERT_URL, json=payload)
response.raise_for_status() # Raise an exception for bad status codes
print(f"Successfully sent overcurrent alert: {payload}")
except requests.exceptions.RequestException as e:
print(f"Failed to send overcurrent alert: {e}")
else:
print("No overcurrent detected in this cycle.")
if __name__ == "__main__":
print("Starting overcurrent event simulator...")
while True:
simulate_overcurrent_detection()
time.sleep(5) # Wait for 5 seconds before the next check