ToonBridge.cc 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include "Toon/Bridge.h"
  2. #include <INIReader.h>
  3. #include <Logging.h>
  4. #include <signal.h>
  5. #include <iostream>
  6. #include <sstream>
  7. #include <string>
  8. int main(int argc, char** argv)
  9. {
  10. try
  11. {
  12. Logging::OpenLog();
  13. Logging::SetLogMask(Logging::Severity::Info);
  14. INIReader iniReader("ToonBridge.ini");
  15. if (iniReader.ParseError() != 0)
  16. throw std::runtime_error("Can't read ToonBridge.ini.");
  17. Logging::Log(Logging::Severity::Info, "Starting ToonBridge");
  18. int port = iniReader.GetInteger("ToonBridge", "Port", 0);
  19. if (port == 0)
  20. throw std::runtime_error("Port directive missing in ini file.");
  21. ToonBridge::Toon::ToonSettings toonSettings;
  22. toonSettings.agreementId = iniReader.Get("Toon", "AgreementID", "");
  23. if (toonSettings.agreementId.empty())
  24. throw std::runtime_error("Toon AgreementID directive missing in ini file.");
  25. toonSettings.accessToken = iniReader.Get("Toon", "AccessToken", "");
  26. if (toonSettings.accessToken.empty())
  27. throw std::runtime_error("Toon AccessToken directive missing in ini file.");
  28. toonSettings.displayCommonName = iniReader.Get("Toon", "DisplayCommonName", "");
  29. if (toonSettings.displayCommonName.empty())
  30. throw std::runtime_error("Toon DisplayCommonName directive missing in ini file.");
  31. toonSettings.applicationId = iniReader.Get("Toon", "ApplicationID", "");
  32. if (toonSettings.applicationId.empty())
  33. throw std::runtime_error("Toon ApplicationID directive missing in ini file.");
  34. toonSettings.callbackUrl = iniReader.Get("Toon", "CallbackURL", "");
  35. if (toonSettings.callbackUrl.empty())
  36. throw std::runtime_error("Toon CallbackURL directive missing in ini file.");
  37. ToonBridge::Toon::MQTTSettings mqttSettings;
  38. mqttSettings.hostname = iniReader.Get("MQTT", "Hostname", "");
  39. if (mqttSettings.hostname.empty())
  40. throw std::runtime_error("MQTT Hostname directive missing in ini file.");
  41. mqttSettings.port = iniReader.GetInteger("MQTT", "Port", 0);
  42. if (mqttSettings.port == 0)
  43. throw std::runtime_error("MQTT Port directive missing in ini file.");
  44. mqttSettings.topic = iniReader.Get("MQTT", "Topic", "");
  45. if (mqttSettings.topic.empty())
  46. throw std::runtime_error("MQTT Topic directive missing in ini file.");
  47. ToonBridge::Toon::Bridge bridge(port, toonSettings, mqttSettings);
  48. Logging::Log(Logging::Severity::Info, "Startup Complete");
  49. bridge.Wait();
  50. Logging::Log(Logging::Severity::Info, "Stopping ToonBridge...");
  51. Logging::CloseLog();
  52. }
  53. catch (const std::exception& e)
  54. {
  55. std::cerr << "Exception caught" << std::endl;
  56. std::stringstream ss;
  57. ss << "Type : " << typeid(e).name() << std::endl;
  58. ss << "ERROR: " << e.what() << std::endl;
  59. Logging::Log(Logging::Severity::Error, ss.str());
  60. Logging::CloseLog();
  61. return -1;
  62. }
  63. return 0;
  64. }