You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
7.1 KiB
C#

using Dapper;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;
using System.Threading.Tasks;
using Txgy.FilesWatcher.DBModels;
using static System.Windows.Forms.Design.AxImporter;
namespace Txgy.FilesWatcher.model
{
public class UploadPostproFile
{
public static void UploadPostproFileOnce(string path, string eventMessage, int workAreaId , out bool isUpload)
{
isUpload = false;
try
{
var currentTime = DateTime.Now;
var mseedFile = new DirectoryInfo(path).GetFiles("*.mseed");
var Dfiles = mseedFile.Where(f => eventMessage.Contains($"{f.Name.Substring(3, 4)}-{f.Name.Substring(7, 2)}-{f.Name.Substring(9, 2)}T{f.Name.Substring(12, 2)}:{f.Name.Substring(14, 2)}:{f.Name.Substring(16, 2)}"));
var files = Directory.GetFiles(path);
foreach (var dFile in Dfiles)
{
string file = dFile.Name;
string tbrealtimeresult = "postproeventresult";
string tbname = "postproeventwavedatas";
string uploadedtbname = "uploadedpostpro";
string sqlNumber = $"SELECT COUNT(*) FROM {uploadedtbname} WHERE filename = '{file}'";
//检查文件是否已经上传
using (var conn = DataBaseConnect.GetInstance.OpenConnection())
{
MySqlCommand cmd = new MySqlCommand(sqlNumber, conn);
var a = conn.QueryFirst<int>(sqlNumber);
if (a == 0)
{
string[] index_file_line = eventMessage.Split(" ",StringSplitOptions.RemoveEmptyEntries);
if (index_file_line[4]== "None")
{
index_file_line[5] = "-4";
}
string EventTime = index_file_line[0].Substring(0, 23);
string OriginTime = EventTime;
//WorkAreaId = 1,更新realtimeeventresult
sqlNumber = $"INSERT INTO {tbrealtimeresult}(WorkAreaId, EventTime, OriginTime, X, Y, Z, ML, LocSta, MLSta, RMS) VALUES('{workAreaId}', '{EventTime}', '{OriginTime}', '{index_file_line[1]}', '{index_file_line[2]}', '{index_file_line[3]}', '{index_file_line[5]}', '{index_file_line[7]}', '{index_file_line[8]}', '{index_file_line[6]}')";
var res = conn.Execute(sqlNumber);
isUpload = true;
// .mseed文件
FileStream fs = new FileStream(dFile.FullName, FileMode.Open, FileAccess.Read);
BinaryReader mbr = new BinaryReader(fs);
Byte[] mseedDatas = mbr.ReadBytes((int)fs.Length);
fs.Close();
//.json文件
string JsonPath = dFile.FullName.Replace(".mseed", "B.json");
fs = new FileStream(JsonPath, FileMode.Open, FileAccess.Read);
BinaryReader jbr = new BinaryReader(fs);
Byte[] jsonDatas = jbr.ReadBytes((int)fs.Length);
string jsonString = System.Text.Encoding.Default.GetString(jsonDatas);
fs.Close();
var options = new JsonSerializerOptions
{
// 整齐打印
WriteIndented = true,
//重新编码,解决中文乱码问题
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
};
sqlNumber = $"SELECT COUNT(*) FROM {tbname} WHERE EventTime = '{EventTime}'";
MySqlCommand md = new MySqlCommand(sqlNumber, conn);
var count = conn.QueryFirst<int>(sqlNumber);
if (count == 0)
{
sqlNumber = $"INSERT INTO {tbname} (EventTime, WorkAreaID, WaveData, JsonFile) VALUES('{EventTime}', '{workAreaId}', @mDatas, @jDatas);";
MySqlCommand mycomm = new MySqlCommand(sqlNumber, conn);
res = conn.Execute(sqlNumber, new { mDatas = mseedDatas, jDatas = jsonDatas });
}
sqlNumber = $"INSERT INTO {uploadedtbname}(filename, uploadtime) VALUES('{file}', '{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}')";
res = conn.Execute(sqlNumber);
}
}
}
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.ToString());
}
}
public static void UploadPostproFileMQ(Action<string, string> mqAction, string path, string eventMessage)
{
try
{
var currentTime = DateTime.Now;
var mseedFile = new DirectoryInfo(path).GetFiles("*.mseed");
var Dfiles = mseedFile.Where(f => eventMessage.Contains($"{f.Name.Substring(3, 4)}-{f.Name.Substring(7, 2)}-{f.Name.Substring(9, 2)}T{f.Name.Substring(12, 2)}:{f.Name.Substring(14, 2)}:{f.Name.Substring(16, 2)}"));
var files = Directory.GetFiles(path);
foreach (var dFile in Dfiles)
{
string file = dFile.Name;
string[] index_file_line = eventMessage.Split(" ", StringSplitOptions.RemoveEmptyEntries);
string EventTime = index_file_line[0].Substring(0, 23);
string OriginTime = EventTime;
//.json文件
string JsonPath = dFile.FullName.Replace(".mseed", "B.json");
FileStream fs = new FileStream(JsonPath, FileMode.Open, FileAccess.Read);
BinaryReader jbr = new BinaryReader(fs);
Byte[] jsonDatas = jbr.ReadBytes((int)fs.Length);
string jsonString = Encoding.Default.GetString(jsonDatas);
fs.Close();
var options = new JsonSerializerOptions
{
// 整齐打印
WriteIndented = true,
//重新编码,解决中文乱码问题
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
};
mqAction($"/watcherdata/post", JsonSerializer.Serialize(new { jsonFile = jsonString, eventMessage = eventMessage }, options));
}
}
catch (Exception ex)
{
// System.Windows.MessageBox.Show(ex.ToString());
}
}
}
}