leon object method

master
ZGGSONG 3 years ago
parent f4de4981ba
commit 8bbe308474

@ -0,0 +1,60 @@
using System;
using Ksat.Logging;
using Ksat.Supplyment.Library.Uploader;
using Ksat.Supplyment.Library.Model.Uploader;
using S3Demo.Model.Uploader;
namespace S3Demo.Uploader
{
public class S3UploadOperator : IUploaderOperator<string>
{
//Log
private ILogger sLog = LoggerFactory.ForContext<S3UploadOperator>();
public string ConvertCacheToRequest(string cache)
{
return cache;
}
public string ConvertRequestToCachel(string requestData)
{
return requestData;
}
public bool ShouldRetryRequest(UploadModel<string> requestModel, out string cancelReason)
{
if (DateTime.Now.Subtract(requestModel.CreateAt).TotalSeconds > 600)
{
cancelReason = "exceed 600s";
return false;
}
if (requestModel.RetryCount > 5)
{
cancelReason = "exceed 5";
return false;
}
cancelReason = null;
return true;
}
public bool ShouldStartRequest(UploadModel<string> requestModel, out string cancelReason)
{
if (DateTime.Now.Subtract(requestModel.CreateAt).TotalSeconds > 600)
{
cancelReason = "exceed 600s";
return false;
}
if (requestModel.RetryCount > 5)
{
cancelReason = "exceed 5";
return false;
}
cancelReason = null;
return true;
}
public IUploaderRequest<string> UploadData(string data)
{
return new S3UploadRequest() { RequestData = data };
}
}
}

@ -0,0 +1,32 @@
using System;
using S3Demo.Uploader;
using Ksat.Supplyment.Library.Uploader;
using Ksat.Supplyment.Library.Model.Uploader;
using Ksat.Logging;
namespace S3Demo.Model.Uploader
{
public class S3UploadRequest : IUploaderRequest<string>
{
//Log
private ILogger sLog = LoggerFactory.ForContext<S3UploadOperator>();
public string RequestData { get; set; }
public event UploadFinishHandler UploadFinish;
public void Cancel()
{
//TODO:
}
public void Start()
{
var ran = new Random();
//TODO:
System.Threading.Thread.Sleep(1000);
UploadFinish(this, ran.Next() % 3 == 0, "No reason");
}
}
}

@ -0,0 +1,28 @@
using System;
using Newtonsoft.Json;
using Ksat.Supplyment.Library.Uploader;
using S3Demo.Uploader;
namespace S3Demo.Model.Uploader
{
public class S3Uploader
{
private static Uploader<string> mUploader;
private static S3UploadOperator mOperator;
public S3Uploader(string UploadID)
{
mOperator = new S3UploadOperator();
mUploader = new Uploader<string>(UploadID, mOperator);
}
public void AddUploadTask(string reqData, string tag)
{
mUploader.AddUploadRequest(reqData, tag);
}
public void Dispose()
{
mUploader.Dispose();
}
}
}

