Verzögerung eingebaut
This commit is contained in:
@@ -10,6 +10,7 @@ import time
|
||||
|
||||
PORT = 8082
|
||||
EXOMY_CONTAINER = 'exomy_autostart'
|
||||
DELAY_FILE = '/tmp/exomy_delay.txt'
|
||||
MOTOR_TEST_LOCK = threading.Lock()
|
||||
|
||||
|
||||
@@ -220,6 +221,33 @@ def collect_status():
|
||||
}
|
||||
|
||||
|
||||
def get_delay():
|
||||
try:
|
||||
with open(DELAY_FILE) as f:
|
||||
return max(0.0, min(10.0, float(f.read().strip())))
|
||||
except Exception:
|
||||
return 0.0
|
||||
|
||||
|
||||
def set_delay(seconds):
|
||||
seconds = max(0.0, min(10.0, float(seconds)))
|
||||
data = str(seconds).encode()
|
||||
try:
|
||||
fd = os.open(DELAY_FILE, os.O_WRONLY | os.O_TRUNC)
|
||||
except FileNotFoundError:
|
||||
fd = os.open(DELAY_FILE, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o666)
|
||||
try:
|
||||
os.write(fd, data)
|
||||
finally:
|
||||
os.close(fd)
|
||||
subprocess.run(
|
||||
['docker', 'exec', EXOMY_CONTAINER, 'bash', '-c',
|
||||
f'source /opt/ros/melodic/setup.bash && rosparam set /delay_seconds {seconds}'],
|
||||
capture_output=True, check=False
|
||||
)
|
||||
return seconds
|
||||
|
||||
|
||||
def restart_camera_service():
|
||||
subprocess.Popen(['systemctl', 'restart', 'exomy-camera-stream.service'])
|
||||
|
||||
@@ -449,6 +477,9 @@ class Handler(http.server.BaseHTTPRequestHandler):
|
||||
self.end_headers()
|
||||
|
||||
def do_GET(self):
|
||||
if self.path == '/api/delay':
|
||||
self.write_json(200, {'delay_seconds': get_delay()})
|
||||
return
|
||||
if self.path == '/api/status':
|
||||
self.write_json(200, collect_status())
|
||||
return
|
||||
@@ -469,6 +500,24 @@ class Handler(http.server.BaseHTTPRequestHandler):
|
||||
self.write_json(404, {'status': 'Unbekannt'})
|
||||
|
||||
def do_POST(self):
|
||||
if self.path == '/api/delay':
|
||||
try:
|
||||
content_length = int(self.headers.get('Content-Length', '0'))
|
||||
except ValueError:
|
||||
content_length = 0
|
||||
raw_body = self.rfile.read(content_length) if content_length > 0 else b'{}'
|
||||
try:
|
||||
payload = json.loads(raw_body.decode('utf-8'))
|
||||
except (UnicodeDecodeError, json.JSONDecodeError):
|
||||
self.write_json(400, {'status': 'Ungültige Anfrage'})
|
||||
return
|
||||
try:
|
||||
seconds = set_delay(payload.get('delay_seconds', 0))
|
||||
self.write_json(200, {'status': 'Delay gesetzt', 'delay_seconds': seconds})
|
||||
except Exception as exc:
|
||||
self.write_json(500, {'status': str(exc)})
|
||||
return
|
||||
|
||||
if self.path == '/api/motor-test/drive-neutral/preview':
|
||||
try:
|
||||
content_length = int(self.headers.get('Content-Length', '0'))
|
||||
|
||||
Reference in New Issue
Block a user