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.

80 lines
4.0 KiB
C#

using Dapper;
using DryIoc;
using Google.Protobuf.WellKnownTypes;
using HandyControl.Controls;
using Microsoft.Xaml.Behaviors.Media;
using MySql.Data.MySqlClient;
using Org.BouncyCastle.Pqc.Crypto.Lms;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Markup;
using System.Xml.Linq;
using Txgy.FilesWatcher.DBModels;
using static System.Net.WebRequestMethods;
namespace Txgy.FilesWatcher.model
{
public class UploadRealtimeFile
{
public static void UploadRealtimeFileOnce(string path, string eventMessage, bool isUploadMQ,int workAreaId= 1)
{
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 = "realtimeeventresult";
string tbname = "realtimewavedatas";
string uploadedtbname = "uploadedrealtime";
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(" ");
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);
// .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", "A.json");
fs = new FileStream(JsonPath, FileMode.Open, FileAccess.Read);
BinaryReader jbr = new BinaryReader(fs);
Byte[] jsonDatas = jbr.ReadBytes((int)fs.Length);
fs.Close();
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)
{
MessageBox.Show(ex.ToString());
}
}
}
}