using BPASmartClient.Business; using BPASmartClient.CustomResource.UserControls; using BPASmartClient.CustomResource.UserControls.MessageShow; using BPASmartClient.Device; using BPA.Helper; using BPASmartClient.IoT; using BPASmartClient.Peripheral; using BPASmartClient.ViewModel; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace BPASmartClient.MilkWithTea { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { public MainConsole mainConsole; public MainWindow() { InitializeComponent(); TextHelper.GetInstance.WriteTextInfo("MOC", "StartShop", "DeviceConfig"); CreateDevice(); //Initialize(); } Storyboard sb = new Storyboard(); DoubleAnimation yd1 = new DoubleAnimation();//实例化浮点动画 DoubleAnimation yd2 = new DoubleAnimation(); private void CreateDevice() { DirectoryInfo directoryInfo = new DirectoryInfo(LocaPath.GetDeviceConfigPath); if(!File.Exists($"{directoryInfo}Moc.json")) { File.WriteAllText($"{directoryInfo}Moc.json", JsonConvert.SerializeObject(GLobal.deviceConfig)); } string JsonString = File.ReadAllText($"{directoryInfo}MOC.json"); var result = JsonConvert.DeserializeObject>(JsonString); if (result != null) { ShopDeviceConfigViewModel.deviceConfig.Clear(); foreach (var item in result) { GLobal.deviceConfig.Add(item); } } if(GLobal.deviceConfig.Count <1) { GLobal.deviceConfig.Add(new DeviceConfigModelJson()); if(GLobal.deviceConfig.ElementAtOrDefault(0).deviceModels.Count <1) { GLobal.deviceConfig.ElementAtOrDefault(0).deviceModels.Add(new DeviceModel()); if(GLobal.deviceConfig.ElementAtOrDefault(0).deviceModels.ElementAtOrDefault(0).communicationDevcies.Count <1) { GLobal.deviceConfig.ElementAtOrDefault(0).deviceModels.ElementAtOrDefault(0).communicationDevcies.Add(new CommunicationModel()); } } } if (GLobal.deviceConfig.ElementAt(0).deviceModels.ElementAt(0).communicationDevcies.ElementAt(0).variables.Count < 1) { for (int i = 0; i < 20; i++) { GLobal.deviceConfig.ElementAt(0).deviceModels.ElementAt(0).communicationDevcies.ElementAt(0).variables.Add(new Variable() { Id = GLobal.deviceConfig.ElementAt(0).deviceModels.ElementAt(0).communicationDevcies.ElementAt(0).variables.Count, Address = string.Empty, ReadLeng = 0 }); } File.WriteAllText($"{LocaPath.GetDeviceConfigPath}MOC.json", JsonConvert.SerializeObject(GLobal.deviceConfig)); } } private void Initialize() { TaskManage.GetInstance.Start(new Action(() => { GetDevices(); mainConsole = new MainConsole(); mainConsole.Start(); }), "启动主控制台", false); ActionManage.GetInstance.Register(new Action(() => { TaskManage.GetInstance.Start(new Action(() => { try { DataVClient.GetInstance.Initialize(); DataVClient.GetInstance.Start(); } catch (Exception ex) { MessageLog.GetInstance.ShowEx(ex.ToString()); } }), "启动主IoT", false); }), "配置初始化完成Iot启动"); ActionManage.GetInstance.Register(new Action((o) => { App.Current.Dispatcher.Invoke(new Action(() => { if (o is IOTCommandModel iot) { switch (iot.CommandName) { case 0://控制类 if (iot.CommandValue != null && iot.CommandValue.Count > 0) { switch (iot.CommandValue.Keys.ToList()[0]) { case "程序启动": //mainConsole.Start(); break; //mainConsole.Stop(); case "程序停止": break; case "程序复位": //mainConsole.Stop(); //mainConsole.Start(); break; default: break; } NoticeDemoViewModel.OpenMsg(EnumPromptType.Info, MainViewModel.GetInstance.window, "大屏控制", iot.CommandValue.Keys.ToList()[0]); } break; case 1://设置类 break; case 2://通知类 if (iot.CommandValue != null && iot.CommandValue.ContainsKey("text")) { NoticeDemoViewModel.OpenMsg(EnumPromptType.Info, MainViewModel.GetInstance.window, "大屏通知", iot.CommandValue["text"]); } break; default: break; } } })); }), "IotBroadcast"); NavButton_Click(new Button() { Tag = "MainControlView" },null); } /// /// 获取设备集合 /// private void GetDevices() { //List IDevices = new List(); //List IPeripherals = new List(); DirectoryInfo directoryInfo = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory); var files = directoryInfo.GetFiles().Where(p => p.FullName.Contains("BPASmartClient.") && p.FullName.Contains("dll")).ToList(); List fileList = new List(); var assemblies = AppDomain.CurrentDomain.GetAssemblies(); var dlls = assemblies?.Where(p => p.FullName.Contains("BPASmartClient.")).ToList(); List dllList = new List(); if (files != null) { foreach (var item in files) { var res = System.IO.Path.GetFileNameWithoutExtension(item.FullName); if (res != null && res.Length > 0 && res.Contains(".") && !res.Contains("dll")) fileList.Add(res); } } if (dlls != null) { dlls.ForEach((item) => { item.GetTypes().ToList().ForEach((type) => { if (type.GetInterfaces().Contains(typeof(IDevice))) { if (!type.FullName.Contains("BaseDevice")) ShopDeviceConfigViewModel.IDevices.Add(type.FullName); } else if (type.GetInterfaces().Contains(typeof(IPeripheral))) { if (!type.FullName.Contains("BasePeripheral")) ShopDeviceConfigViewModel.IPeripherals.Add(type.FullName); } }); dllList.Add(System.IO.Path.GetFileNameWithoutExtension(item.EscapedCodeBase)); }); } dllList.ForEach((item) => { if (fileList.Contains(item)) fileList.Remove(item); }); fileList.ForEach((item) => { Assembly.Load(item).GetTypes().ToList().ForEach((type) => { if (type.GetInterfaces().Contains(typeof(IDevice))) { if (!type.FullName.Contains("BaseDevice")) ShopDeviceConfigViewModel.IDevices.Add(type.FullName); } else if (type.GetInterfaces().Contains(typeof(IPeripheral))) { if (!type.FullName.Contains("BasePeripheral")) ShopDeviceConfigViewModel.IPeripherals.Add(type.FullName); } }); }); } private void Grid_MouseDown(object sender, MouseButtonEventArgs e) { this.DragMove(); } private void CloseButton_Click(object sender, RoutedEventArgs e) { this.Close(); } private void NavButton_Click(object sender, RoutedEventArgs e) { FadeInOut(0, 1, 750, 0); try { if (sender is Button bt ) { Type type = Type.GetType($"BPASmartClient.MilkWithTea.View.{bt.Tag?.ToString()}"); ConstructorInfo cti = type.GetConstructor(System.Type.EmptyTypes); contentRegion.Content = (FrameworkElement)cti.Invoke(null); sb.Begin(); } } catch (Exception ex) { MessageLog.GetInstance.ShowEx($"BPASmartClient 中引发错误,MainWindow.xaml.cs 类MenuItem_Click(),描述:[{ex.Message}]"); } } private void FadeInOut(double opactityFrom ,double opactityTo,double yForm,double yTo) { contentRegion.RenderTransform = new TranslateTransform(); yd1.From = opactityFrom;//动画的起始值 yd1.To = opactityTo;//动画的结束值 yd1.Duration = TimeSpan.FromSeconds(0.8); Storyboard.SetTarget(yd1, contentRegion);//给故事板绑定动画 Storyboard.SetTargetProperty(yd1, new PropertyPath("Opacity"));//动画的依赖属性 yd2.From = yForm;//动画的起始值 yd2.To = yTo;//动画的结束值 yd2.Duration = TimeSpan.FromSeconds(0.5); Storyboard.SetTarget(yd2, contentRegion);//给故事板绑定动画 Storyboard.SetTargetProperty(yd2, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.Y)"));//动画的依赖属性 sb.Children.Add(yd1);//故事板添加动画 sb.Children.Add(yd2); } } }