admin
2022-08-18 67a5e3d825fde17b7b00906ce42b0bd8cafebc4e
ConsoleApplication/L2DataCapture.cpp
@@ -4,28 +4,57 @@
#include "THSActionUtil.h"
#include <thread>
bool L2DataCapture::inited;
 OpenCLExcuter* L2DataCapture::openCLExcuter[THS_FRAME_COUNT];
 TradeQueueCapture* L2DataCapture::tradeQueueCapture[THS_FRAME_COUNT];
bool L2DataCapture::tradeTimeCapture;
OpenCLExcuter* L2DataCapture::openCLExcuter[THS_FRAME_COUNT];
TradeQueueCapture* L2DataCapture::tradeQueueCapture[THS_FRAME_COUNT];
//是否正在执行
 bool L2DataCapture::running;
 bool L2DataCapture::runnings[THS_FRAME_COUNT];
 clock_t L2DataCapture::latest_running_times[THS_FRAME_COUNT];
bool L2DataCapture::running;
bool L2DataCapture::runnings[THS_FRAME_COUNT];
clock_t L2DataCapture::latest_running_times[THS_FRAME_COUNT];
 CallbackFun L2DataCapture::data_callback;
CallbackFun L2DataCapture::data_callback;
  CallbackFun_Trade_Queue L2DataCapture::trade_queue_data_callback;
CallbackFun_Trade_Queue L2DataCapture::trade_queue_data_callback;
 string L2DataCapture::gpCodes[THS_FRAME_COUNT];
string L2DataCapture::gpCodes[THS_FRAME_COUNT];
 void* L2DataCapture::context;
