Advertisement
Poganu

Safefleet-Presences

May 3rd, 2022 (edited)
990
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.84 KB | None | 0 0
  1. ############################################
  2. ######## GPS Trajectory / Presences ########
  3. ############################################
  4. #0. Import libraries
  5. #0. Declare links
  6. #1. Get current time
  7. #2. Authentificate for vehicle ID
  8. #3. Authentificate in Safefleet platform
  9. #3. Use vehicle ID to get presence
  10.  
  11.  
  12. #Get GPS data at specific time from truck
  13. #Currently it does 2 authentifications - only to get the vehicle_id - is it static and we should save it?
  14. #Trebuie să prelucrez rapoarte
  15.  
  16. #0. Import libraries
  17. import subprocess
  18. import requests
  19. import json
  20. import os
  21. import time
  22. import concurrent.futures
  23. from datetime import datetime
  24. import glob
  25. import datetime
  26.  
  27.  
  28. #0. Declare links
  29.  
  30.  
  31. presences_messages = "https://delta.safefleet.eu/safefleet/api/vehicles/21835/presences"
  32.  
  33.  
  34. #1. Get current time
  35. print("----------------1----------------")
  36. print("#1. Get current time")
  37. a = datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ')
  38. print(a)
  39.  
  40.  
  41. #2. Authentificate for vehicle ID
  42. #print("----------------2----------------")
  43. #print("#2. Authentificate for vehicle ID")
  44. '''
  45. #Authentificate vehicle of vehicle id
  46. api_safefleet_authenticate = 'https://delta.safefleet.eu/safefleet/api/authenticate_vehicle'
  47. authenticate = requests.post(api_safefleet_authenticate, headers = {'Content-Type': 'application/json'}, json = {
  48.    'license_plate': "TM-60-JYT",
  49.    'pin': "068318"
  50. })
  51. print("authenticate: ", authenticate.text, "\n")
  52.  
  53. vehicle_id = json.loads(authenticate.text)['vehicle']['vehicle_id']
  54. print("vehicle_id: ", vehicle_id, "\n")
  55. '''
  56.  
  57. vehicle_id = 21838
  58. car_license_plate = "TM-60-JYT"
  59. car_pin = "068318"
  60.  
  61.  
  62. #3. Authentificate in Safefleet platform
  63. print("----------------3----------------")
  64. print("#3. Authentificate in Safefleet platform")
  65.  
  66. api_safefleet_authenticate_vehicle = "https://delta.safefleet.eu/safefleet/api/authenticate_vehicle"
  67. #Authentificate with license plate
  68. authenticate_vehicle = requests.post(api_safefleet_authenticate_vehicle, \
  69.                      headers = {'Content-Type': 'application/json'}, \
  70.                      json = {'license_plate': car_license_plate, 'pin': car_pin})
  71.  
  72. print(f"authenticate_vehicle: {authenticate_vehicle}")
  73. print(f"authenticate_vehicle.text: {authenticate_vehicle.text}")
  74.  
  75. print("")
  76.  
  77. api_safefleet_authenticate_platform = 'https://delta.safefleet.eu/safefleet/api/authenticate'
  78. authenticate = requests.post(api_safefleet_authenticate_platform, \
  79.                headers = {'Content-Type': 'application/json'}, \
  80.                json = {'username': "polymore", 'password': "123456"})
  81. print(f"authenticate: {authenticate}")
  82. print(f"authenticate.text: {authenticate.text}")
  83.  
  84. '''
  85.  
  86. '''
  87. #4. Use vehicle ID to get presence
  88. print("----------------4----------------")
  89. print("#4. Use vehicle ID to get presence")
  90.  
  91. api_safefleet_vehicle_info = 'https://delta.safefleet.eu/safefleet/api/vehicles/'
  92. vehicle_info = requests.get(api_safefleet_vehicle_info + str(vehicle_id), cookies=authenticate.cookies)
  93. print(vehicle_info.url)
  94. parsed_vehicle_info = json.loads(vehicle_info.text)
  95.  
  96. print(f"parsed_vehicle_info: {parsed_vehicle_info}")
  97.  
  98. lat = parsed_vehicle_info['current_info']['lat']
  99. lng = parsed_vehicle_info['current_info']['lng']
  100. print(f"lat: {lat}")
  101. print(f"lng: {lng}")
  102.  
  103. print("----------------5----------------")
  104. #vehicle_info = requests.get(presences_messages, \
  105. #   json={'start_moment': '2022-04-22T11:28:40Z', 'stop_moment': '2022-04-22T11:29:40Z'}, cookies = authenticate.cookies)
  106.  
  107. api_safefleet_authenticate_platform = 'https://delta.safefleet.eu/safefleet/api/authenticate'
  108. authenticate = requests.post(api_safefleet_authenticate_platform, \
  109.                headers = {'Content-Type': 'application/json'}, \
  110.                json = {'username': "polymore", 'password': "123456"})
  111.  
  112. print(authenticate)
  113. print(authenticate.text)
  114. print()
  115.  
  116. api_safefleet_presences = f"https://delta.safefleet.eu/safefleet/api/vehicles/{vehicle_id}/presences/"
  117. print(api_safefleet_presences)
  118.  
  119. #presences = requests.get(api_safefleet_presences, \
  120. #   json = {'start_moment': '2022-05-03', 'stop_moment': '2022-05-03'}, cookies = authenticate.cookies)
  121.  
  122. #print(authenticate.cookies)
  123.  
  124.  
  125. print("----------------6----------------")
  126.  
  127. api_safefleet_presences = f"https://delta.safefleet.eu/safefleet/api/vehicles/{vehicle_id}/presences/"
  128.  
  129. #json=json.loads('{'start_moment': '2022-03-26T03:14:15Z', 'stop_moment': '2022-03-26T03:14:55'}')
  130. json2=json.loads('{"start_moment": "2022-03-26T03:14:15Z", "stop_moment": "2022-03-26T03:14:25Z"}')
  131. vehicle_info = requests.get(api_safefleet_presences, json=json2, cookies = authenticate.cookies)
  132. print(vehicle_info)
  133. print(vehicle_info.text)
  134.  
  135. #presences = requests.get(api_safefleet_presences, \
  136. #    json={'start_moment': '2022-05-03T17:12:44Z', 'stop_moment': '2022-05-03T17:22:44Z'})
  137.  
  138.  
  139. print("----------------7----------------")
  140.  
  141. #presences = requests.get(api_safefleet_presences, cookies = authenticate.cookies)
  142. #print(type(presences))
  143. #print("vehicle_info: ", presences.text, "\n")
  144.  
  145.  
  146. #parsed_vehicle_info = json.loads(presences.text)
  147. #print(type(parsed_vehicle_info))
  148. #print("parsed_vehicle_info: ", parsed_vehicle_info)
  149.  
  150. #print()
  151. #print(parsed_vehicle_info)#['start'])#['moment'])
  152. #print("\n\n")
  153.  
  154.  
  155. try:
  156.     vehicle_id2 = json.loads(authenticate.text)['vehicle']['vehicle_id']
  157.     vehicle_info2 = requests.get(api_safefleet_vehicle_info + str(vehicle_id), cookies=authenticate.cookies)
  158.     parsed_vehicle_info2 = json.loads(vehicle_info2.text)
  159.  
  160.     lat = parsed_vehicle_info2['current_info']['lat']
  161.     lng = parsed_vehicle_info2['current_info']['lng']
  162.     print(lat, lng)
  163.  
  164. except KeyError as e:
  165.     print(f"KeyError: {e}")
  166.  
  167.  
  168. #journey = json.loads(vehicle_info.text)[0]['start']['moment']
  169.  
  170.  
  171. #journey = parsed_vehicle_info[0]['start']['moment']
  172. #print("lat: ", journey)
  173. #lat = parsed_vehicle_info['current_info']['lat']
  174. #lng = parsed_vehicle_info['current_info']['lng']
  175. #print(lat, lng)
  176.  
  177. '''
  178. authenticate = requests.post(api_safefleet_authenticate, headers = {'Content-Type': 'application/json'}, json = {
  179.    'username': "polymore",
  180.    'password': "123456"
  181. })
  182.  
  183. '''
  184. '''
  185. print(authenticate.text)
  186. print()
  187.  
  188. #/vehicles/{vehicle_id}/journeys
  189.  
  190. vehicle_id = json.loads(authenticate.text)['vehicles']['vehicle_id']#['journeys']
  191. print(vehicle_id.text)
  192. print()
  193.  
  194.  
  195.  
  196. #/vehicles/{vehicle_id}/journeys
  197. '''
  198. '''
  199. "journeys": [
  200.            {
  201.                "journey_id": String,
  202.                "start": {
  203.                    "moment": String,
  204.                    "lat": Number,
  205.                    "lng": Number
  206.                },
  207.  
  208. [
  209.    {
  210.        "presence_id": String,
  211.        "moment": String,
  212.        "lat": Number,
  213.        "lng": Number,
  214.        "speed": Number,
  215.        "azimuth": Number,
  216.        "engine": Boolean
  217.    },
  218.    ...
  219. ]
  220. -----------------------------------
  221. [
  222.  {
  223.    "driver_id": null,
  224.    "start": {
  225.      "lat": 46.204275,
  226.      "moment": "2022-04-11T09:49:28Z",
  227.      "lng": 21.3176666
  228.    },
  229.    "max_speed": 0,
  230.    "purpose": null,
  231.    "consumption": {
  232.      "measured": true,
  233.      "value": 0
  234.    },
  235.    "distance": 0.006,
  236.    "idle_duration": 0.0,
  237.    "journey_code": null,
  238.    "stop": {
  239.      "lat": 46.2042,
  240.      "moment": "2022-04-11T09:49:46Z",
  241.      "lng": 21.3176583
  242.    },
  243.    "journey_id": "6253f9bd62768638e1c980b6"
  244.  }
  245. ]
  246.  
  247.  
  248. [
  249.    {
  250.        "journey_id": String,
  251.        "start": {
  252.            "moment": String,
  253.            "lat": Number,
  254.            "lng": Number
  255.        },
  256.        "stop": {
  257.            "moment": String,
  258.            "lat": Number,
  259.            "lng": Number
  260.        },
  261.        "distance": Number,
  262.        "idle_duration": Number,
  263.        "consumption": {
  264.            "value": Number,
  265.            "measured": Boolean
  266.        },
  267.        "max_speed": Number,
  268.        "driver_id": Number,
  269.        "purpose": String,
  270.        "journey_code": String
  271.    },
  272.    ...
  273. ]
  274.  
  275. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement