Administrator
2023-08-21 a019311b0edee6df82a8ec7c0b28b06b22aa4d31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# -*- coding: utf-8 -*-
"""
命令管理器
"""
import json
import logging
import threading
 
from huaxin_client import socket_util
from huaxin_client.client_network import SendResponseSkManager
from log_module.log import logger_local_huaxin_trade_debug
 
MSG_TYPE_HEART = "heart"
# 命令信息
MSG_TYPE_CMD = "cmd"
 
CLIENT_TYPE_TRADE = "trade"
CLIENT_TYPE_DELEGATE_LIST = "delegate_list"
CLIENT_TYPE_DEAL_LIST = "deal_list"
CLIENT_TYPE_POSITION_LIST = "position_list"
CLIENT_TYPE_MONEY = "money"
CLIENT_TYPE_DEAL = "deal"
 
CLIENT_TYPE_CMD_L2 = "l2_cmd"
 
# 心跳时间间隔
HEART_SPACE_TIME = 3
 
 
class TradeActionCallback(object):
    # 交易
    def OnTrade(self, client_id, request_id, type_, data):
        pass
 
    # 委托列表
    def OnDelegateList(self, client_id, request_id):
        pass
 
    # 成交列表
    def OnDealList(self, client_id, request_id):
        pass
 
    # 成交列表
    def OnPositionList(self, client_id, request_id):
        pass
 
    # 获取资金信息
    def OnMoney(self, client_id, request_id):
        pass
 
 
class L2ActionCallback(object):
    # 监听L2数据
    def OnSetL2Position(self, client_id, request_id, codes_data):
        pass
 
 
# 交易指令管理
class TradeCommandManager:
    trade_client_dict = {}
    _instance = None
 
    def __new__(cls, *args, **kwargs):
        if not cls._instance:
            cls._instance = super().__new__(cls, *args, **kwargs)
        return cls._instance
 
    @classmethod
    def init(cls, trade_action_callback, pipe_l2, pipe_strategy):
        cls.action_callback = trade_action_callback
        cls.pipe_strategy = pipe_strategy
        cls.pipe_l2 = pipe_l2
 
    @classmethod
    def __process_command(cls, _type, client_id, result_json):
        try:
            data = result_json["data"]
            print("接收内容", result_json)
            request_id = result_json.get('request_id')
            if not socket_util.is_client_params_sign_right(result_json):
                print("签名错误")
                # 签名出错
                SendResponseSkManager.send_error_response(_type, request_id, client_id,
                                                          {"code": -1, "msg": "签名错误"})
                return
 
            if _type == CLIENT_TYPE_TRADE:
                # 交易
                ctype = data["trade_type"]
                cls.action_callback.OnTrade(client_id, request_id, ctype, data)
            elif _type == CLIENT_TYPE_MONEY:
                cls.action_callback.OnMoney(client_id, request_id)
            elif _type == CLIENT_TYPE_DEAL_LIST:
                cls.action_callback.OnDealList(client_id, request_id)
            elif _type == CLIENT_TYPE_DELEGATE_LIST:
                can_cancel = data["can_cancel"]
                cls.action_callback.OnDelegateList(client_id, request_id, can_cancel)
            elif _type == CLIENT_TYPE_POSITION_LIST:
                cls.action_callback.OnPositionList(client_id, request_id)
        except Exception as e:
            logger_local_huaxin_trade_debug.debug(f"__process_command出错:{result_json}")
            logging.exception(e)
            logging.error(result_json)
 
    @classmethod
    def run_process_command(cls, pipe_strategy):
        if pipe_strategy is None:
            return
        # 本地命令接收
        while True:
            try:
                val = pipe_strategy.recv()
                if val:
                    val = json.loads(val)
                    print("run_process_command",val)
                    _type = val["type"]
                    _data = val["data"]
                    # 查看是否是设置L2的代码
                    if _type == CLIENT_TYPE_CMD_L2:
                        cls.pipe_l2.send(
                            json.dumps({"type": "set_l2_codes", "data": _data}))
                    else:
                        t1 = threading.Thread(target=lambda: cls.__process_command(_type, None, val), daemon=True)
                        t1.start()
            except Exception as e:
                logging.exception(e)
 
    # 维护连接数的稳定
    def run(self, blocking=True):
        if blocking:
            self.run_process_command(self.pipe_strategy)
        else:
            # 接受命令
            t1 = threading.Thread(target=lambda: self.run_process_command(self.pipe_strategy), daemon=True)
            t1.start()
 
 
# L2指令管理
class L2CommandManager:
 
    @classmethod
    def init(cls, l2_action_callback):
        cls.action_callback = l2_action_callback
 
    @classmethod
    def process_command(cls, _type, client_id, result_json):
        data = result_json["data"]
        request_id = result_json["request_id"]
        ctype = data["type"]
        if not socket_util.is_client_params_sign_right(result_json):
            # 签名出错
            SendResponseSkManager.send_error_response(_type, request_id, client_id,
                                                      {"code": -1, "msg": "签名错误"})
            return
        codes_data = data["data"]
        if ctype == CLIENT_TYPE_CMD_L2:
            cls.action_callback.OnSetL2Position(client_id, request_id, codes_data)
 
 
if __name__ == "__main__":
    manager = TradeCommandManager("127.0.0.1", 10008, None)
    manager.run()
    input()