CcGatewaysServiceImpl.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package com.ruoyi.cc.service.impl;
  2. import java.io.File;
  3. import java.nio.file.Files;
  4. import java.nio.file.Path;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. import com.alibaba.fastjson.JSONObject;
  10. import com.ruoyi.cc.service.ICcParamsService;
  11. import com.ruoyi.cc.utils.FsConfPathUtils;
  12. import com.ruoyi.cc.utils.FsEslNameUtils;
  13. import com.ruoyi.common.utils.DateUtils;
  14. import com.ruoyi.common.utils.StringUtils;
  15. import link.thingscloud.freeswitch.esl.EslConnectionUtil;
  16. import link.thingscloud.freeswitch.esl.transport.message.EslMessage;
  17. import lombok.extern.slf4j.Slf4j;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.stereotype.Service;
  20. import com.ruoyi.cc.mapper.CcGatewaysMapper;
  21. import com.ruoyi.cc.domain.CcGateways;
  22. import com.ruoyi.cc.service.ICcGatewaysService;
  23. import com.ruoyi.common.core.text.Convert;
  24. /**
  25. * 线路配置Service业务层处理
  26. *
  27. * @author ruoyi
  28. * @date 2024-12-22
  29. */
  30. @Service
  31. @Slf4j
  32. public class CcGatewaysServiceImpl implements ICcGatewaysService
  33. {
  34. @Autowired
  35. private CcGatewaysMapper ccGatewaysMapper;
  36. @Autowired
  37. private ICcParamsService ccParamsService;
  38. /**
  39. * 查询线路配置
  40. *
  41. * @param id 线路配置主键
  42. * @return 线路配置
  43. */
  44. @Override
  45. public CcGateways selectCcGatewaysById(Long id)
  46. {
  47. return ccGatewaysMapper.selectCcGatewaysById(id);
  48. }
  49. /**
  50. * 查询线路配置列表
  51. *
  52. * @param ccGateways 线路配置
  53. * @return 线路配置
  54. */
  55. @Override
  56. public List<CcGateways> selectCcGatewaysList(CcGateways ccGateways)
  57. {
  58. return ccGatewaysMapper.selectCcGatewaysList(ccGateways);
  59. }
  60. /**
  61. * 新增线路配置
  62. *
  63. * @param ccGateways 线路配置
  64. * @return 结果
  65. */
  66. @Override
  67. public int insertCcGateways(CcGateways ccGateways)
  68. {
  69. return ccGatewaysMapper.insertCcGateways(ccGateways);
  70. }
  71. /**
  72. * 修改线路配置
  73. *
  74. * @param ccGateways 线路配置
  75. * @return 结果
  76. */
  77. @Override
  78. public int updateCcGateways(CcGateways ccGateways)
  79. {
  80. return ccGatewaysMapper.updateCcGateways(ccGateways);
  81. }
  82. /**
  83. * 批量删除线路配置
  84. *
  85. * @param ids 需要删除的线路配置主键
  86. * @return 结果
  87. */
  88. @Override
  89. public int deleteCcGatewaysByIds(String ids)
  90. {
  91. return ccGatewaysMapper.deleteCcGatewaysByIds(Convert.toStrArray(ids));
  92. }
  93. /**
  94. * 删除线路配置信息
  95. *
  96. * @param id 线路配置主键
  97. * @return 结果
  98. */
  99. @Override
  100. public int deleteCcGatewaysById(Long id)
  101. {
  102. return ccGatewaysMapper.deleteCcGatewaysById(id);
  103. }
  104. @Override
  105. public CcGateways selectCcGatewaysByGwName(String gwName) {
  106. List<CcGateways> list = this.selectCcGatewaysList(new CcGateways().setGwName(gwName));
  107. if (list.size() > 0) {
  108. return list.get(0);
  109. }
  110. return null;
  111. }
  112. @Override
  113. public Integer refreshGatewaysFiles() {
  114. List<CcGateways> list = this.selectCcGatewaysList(new CcGateways());
  115. Map<String, List<String>> profileNames = new HashMap<>();
  116. for (CcGateways ccGateways : list) {
  117. List<String> gatewayNames = profileNames.getOrDefault(ccGateways.getProfileName(), new ArrayList<>());
  118. // 废弃的配置,也删除
  119. if (ccGateways.getPurpose() != 0) {
  120. gatewayNames.add(ccGateways.getGwName() + ".xml");
  121. }
  122. profileNames.put(ccGateways.getProfileName(), gatewayNames);
  123. }
  124. String fsConfDirectory = ccParamsService.getParamValueByCode("fs_conf_directory", "");
  125. int deletedCount = 0;
  126. // 遍历每个profile及其对应的gateway名称集合
  127. for (Map.Entry<String, List<String>> entry : profileNames.entrySet()) {
  128. String profile = entry.getKey();
  129. List<String> gatewayNames = entry.getValue();
  130. log.info("==========profile:{}, gatewayNames:{}", profile, JSONObject.toJSONString(gatewayNames));
  131. if (!FsEslNameUtils.isSafeName(profile)) {
  132. log.warn("skip unsafe profile name in refreshGatewaysFiles: {}", profile);
  133. continue;
  134. }
  135. Path profileDir = FsConfPathUtils.resolveProfileDir(fsConfDirectory, profile);
  136. if (profileDir == null || !Files.exists(profileDir) || !Files.isDirectory(profileDir)) {
  137. continue;
  138. }
  139. try {
  140. File[] files = profileDir.toFile().listFiles();
  141. if (files == null) {
  142. continue;
  143. }
  144. for (File file : files) {
  145. String gwName = file.getName().replace(".xml", "");
  146. log.info("====================gwName:" + gwName);
  147. log.info("=====================" + (file.isFile() && !gatewayNames.contains(file.getName())));
  148. log.info("=====================" + (gatewayNames.contains(file.getName())));
  149. if (file.isFile() && !gatewayNames.contains(file.getName())) {
  150. if (!FsEslNameUtils.isSafeName(gwName)) {
  151. log.warn("skip unsafe gwName when cleaning: {}", gwName);
  152. continue;
  153. }
  154. boolean deleted = file.delete();
  155. if (deleted) {
  156. EslMessage eslMessage1 = EslConnectionUtil.sendSyncApiCommand(
  157. "sofia", "profile " + profile + " killgw " + gwName);
  158. if (null != eslMessage1) {
  159. log.info(StringUtils.joinWith("/r/n", eslMessage1.getBodyLines().toArray()));
  160. }
  161. deletedCount++;
  162. log.info("=======Deleted orphaned file: {}", file.getAbsolutePath());
  163. } else {
  164. log.warn("========Failed to delete file: {}", file.getAbsolutePath());
  165. }
  166. }
  167. }
  168. } catch (Exception e) {
  169. log.error("Error processing directory: {}", profileDir, e);
  170. }
  171. }
  172. return deletedCount;
  173. }
  174. }