From 9076545d0b59701f8880088b535b987b8761cddf Mon Sep 17 00:00:00 2001 From: fyf Date: Mon, 23 May 2022 16:34:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=91=E7=AB=AF=E6=97=A5?= =?UTF-8?q?=E5=BF=97=20=E4=B8=8B=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BPASmartClient.IoT/Model/IOT/IOTDevServer.cs | 26 ++++ .../LogOrAlarmIOTViewModel.cs | 115 ++++++++++++++++++ .../LogOrAlarmViewModel.cs | 10 -- BPASmartClient/Control/LogOrAlarmIOTView.xaml | 74 +++++++++++ .../Control/LogOrAlarmIOTView.xaml.cs | 70 +++++++++++ BPASmartClient/Control/LogOrAlarmView.xaml.cs | 10 -- BPASmartClient/MainWindow.xaml | 6 +- 7 files changed, 288 insertions(+), 23 deletions(-) create mode 100644 BPASmartClient.ViewModel/LogOrAlarmIOTViewModel.cs create mode 100644 BPASmartClient/Control/LogOrAlarmIOTView.xaml create mode 100644 BPASmartClient/Control/LogOrAlarmIOTView.xaml.cs diff --git a/BPASmartClient.IoT/Model/IOT/IOTDevServer.cs b/BPASmartClient.IoT/Model/IOT/IOTDevServer.cs index 98de2203..2b9ee391 100644 --- a/BPASmartClient.IoT/Model/IOT/IOTDevServer.cs +++ b/BPASmartClient.IoT/Model/IOT/IOTDevServer.cs @@ -5,6 +5,7 @@ using BPASmartClient.IoT; using Newtonsoft.Json; using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Net; using System.Net.Sockets; @@ -461,6 +462,31 @@ namespace DataVAPI.Tool.IOT } return "192.168.1.124"; } + + /// + /// Http下载文件 + /// + public static string HttpDownloadFile(string url, string path) + { + // 设置参数 + HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; + //发送请求并获取相应回应数据 + HttpWebResponse response = request.GetResponse() as HttpWebResponse; + //直到request.GetResponse()程序才开始向目标网页发送Post请求 + Stream responseStream = response.GetResponseStream(); + //创建本地文件写入流 + Stream stream = new FileStream(path, FileMode.Create); + byte[] bArr = new byte[1024]; + int size = responseStream.Read(bArr, 0, (int)bArr.Length); + while (size > 0) + { + stream.Write(bArr, 0, size); + size = responseStream.Read(bArr, 0, (int)bArr.Length); + } + stream.Close(); + responseStream.Close(); + return path; + } } diff --git a/BPASmartClient.ViewModel/LogOrAlarmIOTViewModel.cs b/BPASmartClient.ViewModel/LogOrAlarmIOTViewModel.cs new file mode 100644 index 00000000..032b9249 --- /dev/null +++ b/BPASmartClient.ViewModel/LogOrAlarmIOTViewModel.cs @@ -0,0 +1,115 @@ +using BPASmartClient.CustomResource.UserControls; +using BPASmartClient.CustomResource.UserControls.MessageShow; +using BPASmartClient.Helper; +using BPASmartClient.IoT; +using DataVAPI.Tool.IOT; +using Microsoft.Toolkit.Mvvm.ComponentModel; +using Microsoft.Toolkit.Mvvm.Input; +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Media; + +namespace BPASmartClient.ViewModel +{ + public class LogOrAlarmIOTViewModel : ObservableObject + { + + #region 单一变量 + private volatile static LogOrAlarmIOTViewModel _Instance; + public static LogOrAlarmIOTViewModel GetInstance() => _Instance ?? (_Instance = new LogOrAlarmIOTViewModel()); + private LogOrAlarmIOTViewModel() + { + Init(); + } + #endregion + #region 变量 + public string api = "https://bpa.black-pa.com:21527/datav/api/Log/QueryLogFile?DeviceName="; + + private ObservableCollection _LogModels; public ObservableCollection LogDataFile + { + get + { + return _LogModels; + } + set + { + if (_LogModels == value) + return; + _LogModels = value; + OnPropertyChanged("LogDataFile"); + } + } + #endregion + #region Command + public RelayCommand QueryCommand { get; set; } + #endregion + + #region 函数 + public void Init() + { + LogDataFile = new ObservableCollection(); + //查询 + QueryCommand = new RelayCommand(() => + { + Res(); + }); + } + + public void Res() + { + if (DataVClient.GetInstance().DeviceDataV != null && DataVClient.GetInstance().DeviceDataV.deviceTable != null) + { + try + { + LogDataFile.Clear(); + api = $"https://bpa.black-pa.com:21527/datav/api/Log/QueryLogFile?DeviceName={DataVClient.GetInstance().DeviceDataV.deviceTable.devicename}"; ; + string json = HttpRequestHelper.HttpGetRequest(api, 1000); + JsonMsg> jsonMsg = Tools.JsonToObjectTools>>(json); + jsonMsg.obj?.data?.ForEach(file => + { + LogDataFile.Add(file); + }); + NoticeDemoViewModel.OpenMsg(EnumPromptType.Info, MainViewModel.GetInstance().window, "提示", $"查询成功,文件数{LogDataFile.Count}!"); + } + catch (Exception ex) + { + NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, MainViewModel.GetInstance().window, "提示", $"查询失败!"); + } + + } + } + #endregion + } + + public class FileModel : ObservableObject + { + public string downloadUrl { get; set; } + public string fileId { get; set; } + public string name { get; set; } + public string size { get; set; } + public string utcCreatedOn { get; set; } + private Brush _foreground; + public Brush foreground + { + get + { + return _foreground; + } + set + { + if (_foreground == value) + return; + _foreground = value; + OnPropertyChanged("foreground"); + } + } + public FileModel() + { + foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#21bb2e")); + } + } +} diff --git a/BPASmartClient.ViewModel/LogOrAlarmViewModel.cs b/BPASmartClient.ViewModel/LogOrAlarmViewModel.cs index 6ed1ed0a..c56566db 100644 --- a/BPASmartClient.ViewModel/LogOrAlarmViewModel.cs +++ b/BPASmartClient.ViewModel/LogOrAlarmViewModel.cs @@ -186,16 +186,6 @@ namespace BPASmartClient.ViewModel }); } - /// - /// 查询告警或日志 - /// - /// - /// - public void QueryLogOrAlarm(DateTime time, string info) - { - - } - /// /// 读取文件 /// diff --git a/BPASmartClient/Control/LogOrAlarmIOTView.xaml b/BPASmartClient/Control/LogOrAlarmIOTView.xaml new file mode 100644 index 00000000..2ec845cc --- /dev/null +++ b/BPASmartClient/Control/LogOrAlarmIOTView.xaml @@ -0,0 +1,74 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/BPASmartClient/Control/LogOrAlarmIOTView.xaml.cs b/BPASmartClient/Control/LogOrAlarmIOTView.xaml.cs new file mode 100644 index 00000000..17370a7f --- /dev/null +++ b/BPASmartClient/Control/LogOrAlarmIOTView.xaml.cs @@ -0,0 +1,70 @@ +using BPASmartClient.CustomResource.UserControls; +using BPASmartClient.CustomResource.UserControls.MessageShow; +using BPASmartClient.Helper; +using BPASmartClient.ViewModel; +using DataVAPI.Tool.IOT; +using Microsoft.Win32; +using System; +using System.Collections.Generic; +using System.Linq; +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.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace BPASmartClient.Control +{ + /// + /// LogOrAlarmIOTView.xaml 的交互逻辑 + /// + public partial class LogOrAlarmIOTView : UserControl + { + public LogOrAlarmIOTView() + { + InitializeComponent(); + this.DataContext = LogOrAlarmIOTViewModel.GetInstance(); + } + /// + /// 下载文件 + /// + /// + /// + private void Button_Click(object sender, RoutedEventArgs e) + { + try + { + Button button = (Button)sender; + if (button.Tag != null) + { + string url=button.Tag.ToString(); + SaveFileDialog openfile = new SaveFileDialog(); + openfile.Filter = "log文件(*.log)|*.log"; + openfile.FileName = button.ToolTip.ToString(); + if (openfile.ShowDialog() == false) + { + return; + } + string path = openfile.FileName; + Tools.HttpDownloadFile(url,path); + NoticeDemoViewModel.OpenMsg(EnumPromptType.Info, MainViewModel.GetInstance().window, "提示", $"下载成功!"); + string msg = string.Format("下载成功,是否打开!"); + if (System.Windows.MessageBox.Show(msg, "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK) + { + logHelper.GetLogConfigInstance().OpenFile(openfile.FileName); + } + } + } + catch (Exception ex) + { + NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, MainViewModel.GetInstance().window, "提示", $"下载失败!"); + } + } + } +} diff --git a/BPASmartClient/Control/LogOrAlarmView.xaml.cs b/BPASmartClient/Control/LogOrAlarmView.xaml.cs index cae385b9..fb589b30 100644 --- a/BPASmartClient/Control/LogOrAlarmView.xaml.cs +++ b/BPASmartClient/Control/LogOrAlarmView.xaml.cs @@ -26,15 +26,5 @@ namespace BPASmartClient.Control InitializeComponent(); this.DataContext = LogOrAlarmViewModel.GetInstance(); } - - private void CheckBox_Checked(object sender, RoutedEventArgs e) - { - - } - - private void CheckBox_Unchecked(object sender, RoutedEventArgs e) - { - - } } } diff --git a/BPASmartClient/MainWindow.xaml b/BPASmartClient/MainWindow.xaml index bbaa780d..f86271e1 100644 --- a/BPASmartClient/MainWindow.xaml +++ b/BPASmartClient/MainWindow.xaml @@ -118,14 +118,14 @@ + Header="云端日志下载" + Tag="LogOrAlarmIOTView" />