终端一体化运控平台
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.
 
 
 

288 lines
11 KiB

  1. using BPASmartClient.Business;
  2. using BPASmartClient.CustomResource.UserControls;
  3. using BPASmartClient.CustomResource.UserControls.MessageShow;
  4. using BPASmartClient.Device;
  5. using BPA.Helper;
  6. using BPASmartClient.IoT;
  7. using BPASmartClient.Peripheral;
  8. using BPASmartClient.ViewModel;
  9. using Newtonsoft.Json;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Reflection;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using System.Windows;
  18. using System.Windows.Controls;
  19. using System.Windows.Data;
  20. using System.Windows.Documents;
  21. using System.Windows.Input;
  22. using System.Windows.Media;
  23. using System.Windows.Media.Animation;
  24. using System.Windows.Media.Imaging;
  25. using System.Windows.Navigation;
  26. using System.Windows.Shapes;
  27. namespace BPASmartClient.MilkWithTea
  28. {
  29. /// <summary>
  30. /// Interaction logic for MainWindow.xaml
  31. /// </summary>
  32. public partial class MainWindow : Window
  33. {
  34. public MainConsole mainConsole;
  35. public MainWindow()
  36. {
  37. InitializeComponent();
  38. TextHelper.GetInstance.WriteTextInfo("MOC", "StartShop", "DeviceConfig");
  39. CreateDevice();
  40. //Initialize();
  41. }
  42. Storyboard sb = new Storyboard();
  43. DoubleAnimation yd1 = new DoubleAnimation();//实例化浮点动画
  44. DoubleAnimation yd2 = new DoubleAnimation();
  45. private void CreateDevice()
  46. {
  47. DirectoryInfo directoryInfo = new DirectoryInfo(LocaPath.GetDeviceConfigPath);
  48. if(!File.Exists($"{directoryInfo}Moc.json"))
  49. {
  50. File.WriteAllText($"{directoryInfo}Moc.json", JsonConvert.SerializeObject(GLobal.deviceConfig));
  51. }
  52. string JsonString = File.ReadAllText($"{directoryInfo}MOC.json");
  53. var result = JsonConvert.DeserializeObject<ObservableCollection<DeviceConfigModelJson>>(JsonString);
  54. if (result != null)
  55. {
  56. ShopDeviceConfigViewModel.deviceConfig.Clear();
  57. foreach (var item in result)
  58. {
  59. GLobal.deviceConfig.Add(item);
  60. }
  61. }
  62. if(GLobal.deviceConfig.Count <1)
  63. {
  64. GLobal.deviceConfig.Add(new DeviceConfigModelJson());
  65. if(GLobal.deviceConfig.ElementAtOrDefault(0).deviceModels.Count <1)
  66. {
  67. GLobal.deviceConfig.ElementAtOrDefault(0).deviceModels.Add(new DeviceModel());
  68. if(GLobal.deviceConfig.ElementAtOrDefault(0).deviceModels.ElementAtOrDefault(0).communicationDevcies.Count <1)
  69. {
  70. GLobal.deviceConfig.ElementAtOrDefault(0).deviceModels.ElementAtOrDefault(0).communicationDevcies.Add(new CommunicationModel());
  71. }
  72. }
  73. }
  74. if (GLobal.deviceConfig.ElementAt(0).deviceModels.ElementAt(0).communicationDevcies.ElementAt(0).variables.Count < 1)
  75. {
  76. for (int i = 0; i < 20; i++)
  77. {
  78. GLobal.deviceConfig.ElementAt(0).deviceModels.ElementAt(0).communicationDevcies.ElementAt(0).variables.Add(new Variable()
  79. {
  80. Id = GLobal.deviceConfig.ElementAt(0).deviceModels.ElementAt(0).communicationDevcies.ElementAt(0).variables.Count,
  81. Address = string.Empty,
  82. ReadLeng = 0
  83. });
  84. }
  85. File.WriteAllText($"{LocaPath.GetDeviceConfigPath}MOC.json", JsonConvert.SerializeObject(GLobal.deviceConfig));
  86. }
  87. }
  88. private void Initialize()
  89. {
  90. TaskManage.GetInstance.Start(new Action(() =>
  91. {
  92. GetDevices();
  93. mainConsole = new MainConsole();
  94. mainConsole.Start();
  95. }), "启动主控制台", false);
  96. ActionManage.GetInstance.Register(new Action(() =>
  97. {
  98. TaskManage.GetInstance.Start(new Action(() =>
  99. {
  100. try
  101. {
  102. DataVClient.GetInstance().Initialize();
  103. DataVClient.GetInstance().Start();
  104. }
  105. catch (Exception ex)
  106. {
  107. MessageLog.GetInstance.ShowEx(ex.ToString());
  108. }
  109. }), "启动主IoT", false);
  110. }), "配置初始化完成Iot启动");
  111. ActionManage.GetInstance.Register(new Action<object>((o) =>
  112. {
  113. App.Current.Dispatcher.Invoke(new Action(() =>
  114. {
  115. if (o is IOTCommandModel iot)
  116. {
  117. switch (iot.CommandName)
  118. {
  119. case 0://控制类
  120. if (iot.CommandValue != null && iot.CommandValue.Count > 0)
  121. {
  122. switch (iot.CommandValue.Keys.ToList()[0])
  123. {
  124. case "程序启动":
  125. //mainConsole.Start();
  126. break;
  127. //mainConsole.Stop();
  128. case "程序停止":
  129. break;
  130. case "程序复位":
  131. //mainConsole.Stop();
  132. //mainConsole.Start();
  133. break;
  134. default:
  135. break;
  136. }
  137. NoticeDemoViewModel.OpenMsg(EnumPromptType.Info, MainViewModel.GetInstance().window, "大屏控制", iot.CommandValue.Keys.ToList()[0]);
  138. }
  139. break;
  140. case 1://设置类
  141. break;
  142. case 2://通知类
  143. if (iot.CommandValue != null && iot.CommandValue.ContainsKey("text"))
  144. {
  145. NoticeDemoViewModel.OpenMsg(EnumPromptType.Info, MainViewModel.GetInstance().window, "大屏通知", iot.CommandValue["text"]);
  146. }
  147. break;
  148. default:
  149. break;
  150. }
  151. }
  152. }));
  153. }), "IotBroadcast");
  154. NavButton_Click(new Button() { Tag = "MainControlView" },null);
  155. }
  156. /// <summary>
  157. /// 获取设备集合
  158. /// </summary>
  159. private void GetDevices()
  160. {
  161. //List<string> IDevices = new List<string>();
  162. //List<string> IPeripherals = new List<string>();
  163. DirectoryInfo directoryInfo = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
  164. var files = directoryInfo.GetFiles().Where(p => p.FullName.Contains("BPASmartClient.") && p.FullName.Contains("dll")).ToList();
  165. List<string> fileList = new List<string>();
  166. var assemblies = AppDomain.CurrentDomain.GetAssemblies();
  167. var dlls = assemblies?.Where(p => p.FullName.Contains("BPASmartClient.")).ToList();
  168. List<string> dllList = new List<string>();
  169. if (files != null)
  170. {
  171. foreach (var item in files)
  172. {
  173. var res = System.IO.Path.GetFileNameWithoutExtension(item.FullName);
  174. if (res != null && res.Length > 0 && res.Contains(".") && !res.Contains("dll")) fileList.Add(res);
  175. }
  176. }
  177. if (dlls != null)
  178. {
  179. dlls.ForEach((item) =>
  180. {
  181. item.GetTypes().ToList().ForEach((type) =>
  182. {
  183. if (type.GetInterfaces().Contains(typeof(IDevice)))
  184. {
  185. if (!type.FullName.Contains("BaseDevice")) ShopDeviceConfigViewModel.IDevices.Add(type.FullName);
  186. }
  187. else if (type.GetInterfaces().Contains(typeof(IPeripheral)))
  188. {
  189. if (!type.FullName.Contains("BasePeripheral")) ShopDeviceConfigViewModel.IPeripherals.Add(type.FullName);
  190. }
  191. });
  192. dllList.Add(System.IO.Path.GetFileNameWithoutExtension(item.EscapedCodeBase));
  193. });
  194. }
  195. dllList.ForEach((item) => { if (fileList.Contains(item)) fileList.Remove(item); });
  196. fileList.ForEach((item) =>
  197. {
  198. Assembly.Load(item).GetTypes().ToList().ForEach((type) =>
  199. {
  200. if (type.GetInterfaces().Contains(typeof(IDevice)))
  201. {
  202. if (!type.FullName.Contains("BaseDevice")) ShopDeviceConfigViewModel.IDevices.Add(type.FullName);
  203. }
  204. else if (type.GetInterfaces().Contains(typeof(IPeripheral)))
  205. {
  206. if (!type.FullName.Contains("BasePeripheral")) ShopDeviceConfigViewModel.IPeripherals.Add(type.FullName);
  207. }
  208. });
  209. });
  210. }
  211. private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
  212. {
  213. this.DragMove();
  214. }
  215. private void CloseButton_Click(object sender, RoutedEventArgs e)
  216. {
  217. this.Close();
  218. }
  219. private void NavButton_Click(object sender, RoutedEventArgs e)
  220. {
  221. FadeInOut(0, 1, 750, 0);
  222. try
  223. {
  224. if (sender is Button bt )
  225. {
  226. Type type = Type.GetType($"BPASmartClient.MilkWithTea.View.{bt.Tag?.ToString()}");
  227. ConstructorInfo cti = type.GetConstructor(System.Type.EmptyTypes);
  228. contentRegion.Content = (FrameworkElement)cti.Invoke(null);
  229. sb.Begin();
  230. }
  231. }
  232. catch (Exception ex)
  233. {
  234. MessageLog.GetInstance.ShowEx($"BPASmartClient 中引发错误,MainWindow.xaml.cs 类MenuItem_Click(),描述:[{ex.Message}]");
  235. }
  236. }
  237. private void FadeInOut(double opactityFrom ,double opactityTo,double yForm,double yTo)
  238. {
  239. contentRegion.RenderTransform = new TranslateTransform();
  240. yd1.From = opactityFrom;//动画的起始值
  241. yd1.To = opactityTo;//动画的结束值
  242. yd1.Duration = TimeSpan.FromSeconds(0.8);
  243. Storyboard.SetTarget(yd1, contentRegion);//给故事板绑定动画
  244. Storyboard.SetTargetProperty(yd1, new PropertyPath("Opacity"));//动画的依赖属性
  245. yd2.From = yForm;//动画的起始值
  246. yd2.To = yTo;//动画的结束值
  247. yd2.Duration = TimeSpan.FromSeconds(0.5);
  248. Storyboard.SetTarget(yd2, contentRegion);//给故事板绑定动画
  249. Storyboard.SetTargetProperty(yd2, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.Y)"));//动画的依赖属性
  250. sb.Children.Add(yd1);//故事板添加动画
  251. sb.Children.Add(yd2);
  252. }
  253. }
  254. }