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