void* L2DataCapture::context;
//将当前时间换算成秒
int getNowSecondTime() {
   time_t timep;
   time(&timep);
   char tmp_h[64];
   strftime(tmp_h, sizeof(tmp_h), "%H", localtime(&timep));
   char tmp_m[64];
   strftime(tmp_m, sizeof(tmp_m), "%M", localtime(&timep));
   char tmp_s[64];
   strftime(tmp_s, sizeof(tmp_s), "%S", localtime(&timep));
   return stoi(string(tmp_h)) * 3600 + stoi(string(tmp_m)) * 60 + stoi(string(tmp_s));
}
//运行
void L2DataCapture::_run(int index)
{
   while (true) {
      if (running && runnings[index]&& gpCodes[index].length()>0) {
      if (tradeTimeCapture) {
         int time = getNowSecondTime();
         //9:20-11:31  12:59-15:01
         bool in = false;
         if ((time >= 9 * 3600 + 20 * 60 && time <= 11 * 3600 + 31 * 60) || (time >= 12 * 3600 + 59 * 60 && time <= 15 * 3600 + 1 * 60)) {
            in = true;
         }
         if (!in) {
            Sleep(2);
            continue;
         }
      }
      if (running && runnings[index] && gpCodes[index].length() > 0) {
         latest_running_times[index] = clock();
         //识别数据
         string code = gpCodes[index];
@@ -34,7 +63,7 @@
            data_callback(index, code, resultList, context);
         }
         catch (...) {
         }
      }
      Sleep(2);
@@ -49,7 +78,7 @@
         //识别数据
         string code = gpCodes[index];
         try {
            list<int> numList= tradeQueueCapture[index]->recognition(CaptureUtil::getHWND(index,CAPTURE_TYPE_TRADE_QUEUE));
            list<int> numList = tradeQueueCapture[index]->recognition(CaptureUtil::getHWND(index, CAPTURE_TYPE_TRADE_QUEUE));
            trade_queue_data_callback(index, code, numList, context);
         }
         catch (...) {
@@ -70,7 +99,12 @@
}
string L2DataCapture::getGPCode(int index) {
    return   gpCodes[index];
   return   gpCodes[index];
}
void L2DataCapture::setTradeTimeCapture(bool enable)
{
   tradeTimeCapture = enable;
}
static string getGPCode(int index);
@@ -113,7 +147,7 @@
   int length = sizeof(runnings) / sizeof(runnings[0]);
   for (int i = 0;i < length;i++) {
      runnings[i] = false;
      thread rt(&(L2DataCapture::_run),i);
      thread rt(&(L2DataCapture::_run), i);
      rt.detach();
      //thread rt_trade_queue(&(L2DataCapture::_run_trade_queue), i);
@@ -184,25 +218,33 @@
   uchar* imgData = (uchar*)malloc(sizeof(uchar) * oimg.rows * oimg.cols);
   //
   if (oimg.channels() == 3)
   {
      openCLExcuter[identify]->rgb2Gray(oimg.data, oimg.cols, oimg.rows, imgData);
   if (oimg.channels() == 1) {
      //黑白图片
      img.data = oimg.data;
   }
   else {
      openCLExcuter[identify]->rgba2Gray(oimg.data, oimg.cols, oimg.rows, imgData);
   }
   img.data = imgData;
   oimg.release();
      if (oimg.channels() == 3)
      {
         openCLExcuter[identify]->rgb2Gray(oimg.data, oimg.cols, oimg.rows, imgData);
      }
      else {
         openCLExcuter[identify]->rgba2Gray(oimg.data, oimg.cols, oimg.rows, imgData);
      }
      img.data = imgData;
      oimg.release();
      clock_t time_2_ = clock();
      if (identify == 0)
      {
         std::cout << "灰度完成: threadid-" << std::this_thread::get_id() << " 耗时:" << (time_2_ - time_1) << endl;
         std::cout <<"通道数:"<<oimg.channels()<<endl;
         LogUtil::debug("灰度完成");
      }
   }
   clock_t time_2 = clock();
   std::cout << "灰度完成: threadid-" << std::this_thread::get_id() << " 耗时:" << (time_2 - time_1) << endl;
   LogUtil::debug("灰度完成");
   //将图像转为1维数组
   //unsigned char* imgData = img.data;
   //图像分割---开始
@@ -214,9 +256,11 @@
      }
   }
   catch (...) {
      //释放内存
      free(imgData);
      throw int(ERROR_CODE_DIVIDE_IMG_FAIL);
   }
   //准备数据
   //一维行图像数据
   int* rowDataOneLevel = (int*)malloc(sizeof(int) * rowDataList.size() * 4);
   list<int*>::iterator e;
   int index = 0;
@@ -229,7 +273,7 @@
      index++;
      free(indexs);
   }
   //一维分块图像数据
   int* rowSplitDataOneLevel = (int*)malloc(sizeof(int) * rowDataList.size() * 4 * 7);
   openCLExcuter[identify]->splitL2RowData(imgData, img.cols, img.rows, rowDataOneLevel, rowDataList.size(), rowSplitDataOneLevel);
   free(rowDataOneLevel);
