BusManger.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using Shaker.Model;
  2. using ShakerManger.Tools;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Configuration;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. namespace ShakerManger.ViewModel
  11. {
  12. internal class BusManger
  13. {
  14. private bool isInit = false;
  15. private BusManger()
  16. {
  17. }
  18. static BusManger()
  19. {
  20. }
  21. private string serverIP = "127.0.0.1";
  22. private int serverPort = 61616;
  23. public string ServerIP { get => serverIP; set => serverIP = value; }
  24. public int ServerPort { get => serverPort; set => serverPort = value; }
  25. public void Init()
  26. {
  27. if (isInit) return;
  28. if (ConfigurationManager.AppSettings.AllKeys.Contains(nameof(ServerIP)))
  29. {
  30. ServerIP = ConfigurationManager.AppSettings[nameof(ServerIP)]!.ToString();
  31. }
  32. else
  33. {
  34. ConfigurationManager.AppSettings.Set(nameof(ServerIP), serverIP);
  35. }
  36. if (ConfigurationManager.AppSettings.AllKeys.Contains(nameof(ServerPort)))
  37. {
  38. try
  39. {
  40. ServerPort = int.Parse(ConfigurationManager.AppSettings[nameof(ServerPort)]!.ToString());
  41. }
  42. catch
  43. {
  44. ConfigurationManager.AppSettings.Set(nameof(ServerPort), serverPort.ToString());
  45. }
  46. }
  47. else
  48. {
  49. ConfigurationManager.AppSettings.Set(nameof(ServerPort), serverPort.ToString());
  50. }
  51. try
  52. {
  53. isInit = true;
  54. Bus = EasyMQ.ActiveHutch.Default.CreateBus(ServerIP,ServerPort, new EasyMQ.MessagePackSerializer());
  55. Bus.RPC.Respond<GetShakerConfigs, ShakerConfigResult>((cliendid, pro) =>
  56. {
  57. if (string.IsNullOrEmpty(cliendid.ClientID)) return new ShakerConfigResult()
  58. {
  59. Message = "客户端ID不能为空!",
  60. Success = false,
  61. };
  62. var mqclientid = pro.GetString(EasyMQ.ActiveHutch.CLIENT_ID_KEY);
  63. bool islogin = false;
  64. if(!string.IsNullOrEmpty(mqclientid))
  65. {
  66. foreach(var val in MainWindowViewModel.Default.Shakers.Shakers)
  67. {
  68. if(val.ClientID == cliendid.ClientID)
  69. {
  70. if(!string.IsNullOrEmpty(val.ActiveMQClintID) && !islogin)
  71. {
  72. islogin = true;
  73. }
  74. val.ActiveMQClintID = mqclientid;
  75. }
  76. }
  77. }
  78. if(islogin)
  79. {
  80. return new ShakerConfigResult()
  81. {
  82. Message = "当前客户端已登录,无法重复启动!",
  83. Success = false,
  84. };
  85. }
  86. return new ShakerConfigResult()
  87. {
  88. Shakers = Sql.Default.Find<Shaker.Model.ShakerControlModel>(x => x.ClientID == cliendid.ClientID).ToList(),
  89. SystemConfig = MainWindowViewModel.Default.SystemConfig.SystemPage.Model,
  90. };
  91. });
  92. Bus.RPC.Respond<UserModel, LoginResult>((user, pro) =>
  93. {
  94. if (pro == null || user == null || string.IsNullOrEmpty(user.UserName)) return new LoginResult()
  95. {
  96. Success = false,
  97. Message = "传入了空参数!"
  98. };
  99. string clientid = pro.GetString(Shaker.Model.GlobalVariable.ClientIDKey);
  100. if (string.IsNullOrEmpty(clientid)) return new LoginResult()
  101. {
  102. Success = false,
  103. Message = "ClientID不能为空"
  104. };
  105. var finduser = Sql.Default.FindFirst<UserModel>(x => x.UserName == user.UserName && x.PassWord == user.PassWord);
  106. if (finduser == null)
  107. {
  108. return new LoginResult() { Success = false, Message = "用户名或密码错误" };
  109. }
  110. else
  111. {
  112. if (finduser.UserType == UserType.SystemUser || finduser.UserType == UserType.SystemAdministrator) return new LoginResult() { Success = true, Message = "", User = finduser };
  113. if (finduser.ClientIDs.Contains(clientid)) return new LoginResult() { Success = true, Message = "", User = finduser };
  114. else
  115. {
  116. return new LoginResult() { Success = false, Message = "当前用户不允许登录此客户端" };
  117. }
  118. }
  119. });
  120. Bus.PubSub.Subscribe<Shaker.Model.LogModel>((log, _) =>
  121. {
  122. if (!Sql.Default.IsConnected) return;
  123. Sql.Default.Insert(log);
  124. });
  125. Bus.PubSub.Subscribe<Shaker.Model.WarnModel>((log, _) =>
  126. {
  127. if (!Sql.Default.IsConnected) return;
  128. Sql.Default.Insert(log);
  129. });
  130. }
  131. catch
  132. {
  133. MessageBoxHelper.Error("服务端未运行,程序退出");
  134. if (Thread.CurrentThread == Application.Current.Dispatcher.Thread)
  135. {
  136. App.Current.Shutdown();
  137. }
  138. else
  139. {
  140. App.Current.Dispatcher.BeginInvoke(new Action(() =>
  141. {
  142. App.Current.Shutdown();
  143. }));
  144. }
  145. return;
  146. }
  147. }
  148. public EasyMQ.IBus Bus { get; private set; }
  149. public static BusManger Defaut { get; } = new BusManger();
  150. }
  151. }