Skip to content

Commit b9bdcba

Browse files
authored
Create sensor_manager.py
1 parent 7e4df55 commit b9bdcba

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

biosensors/sensor_manager.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# biosensors/sensor_manager.py
2+
3+
import time
4+
import random
5+
import json
6+
import logging
7+
8+
# Configure logging
9+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
10+
11+
class SensorManager:
12+
def __init__(self, sensor_ids):
13+
"""
14+
Initialize the SensorManager with a list of sensor IDs.
15+
16+
:param sensor_ids: List of sensor IDs to manage.
17+
"""
18+
self.sensor_ids = sensor_ids
19+
self.connected_sensors = {}
20+
self.data = {}
21+
22+
def connect_sensors(self):
23+
"""
24+
Connect to the biosensors and store their connection status.
25+
"""
26+
for sensor_id in self.sensor_ids:
27+
try:
28+
# Simulate connecting to a sensor (replace with actual connection logic)
29+
self.connected_sensors[sensor_id] = True
30+
logging.info(f"Connected to sensor {sensor_id}.")
31+
except Exception as e:
32+
logging.error(f"Failed to connect to sensor {sensor_id}: {e}")
33+
34+
def collect_data(self):
35+
"""
36+
Collect data from connected biosensors.
37+
38+
:return: A dictionary containing sensor data.
39+
"""
40+
for sensor_id in self.connected_sensors:
41+
if self.connected_sensors[sensor_id]:
42+
try:
43+
# Simulate data collection (replace with actual data retrieval logic)
44+
sensor_data = self._simulate_data_collection(sensor_id)
45+
self.data[sensor_id] = sensor_data
46+
logging.info(f"Collected data from sensor {sensor_id}: {sensor_data}")
47+
except Exception as e:
48+
logging.error(f"Failed to collect data from sensor {sensor_id}: {e}")
49+
return self.data
50+
51+
def _simulate_data_collection(self, sensor_id):
52+
"""
53+
Simulate data collection from a sensor.
54+
55+
:param sensor_id: The ID of the sensor to collect data from.
56+
:return: Simulated sensor data.
57+
"""
58+
# Simulate different types of data (e.g., temperature, heart rate)
59+
return {
60+
"temperature": round(random.uniform(36.0, 38.5), 2), # Simulated temperature in Celsius
61+
"heart_rate": random.randint(60, 100), # Simulated heart rate in BPM
62+
"timestamp": time.time() # Current timestamp
63+
}
64+
65+
def disconnect_sensors(self):
66+
"""
67+
Disconnect from all biosensors.
68+
"""
69+
for sensor_id in self.connected_sensors:
70+
if self.connected_sensors[sensor_id]:
71+
# Simulate disconnecting from a sensor (replace with actual disconnection logic)
72+
self.connected_sensors[sensor_id] = False
73+
logging.info(f"Disconnected from sensor {sensor_id}.")
74+
75+
if __name__ == "__main__":
76+
# Example usage
77+
sensor_ids = ["sensor_1", "sensor_2", "sensor_3"]
78+
manager = SensorManager(sensor_ids)
79+
80+
manager.connect_sensors()
81+
collected_data = manager.collect_data()
82+
print(json.dumps(collected_data, indent=4))
83+
manager.disconnect_sensors()

0 commit comments

Comments
 (0)