ToonBridge.cc 2.7 KB

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