wanghan 6 месяцев назад
Родитель
Сommit
936923a44d

+ 68 - 0
TeamAAS-VM/Core/Management.cs

@@ -8,6 +8,7 @@ using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.Drawing;
 using System.IO;
+using System.IO.Compression;
 using System.Linq;
 using System.Net.Sockets;
 using System.Text;
@@ -2631,6 +2632,39 @@ namespace TeamAAS_VP.Core
                                             if (recordResult)
                                             {
                                                 SendTaskMessage($"{i}:{stationConfig.DeviceName}产品记录保存成功", MessageLevel.Debug);
+                                                //上传数据
+                                                try
+                                                {
+                                                    DateTime dt = DateTime.Now;
+                                                    string filename = $"{ProductMainSN[i]}_{dt.ToString("yyyyMMddHHmmss")}";
+                                                    var device = _configService.GetDeviceInfo();
+                                                    (bool isSuc, string resp) = await _mesService.SendFtpFileAsync(ProductMainSN[i], filename);
+                                                    if (isSuc)
+                                                    {
+                                                        LogHelper.WriteLogMes($"上传数据成功");
+                                                        SendTaskMessage($"上传数据成功", MessageLevel.Debug);
+                                                        string filePath = $"D:\\image\\Recored\\{dt.ToString("yyyy-MM")}\\{dt.ToString("MM-dd")}\\{currentProduct.Name}\\{ProductMainSN[i]}";
+                                                        //复制csv
+                                                        string source_csv = $"{stationConfig.SaveCsvPath}\\{dt.ToString("yyyy-MM")}\\{i}_{stationConfig.DeviceName}_{dt.ToString("yyyy-MM-dd")}.csv";
+                                                        string target_csv = Path.Combine(filePath, Path.GetFileName(source_csv)); ;
+                                                        File.Copy(source_csv, target_csv, true);
+                                                        //复制log
+                                                        string source_log = $"..\\Log\\LogInfo\\{dt.ToString("yyyy-MM")}\\{dt.ToString("yyyy-MM-dd")}.txt";
+                                                        string target_log = Path.Combine(filePath, Path.GetFileName(source_log));
+                                                        File.Copy(source_log, target_log, true);
+                                                        CompressionImage(filePath, filename, device.Station, device.FixtureId);
+                                                    }
+                                                    else
+                                                    {
+                                                        LogHelper.WriteLogMes($"上传数据失败");
+                                                        SendTaskMessage($"上传数据失败", MessageLevel.Error);
+                                                    }
+                                                }
+                                                catch (Exception ex)
+                                                {
+                                                    SendTaskMessage($"上传数据时出错:{ex.Message}", MessageLevel.Error);
+                                                    LogHelper.WriteLogError($"上传数据时出错", ex);
+                                                }
                                             }
                                             else
                                             {
@@ -3072,6 +3106,40 @@ namespace TeamAAS_VP.Core
                 return false;
             }
         }
+
+        #region 压缩图片文件夹
+        /// <summary>
+        /// 压缩文件
+        /// </summary>
+        /// <param name="sourcePath">原路径</param>
+        /// <param name="zipname">压缩后文件的名称</param>
+        /// <param name="station">mes中的station</param>
+        /// <param name="fixid">mes中的fixtureID</param>
+        public async void CompressionImage(string sourcePath, string zipname, string station, string fixid)
+        {
+            string zipPath = "";
+            try
+            {
+                SendTaskMessage($"开始压缩文件", MessageLevel.Info);
+                await Task.Run(() =>
+                {
+                    zipPath = $"Z:\\{station}\\{fixid}\\{DateTime.Now.ToString("yyyyMMdd")}\\{zipname}.zip";//生成的zip文件路径
+                    string path = $"Z:\\{station}\\{fixid}\\{DateTime.Now.ToString("yyyyMMdd")}";
+                    if (!Directory.Exists(path))
+                    {
+                        Directory.CreateDirectory(path);
+                    }
+
+                    ZipFile.CreateFromDirectory(sourcePath, zipPath);
+                });
+            }
+            catch (Exception ex)
+            {
+                LogHelper.WriteLogError($"压缩文件时出错", ex);
+            }
+        }
+
+        #endregion
         #endregion
     }
 }

