admin
2022-08-18 67a5e3d825fde17b7b00906ce42b0bd8cafebc4e
ConsoleApplication/L2DataCapture.cpp
@@ -4,6 +4,8 @@
#include "THSActionUtil.h"
#include <thread>
bool L2DataCapture::inited;
bool L2DataCapture::tradeTimeCapture;
 OpenCLExcuter* L2DataCapture::openCLExcuter[THS_FRAME_COUNT];
 TradeQueueCapture* L2DataCapture::tradeQueueCapture[THS_FRAME_COUNT];
@@ -21,10 +23,37 @@
 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 (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();
         //识别数据
@@ -71,6 +100,11 @@
string L2DataCapture::getGPCode(int index) {
    return   gpCodes[index];
}
void L2DataCapture::setTradeTimeCapture(bool enable)
{
   tradeTimeCapture = enable;
}
static string getGPCode(int index);
@@ -184,6 +218,11 @@
   uchar* imgData = (uchar*)malloc(sizeof(uchar) * oimg.rows * oimg.cols);
   //
   if (oimg.channels() == 1) {
      //黑白图片
      img.data = oimg.data;
   }
   else {
   if (oimg.channels() == 3)
   {
      openCLExcuter[identify]->rgb2Gray(oimg.data, oimg.cols, oimg.rows, imgData);
@@ -193,15 +232,18 @@
   }
   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());
@@ -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,7 +450,8 @@
   }
   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);
@@ -358,7 +469,8 @@
   */
   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);
@@ -367,72 +479,15 @@
   //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 {
@@ -443,8 +498,6 @@
   }
   */
   
   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);
@@ -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,6 +556,7 @@
   list<int*>().swap(rowDataList);
   free(rowSplitDataOneLevel);
   if (identify == 0)
   std::cout << "-------L2行情识别结束任务: threadid-" << std::this_thread::get_id() << "  序号:" << identify << " 耗时:" << clock() - starttime << endl;
   return resultList;
@@ -509,7 +564,9 @@
//捕获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;
   */
   try {
      list<TradeData> resultList = captureLevel2TradeData(img, index);
      return resultList;
   }
   catch (int code) {
   return captureLevel2TradeData(img, index);
   }
   list<TradeData> tempList;
   return tempList;
}