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.

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