+ 9 - 0
TeamAAS-VM/Interfaces/IMesService.cs

@@ -109,5 +109,14 @@ namespace TeamAAS_VP.Interfaces
         /// <param name="timeoutInSeconds"></param>
         /// <returns></returns>
         Task<(bool IsSuccess, DateTime Now)> GetServerTimeAsync(int deviceNo, string sn, double timeoutInSeconds);
+
+        /// <summary>
+        /// ÉÏ´«log
+        /// </summary>
+        /// <param name="sn"></param>
+        /// <param name="name"></param>
+        /// <param name="cancellationToken"></param>
+        /// <returns></returns>
+        Task<(bool IsSuccess, string Response)> SendFtpFileAsync(string sn, string name, CancellationToken cancellationToken = default);
     }
 }

+ 19 - 2
TeamAAS-VM/Models/DeviceInfo.cs

@@ -1,3 +1,4 @@
+using NPOI.POIFS.Crypt;
 using Prism.Mvvm;
 using System;
 using System.Windows;
@@ -97,6 +98,18 @@ namespace TeamAAS_VP.Models
             set { SetProperty(ref _dataInfo, value); }
         }
 
+        private string _LogUrl;
+        /// <summary>
+        /// log的url
+        /// </summary>
+        public string LogUrl { get => _LogUrl; set => SetProperty(ref _LogUrl, value); }
+
+        private string _ApiKey;
+        /// <summary>
+        /// ApiKey
+        /// </summary>
+        public string ApiKey { get => _ApiKey; set => SetProperty(ref _ApiKey, value); }
+
         public DeviceInfo()
         {
             DeviceNo = 0;
@@ -105,7 +118,9 @@ namespace TeamAAS_VP.Models
             FixtureId = string.Empty;
             F = string.Empty;
             comp = string.Empty;
-            MesUrl = string.Empty;
+            MesUrl = string.Empty; 
+            LogUrl = string.Empty;
+            ApiKey = string.Empty;
             EnableMES = false;
         }
 
@@ -126,7 +141,9 @@ namespace TeamAAS_VP.Models
                 SaveCsv = this.SaveCsv,
                 SaveCsvPath = this.SaveCsvPath,
                 EnableDoubleMes = this.EnableDoubleMes,
-                DataInfo = this.DataInfo.Copy()
+                DataInfo = this.DataInfo.Copy(),
+                LogUrl = this.LogUrl,
+                ApiKey = this.ApiKey,
             };
         }
     }

+ 62 - 2
TeamAAS-VM/Services/MesService.cs

@@ -381,15 +381,16 @@ namespace TeamAAS_VP.Services
 
             string res = isSuccess ? "Pass" : "Fail";
             string url = "";
+            string[] strcc = cc.Split(',');
             if (device.F == "PTL")
             {
                 url = $"{device.MesUrl}?c=ADD_RECORD&line={device.Line}&station={device.Station}&fixtureid={device.FixtureId}&sn={sn}&f=PTL&comp={device.comp}:{comp}" +
-                    $"&CC={cc}&AssyPressure={assypress}&result={res}&start_time={start_time.ToString("yyyy-MM-dd HH:mm:ss")}&stop_time={stop_time.ToString("yyyy-MM-dd HH:mm:ss")}";
+                    $"&CC1={strcc[0]}&CC2={strcc[1]}&AssyPressure1={assypress}&result={res}&start_time={start_time.ToString("yyyy-MM-dd HH:mm:ss")}&stop_time={stop_time.ToString("yyyy-MM-dd HH:mm:ss")}";
             }
             else
             {
                 url = $"{device.MesUrl}?c=ADD_RECORD&line={device.Line}&station={device.Station}&fixtureid={device.FixtureId}&sn={sn}&f=PQC" +
-                    $"&CC={cc}&AssyPressure={assypress}&result={res}&start_time={start_time.ToString("yyyy-MM-dd HH:mm:ss")}&stop_time={stop_time.ToString("yyyy-MM-dd HH:mm:ss")}";
+                    $"&CC1={strcc[0]}&CC2={strcc[1]}&AssyPressure1={assypress}&result={res}&start_time={start_time.ToString("yyyy-MM-dd HH:mm:ss")}&stop_time={stop_time.ToString("yyyy-MM-dd HH:mm:ss")}";
             }
             LogHelper.WriteLogMes($"【上传URL】{url}");
             SendTaskMessage($"【上传URL】{url}", MessageLevel.Info);