@@ -268,24 +312,89 @@
   clock_t time_3 = clock();
   //LogUtil::debug("分隔完成");
   std::cout << "分隔完成: threadid-" << std::this_thread::get_id() << " 耗时:" << time_3 - time_2 << endl;
   if (identify == 0)
      std::cout << "分隔完成: threadid-" << std::this_thread::get_id() << " 耗时:" << time_3 - time_2 << "总耗时:" << time_3 - time_1 << endl;
   //图像分割---结束
   //结果初始化
   list<TradeData> resultList;
   for (int i = 0;i < rowDataList.size();i++) {
      TradeData td = TradeData();
      resultList.push_back(td);
   }
   //------非数字识别开始-------
   //图像识别(除开数字的部分)
   int* notNumberResult = (int*)malloc(sizeof(int) * rowDataList.size() * 3);
   openCLExcuter[identify]->recognitionNotNum(img.data, img.cols, img.rows, rowSplitDataOneLevel, 7, rowDataList.size(), notNumberResult);
   int i = 0;
   for (list<TradeData>::iterator ele = resultList.begin();ele != resultList.end();++ele) {
      switch (notNumberResult[i * 3])
      {
      case 0:
         (*ele).cancelTimeUnit = TIME_SECOND;break;
      case 1:
         (*ele).cancelTimeUnit = TIME_MINITE;break;
      case 2:
         (*ele).cancelTimeUnit = TIME_HOUR;break;
      default:
         break;
      }
      switch (notNumberResult[i * 3 + 1])
      {
      case 0:
         (*ele).limitPrice = LIMIT_PRICE_NORMAL;break;
      case 1:
         (*ele).limitPrice = LIMIT_PRICE_UP;break;
      case 2:
         (*ele).limitPrice = LIMIT_PRICE_DOWN;break;
      default:
         break;
      }
      switch (notNumberResult[i * 3 + 2])
      {
      case OPERATE_BUY:
         (*ele).operateType = OPERATE_BUY;
         break;
      case OPERATE_BUY_CANCEL:
         (*ele).operateType = OPERATE_BUY_CANCEL;
         break;
      case OPERATE_SELL:
         (*ele).operateType = OPERATE_SELL;
         break;
      case OPERATE_SELL_CANCEL:
         (*ele).operateType = OPERATE_SELL_CANCEL;
         break;
      case OPERATE_OPERATE_ERROR:
         (*ele).operateType = OPERATE_OPERATE_ERROR;
         break;
      };
      i++;
   }
   free(notNumberResult);
   clock_t time_5 = clock();
   if (identify == 0)
      std::cout << "非数字数据识别完成: threadid-" << std::this_thread::get_id() << " 耗时:" << time_5 - time_3 << "总耗时:" << time_5 - time_1 << endl;
   //------非数字识别结束-------
   //图像识别的数字准备
   //------数字识别开始---------
   unsigned char* totalNumberData = (unsigned char*)malloc(sizeof(unsigned char) * (_NUMBER_L2_HEIGHT * rowDataList.size()) * _NUMBER_L2_WIDTH * 10 * _NUMBER_L2_TOTAL_NUMBER);
   clock_t time_31 = clock();
   std::cout << "数据准备-图像数据准备: threadid-" << std::this_thread::get_id() << " 耗时:" << time_31 - time_3 << endl;
   if (identify == 0)
      std::cout << "数据准备-图像数据准备: threadid-" << std::this_thread::get_id() << " 耗时:" << time_31 - time_3 << "总耗时:" << time_31 - time_1 << endl;
   int* pos = (int*)malloc(sizeof(int) * 4 * 4 * rowDataList.size());
@@ -318,7 +427,7 @@
       start = index * 4 * 4 + 4 * 3;
      start = index * 4 * 4 + 4 * 3;
      pos[start] = rowSplitDataOneLevel[startS + 4 * 5 + 0];
      pos[start + 1] = rowSplitDataOneLevel[startS + 4 * 5 + 1];
      pos[start + 2] = rowSplitDataOneLevel[startS + 4 * 5 + 2];
