Error 405 trying to reset totaldistance

yv67 2 months ago

I am trying to update the toaldistance and hours with a python script
but I got an error 405

Error updating device accumulators: 405 Client Error: Method Not Allowed for url: https://traccar.ipcs.synology.me/devices/4/accumulators

here is my python script :

import requests
import json

def update_device_accumulators(server_url, device_id, total_distance, hours, api_key):
    """
    Updates the total distance and hours of a device using the Traccar API.

    Args:
        server_url (str): The base URL of the Traccar server.
        device_id (int): The ID of the device to update.
        total_distance (float): The new total distance in meters.
        hours (float): The new total hours.
        api_key (str): The API key for authentication.

    Returns:
        bool: True if the update was successful, False otherwise.
    """

    url = f"{server_url}/devices/{device_id}/accumulators"
    headers = {
        "Authorization": f"Basic {api_key}",  # Assuming Basic Auth
        "Content-Type": "application/json"
    }
    payload = {
        "deviceId": device_id,
        "totalDistance": total_distance,
        "hours": hours
    }

    try:
        response = requests.put(url, headers=headers, json=payload)
        response.raise_for_status()  # Raise an exception for bad status codes
        return True
    except requests.exceptions.RequestException as e:
        print(f"Error updating device accumulators: {e}")
        return False
    except json.JSONDecodeError as e:
        print(f"Error decoding response: {e}")
        return False



# Example Usage (replace with your actual values)
server_url = "https://traccar.ipcs.synology.me"  # Replace with your Traccar server URL
device_id = 4  # Replace with the actual device ID
total_distance = 10000.0  # Replace with the new total distance
hours = 5.0  # Replace with the new total hours
api_key = "Traccar" #replace with your username and password

success = update_device_accumulators(server_url, device_id, total_distance, hours, api_key)

if success:
    print("Device accumulators updated successfully!")
else:
    print("Failed to update device accumulators.")

I am stuck, thanks for any help

Anton Tananaev 2 months ago

I think you're missing api in the path. In general I recommend comparing your API requests with what the official web app sends.

yv67 2 months ago

if I api I got this error :

Error updating device accumulators: 400 Client Error: Bad Request for url: https://traccar.ipcs.synology.me/api/devices/4/accumulators
yv67 2 months ago

If I try to reset the totaldistance from the web UI, the url is :

https://traccar.ipcs.synology.me/settings/accumulators/4

what is correct ?

Anton Tananaev 2 months ago

I'm talking about the API web uses, not the web page URL.

yv67 2 months ago

ok, but if I add api in the url I got error 400
I am new with traccar, how to see the API web use ?

Anton Tananaev 2 months ago

Typically you would use your browser developer tools. It's not Traccar specific.

yv67 2 months ago

ok, but with python, I have no browser developer tools ?

must the api be added in the url ?

https://traccar.ipcs.synology.me/api/devices/4/accumulators

but with api added I got an error 400

and without api I got an error 405

thanks for your help

yv67 2 months ago

ok, it works now... the json was not good

Darrylles 14 days ago

yv67,

It would be helpful if you detailed WHAT was wrong with your JSON. These pages aren't just to help YOU personally, but also, perhaps for hundreds of others who may encounter the same problem.

yv67 13 days ago

voici le script python qui fonctionne :
il faut mettre la liste_devices des id des trackers à jour, ici je n'en ai qu'un avec ID = 1
et aussi user, mot de passe et adresse du traccar

import json
import requests

Traccar_user = 'XXXXXXXXXXXXX'
Traccar_password = 'XXXXX'

headers = {'Content-type': 'application/json'}
liste_devices = [1]

def get_device_data(device_id):
    url = f"https://traccar.ipcs.synology.me/api/devices/{device_id}"
    response = requests.get(url, auth=(Traccar_user, Traccar_password), headers=headers)
    return response.json()

def raz_totaldistance(device_id):
    data = {'deviceId': device_id, 'totalDistance': 0, 'hours': 0}
    url = f"https://traccar.XXXXXXXXXX/api/devices/{device_id}/accumulators"
    response = requests.put(url, auth=(Traccar_user, Traccar_password), headers=headers, json=data)
    return response

if __name__ == "__main__":
    for device_id in liste_devices:
      # device_data = get_device_data(device_id)
    #   nom = device_data["name"]
      # print(f"Device Data: {device_data}")
      # print(nom)
        response = raz_totaldistance(device_id)
    #   print(response)
    exit()