using Google.Protobuf; using ivf_tl_Control; using IvfTl.Control.Entity.DTO; using ivf_tl_SerialHelper.Util; using ivf_tl_ServicesImpl.DBServices; using ivf_tl_ServicesImpl.KafkaServices; using ivf_tl_ServicesImpl.MqttServices; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Drawing.Imaging; using System.IO; using System.Security.Principal; using System.Threading.Tasks; using System.Windows; namespace TLTest { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } ComBin comBin = null; private void Button_Click(object sender, RoutedEventArgs e) { //DBServiceImpl.Instance.BuildEntity(); string ss = ""; //return; //StartMain startMain = new StartMain(); //MessageBox.Show(startMain.StartRun()); AppData.Instance.Login("admin", "123456"); Dictionary keyValuePairs = new Dictionary(); for (int i = 1; i < 17; i++) { keyValuePairs.Add(i, null); } for (int i = 1; i < 11; i++) { var aaaaa = AppData.Instance.HouseBin_GetCCDServiceEventTest(i, keyValuePairs); var bbbb = AppData.Instance.HouseBin_GetAutoFocusServiceEventTest(i, keyValuePairs); } //var bbbb = AppData.Instance.HouseBin_GetAutoFocusDBEventTest(5, keyValuePairs); string ss123 = ""; } KafkaService kafkaService = AppData.Instance.KafkaService; MqttService mqttService = AppData.Instance.MqttService; /// /// kafka监听 /// /// /// private async void Button_Click1(object sender, RoutedEventArgs e) { try { await mqttService.PublishAsync("测试12354646"); //kafkaService.kafkaConsumerTest(); } catch (Exception ex) { MessageBox.Show($"{ex.Message}{ex.StackTrace}"); } } /// /// kafka发送 /// /// /// private async void Button_Click2(object sender, RoutedEventArgs e) { try { await kafkaService.kafkaProducerAsync(GetImageDTO()); MessageBox.Show("完成"); } catch (Exception ex) { MessageBox.Show($"{ex.Message}{ex.StackTrace}"); } } private ImageDTO GetImageDTO() { try { ImageDTO imageDTO = new ImageDTO(); imageDTO.TlSn = "TlSn"; imageDTO.HouseSn = int.Parse(this.housesn_TextBox.Text.Trim()); imageDTO.WellSn = 1; imageDTO.CcdSn = "1"; imageDTO.SourceImageName = "1.jpg"; imageDTO.SourceImagePath = "1"; //imageDTO.SourceImageWidth = "1"; //imageDTO.SourceImageHeight = "1"; imageDTO.ImageTime = "2020-10-10 00:00:00"; imageDTO.FertilizationTime = "2020-10-10 00:00:00"; imageDTO.PhotographType = 1; imageDTO.TotalLayer = 1; imageDTO.PictureLayer = 1; imageDTO.ShootingPosition = 1; imageDTO.Clearest = 1; imageDTO.End = 1; imageDTO.EmbryoCultureRecordId = 1; imageDTO.EmbryoId = 1; string fileName = $"{Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, @"DependFile\PicLoadFailed.jpg")}"; imageDTO.ImageData = ByteString.CopyFrom(GetImageData(fileName)); return imageDTO; } catch (Exception ex) { MessageBox.Show($"获取imagedto{ex.Message}{ex.StackTrace}"); return null; } } private byte[] GetImageData(string fileName) { try { if (!File.Exists(fileName)) { return null; } ImageFormat imageFormat = ImageFormat.Jpeg; switch (Path.GetExtension(fileName).ToLower()) { case ".jpg": imageFormat = ImageFormat.Jpeg; break; case ".bmp": imageFormat = ImageFormat.Bmp; break; case ".png": imageFormat = ImageFormat.Png; break; default: break; } System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(fileName); MemoryStream memoryStream = new MemoryStream(); bitmap.Save(memoryStream, imageFormat); var a = memoryStream.ToArray(); memoryStream.Close(); memoryStream.Dispose(); bitmap.Dispose(); return a; } catch (Exception ex) { return null; } } /// /// kafka创建分区 /// /// /// private async void Button_Click3(object sender, RoutedEventArgs e) { try { await kafkaService.CreateTopicAsync(int.Parse(this.paNum_TextBox.Text.Trim())); MessageBox.Show("完成"); } catch (Exception ex) { MessageBox.Show($"{ex.Message}{ex.StackTrace}"); } } /// /// kafka获取分区 /// /// /// private void Button_Click4(object sender, RoutedEventArgs e) { try { kafkaService.GetAllTopic(); MessageBox.Show("完成"); } catch (Exception ex) { MessageBox.Show($"{ex.Message}{ex.StackTrace}"); } } /// /// 判断分区存在,若不存在则创建 /// /// /// private async void Button_Click5(object sender, RoutedEventArgs e) { try { var sdf = await kafkaService.CreateTopicAsync(int.Parse(this.paNum_TextBox.Text.Trim())); MessageBox.Show($"完成{sdf}"); } catch (Exception ex) { MessageBox.Show($"{ex.Message}{ex.StackTrace}"); } } private void Button_Click_1(object sender, RoutedEventArgs e) { try { kafkaService.KafkaSetNameAndIp(this.topic_TextBox.Text.Trim(), this.ip_TextBox.Text.Trim()); } catch (Exception ex) { MessageBox.Show($"{ex.Message}{ex.StackTrace}"); } } private void _channel_ReceiveLogEvent(int arg1, DateTime arg2, string arg3) { WriteRunRecordLog(arg2, $"[ReceiveLogEvent][{arg1}][{arg3}]"); } private void _channel_SendLogEvent(int arg1, DateTime arg2, string arg3) { WriteRunRecordLog(arg2, $"[SendLogEvent][{arg1}][{arg3}]"); } private void _channel_ErrorLogEvent(string obj) { WriteRunRecordLog(DateTime.Now, $"[ErrorLogEvent]{obj}"); } private void _channel_LogEvent(string obj) { WriteRunRecordLog(DateTime.Now, $"[LogEvent]{obj}"); } private void _channel_ExceptionLogEvent(Exception arg1, string arg2, string arg3) { WriteRunRecordLog(DateTime.Now, $"[ExceptionLogEvent][{arg2}][{arg3}][{arg1.Message}:{arg1.StackTrace}]"); } private static object WriteRunRecordLock = new object(); /// /// 记录程序运行情况 /// private static void WriteRunRecordLog(DateTime recordTime, string content) { try { string dirTime = recordTime.ToString("yyyy-MM-dd"); string path = $"{System.AppDomain.CurrentDomain.BaseDirectory}log"; lock (WriteRunRecordLock) { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string filename = Path.Combine(path, "RunRecord.log"); string wroteContent = $"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}] {recordTime.ToString("yyyy-MM-dd HH:mm:ss:fff")} {content}"; using (StreamWriter mySw = File.AppendText(filename)) { mySw.WriteLine(wroteContent); mySw.Close(); } } } catch (Exception ex) { } } } }