123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- using Shaker.Model;
- using ShakerManger.Tools;
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- namespace ShakerManger.ViewModel
- {
- internal class BusManger
- {
- private bool isInit = false;
- private BusManger()
- {
- }
- static BusManger()
- {
- }
- private string serverIP = "127.0.0.1";
- private int serverPort = 61616;
- public string ServerIP { get => serverIP; set => serverIP = value; }
- public int ServerPort { get => serverPort; set => serverPort = value; }
- public void Init()
- {
- if (isInit) return;
- if (ConfigurationManager.AppSettings.AllKeys.Contains(nameof(ServerIP)))
- {
- ServerIP = ConfigurationManager.AppSettings[nameof(ServerIP)]!.ToString();
- }
- else
- {
- ConfigurationManager.AppSettings.Set(nameof(ServerIP), serverIP);
- }
- if (ConfigurationManager.AppSettings.AllKeys.Contains(nameof(ServerPort)))
- {
- try
- {
- ServerPort = int.Parse(ConfigurationManager.AppSettings[nameof(ServerPort)]!.ToString());
- }
- catch
- {
- ConfigurationManager.AppSettings.Set(nameof(ServerPort), serverPort.ToString());
- }
- }
- else
- {
- ConfigurationManager.AppSettings.Set(nameof(ServerPort), serverPort.ToString());
- }
- try
- {
- isInit = true;
- Bus = EasyMQ.ActiveHutch.Default.CreateBus(ServerIP,ServerPort, new EasyMQ.MessagePackSerializer());
- Bus.RPC.Respond<GetShakerConfigs, ShakerConfigResult>((cliendid, pro) =>
- {
- if (string.IsNullOrEmpty(cliendid.ClientID)) return new ShakerConfigResult()
- {
- Message = "客户端ID不能为空!",
- Success = false,
- };
- var mqclientid = pro.GetString(EasyMQ.ActiveHutch.CLIENT_ID_KEY);
- bool islogin = false;
- if(!string.IsNullOrEmpty(mqclientid))
- {
- foreach(var val in MainWindowViewModel.Default.Shakers.Shakers)
- {
- if(val.ClientID == cliendid.ClientID)
- {
- if(!string.IsNullOrEmpty(val.ActiveMQClintID) && !islogin)
- {
- islogin = true;
- }
- val.ActiveMQClintID = mqclientid;
- }
- }
- }
- if(islogin)
- {
- return new ShakerConfigResult()
- {
- Message = "当前客户端已登录,无法重复启动!",
- Success = false,
- };
- }
- return new ShakerConfigResult()
- {
- Shakers = Sql.Default.Find<Shaker.Model.ShakerControlModel>(x => x.ClientID == cliendid.ClientID).ToList(),
- SystemConfig = MainWindowViewModel.Default.SystemConfig.SystemPage.Model,
- };
- });
- Bus.RPC.Respond<UserModel, LoginResult>((user, pro) =>
- {
- if (pro == null || user == null || string.IsNullOrEmpty(user.UserName)) return new LoginResult()
- {
- Success = false,
- Message = "传入了空参数!"
- };
- string clientid = pro.GetString(Shaker.Model.GlobalVariable.ClientIDKey);
- if (string.IsNullOrEmpty(clientid)) return new LoginResult()
- {
- Success = false,
- Message = "ClientID不能为空"
- };
- var finduser = Sql.Default.FindFirst<UserModel>(x => x.UserName == user.UserName && x.PassWord == user.PassWord);
- if (finduser == null)
- {
- return new LoginResult() { Success = false, Message = "用户名或密码错误" };
- }
- else
- {
- if (finduser.UserType == UserType.SystemUser || finduser.UserType == UserType.SystemAdministrator) return new LoginResult() { Success = true, Message = "", User = finduser };
- if (finduser.ClientIDs.Contains(clientid)) return new LoginResult() { Success = true, Message = "", User = finduser };
- else
- {
- return new LoginResult() { Success = false, Message = "当前用户不允许登录此客户端" };
- }
- }
- });
- Bus.PubSub.Subscribe<Shaker.Model.LogModel>((log, _) =>
- {
- if (!Sql.Default.IsConnected) return;
- Sql.Default.Insert(log);
- });
- Bus.PubSub.Subscribe<Shaker.Model.WarnModel>((log, _) =>
- {
- if (!Sql.Default.IsConnected) return;
- Sql.Default.Insert(log);
- });
- }
- catch
- {
- MessageBoxHelper.Error("服务端未运行,程序退出");
- if (Thread.CurrentThread == Application.Current.Dispatcher.Thread)
- {
- App.Current.Shutdown();
- }
- else
- {
- App.Current.Dispatcher.BeginInvoke(new Action(() =>
- {
- App.Current.Shutdown();
- }));
- }
- return;
- }
-
- }
- public EasyMQ.IBus Bus { get; private set; }
- public static BusManger Defaut { get; } = new BusManger();
- }
- }
|