Repository navigation
Expand file tree
/
Copy pathServer.py
More file actions
71 lines (58 loc) · 2.74 KB
/
Copy pathServer.py
File metadata and controls
71 lines (58 loc) · 2.74 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
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
import json
import zmq
import HrZone
from EventTracking import handle_event_tracking
from SummaryStats import summary_stats, event_summary
def main():
# Setup
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:5555")
print("Microservice is listening on port 5555...")
while True:
try:
# get json message from client
message = socket.recv_string()
# convert json string to dictionary
data = json.loads(message)
# extract and validate data
numbers = data.get("numbers")
age = data.get("age")
heart_rate = data.get("heart_rate")
event = data.get("event")
app_name = data.get("app_name")
user_id = data.get("user_id")
# determine which features to calculate based on 'requested' field
all_features = ["total", "min", "max", "average", "heart_rate_zone"]
requested = data.get("requested", all_features) # default to all features if not specified
# prepare response dictionary
response = {}
# Process event tracking (User story 1)
response.update(handle_event_tracking(event))
# Return event summary if requested
if "event_summary" in requested:
if app_name is not None and user_id is not None:
response.update(event_summary(app_name, user_id))
else:
response["error"] = "Event summary requires app_name and user_id."
# calculate heart rate zone if requested (User story 2)
if "heart_rate_zone" in requested:
# calculate heart rate zone if age and heart_rate are provided
if age is not None and heart_rate is not None:
try:
response["heart_rate_zone"] = HrZone.hrZone(age, heart_rate)
except Exception as e:
response["error"] = f"Error calculating heart rate zone: {str(e)}"
# Process summary statistics (User story 3)
response.update(summary_stats(numbers, requested))
if not response:
response["error"] = "No valid data provided. Please include 'numbers' and/or 'age' and 'heart_rate'."
# send the response back to the client
socket.send_string(json.dumps(response))
# handle JSON parsing errors or other exceptions
except json.JSONDecodeError:
socket.send_string(json.dumps({"error": "Invalid JSON"}))
except Exception as e:
socket.send_string(json.dumps({"error": f"Internal error: {str(e)}"}))
if __name__ == "__main__":
main()