@@ -329,7 +438,8 @@
   clock_t time_32 = clock();
   std::cout << "数据准备-位置数据准备: threadid-" << std::this_thread::get_id() << " 耗时:" << time_32 - time_31 << endl;
   if (identify == 0)
      std::cout << "数据准备-位置数据准备: threadid-" << std::this_thread::get_id() << " 耗时:" << time_32 - time_31 << "总耗时:" << time_32 - time_1 << endl;
   unsigned char* zeroData = (unsigned char*)malloc(sizeof(unsigned char) * _NUMBER_L2_WIDTH * _NUMBER_L2_HEIGHT);
   for (int r = 0;r < _NUMBER_L2_HEIGHT;r++) {
@@ -340,12 +450,13 @@
   }
   clock_t time_33 = clock();
   std::cout << "数据准备-0数据准备: threadid-" << std::this_thread::get_id() << " 耗时:" << time_33 - time_32 << endl;
   if (identify == 0)
      std::cout << "数据准备-0数据准备: threadid-" << std::this_thread::get_id() << " 耗时:" << time_33 - time_32 << "总耗时:" << time_33 - time_1 << endl;
   openCLExcuter[identify]->splitL2Num(imgData, img.cols, img.rows, pos, 4 * rowDataList.size(), zeroData, _NUMBER_L2_WIDTH, _NUMBER_L2_HEIGHT, _NUMBER_L2_TOTAL_NUMBER, totalNumberData);
   free(zeroData);
   free(pos);
   /*
@@ -358,81 +469,25 @@
   */
   clock_t time_34 = clock();
   std::cout << "数据准备-数字分隔完成: threadid-" << std::this_thread::get_id() << " 耗时:" << time_34 - time_33 << endl;
   if (identify == 0)
      std::cout << "数据准备-数字分隔完成: threadid-" << std::this_thread::get_id() << " 耗时:" << time_34 - time_33 << "总耗时:" << time_34 - time_1 << endl;
   //准备模板数字
   uchar* templateNums = (unsigned char*)malloc(sizeof(unsigned char) * (_NUMBER_L2_HEIGHT * rowDataList.size()) * _NUMBER_L2_WIDTH * 10 * _NUMBER_L2_TOTAL_NUMBER);
   openCLExcuter[identify]->createNumberTemplates(rowDataList.size(), _NUMBER_L2_WIDTH, _NUMBER_L2_HEIGHT, _NUMBER_L2_TOTAL_NUMBER, ImgUtil::numsOneLevel_level2 ,templateNums);
   openCLExcuter[identify]->createNumberTemplates(rowDataList.size(), _NUMBER_L2_WIDTH, _NUMBER_L2_HEIGHT, _NUMBER_L2_TOTAL_NUMBER, ImgUtil::numsOneLevel_level2, templateNums);
   //ImgUtil::createTemplateNumData(data.size());
   clock_t time_4 = clock();
   std::cout << "数据准备-模板数字准备完成: threadid-" << std::this_thread::get_id() << " 耗时:" << time_4 - time_34 << endl;
   if (identify == 0)
      std::cout << "数据准备-模板数字准备完成: threadid-" << std::this_thread::get_id() << " 耗时:" << time_4 - time_34 << "总耗时:" << time_4 - time_1 << endl;
   std::cout << "数据准备完成: threadid-" << std::this_thread::get_id() << " 耗时:" << time_4 - time_3 << endl;
   //图像识别(除开数字的部分)
   list<TradeData> resultList;
   int* notNumberResult = (int*)malloc(sizeof(int)* rowDataList.size()*3);
   openCLExcuter[identify]->recognitionNotNum(img.data,img.cols,img.rows, rowSplitDataOneLevel,7, rowDataList.size(), notNumberResult);
   if (identify == 0)
      std::cout << "数据准备完成: threadid-" << std::this_thread::get_id() << " 耗时:" << time_4 - time_3 << "总耗时:" << time_4 - time_1 << endl;
   for (int i = 0;i < rowDataList.size();i++) {
      TradeData td = TradeData();
      switch (notNumberResult[i * 3])
      {
      case 0:
         td.cancelTimeUnit = TIME_SECOND;break;
      case 1:
         td.cancelTimeUnit = TIME_MINITE;break;
      case 2:
         td.cancelTimeUnit = TIME_HOUR;break;
      default:
         break;
      }
      switch (notNumberResult[i * 3+1])
      {
      case 0:
         td.limitPrice = LIMIT_PRICE_NORMAL;break;
      case 1:
         td.limitPrice = LIMIT_PRICE_UP;break;
      case 2:
         td.limitPrice = LIMIT_PRICE_DOWN;break;
      default:
         break;
      }
      switch (notNumberResult[i * 3 + 2])
      {
      case OPERATE_BUY:
         td.operateType = OPERATE_BUY;
         break;
      case OPERATE_BUY_CANCEL:
         td.operateType = OPERATE_BUY_CANCEL;
         break;
      case OPERATE_SELL:
         td.operateType = OPERATE_SELL;
         break;
      case OPERATE_SELL_CANCEL:
         td.operateType = OPERATE_SELL_CANCEL;
         break;
      case OPERATE_OPERATE_ERROR:
         td.operateType = OPERATE_OPERATE_ERROR;
         break;
      };
      resultList.push_back(td);
   }
   free(notNumberResult);
   /*
   try {
@@ -442,9 +497,7 @@
      throw ERROR_CODE_RECOGNITION_FAIL;
   }
   */
   clock_t time_5 = clock();
   std::cout << "非数字数据识别完成: threadid-" << std::this_thread::get_id() << " 耗时:" << time_5 - time_4 << endl;
   //数字识别
   uchar** numberResult = openCLExcuter[identify]->recognition_numbers(totalNumberData, templateNums, rowDataList.size() * _NUMBER_L2_HEIGHT, _NUMBER_L2_WIDTH * 10 * _NUMBER_L2_TOTAL_NUMBER, _NUMBER_L2_WIDTH, _NUMBER_L2_HEIGHT, _NUMBER_L2_TOTAL_NUMBER);
