Refactored utils.bus.send_request.

It should return the response output and raise an error if the response
has errors.
This commit is contained in:
Fabio Manganiello 2023-11-04 16:22:27 +01:00
parent f7fe844296
commit 7c780e6650
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774
3 changed files with 15 additions and 13 deletions

View file

@ -29,8 +29,9 @@ def video_feed():
frame = output.frame frame = output.frame
if frame and len(frame): if frame and len(frame):
yield (b'--frame\r\n' yield (
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n'
)
@camera_pi.route('/camera/pi/frame', methods=['GET']) @camera_pi.route('/camera/pi/frame', methods=['GET'])
@ -38,18 +39,18 @@ def video_feed():
def get_frame_img(): def get_frame_img():
filename = os.path.join(tempfile.gettempdir(), 'camera_pi.jpg') filename = os.path.join(tempfile.gettempdir(), 'camera_pi.jpg')
response = send_request('camera.pi.take_picture', image_file=filename) response = send_request('camera.pi.take_picture', image_file=filename)
frame_file = (response.output or {}).get('image_file') frame_file = (response or {}).get('image_file')
assert frame_file is not None assert frame_file is not None
return send_from_directory(os.path.dirname(frame_file), return send_from_directory(
os.path.basename(frame_file)) os.path.dirname(frame_file), os.path.basename(frame_file)
)
@camera_pi.route('/camera/pi/stream', methods=['GET']) @camera_pi.route('/camera/pi/stream', methods=['GET'])
@authenticate() @authenticate()
def get_stream_feed(): def get_stream_feed():
return Response(video_feed(), return Response(video_feed(), mimetype='multipart/x-mixed-replace; boundary=frame')
mimetype='multipart/x-mixed-replace; boundary=frame')
# vim:sw=4:ts=4:et: # vim:sw=4:ts=4:et:

View file

@ -29,16 +29,12 @@ class SoundRoute(StreamingRoute):
@contextmanager @contextmanager
def _audio_stream(self, **kwargs) -> Generator[None, None, None]: def _audio_stream(self, **kwargs) -> Generator[None, None, None]:
response = send_request( send_request(
'sound.record', 'sound.record',
dtype='int16', dtype='int16',
**kwargs, **kwargs,
) )
assert response and not response.is_error(), (
'Streaming error: ' + str(response.errors) if response else '(unknown)'
)
yield yield
send_request('sound.stop_recording') send_request('sound.stop_recording')

View file

@ -64,4 +64,9 @@ def send_request(action, wait_for_response=True, **kwargs):
if kwargs: if kwargs:
msg['args'] = kwargs msg['args'] = kwargs
return send_message(msg, wait_for_response=wait_for_response) rs = send_message(msg, wait_for_response=wait_for_response)
assert rs, 'Got an empty response from the server'
if rs:
assert not rs.errors, '\n'.join(rs.errors)
return rs.output