def upload_sample(host, port, data_path): url = f'http://{host}:{port}/{SERVER_SNAPSHOT_PATH}' with Reader(data_path) as reader: user = reader.get_user() for snapshot in reader: protocol = ProtocolManager(CLIENT_SERVER_PROTOCOL) response = requests.post(url=url, data=protocol.serialize(user, snapshot))
def post_snapshot(): client_server_protocol = ProtocolManager(CLIENT_SERVER_PROTOCOL) server_mq_protocol = ProtocolManager(SERVER_MQ_PROTOCOL) # TODO: https://tedboy.github.io/flask/generated/generated/flask.Request.get_data.html here it says # it's a bad idea to call get_data() method without checking the content length # Idea for security implementation # TODO: Consider setting get_data cache parameter to false client_data = flask.request.get_data() user, snapshot = client_server_protocol.deserialize(client_data) if handler: # TODO: really dont like this, need to understand more clearly what this handler needs to do handler(user, snapshot) # TODO: return viable template return "" user_dict, snapshot_dict = client_server_protocol.convert_to_python_dict( user, snapshot) publishable_user, publishable_snapshot = server_mq_protocol.serialize( user_dict, snapshot_dict) color_image_data = snapshot.color_image.data # TODO: why not serialize also color_image # TODO: fix serialize and use that instead of json dumps, figure out why encode is needed depth_image_data = json.dumps(list( snapshot.depth_image.data)).encode("utf-8") # TODO: dont like how arguments are delivered to function here, fix with how we use snapshot_dict FSM.save(color_image_data, snapshot_dict['color_image_path']) FSM.save(depth_image_data, snapshot_dict['depth_image_path']) mq = MessageQManager(url) # TODO: consider relevant exception mq.publish("user", publishable_user) mq.publish("snapshot", publishable_snapshot) # TODO: return viable template return ""
import matplotlib.pyplot as plt import pathlib from cortexio.platforms.protocols import ProtocolManager from cortexio.utils import FileSystemManager as FSM from cortexio import PARSER_MQ_PROTOCOL, BASE_SNAPSHOT_IMAGE_PATH import numpy import matplotlib serializer = ProtocolManager(PARSER_MQ_PROTOCOL) class DepthImageParser: scheme = "depth_image" @staticmethod def parse(snapshot): width = snapshot["depth_image_width"] height = snapshot["depth_image_height"] path = snapshot["depth_image_path"] # TODO: is user_id really necessary to be included in the snapshot if we can use unique snapshot_id user_id = snapshot["user_id"] snapshot_id = snapshot["snapshot_id"] fig_height, fig_width = _prepare_fig(width, height, path) base_path = pathlib.Path(BASE_SNAPSHOT_IMAGE_PATH) image_path = base_path / user_id / snapshot_id / "depth_image.png" image_path.parent.mkdir(parents=True, exist_ok=True)
import threading from cortexio.platforms.databases import DatabaseManager from cortexio.platforms.protocols import ProtocolManager from cortexio.platforms.messageQ import MessageQManager from cortexio.parsers import parsers from cortexio import SAVER_MQ_PROTOCOL serializer = ProtocolManager(SAVER_MQ_PROTOCOL) class Saver: def __init__(self, db_url): self.db = DatabaseManager(db_url) def save(self, topic, data): deserialized_data = serializer.deserialize(data) if topic == "user": self.db.insert_user(deserialized_data) else: self.db.insert_results(deserialized_data) def run_saver(self, topic, mq_url): mq = MessageQManager(mq_url) def handler(data): self.save(topic, data) mq.consume(topic, handler)
import pytest import requests from cortexio.client import upload_sample from cortexio.platforms.protocols import ProtocolManager from cortexio import CLIENT_SERVER_PROTOCOL client_server_protocol = ProtocolManager(CLIENT_SERVER_PROTOCOL) def test_send_snapshot(data_dir, requests_post_data): sample = data_dir / 'snapshot.gz' upload_sample('127.0.0.1', 12345, sample) message = requests_post_data[0] user, snapshot = client_server_protocol.deserialize(message) assert user.user_id == 42 assert user.username == 'Dan Gittik' assert snapshot.feelings.happiness == 0 assert snapshot.pose.rotation.x == -0.10888676356214629 @pytest.fixture def requests_post_data(monkeypatch): post_message = [] class MockResponse: status_code = 200 def mock_post(url, data): post_message.append(data)
import flask from cortexio.platforms.protocols import ProtocolManager from cortexio.platforms.databases import DatabaseManager from cortexio import API_DB_PROTOCOL, DEFAULT_DATABASE_URL from flask import jsonify, send_file app = flask.Flask(__name__) db = None client_server_protocol = ProtocolManager(API_DB_PROTOCOL) def run_api_server(host, port, database_url): global db db = DatabaseManager(database_url) app.run(host=host, port=port) def _wrap_response(data): response = jsonify(data) response.headers.add('Access-Control-Allow-Origin', '*') return response #TODO: make jsonify call prettier @app.route("/users", methods=['GET']) def get_users(): data = db.get_users() users = [(user["user_id"], user["username"]) for user in data]