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.

785 lines
26 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "protocolKAFKA.h"
  2. #include "../../alg/generic/md5.h"
  3. #include "../json/json_tool.h"
  4. #include <strings.h>
  5. #include <json/value.h>
  6. #include <json/writer.h>
  7. #include <json/reader.h>
  8. //https://il.iherb.com/pr/KAL-Blood-Sugar-Defense-60-Tablets/70138
  9. #define LOG( X ) m_pLog->Log( "KAFKA", this, X )
  10. #define LOGS( X ) basicLogCollection::getInstance()->Log( "KAFKA_S", NULL, X )
  11. #define LOG_ERROR( X ) m_pLog->Log( "KAFKA", this, _LOG_ERROR, X )
  12. /*
  13. CSTR protocolKAFKA::buildURL( CSTR templ, int dim, CSTR devID ){
  14. CSTR RetVal;
  15. RetVal = (CSTR) malloc( 256 );
  16. if (RetVal != NULL) {
  17. if (m_bUseSSL) {
  18. sprintf(RetVal, "https://%s:%d%s", m_pServerIP, m_nServerPort, m_pAccessURL);
  19. } else {
  20. sprintf(RetVal, "http://%s:%d%s", m_pServerIP, m_nServerPort, m_pAccessURL);
  21. };
  22. };
  23. return RetVal;
  24. };
  25. */
  26. CSTR protocolKAFKA::buildUniqID( void ){
  27. CSTR RetVal;
  28. RetVal = (CSTR) malloc(64);
  29. if(RetVal) sprintf( RetVal, "%s", MD5String( getSHDTimeStamp() ) );
  30. return RetVal;
  31. };
  32. /* Default Constructor */
  33. protocolKAFKA::protocolKAFKA( config_setting_t *Settings ){
  34. m_pLog = basicLogCollection::getInstance();
  35. m_pSettings = basicSettingsProvider::getInstance()->getSubNode(Settings, "INFOSTRESS_CONNECTION");
  36. m_pKafka = NULL;
  37. m_pKafkaBrokerSTR = (CSTR)malloc(CLOG);
  38. if( m_pKafkaBrokerSTR == NULL ) return;
  39. memset( m_pKafkaBrokerSTR, 0 , CLOG);
  40. m_pKafkaTopicSTR= (CSTR)malloc(CLOG);
  41. if( m_pKafkaTopicSTR == NULL ) return;
  42. memset( m_pKafkaTopicSTR, 0 , CLOG);
  43. m_pKafkaTopic = NULL;
  44. m_pScannerName = (CSTR)malloc(OS_SCANNER_NAME_SIZE);
  45. if( m_pScannerName == NULL ) return;
  46. memset( m_pScannerName, 0 , OS_SCANNER_NAME_SIZE);
  47. m_pServerIP = (CSTR)malloc(OS_CEP_ADDRESS_SIZE);
  48. if( m_pServerIP == NULL ) return;
  49. memset( m_pServerIP, 0 , OS_CEP_ADDRESS_SIZE);
  50. m_pAccessURL = (CSTR)malloc(CTEXT);
  51. if (m_pAccessURL == NULL) return;
  52. memset(m_pAccessURL, 0, CTEXT);
  53. m_bCurrentStatus = false;
  54. m_bMitigationStatus = false;
  55. m_bEnabled = false;
  56. m_bEnableStatus = false;
  57. if( m_pSettings != NULL ){
  58. int intVar;
  59. double floatVar;
  60. config_setting_t *pSetting;
  61. if (!config_setting_lookup_float(m_pSettings, "LOCATION_LON", &floatVar)) {
  62. m_fLon = floatVar = 35.0938962;
  63. pSetting = config_setting_add(m_pSettings, "LOCATION_LON", CONFIG_TYPE_FLOAT);
  64. config_setting_set_float(pSetting, floatVar);
  65. basicSettingsProvider::getInstance()->compromise();
  66. } else {
  67. m_fLon = floatVar;
  68. };
  69. if (!config_setting_lookup_float(m_pSettings, "LOCATION_LAT", &floatVar)) {
  70. m_fLat = floatVar = 32.904208;
  71. pSetting = config_setting_add(m_pSettings, "LOCATION_LAT", CONFIG_TYPE_FLOAT);
  72. config_setting_set_float(pSetting, floatVar);
  73. basicSettingsProvider::getInstance()->compromise();
  74. } else {
  75. m_fLat = floatVar;
  76. };
  77. if (!config_setting_lookup_bool(m_pSettings, "USE_SSL", &intVar)) {
  78. intVar = m_bUseSSL = false;
  79. LOG("SSL DISABLED[default]");
  80. pSetting = config_setting_add(m_pSettings, "USE_SSL", CONFIG_TYPE_BOOL);
  81. config_setting_set_bool(pSetting, intVar);
  82. basicSettingsProvider::getInstance()->compromise();
  83. } else {
  84. m_bUseSSL = (bool)intVar;
  85. };
  86. if (!config_setting_lookup_bool(m_pSettings, "ENABLE_STATUS_REPORT", &intVar)) {
  87. intVar = m_bEnableStatus = false;
  88. LOG("Status Report DISABLED[default]");
  89. pSetting = config_setting_add(m_pSettings, "ENABLE_STATUS_REPORT", CONFIG_TYPE_BOOL);
  90. config_setting_set_bool(pSetting, intVar);
  91. basicSettingsProvider::getInstance()->compromise();
  92. } else {
  93. m_bEnableStatus = (bool)intVar;
  94. };
  95. if (!config_setting_lookup_string(m_pSettings, "SCANNER_NAME", (const char**)&m_pScannerName)) {
  96. strncpy(m_pScannerName, "LRF_2D_001", 64);
  97. LOG(m_pScannerName);
  98. pSetting = config_setting_add(m_pSettings, "SCANNER_NAME", CONFIG_TYPE_STRING);
  99. config_setting_set_string(pSetting, m_pScannerName);
  100. basicSettingsProvider::getInstance()->compromise();
  101. };
  102. if (!config_setting_lookup_int(m_pSettings, "SCANNER_DIMENTIONS", &m_nScannerDimentions)) {
  103. m_nScannerDimentions = 2;
  104. pSetting = config_setting_add(m_pSettings, "SCANNER_DIMENTIONS", CONFIG_TYPE_INT);
  105. config_setting_set_int(pSetting, m_nScannerDimentions);
  106. basicSettingsProvider::getInstance()->compromise();
  107. };
  108. if (!config_setting_lookup_string(m_pSettings, "KAFKA_BROKER_IP", (const char**)&m_pServerIP)) {
  109. strncpy(m_pServerIP, "192.168.200.204", 16);
  110. LOG(m_pServerIP);
  111. pSetting = config_setting_add(m_pSettings, "KAFKA_BROKER_IP", CONFIG_TYPE_STRING);
  112. config_setting_set_string(pSetting, m_pServerIP);
  113. basicSettingsProvider::getInstance()->compromise();
  114. };
  115. LOG(m_pServerIP);
  116. if (!config_setting_lookup_int(m_pSettings, "KAFKA_BROKER_PORT", &m_nServerPort)) {
  117. m_nServerPort = 80;
  118. pSetting = config_setting_add(m_pSettings, "KAFKA_BROKER_PORT", CONFIG_TYPE_INT);
  119. config_setting_set_int(pSetting, m_nServerPort);
  120. basicSettingsProvider::getInstance()->compromise();
  121. };
  122. sprintf( m_pKafkaBrokerSTR, "%s:%d", m_pServerIP, m_nServerPort );
  123. LOG( m_pKafkaBrokerSTR );
  124. if (!config_setting_lookup_string(m_pSettings, "KAFKA_TOPIC", (const char**)&m_pKafkaTopicSTR)) {
  125. strncpy( m_pKafkaTopicSTR, DFSL_INFOSTRESS_DEFAULT_KAFKA_TOPIC, CTEXT );
  126. pSetting = config_setting_add(m_pSettings, "KAFKA_TOPIC", CONFIG_TYPE_STRING);
  127. config_setting_set_string(pSetting, m_pKafkaTopicSTR);
  128. basicSettingsProvider::getInstance()->compromise();
  129. };
  130. LOG( m_pKafkaTopicSTR );
  131. basicSettingsProvider::getInstance()->validate();
  132. };
  133. };
  134. protocolKAFKA::protocolKAFKA( KAFKA_SETTINGS *Settings ){
  135. m_pLog = basicLogCollection::getInstance();
  136. m_pSettings = NULL;
  137. m_pKafka = NULL;
  138. m_pKafkaBrokerSTR = (CSTR)malloc(CLOG);
  139. if( m_pKafkaBrokerSTR == NULL ) return;
  140. memset( m_pKafkaBrokerSTR, 0 , CLOG);
  141. m_pKafkaTopicSTR= (CSTR)malloc(CLOG);
  142. if( m_pKafkaTopicSTR == NULL ) return;
  143. memset( m_pKafkaTopicSTR, 0 , CLOG);
  144. m_pKafkaTopic = NULL;
  145. m_pScannerName = (CSTR)malloc(OS_SCANNER_NAME_SIZE);
  146. if( m_pScannerName == NULL ) return;
  147. memset( m_pScannerName, 0 , OS_SCANNER_NAME_SIZE);
  148. m_pServerIP = (CSTR)malloc(OS_CEP_ADDRESS_SIZE);
  149. if( m_pServerIP == NULL ) return;
  150. memset( m_pServerIP, 0 , OS_CEP_ADDRESS_SIZE);
  151. m_pAccessURL = (CSTR)malloc(CTEXT);
  152. if (m_pAccessURL == NULL) return;
  153. memset(m_pAccessURL, 0, CTEXT);
  154. m_bCurrentStatus = false;
  155. m_bMitigationStatus = false;
  156. m_bEnabled = false;
  157. m_bEnableStatus = false;
  158. if( Settings!= NULL ) {
  159. strncpy( m_pServerIP, Settings->brocker_addr, 16 );
  160. m_nServerPort = Settings->brocker_port;
  161. };
  162. };
  163. /**
  164. * @brief Message delivery report callback.
  165. *
  166. * This callback is called exactly once per message, indicating if
  167. * the message was succesfully delivered
  168. * (rkmessage->err == RD_KAFKA_RESP_ERR_NO_ERROR) or permanently
  169. * failed delivery (rkmessage->err != RD_KAFKA_RESP_ERR_NO_ERROR).
  170. *
  171. * The callback is triggered from rd_kafka_poll() and executes on
  172. * the application's thread.
  173. */
  174. /*
  175. static void dr_msg_cb( rd_kafka_t *rk, const rd_kafka_message_t *rkmessage, void *opaque ){
  176. if (rkmessage->err)
  177. fprintf(stderr, "%% Message delivery failed: %s\n",
  178. rd_kafka_err2str(rkmessage->err));
  179. else
  180. fprintf(stderr,
  181. "%% Message delivered (%zd bytes, "
  182. "partition %"PRId32")\n",
  183. rkmessage->len, rkmessage->partition);
  184. // The rkmessage is destroyed automatically by librdkafka
  185. };
  186. */
  187. protocolKAFKA::~protocolKAFKA(){
  188. m_bEnabled = false;
  189. if( m_pKafka != NULL ){
  190. /* Wait for final messages to be delivered or fail.
  191. * rd_kafka_flush() is an abstraction over rd_kafka_poll() which
  192. * waits for all messages to be delivered. */
  193. LOG("Flushing final messages..");
  194. m_pKafka->flush( 3000 /* wait for max 3 seconds */);
  195. /* If the output queue is still not empty there is an issue
  196. * with producing messages to the clusters. */
  197. if (m_pKafka->outq_len() > 0) LOG_ERROR("Some messages are not delivered!");
  198. delete m_pKafka;
  199. };
  200. if( m_pScannerName != NULL ) free(m_pScannerName);
  201. if( m_pServerIP != NULL ) free(m_pServerIP);
  202. if( m_pAccessURL != NULL ) free(m_pAccessURL);
  203. };
  204. //https://github.com/edenhill/librdkafka/blob/master/examples/producer.c
  205. //https://github.com/edenhill/librdkafka/blob/master/examples/producer.cpp
  206. void protocolKAFKA::dr_cb( RdKafka::Message &message ){
  207. /* If message.err() is non-zero the message delivery failed permanently
  208. * for the message. */
  209. char tBuffer[1024];
  210. if( message.err() ){
  211. sprintf(tBuffer,"Message delivery failed: %s", message.errstr().c_str() );
  212. LOG_ERROR( tBuffer );
  213. }
  214. #ifdef DEBUG
  215. else {
  216. sprintf(tBuffer,"Message delivered to topic: %s [%d] at offset %d", message.topic_name().c_str(), message.partition(), message.offset() );
  217. LOG( tBuffer );
  218. }
  219. #endif
  220. ;
  221. };
  222. bool protocolKAFKA::init(){
  223. RdKafka::Conf * tProducerConf;
  224. RdKafka::Conf * tTopicConf;
  225. std::string tBuffer;
  226. std::string brokers = m_pKafkaBrokerSTR;
  227. std::string topic = m_pKafkaTopicSTR;
  228. LOG("Begin Init");
  229. if( strlen( m_pKafkaBrokerSTR ) == 0 ) {
  230. LOG_ERROR("No Kafka Brokers are defined!");
  231. return false;
  232. };
  233. tProducerConf = RdKafka::Conf::create( RdKafka::Conf::CONF_GLOBAL );
  234. if( tProducerConf == NULL ) {
  235. LOG_ERROR("Eror Creating Producer Conf!");
  236. return false;
  237. };
  238. LOG("RdKafka::Conf::CONF_GLOBAL Created");
  239. tTopicConf = RdKafka::Conf::create(RdKafka::Conf::CONF_TOPIC);
  240. if( tTopicConf==NULL ){
  241. LOG_ERROR("Eror Creating Topic Conf!");
  242. delete( tProducerConf );
  243. tProducerConf = NULL;
  244. return false;
  245. };
  246. LOG("RdKafka::Conf::CONF_TOPIC Created");
  247. if( tProducerConf->set("bootstrap.servers", brokers, tBuffer ) != RdKafka::Conf::CONF_OK ){
  248. LOG_ERROR( tBuffer.c_str() );
  249. delete( tProducerConf );
  250. delete( tTopicConf );
  251. tProducerConf = NULL;
  252. tTopicConf = NULL;
  253. return false;
  254. };
  255. LOG("bootstrap.servers is SET");
  256. if( tProducerConf->set("metadata.broker.list", brokers, tBuffer ) != RdKafka::Conf::CONF_OK ){
  257. LOG_ERROR( tBuffer.c_str() );
  258. delete( tProducerConf );
  259. delete( tTopicConf );
  260. tProducerConf = NULL;
  261. tTopicConf = NULL;
  262. return false;
  263. };
  264. LOG("metadata.broker.list is SET");
  265. /* Set the delivery report callback.
  266. * This callback will be called once per message to inform
  267. * the application if delivery succeeded or failed.
  268. * See dr_msg_cb() above.
  269. * The callback is only triggered from rd_kafka_poll() and
  270. * rd_kafka_flush(). */
  271. //tProducerConf->set("queue.buffering.max.ms", "100", tBuffer );
  272. //tProducerConf->set("socket.blocking.max.ms", "100", tBuffer );
  273. if( tProducerConf->set( "dr_cb", this, tBuffer ) != RdKafka::Conf::CONF_OK ){
  274. LOG_ERROR( tBuffer.c_str() );
  275. delete( tProducerConf );
  276. delete( tTopicConf );
  277. tProducerConf = NULL;
  278. tTopicConf = NULL;
  279. return false;
  280. };
  281. LOG("dr_cb is SET");
  282. if( tProducerConf->set("default_topic_conf", tTopicConf, tBuffer ) != RdKafka::Conf::CONF_OK ){
  283. LOG_ERROR( tBuffer.c_str() );
  284. delete( tProducerConf );
  285. delete( tTopicConf );
  286. tProducerConf = NULL;
  287. tTopicConf = NULL;
  288. return false;
  289. };
  290. LOG("default_topic_conf is SET");
  291. /*
  292. * Create producer instance.
  293. *
  294. * NOTE: rd_kafka_new() takes ownership of the conf object
  295. * and the application must not reference it again after
  296. * this call.
  297. */
  298. m_pKafka = RdKafka::Producer::create( tProducerConf, tBuffer );
  299. if( m_pKafka == NULL ){
  300. LOG_ERROR( tBuffer.c_str() );
  301. delete( tProducerConf );
  302. delete( tTopicConf );
  303. tProducerConf = NULL;
  304. tTopicConf = NULL;
  305. return false;
  306. };
  307. LOG("RdKafka::Producer Created");
  308. m_pKafkaTopic = RdKafka::Topic::create( m_pKafka, topic, tTopicConf, tBuffer );
  309. if( m_pKafkaTopic == NULL ) {
  310. LOG_ERROR( tBuffer.c_str() );
  311. delete( tProducerConf );
  312. delete( tTopicConf );
  313. tProducerConf = NULL;
  314. tTopicConf = NULL;
  315. return false;
  316. };
  317. LOG("RdKafka::Topic Created");
  318. delete( tProducerConf );
  319. delete( tTopicConf );
  320. tProducerConf = NULL;
  321. tTopicConf = NULL;
  322. LOG("INIT Complete");
  323. m_bEnabled = true;
  324. return true;
  325. };
  326. bool protocolKAFKA::issuePost( CSTR Buffer, int BufferSize ){
  327. if( m_pKafka == NULL) return false;
  328. RdKafka::ErrorCode err;
  329. retry:
  330. err = m_pKafka->produce(
  331. m_pKafkaTopic,
  332. RdKafka::Topic::PARTITION_UA,
  333. RdKafka::Producer::RK_MSG_COPY,
  334. (void *)Buffer,
  335. BufferSize,
  336. NULL,
  337. NULL
  338. );
  339. if( err != RdKafka::ERR_NO_ERROR ) {
  340. ///std::cerr << "% Failed to produce to topic " << topic << ": " << RdKafka::err2str(err) << std::endl;
  341. LOG_ERROR("Failed to produce to topic");
  342. if( err == RdKafka::ERR__QUEUE_FULL ){
  343. LOG("Internal queue is full!");
  344. /* If the internal queue is full, wait for
  345. * messages to be delivered and then retry.
  346. * The internal queue represents both
  347. * messages to be sent and messages that have
  348. * been sent or failed, awaiting their
  349. * delivery report callback to be called.
  350. *
  351. * The internal queue is limited by the
  352. * configuration property
  353. * queue.buffering.max.messages */
  354. m_pKafka->poll(1000/*block for max 1000ms*/);
  355. goto retry;
  356. };
  357. }
  358. #ifdef DEBUG
  359. else {
  360. LOG("MSG Queued OK");
  361. //std::cerr << "% Enqueued message (" << line.size() << " bytes) " << "for topic " << topic << std::endl;
  362. }
  363. #endif
  364. ;
  365. /* A producer application should continually serve
  366. * the delivery report queue by calling poll()
  367. * at frequent intervals.
  368. * Either put the poll call in your main loop, or in a
  369. * dedicated thread, or call it after every produce() call.
  370. * Just make sure that poll() is still called
  371. * during periods where you are not producing any messages
  372. * to make sure previously produced messages have their
  373. * delivery report callback served (and any other callbacks
  374. * you register). */
  375. m_pKafka->poll(100);
  376. //uint8_t cnt = 100;
  377. //while ( cnt && m_pKafka->outq_len() > 0) {
  378. // LOG("Wait ...");
  379. // cnt--;
  380. // m_pKafka->poll(100);
  381. //};
  382. return true;
  383. };
  384. void protocolKAFKA::setScannerName( CSTR Name ){
  385. sprintf( m_pScannerName, "%s", Name );
  386. };
  387. void protocolKAFKA::setProtocolType( bool DeviceIs3D ){
  388. if( DeviceIs3D ) {
  389. m_nScannerDimentions = 3;
  390. } else {
  391. m_nScannerDimentions = 2;
  392. };
  393. };
  394. CSTR protocolKAFKA::assembleStatusData( bool StatusOK ){
  395. CSTR RetVal = NULL;
  396. CSTR TIME_STAMP;
  397. CSTR tSRC;
  398. CSTR tTARGET;
  399. CSTR tID;
  400. RetVal = (CSTR) malloc(CLOG);
  401. if ( RetVal != NULL ) {
  402. TIME_STAMP = getSHDTimeStamp();
  403. if( TIME_STAMP != NULL ) {
  404. tTARGET = (CSTR) malloc(128);
  405. if( tTARGET!= NULL) {
  406. tSRC = (CSTR) malloc(128);
  407. if( tSRC ) {
  408. tID = (CSTR) malloc(64);
  409. if( tID != NULL ) {
  410. sprintf( tID, "%s", MD5String( TIME_STAMP ) );
  411. sprintf( tSRC, DFSL_SENSOR_SOURCE_DTO, DFSL_HARDWARE_TYPE, DFRL_PROVIDER_ID, m_pScannerName );
  412. sprintf( tTARGET, DFSL_STATUS_DTO, TIME_STAMP , defenderStatusMode[((StatusOK)?1:0)] );
  413. sprintf( RetVal,
  414. PACKAGE_SE_STATUS_DATA_ELEMENT,
  415. defenderTypeDescription[DC_STATUS],
  416. TIME_STAMP,
  417. tSRC,
  418. "DFSL_LIDAR_SENSOR_STATUS",
  419. DFSL_DATA_TYPE_STATUS,
  420. tID,
  421. tTARGET
  422. );
  423. free( tID );
  424. free( tSRC );
  425. free( tTARGET );
  426. free( TIME_STAMP );
  427. } else {
  428. free( tSRC );
  429. free( tTARGET );
  430. free( TIME_STAMP );
  431. free( RetVal );
  432. }
  433. } else {
  434. free( tTARGET );
  435. free( TIME_STAMP );
  436. free( RetVal );
  437. };
  438. } else {
  439. free( TIME_STAMP );
  440. free( RetVal );
  441. };
  442. } else {
  443. free( RetVal );
  444. RetVal = NULL;
  445. };
  446. };
  447. return RetVal;
  448. };
  449. CSTR protocolKAFKA::assembleFaultData( uint8_t Code, CSTR Desc ){
  450. CSTR RetVal;
  451. CSTR TIME_STAMP;
  452. CSTR tSRC;
  453. CSTR tTARGET;
  454. CSTR tID;
  455. RetVal = (CSTR) malloc(CLOG);
  456. if ( RetVal != NULL ) {
  457. TIME_STAMP = getSHDTimeStamp();
  458. if( TIME_STAMP != NULL ) {
  459. tTARGET = (CSTR) malloc(128);
  460. if( tTARGET!= NULL) {
  461. tSRC = (CSTR) malloc(128);
  462. if( tSRC ) {
  463. tID = (CSTR) malloc(64);
  464. if( tID != NULL ) {
  465. sprintf( tID, "%s", MD5String( TIME_STAMP ) );
  466. sprintf( tSRC, DFSL_SENSOR_SOURCE_DTO, DFSL_HARDWARE_TYPE, DFRL_PROVIDER_ID, m_pScannerName );
  467. sprintf( tTARGET, DFSL_ERROR_DTO, TIME_STAMP, Code, Desc );
  468. sprintf( RetVal,
  469. PACKAGE_SE_STATUS_DATA_ELEMENT,
  470. Desc,
  471. TIME_STAMP,
  472. tSRC,
  473. "DFSL_LIDARE_SENSOR_FAULT_DATA",
  474. DFSL_DATA_TYPE_ERROR,
  475. tID,
  476. tTARGET
  477. );
  478. free( tID );
  479. free( tSRC );
  480. free( tTARGET );
  481. free( TIME_STAMP );
  482. } else {
  483. free( tSRC );
  484. free( tTARGET );
  485. free( TIME_STAMP );
  486. free( RetVal );
  487. }
  488. } else {
  489. free( tTARGET );
  490. free( TIME_STAMP );
  491. free( RetVal );
  492. };
  493. } else {
  494. free( TIME_STAMP );
  495. free( RetVal );
  496. };
  497. } else {
  498. free( RetVal );
  499. RetVal = NULL;
  500. };
  501. };
  502. return RetVal;
  503. };
  504. CSTR protocolKAFKA::assembleTargetData( bool is3D, int targetID, int Range, int Width, int Azimuth, int Elevation ){
  505. CSTR RetVal = NULL;
  506. RetVal = (CSTR) malloc(CLOG);
  507. if ( RetVal != NULL ) {
  508. CSTR TIME_STAMP = NULL;
  509. TIME_STAMP = getTStr();
  510. if( TIME_STAMP!= NULL ) {
  511. char tID[64];
  512. float Az;
  513. float Ra;
  514. sprintf( tID, "%s", MD5String( TIME_STAMP ) );
  515. free( TIME_STAMP );
  516. Ra = Range;
  517. Az = Azimuth;
  518. if( is3D ) {
  519. float El;
  520. El = Elevation;
  521. sprintf( RetVal, PACKAGE_INFOSTRESS_DATA_3D, tID, m_pScannerName, Az, El, Ra );
  522. } else {
  523. sprintf( RetVal, PACKAGE_INFOSTRESS_DATA_2D, tID, m_pScannerName, Az, Ra, Width );
  524. };
  525. } else {
  526. free( RetVal );
  527. RetVal = NULL;
  528. };
  529. };
  530. return RetVal;
  531. };
  532. static uint32_t INFOSTRESS_MESSAGE_ID = 0;
  533. size_t protocolKAFKA::assembleDefenderJSON( CSTR Buffer, defenderCategoryTypes Type, uint8_t Severity, CSTR Location, CSTR Data ){
  534. char tBuffer[CLOG];
  535. size_t RetVal;
  536. CSTR tBuf;
  537. RetVal = 0;
  538. tBuf = getSHDTimeStamp();
  539. INFOSTRESS_MESSAGE_ID++;
  540. //memset( tBuffer, 0, CLOG );
  541. if( tBuf != NULL ){
  542. //char ID[64];
  543. switch ( Type ) {
  544. case DC_STATUS:
  545. Severity = 0;
  546. break;
  547. case DC_ERROR:
  548. Severity = 2;
  549. break;
  550. case DC_ALARM:
  551. Severity = 4;
  552. break;
  553. default:
  554. Severity = 2;
  555. };
  556. //sprintf( ID, "%s", MD5String( tBuf ) );
  557. if( (m_nScannerDimentions > 2) && ( Type == DC_ALARM ) ) Type = DC_ALARM_3D;
  558. sprintf( tBuffer,
  559. DFSL_SE_ENVELOPE,
  560. //DEFENDER_MESSAGE_ID,
  561. defenderCategories[Type],
  562. defenderTypeSeverity[Severity],
  563. tBuf, tBuf,
  564. Location,
  565. Data
  566. );
  567. RetVal = strlen(tBuffer);
  568. memcpy( Buffer, tBuffer, RetVal );
  569. Buffer[RetVal] = (char) 0;
  570. free( tBuf );
  571. };
  572. return RetVal;
  573. };
  574. bool protocolKAFKA::buildStatusPackage( unsigned char *Buffer, int &BufferSize, bool Status ){
  575. if( Buffer!= NULL ){
  576. CSTR tBuf;
  577. tBuf = assembleStatusData( Status );
  578. #ifdef DEBUG
  579. //LOG(tBuf);
  580. #endif
  581. if( tBuf != NULL ) {
  582. char DFSL_LOCATION[256];
  583. sprintf(DFSL_LOCATION, DFSL_LOCATION_DTO, m_fLon, m_fLat );
  584. BufferSize = assembleDefenderJSON( (CSTR)Buffer, DC_STATUS, ((Status)?0:2), DFSL_LOCATION, tBuf );
  585. #ifdef DEBUG
  586. LOG((CSTR)Buffer);
  587. #endif
  588. free( tBuf );
  589. return true;
  590. }
  591. };
  592. return false;
  593. };
  594. bool protocolKAFKA::buildFaultPackage( unsigned char *Buffer, int &BufferSize, unsigned char Type, CSTR Description ){
  595. if( Buffer!= NULL ){
  596. CSTR tBuf;
  597. tBuf = assembleFaultData( Type, Description );
  598. if( tBuf != NULL ){
  599. char DFSL_LOCATION[256];
  600. sprintf(DFSL_LOCATION, DFSL_LOCATION_DTO, m_fLon, m_fLat );
  601. BufferSize = assembleDefenderJSON( (CSTR)Buffer, DC_ERROR, 3, DFSL_LOCATION, tBuf );
  602. free( tBuf );
  603. return true;
  604. };
  605. };
  606. return false;
  607. };
  608. bool protocolKAFKA::buildEvent2DPackage( unsigned char *Buffer, int &BufferSize, CSTR msgUID, int targetID, float Angle, float Range, float Width){
  609. if( Buffer!= NULL ){
  610. CSTR tBuf;
  611. tBuf = assembleTargetData( false, targetID, Range, Width, Angle, 0 );
  612. if( tBuf != NULL ){
  613. BufferSize = strlen( tBuf );
  614. strncpy( (char *)Buffer, tBuf, BufferSize );
  615. Buffer[BufferSize] = (char) 0;
  616. #ifdef DEBUG
  617. LOG((CSTR)Buffer);
  618. #endif
  619. free( tBuf );
  620. return true;
  621. };
  622. };
  623. return false;
  624. };
  625. bool protocolKAFKA::buildEvent3DPackage( unsigned char *Buffer, int &BufferSize, CSTR msgUID, int targetID, float Angle1, float Angle2, float Range ){
  626. if( Buffer!= NULL ){
  627. CSTR tBuf;
  628. tBuf = assembleTargetData( true, targetID, Range, 0, Angle1, Angle2 );
  629. if( tBuf != NULL ){
  630. BufferSize = strlen( tBuf );
  631. strncpy( (char *)Buffer, tBuf, BufferSize );
  632. Buffer[BufferSize] = (char) 0;
  633. #ifdef DEBUG
  634. LOG((CSTR)Buffer);
  635. #endif
  636. free( tBuf );
  637. return true;
  638. };
  639. };
  640. return false;
  641. };
  642. bool protocolKAFKA::buildClockSyncPackage( unsigned char *Buffer, int &BufferSize ){
  643. /*if( Buffer!= NULL ){
  644. CSTR tBuf;
  645. tBuf = getSHTimeStamp();
  646. if( tBuf != NULL ){
  647. sprintf(
  648. ((char *)(Buffer+BufferSize)),
  649. PACKAGE_ENVELOPE_TEMPLATE_CLOCKSYNC,
  650. tBuf
  651. );
  652. BufferSize = strlen( (char *)Buffer );
  653. free( tBuf );
  654. return true;
  655. };
  656. };*/
  657. return false;
  658. };
  659. bool protocolKAFKA::buildCallBackPackage( unsigned char *Buffer, int &BufferSize, CSTR CallBackPath ){
  660. /*if( Buffer!= NULL ){
  661. sprintf(
  662. ((char *)(Buffer+BufferSize)),
  663. PACKAGE_ENVELOPE_TEMPLATE_REG_CALL,
  664. CallBackPath
  665. );
  666. BufferSize = strlen( (char *)Buffer );
  667. return true;
  668. };*/
  669. return false;
  670. };
  671. bool protocolKAFKA::Alarm2D( float Angle, float Range, float Width, int ID ){
  672. bool RetVal= false;
  673. char Buffer[1024];
  674. int BufferSize = 0;
  675. CSTR UID;
  676. if(!m_bEnabled) return false;
  677. UID = buildUniqID();
  678. if( UID!= NULL ){
  679. RetVal = buildEvent2DPackage( ( unsigned char *) Buffer, BufferSize, UID, ID, Angle, Range, Width );
  680. RetVal = issuePost( Buffer, BufferSize );
  681. free( UID );
  682. };
  683. return RetVal;
  684. };
  685. bool protocolKAFKA::Alarm3D( float Angle1, float Angle2, float Range, int ID ){
  686. bool RetVal= false;
  687. char Buffer[1024];
  688. int BufferSize = 0;
  689. CSTR UID;
  690. if(!m_bEnabled) return false;
  691. UID = buildUniqID();
  692. if( UID!= NULL ){
  693. RetVal = buildEvent3DPackage( ( unsigned char *) Buffer, BufferSize, UID, ID, Angle1, Angle2, Range );
  694. RetVal = issuePost( Buffer, BufferSize );
  695. free( UID );
  696. };
  697. return RetVal;
  698. };
  699. bool protocolKAFKA::VisibilityChanged( bool Worsened ){
  700. if( !m_bEnableStatus ) return true;
  701. bool RetVal= false;
  702. char Buffer[1024];
  703. int BufferSize = 0;
  704. if(!m_bEnabled) return false;
  705. RetVal = buildStatusPackage( ( unsigned char *) Buffer, BufferSize, !Worsened );
  706. RetVal = issuePost( Buffer, BufferSize );
  707. return RetVal;
  708. };
  709. bool protocolKAFKA::StatusUpdate( void ){
  710. if( !m_bEnableStatus ) return true;
  711. bool RetVal= false;
  712. char Buffer[1024];
  713. int BufferSize = 0;
  714. if(!m_bEnabled) return false;
  715. RetVal = buildStatusPackage( ( unsigned char *) Buffer, BufferSize, m_bCurrentStatus );
  716. RetVal = issuePost( Buffer, BufferSize );
  717. return RetVal;
  718. };
  719. bool protocolKAFKA::ErrorReporting( int lvl, CSTR Descr ){
  720. if( !m_bEnableStatus ) return true;
  721. bool RetVal= false;
  722. char Buffer[1024];
  723. int BufferSize = 0;
  724. if(!m_bEnabled) return false;
  725. RetVal = buildFaultPackage( ( unsigned char *) Buffer, BufferSize, lvl, Descr );
  726. RetVal = issuePost( Buffer, BufferSize );
  727. return RetVal;
  728. };
  729. bool protocolKAFKA::StartTimeSynch( void ){
  730. if( !m_bEnableStatus ) return true;
  731. bool RetVal= false;
  732. //LOG("TimeSYnc");
  733. /*char Buffer[1024];
  734. int BufferSize = 0;
  735. CSTR URL = NULL;
  736. URL = buildURL( SERVICE_URL_TEMPLATE_CLOCKSYNC, m_nScannerDimentions, m_pScannerName );
  737. RetVal = buildClockSyncPackage(( unsigned char *) Buffer, BufferSize );
  738. if( URL != NULL ){
  739. RetVal = issuePost( URL, Buffer, BufferSize );
  740. free( URL );
  741. };*/
  742. return RetVal;
  743. };
  744. #undef LOG
  745. #undef LOG_ERROR