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.

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