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.

529 lines
24 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
12 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "servaModBusManager.h"
  2. #include "servaKernel.h"
  3. #define LOG( X ) m_pLog->Log( "MODBUS", 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. m_pLog->Log( "MODBUS", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
  9. }
  10. #else
  11. #define LOG_ERROR( X ) m_pLog->Log( "MODBUS", this, _LOG_ERROR, X );
  12. #endif
  13. servaModBusManager *servaModBusManager::_instance = NULL;
  14. servaModBusManager *servaModBusManager::getInstance(){
  15. if ( _instance == NULL ) {
  16. _instance = new servaModBusManager();
  17. };
  18. return _instance;
  19. };
  20. servaModBusManager::servaModBusManager():basicThread(),basicValidatable(){
  21. m_bStage = MBUS_STAGE_INIT;
  22. m_pDBC = NULL;
  23. m_pLog = basicLogCollection::getInstance();
  24. m_pMaster = servaKernel::getInstance();
  25. if ( m_pMaster == NULL ) return;
  26. m_pDBC = m_pMaster->getDBC();
  27. memset( &m_sManagerData, 0, sizeof( MODBUS_MANAGER_DATA ));
  28. };
  29. servaModBusManager::~servaModBusManager(){
  30. waitThreadEnd( true );
  31. if (m_vRtuUnits.size()) {
  32. for( V_MODBUS_RTU_INFO::iterator idx=m_vRtuUnits.begin(); idx != m_vRtuUnits.end(); idx++ ) {
  33. free( *idx );
  34. };
  35. m_vRtuUnits.clear();
  36. };
  37. };
  38. bool servaModBusManager::create(){
  39. if( m_pMaster == NULL ) return false;
  40. if( m_pDBC == NULL ) return false;
  41. MODBUS_MANAGER_DATA * tData = m_pDBC->getModBusManagerData();
  42. if ( tData ) {
  43. memcpy( &m_sManagerData, tData, sizeof( MODBUS_MANAGER_DATA ) );
  44. free( tData );
  45. };
  46. m_bStage = MBUS_STAGE_INIT;
  47. return init();
  48. };
  49. int servaModBusManager::modbus_get_rtu( CSTR ip, uint16_t port, uint8_t addr, uint8_t &status ){
  50. uint8_t buf[16];
  51. modbus_t * ctx; // Modbus context pointer
  52. int rc; // Return code for error checking
  53. // Create a new Modbus TCP context for a slave device
  54. ctx = modbus_new_tcp( ip, port );
  55. if (ctx == NULL) {
  56. #ifdef DEBUG_EXTRA
  57. LOG( "Unable to create the Modbus context" );
  58. #endif
  59. return -1;
  60. };
  61. // Connect to the Modbus TCP slave device
  62. rc = modbus_connect(ctx);
  63. if (rc == -1) {
  64. #ifdef DEBUG_EXTRA
  65. char tB[256];
  66. sprintf(tB, "Connection failed: %s", modbus_strerror(errno));
  67. LOG( tB );
  68. #endif
  69. modbus_free(ctx);
  70. return -1;
  71. };
  72. rc = modbus_read_bits(ctx, addr, 8, buf ); //0x10
  73. if( rc == -1 ) {
  74. #ifdef DEBUG_EXTRA
  75. char tB[256];
  76. sprintf(tB, "Write register failed: %s", modbus_strerror(errno));
  77. LOG( tB );
  78. #endif
  79. } else {
  80. status = modbus_get_byte_from_bits( buf, 0, 8 );
  81. };
  82. // Close the Modbus TCP connection and free the context
  83. modbus_close(ctx);
  84. modbus_free(ctx);
  85. return rc;
  86. };
  87. int servaModBusManager::modbus_set_rtu( CSTR ip, uint16_t port, uint8_t addr, uint8_t data ){
  88. uint8_t bits[8];
  89. modbus_t * ctx; // Modbus context pointer
  90. int rc; // Return code for error checking
  91. // Create a new Modbus TCP context for a slave device
  92. ctx = modbus_new_tcp( ip, port );
  93. if (ctx == NULL) {
  94. #ifdef DEBUG_EXTRA
  95. LOG( "Unable to create the Modbus context");
  96. #endif
  97. return -1;
  98. };
  99. // Connect to the Modbus TCP slave device
  100. rc = modbus_connect(ctx);
  101. if (rc == -1) {
  102. #ifdef DEBUG_EXTRA
  103. char tB[256];
  104. sprintf(tB, "Connection failed: %s", modbus_strerror(errno));
  105. LOG( tB );
  106. #endif
  107. modbus_free(ctx);
  108. return -1;
  109. };
  110. modbus_set_bits_from_byte( bits, 0, data );
  111. rc = modbus_write_bits(ctx, addr, 8, bits ); //0x10
  112. if (rc == -1) {
  113. #ifdef DEBUG_EXTRA
  114. char tB[256];
  115. sprintf(tB, "Write register failed: %s", modbus_strerror(errno));
  116. LOG( tB );
  117. #endif
  118. };
  119. // Close the Modbus TCP connection and free the context
  120. modbus_close(ctx);
  121. modbus_free(ctx);
  122. return rc;
  123. };
  124. int servaModBusManager::modbus_set_rtu_bit( CSTR ip, uint16_t port, uint8_t addr, uint8_t bit ){
  125. modbus_t * ctx; // Modbus context pointer
  126. int rc; // Return code for error checking
  127. // Create a new Modbus TCP context for a slave device
  128. ctx = modbus_new_tcp( ip, port );
  129. if (ctx == NULL) {
  130. #ifdef DEBUG_EXTRA
  131. LOG( "Unable to create the Modbus context");
  132. #endif
  133. return -1;
  134. };
  135. // Connect to the Modbus TCP slave device
  136. rc = modbus_connect(ctx);
  137. if (rc == -1) {
  138. #ifdef DEBUG_EXTRA
  139. char tB[256];
  140. sprintf(tB, "Connection failed: %s", modbus_strerror(errno));
  141. LOG( tB );
  142. #endif
  143. modbus_free(ctx);
  144. return -1;
  145. };
  146. rc = modbus_write_bit(ctx, addr, bit ); //0x10
  147. if (rc == -1) {
  148. #ifdef DEBUG_EXTRA
  149. char tB[256];
  150. sprintf(tB, "Write register failed: %s", modbus_strerror(errno));
  151. LOG( tB );
  152. #endif
  153. };
  154. // Close the Modbus TCP connection and free the context
  155. modbus_close(ctx);
  156. modbus_free(ctx);
  157. return rc;
  158. };
  159. void servaModBusManager::m_fThreadProc( void ){
  160. m_bThreadStatus = TH_WORKING;
  161. uint32_t oss = time(NULL);
  162. while ( m_bThreadStatus < TH_ENDED ) {
  163. uint32_t css = time(NULL);
  164. switch (m_bStage) {
  165. case MBUS_STAGE_INIT:{
  166. #ifdef DEBUG
  167. LOG("Load Enabled ModBus Modules");
  168. #endif
  169. DBC_UINT64_LIST *tList = m_pDBC->listModBusRTUs( 0, 'E' );
  170. if ( tList ) {
  171. if ( tList->size() ) {
  172. for ( DBC_UINT64_LIST::iterator idx = tList->begin(); idx != tList->end(); idx++ ) {
  173. MODBUS_RTU_INFO *tData = m_pDBC->getModBusRTU( *idx );
  174. if ( tData ) {
  175. m_vRtuUnits.push_back( tData );
  176. };
  177. };
  178. };
  179. delete(tList);
  180. #ifdef DEBUG
  181. LOG("Load ModBus Modules Done");
  182. #endif
  183. } else LOG("No ModBus Modules Defined");
  184. m_bStage = MBUS_STAGE_SYNC;
  185. break;
  186. };
  187. case MBUS_STAGE_SYNC:{
  188. #ifdef DEBUG_EXTRA
  189. LOG("Sync ModBus Modules");
  190. #endif
  191. if( m_vRtuUnits.size() ){
  192. for( V_MODBUS_RTU_INFO::iterator idx = m_vRtuUnits.begin(); idx!= m_vRtuUnits.end(); idx++ ){
  193. int rv = modbus_get_rtu((*idx)->ip, (*idx)->port, (*idx)->output_base_addr, (*idx)->c_OutBits );
  194. if ( rv != -1 ) {
  195. rv = modbus_get_rtu((*idx)->ip, (*idx)->port, (*idx)->input_base_addr, (*idx)->c_InBits );
  196. if ( rv != -1 ) {
  197. (*idx)->status = MBUS_STATUS_ONLINE;
  198. m_pDBC->updateModBusRTU( (*idx)->id, (*idx)->status, (*idx)->c_InBits, (*idx)->c_OutBits );
  199. continue;
  200. }
  201. #ifdef DEBUG_EXTRA
  202. else LOG_ERROR("Read DI Failure");
  203. #endif
  204. }
  205. #ifdef DEBUG_EXTRA
  206. else LOG_ERROR("Read DO Failure");
  207. #endif
  208. (*idx)->status = MBUS_STATUS_OFFLINE;
  209. m_pDBC->updateModBusRTU( (*idx)->id, (*idx)->status, (*idx)->c_InBits, (*idx)->c_OutBits );
  210. };
  211. };
  212. #ifdef DEBUG_EXTRA
  213. LOG("Sync ModBus Modules Done");
  214. #endif
  215. m_bStage = MBUS_STAGE_WORK;
  216. break;
  217. };
  218. case MBUS_STAGE_WORK:{
  219. uint64_t cts = getMilliseconds();
  220. m_mProtector.lock();
  221. if ( m_vActioItems.size() ) {
  222. for( V_MODBUS_ACION_ITEMS::iterator idx = m_vActioItems.begin(); idx != m_vActioItems.end(); ){
  223. if( (*idx)->scheduled_on < cts ) {
  224. MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( (*idx)->module_id );
  225. if ( tRTU ) {
  226. if( ( tRTU->c_OutBits & (*idx)->value ) == 0 ) {
  227. //Set
  228. tRTU->c_OutBits |= (*idx)->value;
  229. int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits );
  230. if ( rv < 0 ) {
  231. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 );
  232. } else {
  233. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits );
  234. };
  235. };
  236. if( ( tRTU->c_OutBits & (*idx)->mask ) != 0 ) {
  237. //Clear
  238. tRTU->c_OutBits &= ~(*idx)->mask;
  239. int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits );
  240. if ( rv < 0 ) {
  241. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 );
  242. } else {
  243. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits );
  244. };
  245. };
  246. free(tRTU);
  247. };
  248. free( *idx );
  249. m_vActioItems.erase( idx );
  250. //Make Just One SCADA per loop
  251. //break;
  252. } else idx++;
  253. };
  254. };
  255. m_mProtector.unlock();
  256. break;
  257. };
  258. case MBUS_STAGE_END:{
  259. m_bThreadStatus = TH_ENDED;
  260. break;
  261. };
  262. };
  263. if ( oss != css ) {
  264. //Each Second
  265. oss = css;
  266. m_bStage = MBUS_STAGE_SYNC;
  267. };
  268. SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  269. };
  270. };
  271. bool servaModBusManager::validate(){
  272. MODBUS_MANAGER_DATA * tData = m_pDBC->getModBusManagerData();
  273. if ( tData ) {
  274. if( memcmp( &m_sManagerData, tData, sizeof( MODBUS_MANAGER_DATA ) ) != 0 ){
  275. #ifdef DEBUG
  276. LOG("Settings was changed ... reapplying");
  277. #endif
  278. memcpy( &m_sManagerData, tData, sizeof( MODBUS_MANAGER_DATA ) );
  279. };
  280. free( tData );
  281. };
  282. return true;
  283. };
  284. bool servaModBusManager::dispatchEvent( EVENT_CONTAINER *Evt ){
  285. if ( Evt == NULL ) return false;
  286. switch ( Evt->m_eType ) {
  287. case EVT_SRV_SESSION_STARTED:{
  288. if( m_sManagerData.automaticAlarmRisingTO ) {
  289. //setup Schedule
  290. SCADA_TRACK_INFO *tData = m_pDBC->getTrackSCADA( Evt->m_nSourceNodeID );
  291. if ( tData ) {
  292. //MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU_Platform( tData->platform_id );
  293. DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( tData->platform_id );
  294. if ( tIDS ) {
  295. for ( DBC_UINT64_LIST::iterator idx=tIDS->begin(); idx != tIDS->end(); idx++ ) {
  296. MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( *idx );
  297. if ( tRTU ) {
  298. MODBUS_ACION_ITEM *tAI = (MODBUS_ACION_ITEM *)malloc(sizeof(MODBUS_ACION_ITEM));
  299. if ( tAI ) {
  300. memset( tAI, 0, sizeof(MODBUS_ACION_ITEM) );
  301. tAI->scheduled_on = getMilliseconds() + ( m_sManagerData.automaticAlarmRisingTO * 1000 );
  302. tAI->module_id = tRTU->id;
  303. tAI->value = ( 1 << tRTU->crossbar[((tData->side == 'U')?MBUS_CROSSBAR_UPPER_TRACK:MBUS_CROSSBAR_BOTTOM_TRACK)] );
  304. m_mProtector.lock();
  305. m_vActioItems.push_back( tAI );
  306. m_mProtector.unlock();
  307. };
  308. free( tRTU );
  309. };
  310. };
  311. };
  312. free( tData );
  313. };
  314. };
  315. break;
  316. };
  317. case EVT_SRV_SESSION_ENDED:{
  318. SCADA_TRACK_INFO *tData = m_pDBC->getTrackSCADA( Evt->m_nSourceNodeID );
  319. if ( tData ) {
  320. //MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU_Platform( tData->platform_id );
  321. DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( tData->platform_id );
  322. if ( tIDS ) {
  323. for ( DBC_UINT64_LIST::iterator idx=tIDS->begin(); idx != tIDS->end(); idx++ ) {
  324. MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( *idx );
  325. if ( tRTU ) {
  326. uint8_t bON = ( 1 << tRTU->crossbar[((tData->side == 'U')?MBUS_CROSSBAR_UPPER_TRACK:MBUS_CROSSBAR_BOTTOM_TRACK)] );
  327. uint8_t bOFF = ~( 1 << tRTU->crossbar[((tData->side == 'U')?MBUS_CROSSBAR_UPPER_TRACK:MBUS_CROSSBAR_BOTTOM_TRACK)] );
  328. //Remove Scheduled Signal change
  329. m_mProtector.lock();
  330. for ( V_MODBUS_ACION_ITEMS::iterator idx=m_vActioItems.begin(); idx!=m_vActioItems.end(); ){
  331. if( (*idx)->module_id == tRTU->id ) {
  332. //check if shceduled signal is current
  333. if( (((*idx)->value & bON ) == bON ) || (((*idx)->mask & bOFF ) == bOFF ) ){
  334. free((*idx));
  335. m_vActioItems.erase( idx );
  336. } else idx++;
  337. //break;
  338. } else idx++;
  339. };
  340. m_mProtector.unlock();
  341. //Mitigate Signal if sent
  342. if ( (tRTU->c_OutBits & bON ) == bON ) {
  343. tRTU->c_OutBits &= bOFF;
  344. int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits );
  345. if ( rv < 0 ) {
  346. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 );
  347. } else {
  348. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits );
  349. };
  350. };
  351. free( tRTU );
  352. };
  353. };
  354. };
  355. free( tData );
  356. };
  357. break;
  358. };
  359. case EVT_SRV_SESSION_RATED:{
  360. if ( Evt->m_pEvtData == NULL ) return false;
  361. if ( m_sManagerData.hazardSignalLevel ) {
  362. SESSION_RATE_DATA *tSRD = (SESSION_RATE_DATA *)Evt->m_pEvtData;
  363. SCADA_TRACK_INFO *tData = m_pDBC->getTrackSCADA( tSRD->track_id );
  364. if ( tData ) {
  365. //MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU_Platform( tData->platform_id );
  366. DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( tData->platform_id );
  367. if ( tIDS ) {
  368. for ( DBC_UINT64_LIST::iterator idx=tIDS->begin(); idx != tIDS->end(); idx++ ) {
  369. MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( *idx );
  370. if ( tRTU ) {
  371. uint8_t bON = ( 1 << tRTU->crossbar[((tData->side == 'U')?MBUS_CROSSBAR_UPPER_TRACK:MBUS_CROSSBAR_BOTTOM_TRACK)] );
  372. uint8_t bOFF = ~( 1 << tRTU->crossbar[((tData->side == 'U')?MBUS_CROSSBAR_UPPER_TRACK:MBUS_CROSSBAR_BOTTOM_TRACK)] );
  373. //Remove Scheduled Signal change
  374. m_mProtector.lock();
  375. for ( V_MODBUS_ACION_ITEMS::iterator idx=m_vActioItems.begin(); idx!=m_vActioItems.end(); ){
  376. //check if shceduled ActionItem is Assigned to Current RTU
  377. if( (*idx)->module_id == tRTU->id ) {
  378. if( (((*idx)->value & bON ) == bON ) || (((*idx)->mask & bOFF ) == bOFF ) ){
  379. free((*idx));
  380. m_vActioItems.erase( idx );
  381. } else idx++;
  382. } else idx++;
  383. };
  384. m_mProtector.unlock();
  385. if ( tSRD->level < m_sManagerData.hazardSignalLevel ) {
  386. //Clear Signal if Set
  387. if ( (tRTU->c_OutBits & bON ) == bON ) {
  388. tRTU->c_OutBits &= bOFF;
  389. int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits );
  390. if ( rv < 0 ) {
  391. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 );
  392. } else {
  393. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits );
  394. };
  395. };
  396. } else {
  397. //Set Signal if was Cleared
  398. if ( (tRTU->c_OutBits & bON ) == 0 ) {
  399. tRTU->c_OutBits |= bON;
  400. int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits );
  401. if ( rv < 0 ) {
  402. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 );
  403. } else {
  404. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits );
  405. };
  406. };
  407. };
  408. free( tRTU );
  409. };
  410. };
  411. };
  412. free( tData );
  413. };
  414. };
  415. break;
  416. };
  417. case EVT_SRV_TEST_MODBUS:{
  418. MODBUS_TEST_DATA *tData = (MODBUS_TEST_DATA *)Evt->m_pEvtData;
  419. if ( tData ) {
  420. //MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU_Platform( tData->platform_id );
  421. DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( tData->platform_id );
  422. if ( tIDS ) {
  423. for ( DBC_UINT64_LIST::iterator idx=tIDS->begin(); idx != tIDS->end(); idx++ ) {
  424. MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( *idx );
  425. if ( tRTU ) {
  426. #ifdef DEBUG_EXTRA
  427. char tB[200];
  428. sprintf( tB, "%s:%d 0x%02x 0x%02x", tRTU->ip, tRTU->port, tRTU->output_base_addr, tData->c_OutBits );
  429. LOG( tB );
  430. #endif
  431. int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tData->c_OutBits );
  432. if ( rv < 0 ) {
  433. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 );
  434. } else {
  435. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tData->c_OutBits );
  436. };
  437. free( tRTU );
  438. };
  439. };
  440. };
  441. };
  442. return false; // No need Propogate this Evt
  443. };
  444. case EVT_SRV_MODULE_HEALTH:{
  445. if ( Evt->m_pEvtData == NULL ) return false;
  446. if ( m_sManagerData.helthSignalLevel ) {
  447. MODULE_HEALTH_STATUS *tData = (MODULE_HEALTH_STATUS *)Evt->m_pEvtData;
  448. if ( tData->status >= m_sManagerData.helthSignalLevel ) {
  449. //MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU_Platform( tData->platform_id );
  450. DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( tData->platform_id );
  451. if ( tIDS ) {
  452. for ( DBC_UINT64_LIST::iterator idx=tIDS->begin(); idx != tIDS->end(); idx++ ) {
  453. MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( *idx );
  454. if (tRTU) {
  455. uint8_t bON = ( 1 << tRTU->crossbar[MBUS_CROSSBAR_HEALTH] );
  456. uint8_t bOFF = ~( 1 << tRTU->crossbar[MBUS_CROSSBAR_HEALTH] );
  457. #ifdef DEBUG_EXTRA
  458. char tB[200];
  459. sprintf( tB, "%s:%d 0x%02x 0x%02x", tRTU->ip, tRTU->port, tRTU->output_base_addr, tData->c_OutBits );
  460. LOG( tB );
  461. #endif
  462. //Set Signal if was Cleared
  463. if ( (tRTU->c_OutBits & bON ) == 0 ) {
  464. tRTU->c_OutBits |= bON;
  465. int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits );
  466. if ( rv < 0 ) {
  467. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 );
  468. } else {
  469. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits );
  470. };
  471. };
  472. free( tRTU );
  473. };
  474. };
  475. delete( tIDS );
  476. };
  477. } else { //Below Level
  478. //MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU_Platform( tData->platform_id );
  479. DBC_UINT64_LIST *tIDS = m_pDBC->listPlatformModBusRTU( tData->platform_id );
  480. if ( tIDS ) {
  481. for ( DBC_UINT64_LIST::iterator idx=tIDS->begin(); idx != tIDS->end(); idx++ ) {
  482. MODBUS_RTU_INFO *tRTU = m_pDBC->getModBusRTU( *idx );
  483. if ( tRTU ) {
  484. uint8_t bON = ( 1 << tRTU->crossbar[MBUS_CROSSBAR_HEALTH] );
  485. uint8_t bOFF = ~( 1 << tRTU->crossbar[MBUS_CROSSBAR_HEALTH] );
  486. #ifdef DEBUG_EXTRA
  487. char tB[200];
  488. sprintf( tB, "%s:%d 0x%02x 0x%02x", tRTU->ip, tRTU->port, tRTU->output_base_addr, tData->c_OutBits );
  489. LOG( tB );
  490. #endif
  491. //Mitigate Signal if sent
  492. if ( (tRTU->c_OutBits & bON ) == bON ) {
  493. tRTU->c_OutBits &= bOFF;
  494. int rv = modbus_set_rtu( tRTU->ip, tRTU->port, tRTU->output_base_addr, tRTU->c_OutBits );
  495. if ( rv < 0 ) {
  496. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_OFFLINE, 0, 0 );
  497. } else {
  498. m_pDBC->updateModBusRTU( tRTU->id, MBUS_STATUS_ONLINE, tRTU->c_InBits, tRTU->c_OutBits );
  499. };
  500. };
  501. free( tRTU );
  502. };
  503. };
  504. };
  505. };
  506. };
  507. break;
  508. };
  509. };
  510. return true;
  511. };
  512. #undef LOG
  513. #undef LOG_ERROR