@@ -535,5 +536,64 @@ namespace TeamAAS_VP.Services
                 _eventAggregator.GetEvent<TaskMessageNotification>().Publish(new Models.MessageStruct() { Message = msg, level = level });
             });
         }
+
+        /// <summary>
+        /// 上传log服务器
+        /// </summary>
+        /// <param name="sn"></param>
+        /// <param name="name"></param>
+        /// <param name="cancellationToken"></param>
+        /// <returns></returns>
+        /// <exception cref="ObjectDisposedException"></exception>
+        public async Task<(bool IsSuccess, string Response)> SendFtpFileAsync(string sn, string name, CancellationToken cancellationToken = default)
+        {
+            // 如果已经释放,则抛出异常,防止在已释放资源上继续操作。
+            if (_disposed) throw new ObjectDisposedException(nameof(MesService));
+
+            var device = _configService.GetDeviceInfo();
+            if (device == null || !device.EnableMES)
+            {
+                return (true, "MES功能未启用!");
+            }
+            if (string.IsNullOrWhiteSpace(device.LogUrl))
+            {
+                return (false, "Missing MES station LogUrl");
+            }
+
+            string url = $"{device.LogUrl}?db=mysql&ApiKey={device.ApiKey}&jsondata={{\"SN\":\"{sn}\",\"LINE\":\"{device.Line}\",\"STATION\":\"{device.Station}\",\"TESTRESULT\":\"PASS\",\"FIXTUREID\":\"{device.FixtureId}\",\"TESTDATETIME\":\"{DateTime.Now.ToString()}\",\"LOGFILENAME\":\"{name}.zip\",\"STATIONTYPE\":\"AE\"}}";
+
+            LogHelper.WriteLogMes($"【上传LogData的URL】{url}");
+            SendTaskMessage($"【上传LogData的URL】{url}", MessageLevel.Info);
+            (bool IsSuccess, string Response) result = (false, string.Empty);
+
+            for (int i = 0; i < 3; i++)
+            {
+                try
+                {
+                    using (var rsp = await _httpClient.GetAsync(url, cancellationToken))
+                    {
+                        var text = await rsp.Content.ReadAsStringAsync();
+                        LogHelper.WriteLogMes($"【上传LogData的HTTP接收】{text}");
+                        SendTaskMessage($"【上传LogData的HTTP接收】{text}", MessageLevel.Info);
+                        // "SFC_OK" 代表和 MES 连接成功
+                        if (text.Contains("\"Code\":1"))
+                        {
+                            return (true, text);
+                        }
+                        result = (false, text);
+                    }
+                }
+                catch (OperationCanceledException)
+                {
+                    result = (false, "Canceled");
+                }
+                catch (Exception ex)
+                {
+                    result = (false, ex.Message);
+                }
+            }
+            return result;
+        }
+
     }
 }

+ 2 - 0
TeamAAS-VM/TeamAAS-VP.csproj

@@ -363,6 +363,8 @@
     <Reference Include="System.Formats.Asn1, Version=9.0.0.9, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
       <HintPath>..\packages\System.Formats.Asn1.9.0.9\lib\net462\System.Formats.Asn1.dll</HintPath>
     </Reference>
