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.

813 lines
27 KiB

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