| | |
| | | import data_process |
| | | import gpcode_manager |
| | | import authority |
| | | import juejin |
| | | import l2_data_manager |
| | | import tool |
| | | import trade_manager |
| | | import l2_code_operate |
| | | |
| | | from log import logger_l2_error, logger_l2_process, logger_device |
| | | from log import logger_l2_error, logger_l2_process, logger_device, logger_trade_delegate |
| | | |
| | | |
| | | class MyTCPServer(socketserver.TCPServer): |
| | |
| | | reset_code_dict = {} |
| | | set_operate_code_state_dict = {} |
| | | l2_data_error_dict = {} |
| | | last_trade_delegate_data = None |
| | | |
| | | def setup(self): |
| | | super().setup() # 可以不调用父类的setup()方法,父类的setup方法什么都没做 |
| | |
| | | # 间隔2s |
| | | if key not in self.reset_code_dict or round( |
| | | time.time() * 1000) - self.reset_code_dict[key] > 2000: |
| | | |
| | | self.l2CodeOperate.set_operate_code_state(client, channel, 0) |
| | | self.reset_code_dict[key] = round(time.time() * 1000) |
| | | if code_ is None: |
| | | code_ = "" |
| | | self.l2CodeOperate.repaire_operate(int(client), int(channel), code_) |
| | | if tool.is_trade_time(): |
| | | self.l2CodeOperate.repaire_operate(int(client), int(channel), code_) |
| | | else: |
| | | key = "{}-{}".format(client, channel) |
| | | if key not in self.set_operate_code_state_dict or round( |
| | |
| | | |
| | | gpcode_manager.set_gp_list(code_list) |
| | | # 重新订阅 |
| | | self.server.pipe.send("resub") |
| | | self.server.pipe.send(json.dumps({"type": "resub"})) |
| | | sync_target_codes_to_ths() |
| | | elif type == 2: |
| | | # 涨停代码 |
| | | codeList = data_process.parseGPCode(_str) |
| | |
| | | elif type == 5: |
| | | # 交易委托信息 |
| | | dataList = data_process.parseList(_str) |
| | | if self.last_trade_delegate_data != _str: |
| | | self.last_trade_delegate_data = _str |
| | | # 保存委托信息 |
| | | logger_trade_delegate.info(dataList) |
| | | try: |
| | | trade_manager.process_trade_delegate_data(dataList) |
| | | except Exception as e: |
| | |
| | | {"code": 0, "data": {"client": int(client_id), "authoritys": json.loads(_authoritys)}}) |
| | | except Exception as e: |
| | | return_str = data_process.toJson({"code": 1, "msg": str(e)}) |
| | | elif type == 40: |
| | | data = data_process.parse(_str)["data"] |
| | | for item in data: |
| | | juejin.accpt_price(item["code"], float(item["price"])) |
| | | |
| | | elif type == 30: |
| | | data = data_process.parse(_str)["data"] |
| | | client_id = data["client"] |
| | |
| | | |
| | | def send_msg(client_id, data): |
| | | _ip = data_process.getActiveClientIP(client_id) |
| | | print("ip", client_id, _ip) |
| | | if _ip is None or len(_ip) <= 0: |
| | | raise Exception("客户端IP为空") |
| | | socketClient = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| | |
| | | time.sleep(5) |
| | | |
| | | |
| | | # 获取采集客户端的状态 |
| | | def get_client_env_state(client): |
| | | result = send_msg(client, {"action": "getEnvState"}) |
| | | result = json.loads(result) |
| | | if result["code"] == 0: |
| | | return json.loads(result["data"]) |
| | | else: |
| | | raise Exception(result["msg"]) |
| | | |
| | | |
| | | # 修复采集客户端 |
| | | def repair_client_env(client): |
| | | result = send_msg(client, {"action": "repairEnv"}) |
| | | result = json.loads(result) |
| | | if result["code"] != 0: |
| | | raise Exception(result["msg"]) |
| | | |
| | | |
| | | # 同步目标标的到同花顺 |
| | | def sync_target_codes_to_ths(): |
| | | codes = gpcode_manager.get_gp_list() |
| | | code_list = [] |
| | | for code in codes: |
| | | code_list.append(code) |
| | | client = authority._get_client_ids_by_rule("client-industry") |
| | | result = send_msg(client[0], {"action": "syncTargetCodes", "data": code_list}) |
| | | return result |
| | | |
| | | |
| | | if __name__ == "__main__": |
| | | code = "000503" |
| | | client_id, pos = gpcode_manager.get_listen_code_pos(code) |
| | | if client_id is not None and pos is not None: |
| | | # 获取涨停价与跌停价 |
| | | max_price = gpcode_manager.get_limit_up_price(code) |
| | | min_price = gpcode_manager.get_limit_down_price(code) |
| | | data = {"action": "repairL2Data", |
| | | "data": {"index": int(pos), "code": code, "min_price": float(min_price), |
| | | "max_price": float(max_price)}} |
| | | send_msg(client_id, data) |
| | | # 有值 |
| | | pass |
| | | try: |
| | | result = get_client_env_state(3) |
| | | print(result) |
| | | except Exception as e: |
| | | print(str(e)) |