diff --git a/BPASmartClient.IoT/BPASmartClient.IoT.csproj b/BPASmartClient.IoT/BPASmartClient.IoT.csproj index 1f09b04b..0f2a2042 100644 --- a/BPASmartClient.IoT/BPASmartClient.IoT.csproj +++ b/BPASmartClient.IoT/BPASmartClient.IoT.csproj @@ -1,19 +1,19 @@  - - net6.0 - + + net6.0 + - - - - - - + + + + + + - - - - + + + + diff --git a/BPASmartClient.IoT/DataVClient.cs b/BPASmartClient.IoT/DataVClient.cs index d4412574..d16c12f4 100644 --- a/BPASmartClient.IoT/DataVClient.cs +++ b/BPASmartClient.IoT/DataVClient.cs @@ -211,12 +211,21 @@ namespace BPASmartClient.IoT /// public void UpDataFile() { - FileUpload.FileRequest(DeviceDataV); + try + { + if (DeviceDataV != null && DeviceDataV.GetIsConnected() && DeviceDataV.deviceTable != null) + { + FileUpload.FileRequest(DeviceDataV); + } + } + catch (Exception ex) + { + MessageLog.GetInstance.Show(ex.Message); + } } #endregion #region 私有 - /// /// 增加告警 /// @@ -284,16 +293,23 @@ namespace BPASmartClient.IoT string uploadId = result.data?.uploadId; FileUpload.SetUploadData(result.id,uploadId); FileUpload.FileSend(DeviceDataV, uploadId); + MessageLog.GetInstance.Show($"请求上传【阿里云】成功,准备上传日志文件【HBL.LogDir{DateTime.Now.ToString("yyyy_M_d")}.log】"); } + else + MessageLog.GetInstance.Show($"【HBL.LogDir{DateTime.Now.ToString("yyyy_M_d")}.log】请求上传【阿里云】失败,{result.message}"); + } else if (DeviceDataV.FileUpLoadSendReplyTopic == topic)//文件上传Topic { FileUploadModelResult result = Tools.JsonToObjectTools(message); if (result.code == 200) { + MessageLog.GetInstance.Show($"今日日志文件“HBL.LogDir{DateTime.Now.ToString("yyyy_M_d")}.log”更新上传【阿里云】成功"); bool complete = result.data.complete; string uploadId = result.data?.uploadId; - } + }else + MessageLog.GetInstance.Show($"今日日志文件“HBL.LogDir{DateTime.Now.ToString("yyyy_M_d")}.log”更新上传【阿里云】失败,{result.message}"); + } else if (DeviceDataV.CancelFileUpLoadSendReplyTopic == topic)//取消文件上传Topic { @@ -315,8 +331,6 @@ namespace BPASmartClient.IoT return property.First().GetValue(info, null); } #endregion - - } //命令实体类 diff --git a/BPASmartClient.IoT/Model/FileUpload.cs b/BPASmartClient.IoT/Model/FileUpload.cs index 97d14551..8407b5a5 100644 --- a/BPASmartClient.IoT/Model/FileUpload.cs +++ b/BPASmartClient.IoT/Model/FileUpload.cs @@ -1,4 +1,5 @@ -using BPASmartDatavDeviceClient.IoT; +using BPASmartClient.Helper; +using BPASmartDatavDeviceClient.IoT; using DataVAPI.Tool.IOT; using System; using System.Collections.Generic; @@ -14,51 +15,51 @@ namespace BPASmartClient.IoT.Model /// public class FileUpload { - public static Dictionary UploadData = new Dictionary(); - public static string FileName=$"HBL.LogDir{DateTime.Now.AddDays(-2).ToString("yyyy_M_d")}"; + public static Dictionary UploadData = new Dictionary(); + public static string FileName = $"HBL.LogDir{DateTime.Now.ToString("yyyy_M_d")}"; public static string path = $"{System.AppDomain.CurrentDomain.BaseDirectory}LogDir\\{FileName}.log"; - public static void SetUploadData(string key,string value) + public static void SetUploadData(string key, string value) { UploadData[key] = value; } public static string GetUploadData(string key) { - return UploadData.ContainsKey(key)? UploadData[key]:string.Empty; + return UploadData.ContainsKey(key) ? UploadData[key] : string.Empty; } /// /// 文件请求上传 /// public static void FileRequest(DataVReport dataV) { - long length= new FileInfo(path).Length; - byte[] FileBlock = File.ReadAllBytes(path); - FileUploadModel fileUpload=new FileUploadModel(); + long length = new FileInfo(path).Length; + byte[] FileBlock = FileContent(path); + FileUploadModel fileUpload = new FileUploadModel(); fileUpload.@params.fileName = FileName; fileUpload.@params.fileSize = length; fileUpload.@params.conflictStrategy = "overwrite";//覆盖模式 //fileUpload.@params.ficMode = "crc64"; //fileUpload.@params.ficValue = CRC.ToCRC16(FileBlock); fileUpload.@params.initUid = $"ab{RandomHelper.GenerateRandomCode()}"; - + //上传到阿里云物联网平台的OSS存储空间中 fileUpload.@params.extraParams.ossOwnerType = "iot-platform"; - + //表示上传到设备所属用户自己的OSS存储空间中 //{ossbucket}/aliyun-iot-device-file/${instanceId}/${productKey}/${serviceId}/${deviceName}/${fileName} //fileUpload.@params.extraParams.ossOwnerType = "device-user"; //fileUpload.@params.extraParams.serviceId = "black"; //fileUpload.@params.extraParams.fileTag = new Dictionary { {"Time", DateTime.Now.ToString("yyyy_M_d") },{"Name", "HBL.LogDir" } }; - - dataV.IOT_Publish(dataV.FileUpLoadTopic,Tools.JsonConvertTools(fileUpload)); + + dataV.IOT_Publish(dataV.FileUpLoadTopic, Tools.JsonConvertTools(fileUpload)); } /// /// 文件上传 /// - public static void FileSend(DataVReport dataV,string uploadId) + public static void FileSend(DataVReport dataV, string uploadId) { long length = new FileInfo(path).Length; - FileSendModel fileSend=new FileSendModel(); + FileSendModel fileSend = new FileSendModel(); fileSend.@params.uploadId = uploadId; fileSend.@params.offset = 0; fileSend.@params.bSize = length; @@ -67,9 +68,9 @@ namespace BPASmartClient.IoT.Model byte[] Header = Encoding.UTF8.GetBytes(Tools.JsonConvertTools(fileSend)); byte HeaderLen_L = (byte)(Header.Length); byte HeaderLen_H = (byte)(Header.Length >> 8); - byte[] FileBlock = File.ReadAllBytes(path);// ReadFile(path); + byte[] FileBlock = FileContent(path); byte[] CRC16 = CRC16Standard.getCRCBytes(FileBlock);//CRC.CRC16(FileBlock); - List message = new List() { HeaderLen_H, HeaderLen_L}; + List message = new List() { HeaderLen_H, HeaderLen_L }; message.AddRange(Header); message.AddRange(FileBlock); message.AddRange(CRC16); @@ -79,12 +80,34 @@ namespace BPASmartClient.IoT.Model /// /// 取消文件上传 /// - public static void FileCancelSend(DataVReport dataV) + public static void FileCancelSend(DataVReport dataV, string uploadId) { - //CancelSend cancel = new CancelSend(); - //dataV.IOT_Publish(dataV.CancelFileUpLoadSendTopic, Tools.JsonConvertTools(cancel)); + CancelSend cancel = new CancelSend(uploadId); + dataV.IOT_Publish(dataV.CancelFileUpLoadSendTopic, Tools.JsonConvertTools(cancel)); } + + /// + /// 读取文件 + /// + /// + /// + private static byte[] FileContent(string fileName) + { + using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) + { + try + { + byte[] buffur = new byte[fs.Length]; + fs.Read(buffur, 0, (int)fs.Length); + return buffur; + } + catch (Exception ex) + { + return new byte[0]; + } + } + } /// /// 读取文件字节 /// @@ -171,7 +194,7 @@ namespace BPASmartClient.IoT.Model /// /// 设备上传文件至OSS存储空间的配置参数 /// - public ConfigureParameters extraParams { get; set; }=new ConfigureParameters(); + public ConfigureParameters extraParams { get; set; } = new ConfigureParameters(); } /// /// 配置参数 @@ -187,14 +210,14 @@ namespace BPASmartClient.IoT.Model /// /// 文件上传相关的业务ID /// - public string serviceId { get; set; } + public string serviceId { get; set; } /// /// 标签 /// public Dictionary fileTag { get; set; } public ConfigureParameters() { - + } } #endregion @@ -222,7 +245,7 @@ namespace BPASmartClient.IoT.Model /// 返回设备端的数据。 /// public ResultData data { get; set; } - + } /// /// 请求响应数据 @@ -323,7 +346,7 @@ namespace BPASmartClient.IoT.Model public CancelSend(string uploadId) { - id= RandomHelper.GenerateRandomCode(); + id = RandomHelper.GenerateRandomCode(); @params = new { uploadId = uploadId }; } } @@ -336,7 +359,7 @@ namespace BPASmartClient.IoT.Model /// /// /// - public static string GenerateRandomCode(int length=10) + public static string GenerateRandomCode(int length = 10) { var result = new StringBuilder(); for (var i = 0; i < length; i++) diff --git a/BPASmartClient.ViewModel/LogViewModel.cs b/BPASmartClient.ViewModel/LogViewModel.cs index 0d137903..c411a2be 100644 --- a/BPASmartClient.ViewModel/LogViewModel.cs +++ b/BPASmartClient.ViewModel/LogViewModel.cs @@ -26,6 +26,9 @@ namespace BPASmartClient.ViewModel public class LogViewModel : ObservableObject { #region 变量 + //定时上报文件到阿里云 + public DispatcherTimer UpDataFileTimer; + //定时清除界面日志 public DispatcherTimer dispatcherTimer; public string ClientId = System.Configuration.ConfigurationManager.AppSettings["ClientId"].ToString(); public ObservableCollection LogDataGridData { get; set; } @@ -231,6 +234,14 @@ namespace BPASmartClient.ViewModel }; dispatcherTimer.Interval = TimeSpan.FromSeconds(10); dispatcherTimer.Start(); + + UpDataFileTimer = new DispatcherTimer(); + UpDataFileTimer.Tick += delegate + { + DataVClient.GetInstance().UpDataFile(); + }; + UpDataFileTimer.Interval = TimeSpan.FromHours(2); + UpDataFileTimer.Start(); } /// /// 增加日志 diff --git a/BPASmartClient/App.config b/BPASmartClient/App.config index 5de4098d..0004434b 100644 --- a/BPASmartClient/App.config +++ b/BPASmartClient/App.config @@ -3,7 +3,7 @@ - + diff --git a/BPASmartClient/MainWindow.xaml.cs b/BPASmartClient/MainWindow.xaml.cs index 9643449a..cfb9fd7a 100644 --- a/BPASmartClient/MainWindow.xaml.cs +++ b/BPASmartClient/MainWindow.xaml.cs @@ -219,8 +219,6 @@ namespace BPASmartClient NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, this, "我是标题", "我是消息内容!我是消息内容!我是消息内容!我是消息内容!"); NoticeDemoViewModel.OpenMsg(EnumPromptType.Warn, this, "我是标题", "我是消息内容!我是消息内容!我是消息内容!我是消息内容!"); NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, this, "我是标题", "我是消息内容!我是消息内容!我是消息内容!我是消息内容!"); - - DataVClient.GetInstance().UpDataFile(); } #endregion