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.

812 lines
26 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( tBuffer.c_str() );
  331. delete( tTopicConf );
  332. return false;
  333. };
  334. #ifdef DEBUG
  335. LOG("RdKafka::Topic Created");
  336. #endif
  337. /*
  338. if( tProducerConf->set( "default_topic_conf", tTopicConf, tBuffer ) != RdKafka::Conf::CONF_OK ){
  339. LOG_ERROR( tBuffer.c_str() );
  340. delete( tTopicConf );
  341. tProducerConf = NULL;
  342. tTopicConf = NULL;
  343. return false;
  344. };
  345. #ifdef DEBUG
  346. LOG("default_topic_conf is SET");
  347. #endif
  348. //*/
  349. retry:
  350. err = m_pKafka->produce(
  351. tKafkaTopic,
  352. RdKafka::Topic::PARTITION_UA,
  353. RdKafka::Producer::RK_MSG_COPY,
  354. (void *)Buffer,
  355. BufferSize,
  356. NULL,
  357. NULL
  358. );
  359. if( err != RdKafka::ERR_NO_ERROR ) {
  360. ///std::cerr << "% Failed to produce to topic " << topic << ": " << RdKafka::err2str(err) << std::endl;
  361. sprintf( tBuf, "Failed to produce to topic[%s]: %s", topic.c_str(), RdKafka::err2str(err).c_str() );
  362. LOG_ERROR( tBuf );
  363. if( err == RdKafka::ERR__QUEUE_FULL ){
  364. LOG_ERROR("Internal queue is full!");
  365. /* If the internal queue is full, wait for
  366. * messages to be delivered and then retry.
  367. * The internal queue represents both
  368. * messages to be sent and messages that have
  369. * been sent or failed, awaiting their
  370. * delivery report callback to be called.
  371. *
  372. * The internal queue is limited by the
  373. * configuration property
  374. * queue.buffering.max.messages */
  375. m_pKafka->poll(1000/*block for max 1000ms*/);
  376. if( rCnt-- ) goto retry;
  377. };
  378. }
  379. #ifdef DEBUG
  380. else {
  381. sprintf( tBuf, "Enqueued message (%d bytes) for topic %s", BufferSize, topic.c_str() );
  382. LOG( tBuf );
  383. }
  384. #endif
  385. ;
  386. /* A producer application should continually serve
  387. * the delivery report queue by calling poll()
  388. * at frequent intervals.
  389. * Either put the poll call in your main loop, or in a
  390. * dedicated thread, or call it after every produce() call.
  391. * Just make sure that poll() is still called
  392. * during periods where you are not producing any messages
  393. * to make sure previously produced messages have their
  394. * delivery report callback served (and any other callbacks
  395. * you register). */
  396. m_pKafka->poll(100);
  397. //uint8_t cnt = 100;
  398. //while ( cnt && m_pKafka->outq_len() > 0) {
  399. // LOG("Wait ...");
  400. // cnt--;
  401. // m_pKafka->poll(100);
  402. //};
  403. delete( tKafkaTopic );
  404. delete( tTopicConf );
  405. tKafkaTopic = NULL;
  406. tTopicConf = NULL;
  407. return true;
  408. };
  409. /*
  410. void protocolKAFKA::setScannerName( CSTR Name ){
  411. sprintf( m_pScannerName, "%s", Name );
  412. };
  413. void protocolKAFKA::setProtocolType( bool DeviceIs3D ){
  414. if( DeviceIs3D ) {
  415. m_nScannerDimentions = 3;
  416. } else {
  417. m_nScannerDimentions = 2;
  418. };
  419. };
  420. CSTR protocolKAFKA::assembleStatusData( bool StatusOK ){
  421. CSTR RetVal = NULL;
  422. CSTR TIME_STAMP;
  423. CSTR tSRC;
  424. CSTR tTARGET;
  425. CSTR tID;
  426. RetVal = (CSTR) malloc(CLOG);
  427. if ( RetVal != NULL ) {
  428. TIME_STAMP = getSHDTimeStamp();
  429. if( TIME_STAMP != NULL ) {
  430. tTARGET = (CSTR) malloc(128);
  431. if( tTARGET!= NULL) {
  432. tSRC = (CSTR) malloc(128);
  433. if( tSRC ) {
  434. tID = (CSTR) malloc(64);
  435. if( tID != NULL ) {
  436. sprintf( tID, "%s", MD5String( TIME_STAMP ) );
  437. sprintf( tSRC, DFSL_SENSOR_SOURCE_DTO, DFSL_HARDWARE_TYPE, DFRL_PROVIDER_ID, m_pScannerName );
  438. sprintf( tTARGET, DFSL_STATUS_DTO, TIME_STAMP , defenderStatusMode[((StatusOK)?1:0)] );
  439. sprintf( RetVal,
  440. PACKAGE_SE_STATUS_DATA_ELEMENT,
  441. defenderTypeDescription[DC_STATUS],
  442. TIME_STAMP,
  443. tSRC,
  444. "DFSL_LIDAR_SENSOR_STATUS",
  445. DFSL_DATA_TYPE_STATUS,
  446. tID,
  447. tTARGET
  448. );
  449. free( tID );
  450. free( tSRC );
  451. free( tTARGET );
  452. free( TIME_STAMP );
  453. } else {
  454. free( tSRC );
  455. free( tTARGET );
  456. free( TIME_STAMP );
  457. free( RetVal );
  458. }
  459. } else {
  460. free( tTARGET );
  461. free( TIME_STAMP );
  462. free( RetVal );
  463. };
  464. } else {
  465. free( TIME_STAMP );
  466. free( RetVal );
  467. };
  468. } else {
  469. free( RetVal );
  470. RetVal = NULL;
  471. };
  472. };
  473. return RetVal;
  474. };
  475. CSTR protocolKAFKA::assembleFaultData( uint8_t Code, CSTR Desc ){
  476. CSTR RetVal;
  477. CSTR TIME_STAMP;
  478. CSTR tSRC;
  479. CSTR tTARGET;
  480. CSTR tID;
  481. RetVal = (CSTR) malloc(CLOG);
  482. if ( RetVal != NULL ) {
  483. TIME_STAMP = getSHDTimeStamp();
  484. if( TIME_STAMP != NULL ) {
  485. tTARGET = (CSTR) malloc(128);
  486. if( tTARGET!= NULL) {
  487. tSRC = (CSTR) malloc(128);
  488. if( tSRC ) {
  489. tID = (CSTR) malloc(64);
  490. if( tID != NULL ) {
  491. sprintf( tID, "%s", MD5String( TIME_STAMP ) );
  492. sprintf( tSRC, DFSL_SENSOR_SOURCE_DTO, DFSL_HARDWARE_TYPE, DFRL_PROVIDER_ID, m_pScannerName );
  493. sprintf( tTARGET, DFSL_ERROR_DTO, TIME_STAMP, Code, Desc );
  494. sprintf( RetVal,
  495. PACKAGE_SE_STATUS_DATA_ELEMENT,
  496. Desc,
  497. TIME_STAMP,
  498. tSRC,
  499. "DFSL_LIDARE_SENSOR_FAULT_DATA",
  500. DFSL_DATA_TYPE_ERROR,
  501. tID,
  502. tTARGET
  503. );
  504. free( tID );
  505. free( tSRC );
  506. free( tTARGET );
  507. free( TIME_STAMP );
  508. } else {
  509. free( tSRC );
  510. free( tTARGET );
  511. free( TIME_STAMP );
  512. free( RetVal );
  513. }
  514. } else {
  515. free( tTARGET );
  516. free( TIME_STAMP );
  517. free( RetVal );
  518. };
  519. } else {
  520. free( TIME_STAMP );
  521. free( RetVal );
  522. };
  523. } else {
  524. free( RetVal );
  525. RetVal = NULL;
  526. };
  527. };
  528. return RetVal;
  529. };
  530. CSTR protocolKAFKA::assembleTargetData( bool is3D, int targetID, int Range, int Width, int Azimuth, int Elevation ){
  531. CSTR RetVal = NULL;
  532. RetVal = (CSTR) malloc(CLOG);
  533. if ( RetVal != NULL ) {
  534. CSTR TIME_STAMP = NULL;
  535. TIME_STAMP = getTStr();
  536. if( TIME_STAMP!= NULL ) {
  537. char tID[64];
  538. float Az;
  539. float Ra;
  540. sprintf( tID, "%s", MD5String( TIME_STAMP ) );
  541. free( TIME_STAMP );
  542. Ra = Range;
  543. Az = Azimuth;
  544. if( is3D ) {
  545. float El;
  546. El = Elevation;
  547. sprintf( RetVal, PACKAGE_INFOSTRESS_DATA_3D, tID, m_pScannerName, Az, El, Ra );
  548. } else {
  549. sprintf( RetVal, PACKAGE_INFOSTRESS_DATA_2D, tID, m_pScannerName, Az, Ra, Width );
  550. };
  551. } else {
  552. free( RetVal );
  553. RetVal = NULL;
  554. };
  555. };
  556. return RetVal;
  557. };
  558. static uint32_t INFOSTRESS_MESSAGE_ID = 0;
  559. size_t protocolKAFKA::assembleDefenderJSON( CSTR Buffer, defenderCategoryTypes Type, uint8_t Severity, CSTR Location, CSTR Data ){
  560. char tBuffer[CLOG];
  561. size_t RetVal;
  562. CSTR tBuf;
  563. RetVal = 0;
  564. tBuf = getSHDTimeStamp();
  565. INFOSTRESS_MESSAGE_ID++;
  566. //memset( tBuffer, 0, CLOG );
  567. if( tBuf != NULL ){
  568. //char ID[64];
  569. switch ( Type ) {
  570. case DC_STATUS:
  571. Severity = 0;
  572. break;
  573. case DC_ERROR:
  574. Severity = 2;
  575. break;
  576. case DC_ALARM:
  577. Severity = 4;
  578. break;
  579. default:
  580. Severity = 2;
  581. };
  582. //sprintf( ID, "%s", MD5String( tBuf ) );
  583. if( (m_nScannerDimentions > 2) && ( Type == DC_ALARM ) ) Type = DC_ALARM_3D;
  584. sprintf( tBuffer,
  585. DFSL_SE_ENVELOPE,
  586. //DEFENDER_MESSAGE_ID,
  587. defenderCategories[Type],
  588. defenderTypeSeverity[Severity],
  589. tBuf, tBuf,
  590. Location,
  591. Data
  592. );
  593. RetVal = strlen(tBuffer);
  594. memcpy( Buffer, tBuffer, RetVal );
  595. Buffer[RetVal] = (char) 0;
  596. free( tBuf );
  597. };
  598. return RetVal;
  599. };
  600. bool protocolKAFKA::buildStatusPackage( unsigned char *Buffer, int &BufferSize, bool Status ){
  601. if( Buffer!= NULL ){
  602. CSTR tBuf;
  603. tBuf = assembleStatusData( Status );
  604. #ifdef DEBUG
  605. //LOG(tBuf);
  606. #endif
  607. if( tBuf != NULL ) {
  608. char DFSL_LOCATION[256];
  609. sprintf(DFSL_LOCATION, DFSL_LOCATION_DTO, m_fLon, m_fLat );
  610. BufferSize = assembleDefenderJSON( (CSTR)Buffer, DC_STATUS, ((Status)?0:2), DFSL_LOCATION, tBuf );
  611. #ifdef DEBUG
  612. LOG((CSTR)Buffer);
  613. #endif
  614. free( tBuf );
  615. return true;
  616. }
  617. };
  618. return false;
  619. };
  620. bool protocolKAFKA::buildFaultPackage( unsigned char *Buffer, int &BufferSize, unsigned char Type, CSTR Description ){
  621. if( Buffer!= NULL ){
  622. CSTR tBuf;
  623. tBuf = assembleFaultData( Type, Description );
  624. if( tBuf != NULL ){
  625. char DFSL_LOCATION[256];
  626. sprintf(DFSL_LOCATION, DFSL_LOCATION_DTO, m_fLon, m_fLat );
  627. BufferSize = assembleDefenderJSON( (CSTR)Buffer, DC_ERROR, 3, DFSL_LOCATION, tBuf );
  628. free( tBuf );
  629. return true;
  630. };
  631. };
  632. return false;
  633. };
  634. bool protocolKAFKA::buildEvent2DPackage( unsigned char *Buffer, int &BufferSize, CSTR msgUID, int targetID, float Angle, float Range, float Width){
  635. if( Buffer!= NULL ){
  636. CSTR tBuf;
  637. tBuf = assembleTargetData( false, targetID, Range, Width, Angle, 0 );
  638. if( tBuf != NULL ){
  639. BufferSize = strlen( tBuf );
  640. strncpy( (char *)Buffer, tBuf, BufferSize );
  641. Buffer[BufferSize] = (char) 0;
  642. #ifdef DEBUG
  643. LOG((CSTR)Buffer);
  644. #endif
  645. free( tBuf );
  646. return true;
  647. };
  648. };
  649. return false;
  650. };
  651. bool protocolKAFKA::buildEvent3DPackage( unsigned char *Buffer, int &BufferSize, CSTR msgUID, int targetID, float Angle1, float Angle2, float Range ){
  652. if( Buffer!= NULL ){
  653. CSTR tBuf;
  654. tBuf = assembleTargetData( true, targetID, Range, 0, Angle1, Angle2 );
  655. if( tBuf != NULL ){
  656. BufferSize = strlen( tBuf );
  657. strncpy( (char *)Buffer, tBuf, BufferSize );
  658. Buffer[BufferSize] = (char) 0;
  659. #ifdef DEBUG
  660. LOG((CSTR)Buffer);
  661. #endif
  662. free( tBuf );
  663. return true;
  664. };
  665. };
  666. return false;
  667. };
  668. bool protocolKAFKA::buildClockSyncPackage( unsigned char *Buffer, int &BufferSize ){
  669. if( Buffer!= NULL ){
  670. CSTR tBuf;
  671. tBuf = getSHTimeStamp();
  672. if( tBuf != NULL ){
  673. sprintf(
  674. ((char *)(Buffer+BufferSize)),
  675. PACKAGE_ENVELOPE_TEMPLATE_CLOCKSYNC,
  676. tBuf
  677. );
  678. BufferSize = strlen( (char *)Buffer );
  679. free( tBuf );
  680. return true;
  681. };
  682. };
  683. return false;
  684. };
  685. bool protocolKAFKA::buildCallBackPackage( unsigned char *Buffer, int &BufferSize, CSTR CallBackPath ){
  686. if( Buffer!= NULL ){
  687. sprintf(
  688. ((char *)(Buffer+BufferSize)),
  689. PACKAGE_ENVELOPE_TEMPLATE_REG_CALL,
  690. CallBackPath
  691. );
  692. BufferSize = strlen( (char *)Buffer );
  693. return true;
  694. };
  695. return false;
  696. };
  697. bool protocolKAFKA::Alarm2D( float Angle, float Range, float Width, int ID ){
  698. bool RetVal= false;
  699. char Buffer[1024];
  700. int BufferSize = 0;
  701. CSTR UID;
  702. if(!m_bEnabled) return false;
  703. UID = buildUniqID();
  704. if( UID!= NULL ){
  705. RetVal = buildEvent2DPackage( ( unsigned char *) Buffer, BufferSize, UID, ID, Angle, Range, Width );
  706. RetVal = issuePost( Buffer, BufferSize );
  707. free( UID );
  708. };
  709. return RetVal;
  710. };
  711. bool protocolKAFKA::Alarm3D( float Angle1, float Angle2, float Range, int ID ){
  712. bool RetVal= false;
  713. char Buffer[1024];
  714. int BufferSize = 0;
  715. CSTR UID;
  716. if(!m_bEnabled) return false;
  717. UID = buildUniqID();
  718. if( UID!= NULL ){
  719. RetVal = buildEvent3DPackage( ( unsigned char *) Buffer, BufferSize, UID, ID, Angle1, Angle2, Range );
  720. RetVal = issuePost( Buffer, BufferSize );
  721. free( UID );
  722. };
  723. return RetVal;
  724. };
  725. bool protocolKAFKA::VisibilityChanged( bool Worsened ){
  726. if( !m_bEnableStatus ) return true;
  727. bool RetVal= false;
  728. char Buffer[1024];
  729. int BufferSize = 0;
  730. if(!m_bEnabled) return false;
  731. RetVal = buildStatusPackage( ( unsigned char *) Buffer, BufferSize, !Worsened );
  732. RetVal = issuePost( Buffer, BufferSize );
  733. return RetVal;
  734. };
  735. bool protocolKAFKA::StatusUpdate( void ){
  736. if( !m_bEnableStatus ) return true;
  737. bool RetVal= false;
  738. char Buffer[1024];
  739. int BufferSize = 0;
  740. if(!m_bEnabled) return false;
  741. RetVal = buildStatusPackage( ( unsigned char *) Buffer, BufferSize, m_bCurrentStatus );
  742. RetVal = issuePost( Buffer, BufferSize );
  743. return RetVal;
  744. };
  745. bool protocolKAFKA::ErrorReporting( int lvl, CSTR Descr ){
  746. if( !m_bEnableStatus ) return true;
  747. bool RetVal= false;
  748. char Buffer[1024];
  749. int BufferSize = 0;
  750. if(!m_bEnabled) return false;
  751. RetVal = buildFaultPackage( ( unsigned char *) Buffer, BufferSize, lvl, Descr );
  752. RetVal = issuePost( Buffer, BufferSize );
  753. return RetVal;
  754. };
  755. bool protocolKAFKA::StartTimeSynch( void ){
  756. if( !m_bEnableStatus ) return true;
  757. bool RetVal= false;
  758. //LOG("TimeSYnc");
  759. char Buffer[1024];
  760. int BufferSize = 0;
  761. CSTR URL = NULL;
  762. URL = buildURL( SERVICE_URL_TEMPLATE_CLOCKSYNC, m_nScannerDimentions, m_pScannerName );
  763. RetVal = buildClockSyncPackage(( unsigned char *) Buffer, BufferSize );
  764. if( URL != NULL ){
  765. RetVal = issuePost( URL, Buffer, BufferSize );
  766. free( URL );
  767. };
  768. return RetVal;
  769. };
  770. */
  771. #undef LOG
  772. #undef LOG_ERROR