Browse Source

本地日志文件上报阿里云

样式分支
fyf 2 years ago
parent
commit
bee9943612
6 changed files with 92 additions and 46 deletions
  1. +13
    -13
      BPASmartClient.IoT/BPASmartClient.IoT.csproj
  2. +19
    -5
      BPASmartClient.IoT/DataVClient.cs
  3. +48
    -25
      BPASmartClient.IoT/Model/FileUpload.cs
  4. +11
    -0
      BPASmartClient.ViewModel/LogViewModel.cs
  5. +1
    -1
      BPASmartClient/App.config
  6. +0
    -2
      BPASmartClient/MainWindow.xaml.cs

+ 13
- 13
BPASmartClient.IoT/BPASmartClient.IoT.csproj View File

@@ -1,19 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Aliyun.OSS.SDK" Version="2.13.0" />
<PackageReference Include="M2Mqtt" Version="4.3.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="6.0.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Aliyun.OSS.SDK" Version="2.13.0" />
<PackageReference Include="M2Mqtt" Version="4.3.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\BPASmartClient.Business\BPASmartClient.Business.csproj" />
<ProjectReference Include="..\BPASmartClient.Helper\BPASmartClient.Helper.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\BPASmartClient.Business\BPASmartClient.Business.csproj" />
<ProjectReference Include="..\BPASmartClient.Helper\BPASmartClient.Helper.csproj" />
</ItemGroup>

</Project>

+ 19
- 5
BPASmartClient.IoT/DataVClient.cs View File

@@ -211,12 +211,21 @@ namespace BPASmartClient.IoT
/// </summary>
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 私有

/// <summary>
/// 增加告警
/// </summary>
@@ -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<FileUploadModelResult>(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


}

//命令实体类


+ 48
- 25
BPASmartClient.IoT/Model/FileUpload.cs View File

@@ -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
/// </summary>
public class FileUpload
{
public static Dictionary<string, string> UploadData = new Dictionary<string, string>();
public static string FileName=$"HBL.LogDir{DateTime.Now.AddDays(-2).ToString("yyyy_M_d")}";
public static Dictionary<string, string> UploadData = new Dictionary<string, string>();
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;
}
/// <summary>
/// 文件请求上传
/// </summary>
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<string, string> { {"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));
}

/// <summary>
/// 文件上传
/// </summary>
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<byte> message = new List<byte>() { HeaderLen_H, HeaderLen_L};
List<byte> message = new List<byte>() { HeaderLen_H, HeaderLen_L };
message.AddRange(Header);
message.AddRange(FileBlock);
message.AddRange(CRC16);
@@ -79,12 +80,34 @@ namespace BPASmartClient.IoT.Model
/// <summary>
/// 取消文件上传
/// </summary>
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));
}


/// <summary>
/// 读取文件
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
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];
}
}
}
/// <summary>
/// 读取文件字节
/// </summary>
@@ -171,7 +194,7 @@ namespace BPASmartClient.IoT.Model
/// <summary>
/// 设备上传文件至OSS存储空间的配置参数
/// </summary>
public ConfigureParameters extraParams { get; set; }=new ConfigureParameters();
public ConfigureParameters extraParams { get; set; } = new ConfigureParameters();
}
/// <summary>
/// 配置参数
@@ -187,14 +210,14 @@ namespace BPASmartClient.IoT.Model
/// <summary>
/// 文件上传相关的业务ID
/// </summary>
public string serviceId { get; set; }
public string serviceId { get; set; }
/// <summary>
/// 标签
/// </summary>
public Dictionary<string, string> fileTag { get; set; }
public ConfigureParameters()
{
}
}
#endregion
@@ -222,7 +245,7 @@ namespace BPASmartClient.IoT.Model
/// 返回设备端的数据。
/// </summary>
public ResultData data { get; set; }
}
/// <summary>
/// 请求响应数据
@@ -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
/// </summary>
/// <param name="length"></param>
/// <returns></returns>
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++)


+ 11
- 0
BPASmartClient.ViewModel/LogViewModel.cs View File

@@ -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<LogModel> 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();
}
/// <summary>
/// 增加日志


+ 1
- 1
BPASmartClient/App.config View File

@@ -3,7 +3,7 @@
<appSettings>
<!--通用配置-->
<!--1:且时且多冰淇淋咖啡机,2:且时且多煮面机,3:海科煮面机测试店铺-->
<add key="ClientId" value="51"/>
<add key="ClientId" value="250"/>
<!--<add key="ApolloUri" value="http://10.2.1.21:28080"/>
<add key="OrderServiceUri" value="http://10.2.1.26:21527/order/"/>
<add key="StockServiceUri" value="http://10.2.1.26:21527/stock/"/>-->


+ 0
- 2
BPASmartClient/MainWindow.xaml.cs View File

@@ -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


Loading…
Cancel
Save