| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- using System.Configuration;
- using System.Reflection;
- using Abp.Configuration;
- using Abp.Dependency;
- using Abp.Runtime.Caching;
- using IwbZero.ToolCommon;
- using IwbZero.ToolCommon.LogHelpers;
- using IwbZero.ToolCommon.StringModel;
- using uPLibrary.Networking.M2Mqtt.Messages;
- using WeEngine.CommonDto;
- using WeEngine.Enum;
- using WeMessageService;
- namespace WeEngine.Message
- {
- public class WeEngineMsgClientManager : ISingletonDependency
- {
- public WeEngineMsgClientManager(ISettingManager settingManager, ICacheManager cacheManager)
- {
- SettingManager = settingManager;
- CacheManager = cacheManager;
- if (Client.NotValidate)
- {
- RegisterClient();
- }
- }
- private static M2MqttMsgClient Client = new M2MqttMsgClient();
- //public IIwbMsgClient Client { get; set; }
- private ISettingManager SettingManager { get; }
- private ICacheManager CacheManager { get; }
- public string ClientId { get; set; }
- /// <summary>
- /// 强制刷新客户端
- /// </summary>
- public void RefreshClient()
- {
- this.LogInfo($"客户端{ClientId}开始强制刷新!");
- Client.DisConnect();
- Client = new M2MqttMsgClient();
- RegisterClient();
- SendMessage(ClientId, $"客户端{ClientId}已强制刷新!");
- SendMessage("WeApp", $"客户端{ClientId}已强制刷新!");
- }
- /// <summary>
- /// 注册客户端
- /// </summary>
- public void RegisterClient()
- {
- ClientId = "WeEngine";
- #if DEBUG
- ClientId += "-TEST";
- var result = new MsgClientDto()
- {
- AppId = ClientId,
- Name = "智慧推演引擎",
- SecretKey = "WePlatformWeEngine",
- ServerIp = "localhost",
- ServerPort = 1885,
- };
- #else
- string url = "";//SettingManager.GetSettingValue(IwbSettingNames.DateCenterUrl);
- url = url.IsEmpty()
- ? ConfigurationManager.AppSettings["DataCenter.Platform.BaseUrl"] ?? "http://shvber.com:5025/"
- : url;
- this.LogInfo(url);
- var result = GetMsgClientDto(ClientId, url);
- if (result == null)
- {
- //throw new UserFriendlyException("未查询到消息客户端配置!");
- this.LogError($"未查询到消息客户端{ClientId}的配置!");
- return;
- }
- #endif
- ClientId = result.AppId;
- Client.SetOptions(result.ServerIp, result.ServerPort, result.AppId, result.Name, result.SecretKey);
- Client.SetReceivedHandler(ReceivedHandler);
- Client.HeartbeatTopic.Add("WeApp");
- Subscribe();
- }
- private void Subscribe()
- {
- if (Client.CheckClient())
- {
- Client.SubscribeTopic(ClientId, false);
- Client.SubscribeTopic("Heartbeat");
- var t = typeof(MessageType);
- var fts = t.GetFields(BindingFlags.Static | BindingFlags.Public);
- foreach (var f in fts)
- {
- string name = f.Name;
- Client.SubscribeTopic($"{name}/+");
- //AsyncHelper.RunSync(() => Client.SubscribeTopic(name));
- }
- }
- }
- public void SendMessage(WeMessage message, MessageType type, string clientId = null)
- {
- if (Client.NotValidate)
- {
- RegisterClient();
- }
- var topic = message.RunningId.IsEmpty() ? type.ToString() : $"{type}/{message.RunningId}";
- Client.SendMsg(topic, message.Obj2String(), clientId);
- }
- public void SendMessage(WeMessage message)
- {
- if (Client.NotValidate)
- {
- RegisterClient();
- }
- var topic = message.RunningId.IsEmpty() ? message.Type.ToString() : $"{message.Type}/{message.RunningId}";
- Client.SendMsg(topic, message.Obj2String(), message.TargetClientId);
- }
- public void SendMessage(string msg)
- {
- if (Client.NotValidate)
- {
- RegisterClient();
- }
- Client.SendMessage(ClientId, msg.Obj2String());
- }
- public void SendMessage(string topic, string msg)
- {
- if (Client.NotValidate)
- {
- RegisterClient();
- }
- Client.SendMessage(topic, msg.Obj2String());
- }
- /// <summary>
- /// 消息接受回调处理
- /// </summary>
- /// <param name="msg"></param>
- private void ReceiveMsgHandler(string msg)
- {
- var message = msg.Str2Obj<WeMessage>();
- if (message != null)
- {
- switch (message.Type)
- {
- case MessageType.Environment:
- break;
- case MessageType.RunningInfo:
- break;
- case MessageType.CommonMessage:
- break;
- case MessageType.GuideInfo:
- break;
- }
- }
- else
- {
- typeof(WeEngineMsgClientManager).LogError("MSG ERROR:" + msg);
- }
- }
- /// <summary>
- /// 消息接受回调处理
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void ReceivedHandler(object sender, MqttMsgPublishEventArgs e)
- {
- if (e.Topic == ClientId || e.Topic == $"{ClientId}/Heartbeat")
- {
- return;
- }
- string msg = System.Text.Encoding.UTF8.GetString(e.Message);
- ReceiveMsgHandler(msg);
- }
- //private static IMqttApplicationMessageReceivedHandler ReceivedHandler => new MqttApplicationMessageReceivedHandlerDelegate(
- // (arg) =>
- // {
- // string payload = System.Text.Encoding.UTF8.GetString(arg.ApplicationMessage.Payload);
- // var message = payload.Str2Obj<WeMessage>();
- // if (message != null)
- // {
- // switch (message.MsgType)
- // {
- // case MessageType.Environment:
- // break;
- // case MessageType.RunningInfo:
- // break;
- // case MessageType.CommonMessage:
- // break;
- // case MessageType.GuideInfo:
- // break;
- // }
- // }
- // else
- // {
- // typeof(WeEngineMsgClientManager).LogError("Message ERROR:" + payload);
- // }
- // });
- /// <summary>
- /// 获取消息客户端信息
- /// </summary>
- /// <returns></returns>
- public MsgClientDto GetMsgClientDto(string appid, string urlPrev)
- {
- return CacheManager.GetCache("WeEngineClient").Get($"{appid}", () =>
- {
- var url =
- $"{urlPrev.Ew("/")}api/services/WePlatform/MessageServer/GetClient?appid={appid}";
- var result = url.RequestPost("");
- var dto = result.Str2Obj<MsgClientDto>();
- return dto;
- });
- }
- }
- }
|