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.

252 lines
8.0 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
​
​
​
​
​
​
​
​
​
​
​
  1. #include "rgooseKafkaModule.h"
  2. #include "../hardware/soc/general/3DLRF.h"
  3. #define LOG( X ) argusKernelModule::m_pLog->Log( "KM", this, X )
  4. #ifdef DEBUG
  5. #define LOG_ERROR( X ) {\
  6. char _tBfrErrMSG2w34[256];\
  7. sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  8. argusKernelModule::m_pLog->Log( "KM", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
  9. }
  10. #else
  11. #define LOG_ERROR( X ) argusKernelModule::m_pLog->Log( "KM", this, _LOG_ERROR, X );
  12. #endif
  13. argusKafkaModule::argusKafkaModule( uint32_t ModuleID ): argusKernelModule( ModuleID, ARGUS_MODULE_KAFKA ){
  14. m_pProtocol = NULL;
  15. m_pEvtTemplates = (KAFKA_TEMPLATE***) calloc(EVT_CLASS_COUNT, sizeof(KAFKA_TEMPLATE**));
  16. if( m_pEvtTemplates!= NULL ) {
  17. memset( m_pEvtTemplates, 0, EVT_CLASS_COUNT * sizeof(KAFKA_TEMPLATE**));
  18. } else LOG_ERROR("EVT Templates Memory Allocation Error!");
  19. };
  20. argusKafkaModule::~argusKafkaModule(){
  21. waitThreadEnd( true );
  22. if( m_pProtocol!= NULL ) {
  23. delete( m_pProtocol );
  24. };
  25. if( m_pEvtTemplates!= NULL ) {
  26. for(int i=0; i < EVT_CLASS_COUNT; i++ ) {
  27. if( m_pEvtTemplates[i] != NULL ){
  28. for( int j=0; j< EVT_COUNT; j++ ) {
  29. if( m_pEvtTemplates[i][j] != NULL ){
  30. if( m_pEvtTemplates[i][j]->tpl ) free( m_pEvtTemplates[i][j]->tpl );
  31. free( m_pEvtTemplates[i][j] );
  32. };
  33. };
  34. free( m_pEvtTemplates[i] );
  35. };
  36. };
  37. free( m_pEvtTemplates );
  38. };
  39. };
  40. /* Periodical Job Handler */
  41. void argusKafkaModule::m_fThreadProc( void ){
  42. m_bThreadStatus = TH_WORKING;
  43. while( m_bThreadStatus < TH_ENDED ) {
  44. m_llTicks ++;//= getMilliseconds();
  45. if( m_llTicks % 20 == 0 ) { // each 2 seconds
  46. //m_pProtocol->StatusUpdate();
  47. //m_lCycleCounter == 0;
  48. //char tBuff[256];
  49. //sprintf(tBuff,"MY MESSAGE %dd", m_llTicks );
  50. //LOG( tBuff );
  51. //m_pProtocol->issuePost( "My_TEST_TOPIC", tBuff, strlen( tBuff ) );
  52. };
  53. /* No need for time Sync
  54. if( m_llTicks % 350 == 0 ) { // each 35 seconds
  55. StartTimeSynch();
  56. //m_lCycleCounter == 0;
  57. };
  58. */
  59. SLEEP(100);
  60. };
  61. };
  62. bool argusKafkaModule::startService(){
  63. if( reloadSettings() ) {
  64. if( m_pSettings!= NULL ){
  65. m_pProtocol = new protocolKAFKA( m_pSettings );
  66. if( m_pProtocol->init() ){
  67. // No need pereodical jobs
  68. //return init();
  69. return true;
  70. };
  71. };
  72. };
  73. return false;
  74. };
  75. bool argusKafkaModule::stopService(){
  76. waitThreadEnd( true );
  77. return true;
  78. };
  79. bool argusKafkaModule::reloadSettings(){
  80. if( m_pDBC==NULL ) return false;
  81. m_pSettings = m_pDBC->getSettingsKAFKA( m_nDBAUXID );
  82. if( m_pSettings != NULL ) {
  83. if( m_pEvtTemplates != NULL ) {
  84. // Clear Templates
  85. for(int i = 0; i < EVT_CLASS_COUNT; i++ ){
  86. for(int j = 0; j < EVT_COUNT; j++ ){
  87. if( m_pEvtTemplates[i] != NULL ){
  88. if( m_pEvtTemplates[i][j] != NULL ){
  89. if( m_pEvtTemplates[i][j]->tpl ) free( m_pEvtTemplates[i][j]->tpl );
  90. free( m_pEvtTemplates[i][j] );
  91. m_pEvtTemplates[i][j] = NULL;
  92. };
  93. };
  94. };
  95. free( m_pEvtTemplates[i] );
  96. m_pEvtTemplates[i] = NULL;
  97. };
  98. for(int i = 0; i < EVT_CLASS_COUNT; i++ ){
  99. for(int j = 0; j < EVT_COUNT; j++ ){
  100. KAFKA_TEMPLATE * tT;
  101. tT = m_pDBC->getKafkaTemplateForEvent( m_nDBID, i, j );
  102. if( tT != NULL ) {
  103. if( m_pEvtTemplates[i]== NULL ) {
  104. m_pEvtTemplates[i]=(KAFKA_TEMPLATE**)calloc(EVT_COUNT,sizeof(KAFKA_TEMPLATE*));
  105. memset( m_pEvtTemplates[i], 0, EVT_COUNT * sizeof(KAFKA_TEMPLATE*));
  106. };
  107. m_pEvtTemplates[i][j] = tT;
  108. };
  109. };
  110. };
  111. };
  112. return true;
  113. };
  114. return false;
  115. };
  116. CSTR argusKafkaModule::buildTemplate(
  117. CSTR tpl,
  118. uint32_t source_id,
  119. CSTR ts,
  120. CSTR uuid,
  121. CSTR az,
  122. CSTR range,
  123. CSTR el
  124. ){
  125. CSTR ct1, ct2;
  126. char tVar[64];
  127. if( tpl == NULL ) return NULL;
  128. sprintf( tVar, "%d", source_id );
  129. ct1 = strReplace( tpl, KAFKA_TPL_SUBST_ID, tVar );
  130. if( ct1 ) {
  131. ct2 = strReplace( ct1, KAFKA_TPL_SUBST_TS, ts );
  132. if( ct2 ) {
  133. free( ct1 );
  134. ct1 = ct2;
  135. };
  136. if( uuid ){
  137. ct2 = strReplace( ct1, KAFKA_TPL_SUBST_UUID, uuid );
  138. if( ct2 ) {
  139. free( ct1 );
  140. ct1 = ct2;
  141. };
  142. };
  143. if( az ){
  144. ct2 = strReplace( ct1, KAFKA_TPL_SUBST_AZ, az );
  145. if( ct2 ) {
  146. free( ct1 );
  147. ct1 = ct2;
  148. };
  149. };
  150. if( range ){
  151. ct2 = strReplace( ct1, KAFKA_TPL_SUBST_RANGE, range );
  152. if( ct2 ) {
  153. free( ct1 );
  154. ct1 = ct2;
  155. };
  156. };
  157. if( el ){
  158. ct2 = strReplace( ct1, KAFKA_TPL_SUBST_EL, el );
  159. if( ct2 ) {
  160. free( ct1 );
  161. ct1 = ct2;
  162. };
  163. };
  164. return ct1;
  165. };
  166. return NULL;
  167. };
  168. void argusKafkaModule::dispatchEvent( basicEvent *Evt ){
  169. #ifdef DEBUG
  170. LOG( "EVT DISPATCHER");
  171. #endif
  172. if( Evt == NULL ) {
  173. LOG( "Evt NULL");
  174. return;
  175. };
  176. if( m_pEvtTemplates[ Evt->m_eClass ]!= NULL ) {
  177. // We have defined Channel for that class Events
  178. #ifdef DEBUG
  179. char tBuffer[256];
  180. sprintf( tBuffer, "Event Class %d Type %d ", Evt->m_eClass , Evt->m_eType );
  181. LOG( tBuffer );
  182. #endif
  183. // Skip Event Handling by flags
  184. if( !m_bEvtFlags[ Evt->m_eType ] ) return;
  185. // Hadle Event
  186. CSTR tDATA;
  187. if( m_pEvtTemplates[ Evt->m_eClass ][ Evt->m_eType ] != NULL ) {
  188. CLIENT_EVENT *tCEVT;
  189. tCEVT = ( CLIENT_EVENT *) Evt->m_pEvtData;
  190. tDATA = buildTemplate( m_pEvtTemplates[ Evt->m_eClass ][ Evt->m_eType ]->tpl,
  191. Evt->m_nSourceUID,
  192. getUniqID_MD5()
  193. );
  194. if( tDATA!= NULL ) {
  195. m_pProtocol->issuePost( m_pEvtTemplates[ Evt->m_eClass ][ Evt->m_eType ]->topic, tDATA, strlen( tDATA ) );
  196. };
  197. };
  198. /*
  199. switch( Evt->m_eType ) {
  200. case EVT_TARGET_1D:
  201. case EVT_TARGET_2D_WHITE:
  202. case EVT_TARGET_2D_BLACK:
  203. case EVT_TARGET_3D_WHITE:
  204. case EVT_TARGET_3D_BLACK:{
  205. CLIENT_EVENT *tCEVT;
  206. tCEVT = ( CLIENT_EVENT *) Evt->m_pEvtData;
  207. if( tCEVT != NULL ){
  208. float Az, El;
  209. uint32_t cRange;
  210. cRange = (uint32_t)(((float) tCEVT->range ) * 0.0732421875f ) ; // To meters
  211. #ifdef PRU_SLAVE
  212. make3Ddeg( tCEVT->th_id, Az, El, false );
  213. #else
  214. make3Ddeg( tCEVT->th_id, Az, El, true );
  215. #endif
  216. m_pProtocol->Alarm3D( (int)Az, (int)El, cRange , tCEVT->k_id );
  217. };
  218. };
  219. break;
  220. case EVT_ERROR:
  221. m_pProtocol->SetStatus( false );
  222. break;
  223. case EVT_SCAN:
  224. //StatusUpdate();
  225. //break;
  226. case EVT_FLIP:
  227. m_pProtocol->SetStatus( true );
  228. //StatusUpdate();
  229. break;
  230. case EVT_COMMAND:
  231. break;
  232. default:
  233. LOG_ERROR("Unknown Event!");
  234. };
  235. */
  236. };
  237. };
  238. #undef LOG
  239. #undef LOG_ERROR