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.

188 lines
6.5 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "protocolZLMEDIA.h"
  2. #include "../json/json_tool.h"
  3. #include <json/value.h>
  4. #include <json/writer.h>
  5. #include <json/reader.h>
  6. #define LOG( X ) basicLogCollection::getInstance()->Log( "ZLMP", this, X )
  7. #define LOG_ERROR( X ) basicLogCollection::getInstance()->Log( "ZLMP", this, _LOG_ERROR, X )
  8. protocolZLMEDIA::protocolZLMEDIA( config_setting_t *Settings ): protocolHTTP( basicSettingsProvider::getInstance()->getSubNode(Settings, "ZLMEDIA_PROTOCOL") ){
  9. m_pLog = basicLogCollection::getInstance();
  10. m_pSettings = basicSettingsProvider::getInstance()->getSubNode(Settings, "ZLMEDIA_PROTOCOL");
  11. /*
  12. if( m_pSettings != NULL ){
  13. config_setting_t *pSetting;
  14. if (!config_setting_lookup_string(m_pSettings, "SCANNER_NAME", (const char**)&m_pScannerName)){
  15. strncpy( m_pScannerName, "LRF_2D_001", 64 );
  16. LOG( m_pScannerName );
  17. pSetting = config_setting_add(m_pSettings, "SCANNER_NAME", CONFIG_TYPE_STRING);
  18. config_setting_set_string(pSetting, m_pScannerName);
  19. basicSettingsProvider::getInstance()->compromise();
  20. };
  21. if (!config_setting_lookup_int(m_pSettings, "SCANNER_DIMENTIONS", &m_nScannerDimentions )){
  22. m_nScannerDimentions = 2;
  23. pSetting = config_setting_add(m_pSettings, "SCANNER_DIMENTIONS", CONFIG_TYPE_INT);
  24. config_setting_set_int(pSetting, m_nScannerDimentions);
  25. basicSettingsProvider::getInstance()->compromise();
  26. };
  27. if (!config_setting_lookup_string(m_pSettings, "LCU_IP_ADDRESS", (const char**)&m_pServerIP)){
  28. strncpy( m_pServerIP, "192.168.200.204", 16 );
  29. LOG( m_pServerIP );
  30. pSetting = config_setting_add(m_pSettings, "LCU_IP_ADDRESS", CONFIG_TYPE_STRING);
  31. config_setting_set_string(pSetting, m_pServerIP);
  32. basicSettingsProvider::getInstance()->compromise();
  33. };
  34. if (!config_setting_lookup_int(m_pSettings, "LCU_IP_PORT", &m_nServerPort)){
  35. m_nServerPort = 80;
  36. pSetting = config_setting_add(m_pSettings, "LCU_IP_PORT", CONFIG_TYPE_INT);
  37. config_setting_set_int(pSetting, m_nServerPort);
  38. basicSettingsProvider::getInstance()->compromise();
  39. };
  40. basicSettingsProvider::getInstance()->validate();
  41. }; //*/
  42. };
  43. protocolZLMEDIA::protocolZLMEDIA( CSTR key, CSTR host, CSTR ip, uint32_t port ): protocolHTTP( NULL ){
  44. m_pLog = basicLogCollection::getInstance();
  45. m_pSettings = NULL;
  46. LOG("Param1");
  47. strncpy( m_pSecret, key, 255 );
  48. LOG("Param2");
  49. strncpy( m_pAccessPoint, key, 255 );
  50. LOG("Param3");
  51. strncpy( m_pIP, ip, 16 );
  52. LOG("Done!");
  53. m_bSessionOnGo = false;
  54. m_bServerIsPresent = false;
  55. m_nPort = port;
  56. m_bUseSSL = ( m_nPort == 433 );
  57. LOG("Check Online!");
  58. isOnline();
  59. LOG("Done!");
  60. };
  61. protocolZLMEDIA::~protocolZLMEDIA(){
  62. };
  63. bool protocolZLMEDIA::packetHandler( protocolPackage *Package ){
  64. LOG("Pack Handler!!");
  65. if( Package == NULL ){
  66. m_bSessionOnGo = false;
  67. return false;
  68. };
  69. LOG("Pack not NULL");
  70. if( Package->m_nSize == 0 ) {
  71. m_bSessionOnGo = false;
  72. return false;
  73. };
  74. LOG("Size not NULL");
  75. if( Package->m_pData == NULL ){
  76. m_bSessionOnGo = false;
  77. return false;
  78. };
  79. LOG("Data not NULL");
  80. if( Package->m_nCode != 200 ){
  81. m_bSessionOnGo = false;
  82. return false;
  83. };
  84. LOG("Code is 200");
  85. m_bServerIsPresent = true;
  86. try{
  87. Json::Value root;
  88. Json::Reader reader;
  89. if( !reader.parse( (char *)Package->m_pData, root ) ){
  90. LOG_ERROR( "Error parsing the string" );
  91. m_bSessionOnGo = false;
  92. return false;
  93. };
  94. for( Json::Value::const_iterator outer = root.begin() ; outer != root.end() ; outer++ ){
  95. for( Json::Value::const_iterator inner = (*outer).begin() ; inner!= (*outer).end() ; inner++ ){
  96. char tBuffer[256];
  97. sprintf( tBuffer,"Data: %s: %s ", inner.key().asString().c_str(), (*inner).asString().c_str() );
  98. LOG( tBuffer );
  99. };
  100. };
  101. } catch (exception e ){
  102. LOG_ERROR("Exception!!");
  103. m_bSessionOnGo = false;
  104. return false;
  105. };
  106. m_bSessionOnGo = false;
  107. return true;
  108. };
  109. // Issue New Online Check but return current value
  110. bool protocolZLMEDIA::isOnline(){
  111. if( !m_bServerIsPresent ) { // Just First Time to set initial flag
  112. if( !m_bSessionOnGo ) {
  113. char URL[256];
  114. uint8_t timeout = 100; // 1 sec.
  115. m_bSessionOnGo = true;
  116. if( m_bUseSSL ) {
  117. sprintf(URL,"https://%s:%d/%s?secret=%s", m_pIP, m_nPort, ZL_LIST_API, m_pSecret );
  118. } else {
  119. sprintf(URL,"http://%s:%d/%s?secret=%s", m_pIP, m_nPort, ZL_LIST_API, m_pSecret ); //m_pAccessPoint
  120. };
  121. LOG( URL );
  122. //ToDo: Check if OK to send nothing for checking Onlines
  123. //int len = 0;
  124. //issuePost( URL, NULL, len );
  125. issueGet( URL );
  126. while( m_bSessionOnGo && timeout-- ) SLEEP(10);
  127. };
  128. #ifdef DEBUG
  129. if(m_bServerIsPresent) {
  130. LOG("Server is Online");
  131. } else {
  132. LOG("Server is Onffline");
  133. };
  134. #endif
  135. }
  136. return m_bServerIsPresent;
  137. };
  138. bool protocolZLMEDIA::restartServer(){
  139. return false;
  140. };
  141. bool protocolZLMEDIA::getConfig(){
  142. return false;
  143. };
  144. CSTR protocolZLMEDIA::addStreamProxy(){
  145. return NULL;
  146. };
  147. bool protocolZLMEDIA::delStreamProxy(CSTR key){
  148. return false;
  149. };
  150. bool protocolZLMEDIA::listMedia(){
  151. if( m_bServerIsPresent ) { // Just First Time to set initial flag
  152. LOG("LIST MEDIA");
  153. if( !m_bSessionOnGo ) {
  154. char URL[256];
  155. uint8_t timeout = 100; // 1 sec.
  156. m_bSessionOnGo = true;
  157. if( m_bUseSSL ) {
  158. sprintf(URL,"https://%s:%d/%s?secret=%s", m_pIP, m_nPort, ZL_LIST_MEDIA, m_pSecret );
  159. } else {
  160. sprintf(URL,"http://%s:%d/%s?secret=%s", m_pIP, m_nPort, ZL_LIST_MEDIA, m_pSecret ); //m_pAccessPoint
  161. };
  162. LOG( URL );
  163. //ToDo: Check if OK to send nothing for checking Onlines
  164. //int len = 0;
  165. //issuePost( URL, NULL, len );
  166. issueGet( URL );
  167. while( m_bSessionOnGo && timeout-- ) SLEEP(10);
  168. return ( timeout > 0 );
  169. } else LOG("LIST MEDIA - SESSION BUSY!");
  170. };
  171. return false;
  172. };
  173. bool protocolZLMEDIA::requestIsFinished(){
  174. return (m_bSessionOnGo == false);
  175. };
  176. #undef LOG
  177. #undef LOG_ERROR