| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- using System;
- using System.Threading.Tasks;
- using Abp.Auditing;
- using Abp.Configuration;
- using Abp.Domain.Repositories;
- using Abp.Runtime.Caching;
- using IwbZero.ToolCommon;
- using IwbZero.ToolCommon.StringModel;
- using WeEngine.CommonDto;
- using WePlatform.Configuration;
- using WePlatform.ThirdPartySystem;
- namespace WePlatform.DataCenter.MessageServer
- {
- /// <summary>
- /// 消息服务
- /// </summary>
- [DisableAuditing]
- public class MessageServerAppService:DataCenterAppServiceBase, IMessageServerAppService
- {
- public MessageServerAppService(ICacheManager cacheManager, IRepository<ThirdPartySystemInfo, string> tpsRepository)
- {
- TpsRepository = tpsRepository;
- CacheManager = cacheManager;
- }
- private string ServiceIp => SettingManager.GetSettingValue(IwbSettingNames.MqttServiceIp);
- private int ServicePort => GetServicePort();
- private int HttpServicePort => GetServicePort(true);
- private int GetServicePort(bool isHttp=false)
- {
- var portStr =
- SettingManager.GetSettingValue(isHttp
- ? IwbSettingNames.HttpMqttServicePort
- : IwbSettingNames.MqttServicePort);
- if (int.TryParse(portStr, out var port))
- {
- return port;
- }
- return 0;
- }
- protected IRepository<ThirdPartySystemInfo,string> TpsRepository { get; }
- /// <summary>
- /// 获取客户端配置数据
- /// </summary>
- /// <param name="appId"></param>
- /// <returns></returns>
- [DisableAuditing]
- public async Task<MsgClientDto> GetClient(string appId)
- {
- var entity = await TpsRepository.FirstOrDefaultAsync(a => a.AppId == appId);
- if (entity == null)
- {
- CheckErrors($"未查询到Id为【{appId}】的应用!");
- return null;
- }
- if (!entity.IsActive)
- {
- CheckErrors($"应用已被锁定,请联系管理员!");
- }
- if (entity.ExpiredDate != null && entity.ExpiredDate < DateTime.Now.Date)
- {
- CheckErrors($"应用已过期,请联系管理员!");
- }
- if (!entity.IsRegistered)
- {
- string url = $"{ServiceIp}:{HttpServicePort}/api/mqtt/regClient";
- var result = url.RequestPost(new
- {
- Id=entity.AppId,
- ClientName= entity.Name,
- Password= entity.SecretKey,
- ClientState=1
- }.Obj2String());
- if (result != null && result.Contains("\"isSuccess\":true"))
- {
- entity.IsRegistered = true;
- await TpsRepository.UpdateAsync(entity);
- }
- }
- return new MsgClientDto()
- {
- AppId = appId,
- Name = entity.Name,
- SecretKey = entity.SecretKey,
- ServerIp = ServiceIp,
- ServerPort =ServicePort
- };
- }
- }
- }
|