+    <Reference Include="System.IO.Compression" />
+    <Reference Include="System.IO.Compression.FileSystem" />
     <Reference Include="System.IO.Pipelines, Version=9.0.0.9, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
       <HintPath>..\packages\System.IO.Pipelines.9.0.9\lib\net462\System.IO.Pipelines.dll</HintPath>
     </Reference>

+ 45 - 19
TeamAAS-VM/Views/SettingView.xaml

@@ -345,6 +345,12 @@
                                     <RowDefinition Height="Auto" />
                                     <RowDefinition Height="Auto" />
                                     <RowDefinition Height="Auto" />
+                                    <RowDefinition Height="Auto" />
+                                    <RowDefinition Height="Auto" />
+                                    <RowDefinition Height="Auto" />
+                                    <RowDefinition Height="Auto" />
+                                    <RowDefinition Height="Auto" />
+                                    <RowDefinition Height="Auto" />
                                 </Grid.RowDefinitions>
                                 <Grid.ColumnDefinitions>
                                     <ColumnDefinition Width="auto" />
@@ -443,31 +449,51 @@
                                          Margin="6,2" />
 
                                 <TextBlock Grid.Row="9"
+                                           Grid.Column="0"
+                                           Text="LogUrl:"
+                                           HorizontalAlignment="Right"
+                                           VerticalAlignment="Center" />
+                                <TextBox Grid.Row="9"
+                                         Grid.Column="1"
+                                         Text="{Binding SelectDeviceInfo.LogUrl, Mode=TwoWay}"
+                                         Margin="6,2" />
+
+                                <TextBlock Grid.Row="10"
+                                           Grid.Column="0"
+                                           Text="apikey:"
+                                           HorizontalAlignment="Right"
+                                           VerticalAlignment="Center" />
+                                <TextBox Grid.Row="10"
+                                         Grid.Column="1"
+                                         Text="{Binding SelectDeviceInfo.ApiKey, Mode=TwoWay}"
+                                         Margin="6,2" />
+                                
+                                <TextBlock Grid.Row="11"
                                            Grid.Column="0"
                                            Text="启用 MES:"
                                            HorizontalAlignment="Right"
                                            VerticalAlignment="Center" />
-                                <CheckBox Grid.Row="9"
+                                <CheckBox Grid.Row="11"
                                           Grid.Column="1"
                                           IsChecked="{Binding SelectDeviceInfo.EnableMES, Mode=TwoWay}"
                                           Margin="6,2" />
-                                <CheckBox Grid.Row="10"
+                                <CheckBox Grid.Row="12"
                                           Grid.Column="1"
                                           Content="是否启用两次产品编号"
                                           IsChecked="{Binding SelectDeviceInfo.EnableDoubleMes, Mode=TwoWay}"
                                           Margin="6,2" />
-                                <CheckBox Grid.Row="11"
+                                <CheckBox Grid.Row="13"
                                           Grid.Column="1"
                                           Content="是否保存CSV文件"
                                           IsChecked="{Binding SelectDeviceInfo.SaveCsv, Mode=TwoWay}"
                                           Margin="6,2" />
-                                <TextBlock Grid.Row="12"
+                                <TextBlock Grid.Row="14"
                                            Grid.Column="0"
                                            Text="保存的CSV文件路径:"
                                            HorizontalAlignment="Right"
                                            VerticalAlignment="Center" />
                                 <DockPanel HorizontalAlignment="Stretch"
-                                           Grid.Row="12"
+                                           Grid.Row="14"
                                            Grid.Column="1"
                                            Margin="6,2">
                                     <Button DockPanel.Dock="Right"
@@ -482,12 +508,12 @@
 
                                 </DockPanel>
                                 <!--CTQ 数据信息-->
