import json
|
import logging
|
|
from api.outside_api_command_manager import ActionCallback
|
from huaxin_client.client_network import SendResponseSkManager
|
from strategy import strategy_params_settings
|
from utils import socket_util, middle_api_protocol
|
|
|
class MyAPICallback(ActionCallback):
|
|
@classmethod
|
def __send_response(cls, data_bytes):
|
sk = SendResponseSkManager.create_send_response_sk(addr=middle_api_protocol.SERVER_HOST,
|
port=middle_api_protocol.SERVER_PORT)
|
try:
|
data_bytes = socket_util.load_header(data_bytes)
|
sk.sendall(data_bytes)
|
result, header_str = socket_util.recv_data(sk)
|
result = json.loads(result)
|
if result["code"] != 0:
|
raise Exception(result['msg'])
|
finally:
|
sk.close()
|
|
def send_response(self, data, _client_id, _request_id):
|
data_bytes = json.dumps({"type": "response", "data": data, "client_id": _client_id,
|
"request_id": _request_id}).encode('utf-8')
|
for i in range(3):
|
try:
|
self.__send_response(data_bytes)
|
# print("发送数据成功")
|
break
|
except Exception as e1:
|
logging.exception(e1)
|
|
def __on_get_settings(self):
|
"""
|
获取交易参数
|
@return:
|
"""
|
result = strategy_params_settings.settings.to_json_str()
|
return json.dumps({"code": 0, "data": result})
|
|
def __on_get_env(self):
|
"""
|
获取环境信息
|
@return:
|
"""
|
return json.dumps({"code": 0, "data": {}, "msg": "测试结果"})
|
|
def OnCommonRequest(self, client_id, request_id, data):
|
ctype = data["ctype"]
|
result_str = ''
|
if ctype == "get_settings":
|
result_str = self.__on_get_settings()
|
elif ctype == 'get_env':
|
result_str = self.__on_get_env()
|
|
self.send_response(result_str, client_id, request_id)
|