@@ -468,12 +521,12 @@
      string price = "";
      price.append(to_string(lineData[6+2])).append(to_string(lineData[7+2])).append(to_string(lineData[8+2])).append(to_string(lineData[9+2]));
      price.append(to_string(lineData[6 + 2])).append(to_string(lineData[7 + 2])).append(to_string(lineData[8 + 2])).append(to_string(lineData[9 + 2]));
      price.append(".");
      price.append(to_string(lineData[10+2])).append(to_string(lineData[11+2]));
      price.append(to_string(lineData[10 + 2])).append(to_string(lineData[11 + 2]));
      string num = "";
      num.append(to_string(lineData[12+2])).append(to_string(lineData[13+2])).append(to_string(lineData[14+2])).append(to_string(lineData[15+2])).append(to_string(lineData[16+2]));
      num.append(to_string(lineData[12 + 2])).append(to_string(lineData[13 + 2])).append(to_string(lineData[14 + 2])).append(to_string(lineData[15 + 2])).append(to_string(lineData[16 + 2]));
      (*tradeEle).time = time;
@@ -492,7 +545,8 @@
   clock_t time_6 = clock();
   //LogUtil::debug("识别完成");
   std::cout << "数字识别完成: threadid-" << std::this_thread::get_id() << " 耗时:" << time_6 - time_5 << endl;
   if (identify == 0)
      std::cout << "数字识别完成: threadid-" << std::this_thread::get_id() << " 耗时:" << time_6 - time_5 << "总耗时:" << time_6 - time_1 << endl;
   //释放内存
   //img.release();
@@ -502,14 +556,17 @@
   list<int*>().swap(rowDataList);
   free(rowSplitDataOneLevel);
   std::cout << "-------L2行情识别结束任务: threadid-" << std::this_thread::get_id() << "  序号:" << identify << " 耗时:" << clock() - starttime << endl;
   if (identify == 0)
      std::cout << "-------L2行情识别结束任务: threadid-" << std::this_thread::get_id() << "  序号:" << identify << " 耗时:" << clock() - starttime << endl;
   return resultList;
}
//捕获level2的盘口数据
list<TradeData> L2DataCapture::captureLevel2TradeData(HWND hwnd, int index) throw(int) {
   clock_t  starttime = clock();
   cv::Mat img = CaptureUtil::capture(hwnd);
   /*
   string path1 = "E:\\temp\\";
@@ -521,6 +578,13 @@
   cv::imwrite(path1, img);
   std::map<string, TradeData> map;
   */
   return captureLevel2TradeData(img, index);
   try {
      list<TradeData> resultList = captureLevel2TradeData(img, index);
      return resultList;
   }
   catch (int code) {
   }
   list<TradeData> tempList;
   return tempList;
}