-                                <TextBlock Grid.Row="13"
+                                <TextBlock Grid.Row="15"
                                            Grid.Column="0"
                                            Text="PROCESS_NAME:"
                                            HorizontalAlignment="Right"
                                            VerticalAlignment="Center" />
-                                <TextBox Grid.Row="13"
+                                <TextBox Grid.Row="15"
                                          Grid.Column="1"
                                          Text="{Binding SelectDeviceInfo.DataInfo.PROCESS_NAME, Mode=TwoWay}"
                                          Margin="6,2" />
@@ -512,62 +538,62 @@
                      Text="{Binding SelectDeviceInfo.DataInfo.CTQ_USL_Pressure, Mode=TwoWay}"
                      Margin="6,2" />-->
 
-                                <TextBlock Grid.Row="16"
+                                <TextBlock Grid.Row="18"
                                            Grid.Column="0"
                                            Text="SUPPLIER_NAME:"
                                            HorizontalAlignment="Right"
                                            VerticalAlignment="Center" />
-                                <TextBox Grid.Row="16"
+                                <TextBox Grid.Row="18"
                                          Grid.Column="1"
                                          Text="{Binding SelectDeviceInfo.DataInfo.SUPPLIER_NAME, Mode=TwoWay}"
                                          Margin="6,2" />
 
-                                <TextBlock Grid.Row="17"
+                                <TextBlock Grid.Row="19"
                                            Grid.Column="0"
                                            Text="COMMODITY_TYPE:"
                                            HorizontalAlignment="Right"
                                            VerticalAlignment="Center" />
-                                <TextBox Grid.Row="17"
+                                <TextBox Grid.Row="19"
                                          Grid.Column="1"
                                          Text="{Binding SelectDeviceInfo.DataInfo.COMMODITY_TYPE, Mode=TwoWay}"
                                          Margin="6,2" />
 
-                                <TextBlock Grid.Row="18"
+                                <TextBlock Grid.Row="20"
                                            Grid.Column="0"
                                            Text="REVISION:"
                                            HorizontalAlignment="Right"
                                            VerticalAlignment="Center" />
-                                <TextBox Grid.Row="18"
+                                <TextBox Grid.Row="20"
                                          Grid.Column="1"
                                          Text="{Binding SelectDeviceInfo.DataInfo.REVISION, Mode=TwoWay}"
                                          Margin="6,2" />
 
-                                <TextBlock Grid.Row="19"
+                                <TextBlock Grid.Row="21"
                                            Grid.Column="0"
                                            Text="MFG_LOT#:"
                                            HorizontalAlignment="Right"
                                            VerticalAlignment="Center" />
-                                <TextBox Grid.Row="19"
+                                <TextBox Grid.Row="21"
                                          Grid.Column="1"
                                          Text="{Binding SelectDeviceInfo.DataInfo.MFG_LOT, Mode=TwoWay}"
                                          Margin="6,2" />
 
-                                <TextBlock Grid.Row="20"
+                                <TextBlock Grid.Row="22"
                                            Grid.Column="0"
                                            Text="MFG_ASSY_LINE:"
                                            HorizontalAlignment="Right"
                                            VerticalAlignment="Center" />
-                                <TextBox Grid.Row="20"
+                                <TextBox Grid.Row="22"
                                          Grid.Column="1"
                                          Text="{Binding SelectDeviceInfo.DataInfo.MFG_ASSY_LINE, Mode=TwoWay}"
                                          Margin="6,2" />
 
-                                <TextBlock Grid.Row="21"
+                                <TextBlock Grid.Row="23"
                                            Grid.Column="0"
                                            Text="PROCESS_MACHINE_ID:"
                                            HorizontalAlignment="Right"
                                            VerticalAlignment="Center" />
-                                <TextBox Grid.Row="21"
+                                <TextBox Grid.Row="23"
                                          Grid.Column="1"
                                          Text="{Binding SelectDeviceInfo.DataInfo.PROCESS_MACHINE_ID, Mode=TwoWay}"
                                          Margin="6,2" />