终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

377 lines
15 KiB

  1. using BPA.Message;
  2. using BPASmartClient.Business;
  3. using BPASmartClient.Control;
  4. using BPASmartClient.CustomResource.UserControls;
  5. using BPASmartClient.CustomResource.UserControls.MessageShow;
  6. using BPASmartClient.Device;
  7. using BPASmartClient.EventBus;
  8. using BPASmartClient.Helper;
  9. using BPASmartClient.IoT;
  10. using BPASmartClient.Message;
  11. using BPASmartClient.Model;
  12. using BPASmartClient.Model.冰淇淋.Enum;
  13. using BPASmartClient.Model.咖啡机.Enum;
  14. using BPASmartClient.Peripheral;
  15. using BPASmartClient.ViewModel;
  16. using Newtonsoft.Json;
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Collections.ObjectModel;
  20. using System.IO;
  21. using System.Linq;
  22. using System.Reflection;
  23. using System.Text;
  24. using System.Threading.Tasks;
  25. using System.Windows;
  26. using System.Windows.Controls;
  27. using System.Windows.Data;
  28. using System.Windows.Documents;
  29. using System.Windows.Input;
  30. using System.Windows.Media;
  31. using System.Windows.Media.Imaging;
  32. using System.Windows.Navigation;
  33. using System.Windows.Shapes;
  34. namespace BPASmartClient
  35. {
  36. /// <summary>
  37. /// Interaction logic for MainWindow.xaml
  38. /// </summary>
  39. public partial class MainWindow : Window
  40. {
  41. public MainConsole mainConsole;
  42. public MainWindow()
  43. {
  44. InitializeComponent();
  45. MainViewModel.GetInstance().window = this;
  46. this.DataContext = MainViewModel.GetInstance();
  47. ActionManage.GetInstance.Register(new Action(() =>
  48. {
  49. myMenu.Visibility = Visibility.Visible;
  50. myPanel.Visibility = Visibility.Collapsed;
  51. MenuItem_Click(new MenuItem() { Tag = "LogView", Header = "日志监视" }, null);
  52. }), "LoginSuccess");
  53. Initialize();
  54. }
  55. #region 私有
  56. /// <summary>
  57. /// 初始化
  58. /// </summary>
  59. private void Initialize()
  60. {
  61. this.ButClose.Click += (o, e) =>
  62. {
  63. MessageBoxResult res = MessageBox.Show("确定关闭上位机软件?", "警告", MessageBoxButton.OKCancel);
  64. if(res == MessageBoxResult.OK)
  65. {
  66. this.Close();
  67. }
  68. };
  69. this.br.MouseLeftButtonDown += (o, e) =>
  70. {
  71. if (e.ClickCount > 1)
  72. {
  73. if (this.WindowState == WindowState.Maximized)
  74. this.WindowState = WindowState.Normal;
  75. else if (this.WindowState == WindowState.Normal)
  76. this.WindowState = WindowState.Maximized;
  77. }
  78. if (e.LeftButton == MouseButtonState.Pressed) this.DragMove();
  79. };
  80. MenuItem_Click(new MenuItem() { Tag = "LogView", Header = "日志监视" }, null);
  81. ThreadManage.GetInstance().Start(new Action(() =>
  82. {
  83. GetDevices();
  84. mainConsole = new MainConsole();
  85. mainConsole.Start();
  86. }), "启动主控制台", false);
  87. ActionManage.GetInstance.Register(new Action(() =>
  88. {
  89. ThreadManage.GetInstance().Start(new Action(() =>
  90. {
  91. try
  92. {
  93. DataVClient.GetInstance().Initialize();
  94. DataVClient.GetInstance().Start();
  95. }
  96. catch (Exception ex)
  97. {
  98. MessageLog.GetInstance.ShowEx(ex.ToString());
  99. }
  100. }), "启动主IoT", false);
  101. }), "配置初始化完成Iot启动");
  102. ActionManage.GetInstance.Register(new Action<object>((o) =>
  103. {
  104. App.Current.Dispatcher.Invoke(new Action(() =>
  105. {
  106. if (o is IOTCommandModel iot)
  107. {
  108. switch (iot.CommandName)
  109. {
  110. case 0://控制类
  111. if (iot.CommandValue != null && iot.CommandValue.Count > 0)
  112. {
  113. switch (iot.CommandValue.Keys.ToList()[0])
  114. {
  115. case "程序启动":
  116. //mainConsole.Start();
  117. break;
  118. //mainConsole.Stop();
  119. case "程序停止":
  120. break;
  121. case "程序复位":
  122. //mainConsole.Stop();
  123. //mainConsole.Start();
  124. break;
  125. default:
  126. break;
  127. }
  128. NoticeDemoViewModel.OpenMsg(EnumPromptType.Info, MainViewModel.GetInstance().window, "大屏控制", iot.CommandValue.Keys.ToList()[0]);
  129. }
  130. break;
  131. case 1://设置类
  132. break;
  133. case 2://通知类
  134. if (iot.CommandValue != null && iot.CommandValue.ContainsKey("text"))
  135. {
  136. NoticeDemoViewModel.OpenMsg(EnumPromptType.Info, MainViewModel.GetInstance().window, "大屏通知", iot.CommandValue["text"]);
  137. }
  138. break;
  139. default:
  140. break;
  141. }
  142. }
  143. }));
  144. }), "IotBroadcast");
  145. }
  146. /// <summary>
  147. /// 获取设备集合
  148. /// </summary>
  149. private void GetDevices()
  150. {
  151. //List<string> IDevices = new List<string>();
  152. //List<string> IPeripherals = new List<string>();
  153. DirectoryInfo directoryInfo = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
  154. var files = directoryInfo.GetFiles().Where(p => p.FullName.Contains("BPASmartClient.") && p.FullName.Contains("dll")).ToList();
  155. List<string> fileList = new List<string>();
  156. var assemblies = AppDomain.CurrentDomain.GetAssemblies();
  157. var dlls = assemblies?.Where(p => p.FullName.Contains("BPASmartClient.")).ToList();
  158. List<string> dllList = new List<string>();
  159. if (files != null)
  160. {
  161. foreach (var item in files)
  162. {
  163. var res = System.IO.Path.GetFileNameWithoutExtension(item.FullName);
  164. if (res != null && res.Length > 0 && res.Contains(".") && !res.Contains("dll")) fileList.Add(res);
  165. }
  166. }
  167. if (dlls != null)
  168. {
  169. dlls.ForEach((item) =>
  170. {
  171. item.GetTypes().ToList().ForEach((type) =>
  172. {
  173. if (type.GetInterfaces().Contains(typeof(IDevice)))
  174. {
  175. if (!type.FullName.Contains("BaseDevice")) ShopDeviceConfigViewModel.IDevices.Add(type.FullName);
  176. }
  177. else if (type.GetInterfaces().Contains(typeof(IPeripheral)))
  178. {
  179. if (!type.FullName.Contains("BasePeripheral")) ShopDeviceConfigViewModel.IPeripherals.Add(type.FullName);
  180. }
  181. });
  182. dllList.Add(System.IO.Path.GetFileNameWithoutExtension(item.EscapedCodeBase));
  183. });
  184. }
  185. dllList.ForEach((item) => { if (fileList.Contains(item)) fileList.Remove(item); });
  186. fileList.ForEach((item) =>
  187. {
  188. Assembly.Load(item).GetTypes().ToList().ForEach((type) =>
  189. {
  190. if (type.GetInterfaces().Contains(typeof(IDevice)))
  191. {
  192. if (!type.FullName.Contains("BaseDevice")) ShopDeviceConfigViewModel.IDevices.Add(type.FullName);
  193. }
  194. else if (type.GetInterfaces().Contains(typeof(IPeripheral)))
  195. {
  196. if (!type.FullName.Contains("BasePeripheral")) ShopDeviceConfigViewModel.IPeripherals.Add(type.FullName);
  197. }
  198. });
  199. });
  200. }
  201. /// <summary>
  202. /// 菜单切换栏
  203. /// </summary>
  204. private void MenuItem_Click(object sender, RoutedEventArgs e)
  205. {
  206. try
  207. {
  208. if (sender is MenuItem)
  209. {
  210. Type type = Type.GetType($"BPASmartClient.Control.{(sender as MenuItem).Tag?.ToString()}");
  211. if (type == null) { MenuClick((sender as MenuItem).Tag?.ToString()); return; }
  212. ConstructorInfo cti = type.GetConstructor(System.Type.EmptyTypes);
  213. contentRegion.Content = (FrameworkElement)cti.Invoke(null);
  214. Title.Text = (sender as MenuItem).Header?.ToString() + "界面";
  215. }
  216. else if(sender is Button)
  217. {
  218. Type type = Type.GetType($"BPASmartClient.Control.{(sender as Button).Tag?.ToString()}");
  219. ConstructorInfo cti = type.GetConstructor(System.Type.EmptyTypes);
  220. contentRegion.Content = (FrameworkElement)cti.Invoke(null);
  221. Title.Text = (sender as Button).Content?.ToString() + "界面";
  222. }
  223. }
  224. catch (Exception ex)
  225. {
  226. MessageLog.GetInstance.ShowEx($"BPASmartClient 中引发错误,MainWindow.xaml.cs 类MenuItem_Click(),描述:[{ex.Message}]");
  227. }
  228. }
  229. private void Debug_Click(object sender, RoutedEventArgs e)
  230. {
  231. try
  232. {
  233. if (sender is MenuItem)
  234. {
  235. // Type type = Assembly.Load("BPASmartClient.MorkT").GetType("BPASmartClient.MorkT.View.DebugView");
  236. // Type type = Assembly.Load("BPASmartClient.MorkF").GetType("BPASmartClient.MorkF.View.DebugView");
  237. Type type = Assembly.Load("BPASmartClient.MorkM").GetType("BPASmartClient.MorkM.View.DebugView");
  238. ConstructorInfo cti = type.GetConstructor(System.Type.EmptyTypes);
  239. contentRegion.Content = (FrameworkElement)cti.Invoke(null);
  240. Title.Text = (sender as MenuItem).Header?.ToString() + "界面";
  241. }
  242. }
  243. catch (Exception ex)
  244. {
  245. MessageLog.GetInstance.ShowEx($"BPASmartClient 中引发错误,MainWindow.xaml.cs 类MenuItem_Click(),描述:[{ex.Message}]");
  246. }
  247. }
  248. #endregion
  249. #region 公用
  250. /// <summary>
  251. /// 菜单点击
  252. /// </summary>
  253. /// <param name="name"></param>
  254. public void MenuClick(string name)
  255. {
  256. try
  257. {
  258. switch (name)
  259. {
  260. case "Close":
  261. this.Close();
  262. break;
  263. case "IOT":
  264. BrowserHelper.OpenDefaultBrowserUrl("http://iot.black-pa.com");
  265. break;
  266. }
  267. }
  268. catch (Exception ex)
  269. {
  270. MessageLog.GetInstance.ShowEx($"BPASmartClient 中引发错误,MainWindow.xaml.cs 类MenuClick(),描述:[{ex.Message}]");
  271. }
  272. }
  273. #endregion
  274. #region 临时测试代码
  275. /// <summary>
  276. /// 测试按钮
  277. /// </summary>
  278. /// <param name="sender"></param>
  279. /// <param name="e"></param>
  280. private void Button_Click(object sender, RoutedEventArgs e)
  281. {
  282. //new Demo_MakeCoffeeEvent() { Id = 1 }.Publish();
  283. //new DRCoffee_MakeCoffeeEvent() { Id = 1, DrinkCode= DrCoffeeDrinksCode.两杯意式浓缩 }.Publish();
  284. //new DRCoffee_CoffeeCommCmdEvent() { Id = 1,CommCmd = DrCoffeeCommCmd.冲煮系统快速冲洗指令}.Publish();
  285. MorkOrderPush morkOrderPush = new MorkOrderPush() { DeviceId = 2 };
  286. var temp = BPAPackage.Make(morkOrderPush, 12, BPA.Message.Enum.DeviceClientType.MORKS);
  287. Plugin.GetInstance().GetPlugin<MQTTMgr>().Publish("DOWN/MORKS/ORDER/12", temp.Serialize());
  288. }
  289. /// <summary>
  290. /// 初始化
  291. /// </summary>
  292. /// <param name="sender"></param>
  293. /// <param name="e"></param>
  294. private void init_Click(object sender, RoutedEventArgs e)
  295. {
  296. ActionManage.GetInstance.Send("InitDevice");
  297. }
  298. /// <summary>
  299. /// 登录
  300. /// </summary>
  301. /// <param name="sender"></param>
  302. /// <param name="e"></param>
  303. private void Button_Login(object sender, RoutedEventArgs e)
  304. {
  305. try
  306. {
  307. Type type = Type.GetType($"BPASmartClient.Control.AdminstratorsView");
  308. if (type == null) { MenuClick((sender as MenuItem).Tag?.ToString()); return; }
  309. ConstructorInfo cti = type.GetConstructor(System.Type.EmptyTypes);
  310. contentRegion.Content = (FrameworkElement)cti.Invoke(null);
  311. Title.Text = "管理员登录界面";
  312. }
  313. catch (Exception ex)
  314. {
  315. MessageLog.GetInstance.ShowEx($"BPASmartClient 中引发错误,MainWindow.xaml.cs 类MenuItem_Click(),描述:[{ex.Message}]");
  316. }
  317. }
  318. /// <summary>
  319. /// 注销
  320. /// </summary>
  321. /// <param name="sender"></param>
  322. /// <param name="e"></param>
  323. private void Button_LogOut(object sender, RoutedEventArgs e)
  324. {
  325. myMenu.Visibility = Visibility.Hidden;
  326. myPanel.Visibility = Visibility.Visible;
  327. }
  328. #endregion
  329. private void DeviceMonitor_Click(object sender, RoutedEventArgs e)
  330. {
  331. string deviceName = string.Empty;
  332. var result = Plugin.GetInstance().GetPlugin<ConfigMgr>()?.deviceConfigModelJsons;
  333. if (result != null)
  334. {
  335. foreach (var shop in result)//店铺集合
  336. {
  337. deviceName = shop.deviceModels[0].DeviceName;
  338. }
  339. }
  340. if(deviceName != null)
  341. {
  342. Type type = Assembly.Load($"BPASmartClient.{deviceName}").GetType($"BPASmartClient.{deviceName}.View.DebugView");
  343. if (type == null) return;
  344. ConstructorInfo cti = type.GetConstructor(System.Type.EmptyTypes);
  345. contentRegion.Content = (FrameworkElement)cti.Invoke(null);
  346. Title.Text = "设备监控界面";
  347. }
  348. }
  349. }
  350. }