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.

302 lines
9.7 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. m_bEnabled = false;
  22. waitThreadEnd( true );
  23. if( m_pProtocol!= NULL ) {
  24. delete( m_pProtocol );
  25. };
  26. if( m_pEvtTemplates!= NULL ) {
  27. for(int i=0; i < EVT_CLASS_COUNT; i++ ) {
  28. if( m_pEvtTemplates[i] != NULL ){
  29. for( int j=0; j< EVT_COUNT; j++ ) {
  30. if( m_pEvtTemplates[i][j] != NULL ){
  31. if( m_pEvtTemplates[i][j]->tpl ) free( m_pEvtTemplates[i][j]->tpl );
  32. free( m_pEvtTemplates[i][j] );
  33. };
  34. };
  35. free( m_pEvtTemplates[i] );
  36. };
  37. };
  38. free( m_pEvtTemplates );
  39. };
  40. };
  41. /* Periodical Job Handler */
  42. void argusKafkaModule::m_fThreadProc( void ){
  43. #ifdef SIMULATOR
  44. float sim_Az = 0.f;
  45. float sim_El = 20.f;
  46. float sim_Ran = 80.35f;
  47. float tr_Az_step = 0.3f;
  48. uint32_t src = 128;
  49. #endif
  50. #ifdef DEBUG
  51. LOG("Pereodical Jobs Started");
  52. #endif
  53. m_bThreadStatus = TH_WORKING;
  54. if( !m_bEnabled ) {
  55. m_bThreadStatus = TH_ENDED;
  56. #ifdef DEBUG
  57. LOG("Module is Disabled!");
  58. #endif
  59. };
  60. while( m_bThreadStatus < TH_ENDED ) {
  61. m_llTicks ++;//= getMilliseconds();
  62. if( m_llTicks % 20 == 0 ) { // each 2 seconds
  63. sim_Az += tr_Az_step;
  64. #ifdef SIMULATOR
  65. for(int i=0; i < EVT_CLASS_COUNT; i++ ) {
  66. if( m_pEvtTemplates[i] != NULL ){
  67. for( int j=0; j< EVT_COUNT; j++ ) {
  68. if( m_pEvtTemplates[i][j] != NULL ){
  69. CSTR tpl;
  70. CSTR ts;
  71. CSTR uuid;
  72. char AZ[10];
  73. char EL[10];
  74. char RN[10];
  75. sprintf( AZ, "%.3f", sim_Az);
  76. sprintf( EL, "%.3f", sim_El);
  77. sprintf( RN, "%.3f", sim_Ran);
  78. uuid = getUniqID_UUID();
  79. ts = getTimeStamp();
  80. tpl = buildTemplate( m_pEvtTemplates[i][j]->tpl, src, ts, uuid, AZ, RN, EL );
  81. if( tpl ) {
  82. m_pProtocol->issuePost( m_pEvtTemplates[i][j]->topic, tpl, strlen( tpl ) );
  83. free( tpl );
  84. };
  85. if( uuid ) free( uuid );
  86. if( ts ) free( ts );
  87. };
  88. };
  89. };
  90. };
  91. #endif
  92. };
  93. /* No need for time Sync
  94. if( m_llTicks % 350 == 0 ) { // each 35 seconds
  95. StartTimeSynch();
  96. //m_lCycleCounter == 0;
  97. };
  98. */
  99. SLEEP(100);
  100. };
  101. };
  102. bool argusKafkaModule::startService(){
  103. if(!m_bEnabled) return false;
  104. if( reloadSettings() ) {
  105. if( m_pSettings!= NULL ){
  106. m_pProtocol = new protocolKAFKA( m_pSettings );
  107. if( m_pProtocol->init() ){
  108. // No need pereodical jobs unless SIMULATOR
  109. #ifdef SIMULATOR
  110. LOG("Start PJ");
  111. return init();
  112. #else
  113. return true;
  114. #endif
  115. };
  116. };
  117. };
  118. return false;
  119. };
  120. bool argusKafkaModule::stopService(){
  121. waitThreadEnd( true );
  122. return true;
  123. };
  124. bool argusKafkaModule::reloadSettings(){
  125. if( m_pDBC==NULL ) return false;
  126. if(! m_bEnabled) return false;
  127. m_pSettings = m_pDBC->getSettingsKAFKA( m_nDBAUXID );
  128. if( m_pSettings != NULL ) {
  129. if( m_pEvtTemplates != NULL ) {
  130. // Clear Templates
  131. for(int i = 0; i < EVT_CLASS_COUNT; i++ ){
  132. for(int j = 0; j < EVT_COUNT; j++ ){
  133. if( m_pEvtTemplates[i] != NULL ){
  134. if( m_pEvtTemplates[i][j] != NULL ){
  135. if( m_pEvtTemplates[i][j]->tpl ) free( m_pEvtTemplates[i][j]->tpl );
  136. free( m_pEvtTemplates[i][j] );
  137. m_pEvtTemplates[i][j] = NULL;
  138. };
  139. };
  140. };
  141. free( m_pEvtTemplates[i] );
  142. m_pEvtTemplates[i] = NULL;
  143. };
  144. for(int i = 0; i < EVT_CLASS_COUNT; i++ ){
  145. for(int j = 0; j < EVT_COUNT; j++ ){
  146. KAFKA_TEMPLATE * tT;
  147. tT = m_pDBC->getKafkaTemplateForEvent( m_nDBID, i, j );
  148. if( tT != NULL ) {
  149. if( m_pEvtTemplates[i]== NULL ) {
  150. m_pEvtTemplates[i]=(KAFKA_TEMPLATE**)calloc(EVT_COUNT,sizeof(KAFKA_TEMPLATE*));
  151. memset( m_pEvtTemplates[i], 0, EVT_COUNT * sizeof(KAFKA_TEMPLATE*));
  152. };
  153. m_pEvtTemplates[i][j] = tT;
  154. };
  155. };
  156. };
  157. };
  158. return true;
  159. };
  160. return false;
  161. };
  162. CSTR argusKafkaModule::buildTemplate(
  163. CSTR tpl,
  164. uint32_t source_id,
  165. CSTR ts,
  166. CSTR uuid,
  167. CSTR az,
  168. CSTR range,
  169. CSTR el
  170. ){
  171. CSTR ct1, ct2;
  172. char sid[64];
  173. if( tpl ) {
  174. sprintf( sid, "%d", source_id );
  175. ct1 = strReplace( tpl, KAFKA_TPL_SUBST_ID, sid );
  176. if( ct1 ) {
  177. ct2 = strReplace( ct1, KAFKA_TPL_SUBST_TS, ts );
  178. if( ct2 ) {
  179. free( ct1 );
  180. ct1 = ct2;
  181. };
  182. if( uuid ){
  183. ct2 = strReplace( ct1, KAFKA_TPL_SUBST_UUID, uuid );
  184. if( ct2 ) {
  185. free( ct1 );
  186. ct1 = ct2;
  187. };
  188. };
  189. if( az ){
  190. ct2 = strReplace( ct1, KAFKA_TPL_SUBST_AZ, az );
  191. if( ct2 ) {
  192. free( ct1 );
  193. ct1 = ct2;
  194. };
  195. };
  196. if( range ){
  197. ct2 = strReplace( ct1, KAFKA_TPL_SUBST_RANGE, range );
  198. if( ct2 ) {
  199. free( ct1 );
  200. ct1 = ct2;
  201. };
  202. };
  203. if( el ){
  204. ct2 = strReplace( ct1, KAFKA_TPL_SUBST_EL, el );
  205. if( ct2 ) {
  206. free( ct1 );
  207. ct1 = ct2;
  208. };
  209. };
  210. return ct1;
  211. };
  212. }
  213. return NULL;
  214. };
  215. void argusKafkaModule::dispatchEvent( basicEvent *Evt ){
  216. #ifdef DEBUG
  217. LOG( "EVT DISPATCHER");
  218. #endif
  219. if( Evt == NULL ) {
  220. LOG( "Evt NULL");
  221. return;
  222. };
  223. if( !m_bEnabled )return;
  224. if( m_pEvtTemplates[ Evt->m_eClass ]!= NULL ) {
  225. // We have defined Channel for that class Events
  226. #ifdef DEBUG
  227. char tBuffer[256];
  228. sprintf( tBuffer, "Event Class %d Type %d ", Evt->m_eClass , Evt->m_eType );
  229. LOG( tBuffer );
  230. #endif
  231. // Skip Event Handling by flags
  232. if( !m_bEvtFlags[ Evt->m_eType ] ) return;
  233. // Hadle Event
  234. CSTR tDATA;
  235. if( m_pEvtTemplates[ Evt->m_eClass ][ Evt->m_eType ] != NULL ) {
  236. CLIENT_EVENT *tCEVT;
  237. tCEVT = ( CLIENT_EVENT *) Evt->m_pEvtData;
  238. tDATA = buildTemplate( m_pEvtTemplates[ Evt->m_eClass ][ Evt->m_eType ]->tpl,
  239. Evt->m_nSourceUID,
  240. getUniqID_MD5()
  241. );
  242. if( tDATA!= NULL ) {
  243. m_pProtocol->issuePost( m_pEvtTemplates[ Evt->m_eClass ][ Evt->m_eType ]->topic, tDATA, strlen( tDATA ) );
  244. };
  245. };
  246. /*
  247. switch( Evt->m_eType ) {
  248. case EVT_TARGET_1D:
  249. case EVT_TARGET_2D_WHITE:
  250. case EVT_TARGET_2D_BLACK:
  251. case EVT_TARGET_3D_WHITE:
  252. case EVT_TARGET_3D_BLACK:{
  253. CLIENT_EVENT *tCEVT;
  254. tCEVT = ( CLIENT_EVENT *) Evt->m_pEvtData;
  255. if( tCEVT != NULL ){
  256. float Az, El;
  257. uint32_t cRange;
  258. cRange = (uint32_t)(((float) tCEVT->range ) * 0.0732421875f ) ; // To meters
  259. #ifdef PRU_SLAVE
  260. make3Ddeg( tCEVT->th_id, Az, El, false );
  261. #else
  262. make3Ddeg( tCEVT->th_id, Az, El, true );
  263. #endif
  264. m_pProtocol->Alarm3D( (int)Az, (int)El, cRange , tCEVT->k_id );
  265. };
  266. };
  267. break;
  268. case EVT_ERROR:
  269. m_pProtocol->SetStatus( false );
  270. break;
  271. case EVT_SCAN:
  272. //StatusUpdate();
  273. //break;
  274. case EVT_FLIP:
  275. m_pProtocol->SetStatus( true );
  276. //StatusUpdate();
  277. break;
  278. case EVT_COMMAND:
  279. break;
  280. default:
  281. LOG_ERROR("Unknown Event!");
  282. };
  283. */
  284. };
  285. };
  286. #undef LOG
  287. #undef LOG_ERROR