Spaces:
Sleeping
Sleeping
Update services/salesforce_dispatcher.py
Browse files
services/salesforce_dispatcher.py
CHANGED
@@ -1,29 +1,41 @@
|
|
1 |
-
# services/salesforce_dispatcher.py
|
2 |
import json
|
|
|
3 |
import logging
|
|
|
4 |
|
5 |
-
#
|
6 |
-
logging.basicConfig(
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
def dispatch_to_salesforce(detections, timestamp):
|
10 |
"""
|
11 |
-
Dispatch detection data to Salesforce.
|
12 |
Args:
|
13 |
-
detections:
|
14 |
-
timestamp: Timestamp of the
|
15 |
-
Returns:
|
16 |
-
bool: Success status
|
17 |
"""
|
18 |
try:
|
19 |
-
#
|
20 |
-
|
21 |
"timestamp": timestamp,
|
22 |
-
"detections": detections
|
|
|
|
|
|
|
23 |
}
|
24 |
-
|
25 |
-
#
|
26 |
-
|
|
|
|
|
|
|
|
|
27 |
except Exception as e:
|
28 |
-
|
29 |
-
return False
|
|
|
|
|
1 |
import json
|
2 |
+
import os
|
3 |
import logging
|
4 |
+
from datetime import datetime
|
5 |
|
6 |
+
# Setup logging
|
7 |
+
logging.basicConfig(
|
8 |
+
filename="app.log",
|
9 |
+
level=logging.INFO,
|
10 |
+
format="%(asctime)s - %(levelname)s - %(message)s"
|
11 |
+
)
|
12 |
+
|
13 |
+
# Directory for Salesforce logs
|
14 |
+
SALESFORCE_LOG_DIR = "salesforce_logs"
|
15 |
+
os.makedirs(SALESFORCE_LOG_DIR, exist_ok=True)
|
16 |
|
17 |
def dispatch_to_salesforce(detections, timestamp):
|
18 |
"""
|
19 |
+
Dispatch detection data to Salesforce (simulated by logging to a JSON file).
|
20 |
Args:
|
21 |
+
detections: Dict containing detection data (items, metrics, etc.)
|
22 |
+
timestamp: Timestamp of the detection
|
|
|
|
|
23 |
"""
|
24 |
try:
|
25 |
+
# Prepare data
|
26 |
+
data = {
|
27 |
"timestamp": timestamp,
|
28 |
+
"detections": detections["items"],
|
29 |
+
"metrics": detections["metrics"],
|
30 |
+
"frame_count": detections["frame_count"],
|
31 |
+
"gps_coordinates": detections["gps_coordinates"]
|
32 |
}
|
33 |
+
|
34 |
+
# Write to JSON file
|
35 |
+
log_file = os.path.join(SALESFORCE_LOG_DIR, f"detections_{timestamp.replace(':', '-')}.json")
|
36 |
+
with open(log_file, "w") as f:
|
37 |
+
json.dump(data, f, indent=2)
|
38 |
+
|
39 |
+
logging.info(f"Dispatched detections to Salesforce log: {log_file}")
|
40 |
except Exception as e:
|
41 |
+
logging.error(f"Error dispatching to Salesforce: {str(e)}")
|
|