MessageServerAppService.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.Threading.Tasks;
  3. using Abp.Auditing;
  4. using Abp.Configuration;
  5. using Abp.Domain.Repositories;
  6. using Abp.Runtime.Caching;
  7. using IwbZero.ToolCommon;
  8. using IwbZero.ToolCommon.StringModel;
  9. using WeEngine.CommonDto;
  10. using WePlatform.Configuration;
  11. using WePlatform.ThirdPartySystem;
  12. namespace WePlatform.DataCenter.MessageServer
  13. {
  14. /// <summary>
  15. /// 消息服务
  16. /// </summary>
  17. [DisableAuditing]
  18. public class MessageServerAppService:DataCenterAppServiceBase, IMessageServerAppService
  19. {
  20. public MessageServerAppService(ICacheManager cacheManager, IRepository<ThirdPartySystemInfo, string> tpsRepository)
  21. {
  22. TpsRepository = tpsRepository;
  23. CacheManager = cacheManager;
  24. }
  25. private string ServiceIp => SettingManager.GetSettingValue(IwbSettingNames.MqttServiceIp);
  26. private int ServicePort => GetServicePort();
  27. private int HttpServicePort => GetServicePort(true);
  28. private int GetServicePort(bool isHttp=false)
  29. {
  30. var portStr =
  31. SettingManager.GetSettingValue(isHttp
  32. ? IwbSettingNames.HttpMqttServicePort
  33. : IwbSettingNames.MqttServicePort);
  34. if (int.TryParse(portStr, out var port))
  35. {
  36. return port;
  37. }
  38. return 0;
  39. }
  40. protected IRepository<ThirdPartySystemInfo,string> TpsRepository { get; }
  41. /// <summary>
  42. /// 获取客户端配置数据
  43. /// </summary>
  44. /// <param name="appId"></param>
  45. /// <returns></returns>
  46. [DisableAuditing]
  47. public async Task<MsgClientDto> GetClient(string appId)
  48. {
  49. var entity = await TpsRepository.FirstOrDefaultAsync(a => a.AppId == appId);
  50. if (entity == null)
  51. {
  52. CheckErrors($"未查询到Id为【{appId}】的应用!");
  53. return null;
  54. }
  55. if (!entity.IsActive)
  56. {
  57. CheckErrors($"应用已被锁定,请联系管理员!");
  58. }
  59. if (entity.ExpiredDate != null && entity.ExpiredDate < DateTime.Now.Date)
  60. {
  61. CheckErrors($"应用已过期,请联系管理员!");
  62. }
  63. if (!entity.IsRegistered)
  64. {
  65. string url = $"{ServiceIp}:{HttpServicePort}/api/mqtt/regClient";
  66. var result = url.RequestPost(new
  67. {
  68. Id=entity.AppId,
  69. ClientName= entity.Name,
  70. Password= entity.SecretKey,
  71. ClientState=1
  72. }.Obj2String());
  73. if (result != null && result.Contains("\"isSuccess\":true"))
  74. {
  75. entity.IsRegistered = true;
  76. await TpsRepository.UpdateAsync(entity);
  77. }
  78. }
  79. return new MsgClientDto()
  80. {
  81. AppId = appId,
  82. Name = entity.Name,
  83. SecretKey = entity.SecretKey,
  84. ServerIp = ServiceIp,
  85. ServerPort =ServicePort
  86. };
  87. }
  88. }
  89. }