@ -5,6 +5,7 @@ using Amazon;
using Amazon.Runtime; using Amazon.Runtime;
using Amazon.S3; using Amazon.S3;
using S3Demo.Model; using S3Demo.Model;
using S3Demo.Model.Uploader;
using Ksat.Supplyment.Library.Model; using Ksat.Supplyment.Library.Model;
using Ksat.Supplyment.Library.Uploader; using Ksat.Supplyment.Library.Uploader;
using Ksat.Supplyment.Library.Model.Uploader; using Ksat.Supplyment.Library.Model.Uploader;
@ -19,7 +20,7 @@ namespace S3Demo
private static BasicAWSCredentials credentials = new BasicAWSCredentials("admin", "admin123."); private static BasicAWSCredentials credentials = new BasicAWSCredentials("admin", "admin123.");
//private static BasicAWSCredentials credentials = new BasicAWSCredentials("Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG"); //private static BasicAWSCredentials credentials = new BasicAWSCredentials("Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG");
private static string bucketName = "minio/test"; private static string bucketName = "minio/test";
// 受监控的目录,完整目录如:"D:\ClientDir\line01\AOI\20211129\133055851SN001OK.png" // 受监控的目录,完整目录如:"D:\ClientDir\line01\AOI\20211129\133055851SN001.png"
private static string[] paths = { @"D:\ClientDir" }; private static string[] paths = { @"D:\ClientDir" };
private static string suffix = "*.png"; private static string suffix = "*.png";
@ -222,38 +223,21 @@ namespace S3Demo
} }
} }
#endregion #endregion
/// <summary> /// <summary>
/// 创建文件事件 /// 创建文件
/// </summary> /// </summary>
/// <param name="sender"></param> /// <param name="sender"></param>
/// <param name="e"></param> /// <param name="e"></param>
/// <remarks>
/// 主要逻辑
/// </remarks>
private static async void OnCreated(object sender, FileSystemEventArgs e) private static async void OnCreated(object sender, FileSystemEventArgs e)
{ {
string path = e.FullPath; Waiting(e.FullPath);
Waiting(path);//等待文件创建完成 await DisposeFile(e.FullPath);
await DisposeFile(path);//处理文件
} }
/// <summary>
private static async Task DisposeFile(string path) /// 等待onCreated事件处理完成
{ /// </summary>
// D:\ClientDir\line01\AOI\20211129\085023891P107CN14T00001.png /// <param name="path">被创建的文件路径</param>
string lineName = Path.GetDirectoryName(path).Split('\\')[2];
string equipmentName = Path.GetDirectoryName(path).Split('\\')[3];
string date = Path.GetDirectoryName(path).Split('\\')[4];
string fileName = Path.GetFileName(path);
string datetime = date + fileName.Substring(0, 9);
string dt_fomat = DateTime.ParseExact(datetime, "yyyyMMddHHmmssfff", System.Globalization.CultureInfo.CurrentCulture).ToString("yyyy-MM-dd HH:mm:ss:fff");
string sn = fileName.Substring(9, 14);
string now = DateTime.Now.ToString("yyyyMMddHHmmssfff");
String[] tags = new String[] { sn, dt_fomat };
string S3Path = lineName + "/" + equipmentName + "/" + date + "/" + fileName;
string recv = await ObjectModel.PutObjectsWithTagsAsync(client, bucketName, S3Path, path, tags);
Console.WriteLine(recv);
}
private static void Waiting(string path) private static void Waiting(string path)
{ {
try try
@ -273,6 +257,30 @@ namespace S3Demo
catch { } catch { }
} }
/// <summary>
/// 处理文件
/// </summary>
/// <param name="path">文件路径</param>
/// <returns></returns>
private static async Task DisposeFile(string path)
{
// D:\ClientDir\line01\AOI\20211129\085023891P107CN14T00001.png
string lineName = Path.GetDirectoryName(path).Split('\\')[2];
string equipmentName = Path.GetDirectoryName(path).Split('\\')[3];
string date = Path.GetDirectoryName(path).Split('\\')[4];
string fileName = Path.GetFileName(path);
string datetime = date + fileName.Substring(0, 9);
string dt_fomat = DateTime.ParseExact(datetime, "yyyyMMddHHmmssfff", System.Globalization.CultureInfo.CurrentCulture).ToString("yyyy-MM-dd HH:mm:ss:fff");
string sn = fileName.Substring(9, 14);
string now = DateTime.Now.ToString("yyyyMMddHHmmssfff");
String[] tags = new String[] { sn, dt_fomat };
string S3Path = lineName + "/" + equipmentName + "/" + date + "/" + fileName;
string recv = await ObjectModel.PutObjectsWithTagsAsync(client, bucketName, S3Path, path, tags);
Console.WriteLine("recv from S3: " + recv);
//引入统一上传方式
//new S3Uploader(lineName).AddUploadTask(S3Path, sn);
}
/// <summary> /// <summary>
/// 出现错误 /// 出现错误
/// </summary> /// </summary>

Binary file not shown.
Loading…
Cancel
Save