diff options
author | Evgeny Zinoviev <me@ch1p.io> | 2022-06-14 02:44:43 +0300 |
---|---|---|
committer | Evgeny Zinoviev <me@ch1p.io> | 2022-06-14 22:56:46 +0300 |
commit | e3d3d6b76010a6dd5c417f017339bec17fb07887 (patch) | |
tree | 42cb6194504ae863db2bf7d21ef9e2acd41d0fd2 /test/test_record_upload.py | |
parent | 600fdf99ffd893857c9cdb9e68140766a963bd17 (diff) |
media: refactor sound_node, introduce camera_node
Diffstat (limited to 'test/test_record_upload.py')
-rwxr-xr-x | test/test_record_upload.py | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/test/test_record_upload.py b/test/test_record_upload.py new file mode 100755 index 0000000..a0c3faf --- /dev/null +++ b/test/test_record_upload.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python3 +import logging +import sys +import os.path +sys.path.extend([ + os.path.realpath( + os.path.join(os.path.dirname(os.path.join(__file__)), '..', '..') + ) +]) + +import time + +from src.home.api import WebAPIClient, RequestParams +from src.home.config import config +from src.home.media import SoundRecordClient +from src.home.util import parse_addr + +logger = logging.getLogger(__name__) + + +# record callbacks +# ---------------- + +def record_error(info: dict, userdata: dict): + node = userdata['node'] + # TODO + + +def record_finished(info: dict, fn: str, userdata: dict): + logger.info('record finished: ' + str(info)) + + node = userdata['node'] + api.upload_recording(fn, node, info['id'], int(info['start_time']), int(info['stop_time'])) + + +# api client callbacks +# -------------------- + +def api_error_handler(exc, name, req: RequestParams): + if name == 'upload_recording': + logger.error('failed to upload recording, exception below') + logger.exception(exc) + + else: + logger.error(f'api call ({name}, params={req.params}) failed, exception below') + logger.exception(exc) + + +def api_success_handler(response, name, req: RequestParams): + if name == 'upload_recording': + node = req.params['node'] + rid = req.params['record_id'] + + logger.debug(f'successfully uploaded recording (node={node}, record_id={rid}), api response:' + str(response)) + + # deleting temp file + try: + os.unlink(req.files['file']) + except OSError as exc: + logger.error(f'error while deleting temp file:') + logger.exception(exc) + + record.forget(node, rid) + + +if __name__ == '__main__': + config.load('test_record_upload') + + nodes = {} + for name, addr in config['nodes'].items(): + nodes[name] = parse_addr(addr) + record = SoundRecordClient(nodes, + error_handler=record_error, + finished_handler=record_finished, + download_on_finish=True) + + api = WebAPIClient() + api.enable_async(error_handler=api_error_handler, + success_handler=api_success_handler) + + record_id = record.record('localhost', 3, {'node': 'localhost'}) + print(f'record_id: {record_id}') + + while True: + try: + time.sleep(0.1) + except (KeyboardInterrupt, SystemExit): + break
\ No newline at end of file |