bulletin_broad_show.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // Created by songchao.chen on 2019/6/26.
  3. //
  4. #include <config_file.h>
  5. #include <boost/algorithm/string/split.hpp>
  6. #include <boost/algorithm/string.hpp>
  7. #include <fstream>
  8. #include <unistd.h>
  9. #include <ctime>
  10. #include "bulletin_broad_show.h"
  11. #include "mine_business.h"
  12. #include <sys/stat.h>
  13. #include "log.h"
  14. bulletin_broad_show::bulletin_broad_show()
  15. {
  16. }
  17. bulletin_broad_show* bulletin_broad_show::inst()
  18. {
  19. static bulletin_broad_show mb;
  20. return &mb;
  21. }
  22. void bulletin_broad_show::OnInit(config_file * config)
  23. {
  24. if (config == nullptr)
  25. {
  26. return;
  27. }
  28. //公告牌显示设置
  29. m_bulletin_board_time = config->get("bulletinboard.showtime",1*60);
  30. std::string tmpSz = config->get("bulletinboard.show_filepath","../log/bulletin");
  31. set_bulletin_board_path(tmpSz);
  32. m_bulletin_board_file_name = config->get("bulletinboard.show_filename","bulletin.txt");
  33. }
  34. void bulletin_broad_show::CreateDirectoryEx(const std::string & sPathName )
  35. {
  36. std::vector<std::string> vecSegTag;
  37. boost::split(vecSegTag, sPathName, boost::is_any_of("/"));
  38. if (vecSegTag.size() == 1) //数据发生错误
  39. {
  40. return;
  41. }
  42. std::string curPath = "";
  43. for (int i = 0 ; i < (int)vecSegTag.size() ; i++)
  44. {
  45. curPath += vecSegTag[i];
  46. if(vecSegTag[i].length() > 1 && vecSegTag[i][0] != '.' )
  47. {
  48. int a = access(curPath.c_str(), F_OK);
  49. if(a == -1)
  50. {
  51. mkdir(curPath.c_str(),0755);
  52. }
  53. }
  54. curPath += "/";
  55. }
  56. }
  57. void bulletin_broad_show::run_bulletin_board()
  58. {
  59. time_t t = std::time(nullptr);
  60. if (t - m_lastshow_bulletin_time > m_bulletin_board_time )
  61. {
  62. //把当前文件写到文件中
  63. m_lastshow_bulletin_time = t;
  64. uint32_t num = mine_business::inst()->get_mine_display_staff_num();
  65. std::string path = m_bulletin_board_path;
  66. static bool g_bCreatePath = false;
  67. if (!g_bCreatePath)
  68. {
  69. CreateDirectoryEx(path); //创建目录
  70. g_bCreatePath = true;
  71. }
  72. path += "/" + m_bulletin_board_file_name;
  73. std::ofstream f1(path.c_str());//打开文件用于写,若文件不存在就创建它
  74. if(!f1.is_open()) {
  75. return;//打开文件失败则结束运行
  76. }
  77. std::string szText = "井下当前总人数: " + std::to_string(num) + "人";
  78. f1 << szText << std::endl;
  79. f1.close(); //关闭文件
  80. log_info("show_bulletin_board : %s" ,szText.c_str());
  81. }
  82. }
  83. void bulletin_broad_show::set_bulletin_board_path(std::string & path)
  84. {
  85. if (path.empty()) return;
  86. for(unsigned i = path.length() - 1 ; i > 0 ;i--)
  87. {
  88. if ( path[i] == '/')
  89. path.pop_back();
  90. else
  91. break;
  92. }
  93. m_bulletin_board_path = path;
  94. }