updated trips

This commit is contained in:
2025-06-03 16:34:15 +03:00
parent bfe81692b6
commit 57bc5c4f81
6 changed files with 176 additions and 90 deletions

103
main.py
View File

@@ -226,30 +226,28 @@ class GetTripFromServer(Screen): # Renamed from HomeScreen
else:
self.ids.devices_spinner.background_color = (1, 1, 1, 1) # Reset to white if no valid device is selected
def open_date_picker(self):
"""Open a popup to select a date."""
def open_date_picker(self, which):
"""Open a popup to select a date for start or end."""
today = date.today()
selected_date = [None] # Use a mutable object to store the selected date
selected_date = [None]
def on_date_selected(instance):
selected_date[0] = instance.text
# Update the button text with the selected date
self.ids.date_picker_button.text = f"Date: {today.year}-{today.month:02d}-{int(selected_date[0]):02d}"
# Change the background color of the button to green
self.ids.date_picker_button.background_color = (0.008, 0.525, 0.290, 1) # Green color (#02864A)
print(f"Date selected: {self.ids.date_picker_button.text}")
date_str = f"{today.year}-{today.month:02d}-{int(selected_date[0]):02d}"
if which == 'start':
self.ids.start_date_picker_button.text = date_str
else:
self.ids.end_date_picker_button.text = date_str
popup.dismiss()
# Create a popup with a grid layout for the days
layout = GridLayout(cols=7, spacing=5, padding=10)
for day in range(1, 32): # Assuming a maximum of 31 days in a month
for day in range(1, 32):
try:
current_date = date(today.year, today.month, day)
button = Button(text=str(day), size_hint=(None, None), size=(40, 40))
button.bind(on_press=on_date_selected)
layout.add_widget(button)
except ValueError:
# Skip invalid dates (e.g., February 30)
pass
popup = Popup(title="Select a Date", content=layout, size_hint=(0.8, 0.8))
@@ -258,21 +256,92 @@ class GetTripFromServer(Screen): # Renamed from HomeScreen
def get_trip_server_data(self):
"""Handle the Get trip server data button press."""
selected_device = self.ids.devices_spinner.text
selected_date = self.ids.date_picker_button.text
start_date = self.ids.start_date_picker_button.text
end_date = self.ids.end_date_picker_button.text
if selected_device == "Loading devices..." or selected_device == "No devices found":
print("No valid device selected.")
self.ids.result_label.text = "Please select a valid device."
return
if selected_date == "Select Date":
if start_date == "Select Date" or end_date == "Select Date":
print("No valid date selected.")
self.ids.result_label.text = "Please select a valid date."
self.ids.result_label.text = "Please select valid start and end dates."
return
# Simulate fetching trip data from the server
print(f"Fetching trip data for device: {selected_device} on date: {selected_date}")
self.ids.result_label.text = f"Fetching trip data for {selected_device} on {selected_date}..."
# Fetch trip data from the server
print(f"Fetching trip data for device: {selected_device} from {start_date} to {end_date}")
self.ids.result_label.text = f"Fetching trip data for {selected_device} from {start_date} to {end_date}..."
positions = self.fetch_positions_for_selected_day()
if positions:
print("Positions received:")
for pos in positions:
print(f"{pos['deviceTime']}: {pos['latitude']}, {pos['longitude']}")
else:
print("No positions found or error occurred.")
def fetch_positions_for_selected_day(self):
"""Fetch all positions for the selected device and date/time range from the Traccar server."""
settings = check_server_settings()
if not settings:
self.ids.result_label.text = "Server settings not found."
return []
server_url = settings["server_url"]
token = settings["token"]
selected_device = self.ids.devices_spinner.text
if selected_device not in self.device_mapping:
self.ids.result_label.text = "Please select a valid device."
return []
device_id = self.device_mapping[selected_device]
# Get start/end date and hour from UI
start_date = self.ids.start_date_picker_button.text
start_hour = self.ids.start_hour_spinner.text
end_date = self.ids.end_date_picker_button.text
end_hour = self.ids.end_hour_spinner.text
# Validate
if "Select" in start_date or "Select" in end_date:
self.ids.result_label.text = "Please select both start and end dates."
return []
# Build ISO 8601 time strings
from_time = f"{start_date}T{start_hour}:00:00Z"
to_time = f"{end_date}T{end_hour}:59:59Z"
# Prepare request for /reports/route
url = f"{server_url}/reports/route"
headers = {"Authorization": f"Bearer {token}", "Accept": "application/json"}
params = {
"deviceId": device_id,
"from": from_time,
"to": to_time
}
try:
print(f"Request Payload: {params}")
response = requests.get(url, params=params, headers=headers, timeout=15)
print(f"Response Status Code: {response.status_code}")
print(f"Response Content: {response.text}")
if response.status_code == 200:
positions = response.json()
print(f"Retrieved {len(positions)} positions.")
self.ids.result_label.text = f"Retrieved {len(positions)} positions."
return positions
elif response.status_code == 400:
self.ids.result_label.text = "Bad Request: Please check the request payload and token."
return []
else:
self.ids.result_label.text = f"Failed: {response.status_code} - {response.reason}"
return []
except requests.exceptions.RequestException as e:
self.ids.result_label.text = f"Error fetching positions: {str(e)}"
return []
class RegisterScreen(Screen):