You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

393 lines
12 KiB

​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
6 years ago
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "MEKOROT_Kernel.h"
  2. #define LOG( X ) m_pLog->Log( "KRNL", this, X )
  3. #define LOG_ERROR( X ) m_pLog->Log( "KRNL", this, _LOG_ERROR, X )
  4. bool getconchar(KEY_EVENT_RECORD& krec) {
  5. DWORD cc, NOF;
  6. INPUT_RECORD irec;
  7. HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
  8. if (h == NULL) {
  9. return false; // console not found
  10. };
  11. if (!GetNumberOfConsoleInputEvents(h, &NOF)) return false;
  12. if( NOF ) for(int i=0;i<NOF; i++)
  13. {
  14. ReadConsoleInput(h, &irec, 1, &cc);
  15. if (irec.EventType == KEY_EVENT
  16. && ((KEY_EVENT_RECORD&)irec.Event).bKeyDown
  17. )//&& ! ((KEY_EVENT_RECORD&)irec.Event).wRepeatCount )
  18. {
  19. krec = (KEY_EVENT_RECORD&)irec.Event;
  20. return true;
  21. }
  22. }
  23. return false; //future ????
  24. };
  25. mekorotKernel::mekorotKernel(config_setting_t* ptr) : basicEngine(), basicThread(), basicMediaDestination(), protocolLGS_CLIENT() {
  26. m_pLog = basicLogCollection::getInstance();
  27. m_pSettings = basicSettingsProvider::getInstance()->getSubNode(ptr, (CSTR)"KERNEL");
  28. m_bStage = MEKOROT_KRNL_STG_INIT;
  29. m_pSerial = NULL;
  30. m_pSerialDevice[0] = (char)0;
  31. m_nSerialSpeed = 19200;
  32. m_bDeviceID[0] = m_bDeviceID[1] = m_bDeviceID[2] = -1;
  33. m_bDeviceFlags[0] = m_bDeviceFlags[1] = m_bDeviceFlags[2] = false;
  34. m_bDeviceTO[0] = m_bDeviceTO[1] = m_bDeviceTO[2] = 0;
  35. m_bDeviceAlarms[0] = m_bDeviceAlarms[1] = m_bDeviceAlarms[2] = false;
  36. m_pVideoOutput = NULL;
  37. m_pStorageBase = NULL;
  38. m_nVideoInputs = 0;
  39. m_nWidth = 320;
  40. m_nHeight = 240;
  41. m_bHorizontal = true;
  42. m_nCLoopMask = 0xFFFFFFFF;
  43. m_nCLoopCheck = 0;
  44. };
  45. mekorotKernel::~mekorotKernel() {
  46. if (m_pSerial != NULL) delete( m_pSerial );
  47. };
  48. bool mekorotKernel::init(void){
  49. config_setting_t *pSetting;
  50. pSetting = config_setting_get_member(m_pSettings, MEKOROT_KRNL_SERIAL_LINE_TAG);
  51. if (pSetting != NULL) {
  52. sprintf(m_pSerialDevice, "%s", config_setting_get_string(pSetting));
  53. } else {
  54. sprintf(m_pSerialDevice, "\\\\.\\COM2");
  55. pSetting = config_setting_add(m_pSettings, MEKOROT_KRNL_SERIAL_LINE_TAG, CONFIG_TYPE_STRING);
  56. basicSettingsProvider::getInstance()->compromise();
  57. if (pSetting == NULL) {
  58. LOG_ERROR("Failure while adding setting!");
  59. return false;
  60. };
  61. config_setting_set_string(pSetting, m_pSerialDevice);
  62. };
  63. pSetting = config_setting_get_member(m_pSettings, MEKOROT_KRNL_SERIAL_SPEED_TAG);
  64. if (pSetting != NULL) {
  65. m_nSerialSpeed = config_setting_get_int(pSetting);
  66. } else {
  67. pSetting = config_setting_add(m_pSettings, MEKOROT_KRNL_SERIAL_SPEED_TAG, CONFIG_TYPE_INT);
  68. basicSettingsProvider::getInstance()->compromise();
  69. if (pSetting == NULL) {
  70. LOG_ERROR("Failure while adding setting!");
  71. return false;
  72. };
  73. config_setting_set_int(pSetting, m_nSerialSpeed );
  74. };
  75. pSetting = config_setting_get_member(m_pSettings, MEKOROT_KRNL_DEVICE_0_ID_TAG);
  76. if (pSetting != NULL) {
  77. m_bDeviceID[0] = config_setting_get_int(pSetting);
  78. } else {
  79. pSetting = config_setting_add(m_pSettings, MEKOROT_KRNL_DEVICE_0_ID_TAG, CONFIG_TYPE_INT);
  80. basicSettingsProvider::getInstance()->compromise();
  81. if (pSetting == NULL) {
  82. LOG_ERROR("Failure while adding setting!");
  83. return false;
  84. };
  85. config_setting_set_int(pSetting, m_bDeviceID[0]);
  86. };
  87. pSetting = config_setting_get_member(m_pSettings, MEKOROT_KRNL_DEVICE_1_ID_TAG);
  88. if (pSetting != NULL) {
  89. m_bDeviceID[1] = config_setting_get_int(pSetting);
  90. } else {
  91. pSetting = config_setting_add(m_pSettings, MEKOROT_KRNL_DEVICE_1_ID_TAG, CONFIG_TYPE_INT);
  92. basicSettingsProvider::getInstance()->compromise();
  93. if (pSetting == NULL) {
  94. LOG_ERROR("Failure while adding setting!");
  95. return false;
  96. };
  97. config_setting_set_int(pSetting, m_bDeviceID[1]);
  98. };
  99. pSetting = config_setting_get_member(m_pSettings, MEKOROT_KRNL_DEVICE_2_ID_TAG);
  100. if (pSetting != NULL) {
  101. m_bDeviceID[2] = config_setting_get_int(pSetting);
  102. } else {
  103. pSetting = config_setting_add(m_pSettings, MEKOROT_KRNL_DEVICE_2_ID_TAG, CONFIG_TYPE_INT);
  104. basicSettingsProvider::getInstance()->compromise();
  105. if (pSetting == NULL) {
  106. LOG_ERROR("Failure while adding setting!");
  107. return false;
  108. };
  109. config_setting_set_int(pSetting, m_bDeviceID[2]);
  110. };
  111. basicSettingsProvider::getInstance()->validate();
  112. m_nInitialFSID = (uint32_t)time(NULL);
  113. /* Storage Init */
  114. m_pStorageProvider = storageProvider::getInstance( m_pStorageBase );
  115. m_pVideoOutput = new lapsedVideoRecorder();
  116. if (m_pVideoOutput == NULL) {
  117. LOG_ERROR("TimeLapse Recorder on create Failure!");
  118. // m_bAssembleMP4 = false;
  119. return false;
  120. } else {
  121. m_pVideoOutput->setFrameLapse( MAX_RECORD_FRAMES );
  122. };
  123. pSetting = basicSettingsProvider::getInstance()->getSubNode(m_pSettings, KNL_SETTINGS_VIDEO_SECTION);
  124. int mCount = config_setting_length(pSetting);
  125. if (mCount) {
  126. char tBuffer[256];
  127. sprintf(tBuffer, "Defined %d Video Input", mCount);
  128. LOG(tBuffer);
  129. m_pVideoNames = (CSTR*)calloc(mCount, sizeof(CSTR*));
  130. for( int i=0; i < mCount; i++ ){ m_pVideoNames[i]= (CSTR) malloc(256); };
  131. m_pVideoInputs = (basicMediaSource**)calloc(mCount, sizeof(basicMediaSource*));
  132. m_pVideoFrame = (cv::Mat**) calloc(mCount, sizeof(cv::Mat*));
  133. memset(m_pVideoInputs, 0, sizeof(m_pVideoInputs));
  134. memset(m_pVideoFrame, 0, sizeof(m_pVideoFrame));
  135. if (m_pVideoInputs != NULL) {
  136. config_setting_t* tRec;
  137. config_setting_t* ttRec;
  138. int tIID, tWIDTH, tHEIGHT, tSCALEW, tSCALEH;
  139. bool tSCALED;
  140. double tFPS;
  141. m_nVideoInputs = mCount;
  142. for (int index = 0; index < m_nVideoInputs; index++) {
  143. m_pVideoInputs[index] = (basicMediaSource*)new videoAVLIBSource();
  144. if (!((videoAVLIBSource*)m_pVideoInputs[index])->_open(tBuffer, false, (float)tFPS, -1, tWIDTH, tHEIGHT, tSCALED, tSCALEW, tSCALEH)) {
  145. delete((videoAVLIBSource*)m_pVideoInputs[index]);
  146. m_pVideoInputs[index] = NULL;
  147. LOG_ERROR("Source Open Failure.");
  148. } else {
  149. if (tSCALED) {
  150. if (m_nWidth < tSCALEW) m_nWidth = tSCALEW;
  151. if (m_nHeight < tSCALEH) m_nHeight = tSCALEH;
  152. } else {
  153. if (m_nWidth < tWIDTH) m_nWidth = tWIDTH;
  154. if (m_nHeight < tHEIGHT) m_nHeight = tHEIGHT;
  155. };
  156. m_pVideoFrame[index] = new cv::Mat(tHEIGHT, tWIDTH, CV_8UC3);
  157. m_pVideoInputs[index]->attachDestination(this);
  158. LOG("Source Opened OK.");
  159. };
  160. };
  161. };
  162. };
  163. m_bStage = MEKOROT_KRNL_STG_CONNECT;
  164. return basicThread::init();
  165. };
  166. void mekorotKernel::m_fThreadProc(void){
  167. m_bThreadStatus = TH_WORKING;
  168. while (m_bThreadStatus < TH_ENDED) {
  169. KEY_EVENT_RECORD krec;
  170. if( getconchar(krec) ){
  171. //Parse KeyBoard Input
  172. switch (krec.wVirtualKeyCode) {
  173. case 13:
  174. case 32:
  175. case 82:{
  176. // 'r' Or 'spc' or 'etr' = Start/Stop video record
  177. break;
  178. };
  179. case 81:
  180. case 75: {
  181. // 'q' Or 'k' - Exit Application
  182. LOG("Exit by Operator Request!");
  183. m_bThreadStatus = TH_ENDED;
  184. continue;
  185. };
  186. default: {
  187. char tBuffer[128];
  188. sprintf(tBuffer, "Key Stroke Code: %d", krec.wVirtualKeyCode);
  189. LOG(tBuffer);
  190. break;
  191. };
  192. };
  193. };
  194. switch( m_bStage ) {
  195. case MEKOROT_KRNL_STG_CONNECT:{
  196. if (m_pSerial == NULL) {
  197. m_pSerial = new serialPort(m_pSerialDevice, m_nSerialSpeed);
  198. if (m_pSerial != NULL) {
  199. setTransport( m_pSerial );
  200. m_bStage = MEKOROT_KRNL_STG_PING_DEVICES;
  201. };
  202. };
  203. break;
  204. };
  205. case MEKOROT_KRNL_STG_PING_DEVICES: {
  206. bool LB = true;
  207. for (uint8_t index = 0; index < 3; index++) {
  208. if (m_bDeviceID[index] != -1 && !m_bDeviceFlags[index]){
  209. LB = false;
  210. if (setDeviceID(m_bDeviceID[index])) {
  211. if (!getState()) {
  212. LOG_ERROR("Get STATE FAILURE!");
  213. };
  214. } else {
  215. LOG_ERROR("Set DevID FAILURE!");
  216. };
  217. m_bDeviceTO[index] = getMilliseconds();
  218. };
  219. };
  220. if (LB) {
  221. m_bStage = MEKOROT_KRNL_STG_WORKING;
  222. } else {
  223. SLEEP(KERNEL_SLOW_SLEEP_TIME); // SLOW wait responce
  224. };
  225. break;
  226. };
  227. case MEKOROT_KRNL_STG_WORKING: {
  228. unsigned long long cTS;
  229. cTS = getMilliseconds();
  230. bool AllOffLine = true;
  231. for (uint8_t index = 0; index < 3; index++) {
  232. if (!m_bDeviceFlags[index]) continue;
  233. AllOffLine = false;
  234. if ((cTS - m_bDeviceTO[index]) > 3000) {
  235. LOG_ERROR("Device TO Detected!");
  236. m_bDeviceFlags[index] = false;
  237. m_bStage = MEKOROT_KRNL_STG_PING_DEVICES;
  238. continue;
  239. };
  240. if (setDeviceID(m_bDeviceID[index])) {
  241. if (!getState2()) {
  242. LOG_ERROR("Get STATE2 FAILURE!");
  243. m_bDeviceFlags[index] = false;
  244. m_bStage = MEKOROT_KRNL_STG_PING_DEVICES;
  245. };
  246. } else {
  247. LOG_ERROR("Set DevID FAILURE!");
  248. m_bDeviceFlags[index] = false;
  249. m_bStage = MEKOROT_KRNL_STG_PING_DEVICES;
  250. continue;
  251. };
  252. };
  253. if (AllOffLine) {
  254. m_bStage = MEKOROT_KRNL_STG_PING_DEVICES;
  255. continue;
  256. } else {
  257. SLEEP(KERNEL_SLOW_SLEEP_TIME);
  258. };
  259. break;
  260. };
  261. };
  262. };
  263. };
  264. bool mekorotKernel::packetHandler(protocolPackage* Package) {
  265. uint8_t cDeviceID;
  266. uint8_t cDeviceIDX = 255;
  267. if( Package == NULL ) return true;
  268. if( Package->m_nSize < 4 ) return true;
  269. cDeviceID = Package->m_pData[2];
  270. for (uint8_t idx = 0; idx < 3; idx++) {
  271. if (cDeviceID == m_bDeviceID[idx]) cDeviceIDX = idx;
  272. };
  273. if (cDeviceIDX == 255) {
  274. LOG_ERROR("Package from not registered device!");
  275. return true;
  276. };
  277. m_bDeviceTO[cDeviceIDX] = getMilliseconds();
  278. if (!m_bDeviceFlags[cDeviceIDX]) {
  279. char tBuff[64];
  280. sprintf(tBuff, "Device %d Became OnLine", cDeviceIDX);
  281. LOG(tBuff);
  282. m_bDeviceFlags[cDeviceIDX] = true;
  283. };
  284. switch (cDeviceIDX) {
  285. case 0: {
  286. LOG("Left Vertical");
  287. break;
  288. };
  289. case 1: {
  290. LOG("Center Horisontal");
  291. break;
  292. };
  293. case 2: {
  294. LOG("Right Vertical");
  295. break;
  296. };
  297. };
  298. return true;// to erase data
  299. };
  300. void mekorotKernel::pushFrame(cv::Mat* Frame, void* src) {
  301. if (src == NULL) {
  302. return;
  303. };
  304. if (m_nFrameCounter == 0) m_lStartTS = getMilliseconds();
  305. m_nFrameCounter++;
  306. for (int index = 0; index < m_nVideoInputs; index++) {
  307. if (src == m_pVideoInputs[index]) {
  308. Frame->copyTo((*m_pVideoFrame[index]));
  309. m_nCLoopMask &= ~(1 << index);
  310. break;
  311. };
  312. };
  313. //If we get all frames;
  314. if (m_pFinalFrame != NULL) {
  315. if ((m_nCLoopMask & m_nCLoopCheck) == 0) {
  316. int cx, cy;
  317. cx = 0; cy = 15;
  318. m_pMatProtector->lock();
  319. //rectangle( (*m_pFinalFrame), Point(0,0), Point(m_nWidth, m_nHeight), Scalar(0,0,0), CV_FILLED );
  320. for (int index = 0; index < m_nVideoInputs; index++) {
  321. if (m_bHorizontal) {
  322. m_pVideoFrame[index]->copyTo((*m_pFinalFrame)(cv::Rect(cx, cy, m_pVideoFrame[index]->cols, m_pVideoFrame[index]->rows)));
  323. cx += m_pVideoFrame[index]->cols;
  324. }
  325. else {
  326. m_pVideoFrame[index]->copyTo((*m_pFinalFrame)(cv::Rect(cx, cy, m_pVideoFrame[index]->cols, m_pVideoFrame[index]->rows)));
  327. cy += m_pVideoFrame[index]->rows;
  328. };
  329. };
  330. int X, W = 260;
  331. X = (m_pFinalFrame->cols - W) >> 1;
  332. //rectangle( (*m_pFinalFrame), Point( X, 10 ), Point( X+W, 25 ), Scalar( 0, 0, 0 ), CV_FILLED );
  333. rectangle((*m_pFinalFrame), Point(0, 0), Point(m_pFinalFrame->cols, 15), Scalar(0, 0, 0), cv::FILLED);
  334. long long aMilliseconds = getMilliseconds();
  335. long long tMilliseconds;
  336. float fps = 0.f;
  337. tMilliseconds = aMilliseconds - m_lStartTS;
  338. if (tMilliseconds > 0 && m_nVideoInputs > 0) {
  339. fps = ((float)(m_nFrameCounter / m_nVideoInputs) * 1000.f) / (float)tMilliseconds;
  340. };
  341. char DateStr[200];
  342. sprintf(DateStr, "TS:%010lu", tMilliseconds);
  343. putText((*m_pFinalFrame), DateStr, Point(X, 12), FONT_HERSHEY_SIMPLEX, .4, Scalar(0, 255, 255), 1);
  344. sprintf(DateStr, "FID:%06lu", m_nFrameCounter);
  345. putText((*m_pFinalFrame), DateStr, Point(X + 110, 12), FONT_HERSHEY_SIMPLEX, .4, Scalar(0, 255, 0), 1);
  346. sprintf(DateStr, "FPS:%03.2f", fps);
  347. putText((*m_pFinalFrame), DateStr, Point(X + 190, 12), FONT_HERSHEY_SIMPLEX, .4, Scalar(255, 0, 255), 1);
  348. if (m_bRecordIsActive) {
  349. rectangle((*m_pFinalFrame), Point(X + 260, 3), Point(X + 275, 12), Scalar(0, 0, 255), cv::FILLED);
  350. }
  351. else {
  352. rectangle((*m_pFinalFrame), Point(X + 260, 3), Point(X + 275, 12), Scalar(0, 255, 0), cv::FILLED);
  353. };
  354. //if( m_bAssembleMP4 ) {
  355. m_pVideoOutput->pushFrame(m_pFinalFrame);
  356. //};
  357. m_pMatProtector->unlock();
  358. // Assemble Final Frame;
  359. m_nCLoopMask = 0xFFFFFFFF;
  360. };
  361. };
  362. };
  363. bool mekorotKernel::copyFrame(cv::Mat& Frame) {
  364. if (m_pFinalFrame == NULL) return false;
  365. m_pMatProtector->lock();
  366. if (m_pFinalFrame == NULL) return false;
  367. m_pFinalFrame->copyTo(Frame);
  368. m_pMatProtector->unlock();
  369. return true;
  370. };
  371. #undef LOG
  372. #undef LOG_ERROR