Files
Mir_Server/mission_utils.py
2025-03-18 12:20:06 +02:00

106 lines
4.0 KiB
Python

import time
import sqlite3
import requests
from instances.db import DB_PATH
def delayed_post_mission(mission_id):
print("Starting delayed_post_mission function")
time.sleep(5) # Delay for 5 seconds
post_mission(mission_id)
def post_mission(mission_id):
print("Starting post_mission function")
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute("SELECT ip, authorization, username, password FROM mir_server_missions LIMIT 1")
row = c.fetchone()
conn.close()
if row:
ip, authorization, username, password = row
print(f"Retrieved MIR server details: IP={ip}, Authorization={authorization}")
f_host = f"http://{ip}/api/v2.0.0/"
headers = {
"accept": "application/json",
"Authorization": authorization,
"Accept-Language": "en_US",
"Content-Type": "application/json"
}
print(f"Sending POST request to {f_host}mission_scheduler with mission_id={mission_id}")
response = requests.post(f"{f_host}mission_scheduler", headers=headers, json=mission_id)
print(f"Received response with status code {response.status_code}")
if response.status_code in {200, 201}:
print("Mission sent successfully")
response_data = response.json()
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute("INSERT INTO requested_missions (state, start_time, mission, mission_name, robot_id, id) VALUES (?, ?, ?, ?, ?, ?)",
(response_data['state'], response_data['start_time'], response_data['mission'], response_data['mission_name'], response_data['robot_id'], response_data['id']))
conn.commit()
conn.close()
else:
print(f"Failed to send mission, status code: {response.status_code}, response: {response.text}")
else:
print("No MIR server details found in the database")
def get_robots():
fleetip = "10.76.153.4"
f_host = f"http://{fleetip}/api/v2.0.0/"
headers = {
"accept": "application/json",
"Authorization": "Basic ZGlzdHJpYnV0b3I6Y2RjYjhiNjAzYzFhZDNjNjVkZTM4ZGY2OWU5YjFkM2ZhMDA2OWEwMDcyMzZkMDNkOGVhNjMyNDVhMDg3YjJkZA==",
"Accept-Language": "en_US",
"Content-Type": "application/json"
}
# Get robots URL for next posts
response = requests.get(f_host + "robots", headers=headers)
print(f"Response status code: {response.status_code}")
print(f"Response content: {response.text}")
if response.status_code == 200:
try:
robots = response.json()
print(robots)
except requests.exceptions.JSONDecodeError as e:
print(f"Error decoding JSON: {e}")
return
except ValueError as e:
print(f"Error decoding JSON: {e}")
return
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
for robot in robots:
ip = robot['ip']
url = robot['url']
c.execute('''INSERT OR REPLACE INTO robots (ip, url) VALUES (?, ?)''', (ip, url))
conn.commit()
conn.close()
else:
print(f"Failed to get robots. Status code: {response.status_code}, Response: {response.text}")
def get_status(robotid):
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute("SELECT ip, url FROM robots WHERE ip = ?", (robotid,))
row = c.fetchone()
conn.close()
if row:
ip, url = row
host = f"http://{ip}/api"
headers = {
"accept": "application/json",
"Authorization": "Basic ZGlzdHJpYnV0b3I6Y2RjYjhiNjAzYzFhZDNjNjVkZTM4ZGY2OWU5YjFkM2ZhMDA2OWEwMDcyMzZkMDNkOGVhNjMyNDVhMDg3YjJkZA==",
"Accept-Language": "en_US",
"Content-Type": "application/json"
}
get_robot_id = requests.get(host + url, headers=headers)
status = get_robot_id.json()
mission_text = status['status']['mission_text']
return mission_text
else:
print("No robot details found in the database")
return None