Browse Source

修改产线看板产量接口,修改工单产量计数逻辑

mengshunguo 1 month ago
parent
commit
d7e8ba1d7e

+ 212 - 7
SCADA_DAQ/Customer/Machines/SampleMachine.cs

@@ -5,7 +5,11 @@ using SCADA;
 using SCADA.Comm;
 using SCADA.CommonLib;
 using SCADA.CommonLib.Data.DIL;
+using SCADA.CommonLib.Helper;
 using SCADA.Drive;
+using SqlSchema.DBNames;
+using SCADA_DAQ.Customer.Models.RecordManagement;
+using SCADA_DAQ.Customer.Models.TaskManagement;
 using SCADA_DAQ.Plugin.Machine;
 using SCADA_DAQ.Plugin.Machine.Device;
 using System;
@@ -18,6 +22,10 @@ using System.Speech.Synthesis;
 using System.Threading;
 using System.Threading.Tasks;
 using System.Windows;
+using System.Windows.Forms;
+using Microsoft.DwayneNeed.Numerics;
+using System.Net.NetworkInformation;
+using System.Runtime.InteropServices;
 
 namespace SCADA_DAQ.Customer.Machines
 {
@@ -27,6 +35,35 @@ namespace SCADA_DAQ.Customer.Machines
     [DisplayName("客户自定义设备")]
     public partial class SampleMachine : BasePLCMachine
     {
+        /// <summary>
+        /// sql server
+        /// </summary>
+        private static SqlSchema.DIL.DILDB DAL = Env.SgIDAL;
+
+        /// <summary>
+        /// sqlite
+        /// </summary>
+        private static DIL.DILDB sqliteDAL = Env.DAL;
+
+        /// <summary>
+        /// 当前工单任务
+        /// </summary>
+        private TaskManagementModel TaskManagement;
+
+        /// <summary>
+        /// 第一次车胎压的时间
+        /// </summary>
+        private DateTime FirstTime;
+
+        /// <summary>
+        /// 车胎压的次数
+        /// </summary>
+        private int NumberOfTimes;
+
+        /// <summary>
+        /// 服务器连接状态
+        /// </summary>
+        private bool ServerConncetStatus;
         /// <summary>
         /// 报警合集
         /// </summary>
@@ -38,6 +75,8 @@ namespace SCADA_DAQ.Customer.Machines
 
         public DateTime StartTime { get; set; } = new DateTime();
 
+
+
         /// <summary>
         /// 
         /// </summary>
@@ -50,8 +89,7 @@ namespace SCADA_DAQ.Customer.Machines
             // .ToLookup(t => t.AlarmCode).ToDictionary(t => t.Key, t => t.First());
 
             Env.Schedual.DateTimeChanged += Schedual_DateTimeChanged;
-
-
+            //Init();
         }
 
         private void Schedual_DateTimeChanged(object sender, DateTimeChangedArgs e)
@@ -85,7 +123,12 @@ namespace SCADA_DAQ.Customer.Machines
                     StateID = "";
                     IsConnected = false;
                 }
-               
+
+            }
+            if (e.ChangeType == DateTimeChangeType.SecondChanged && e.TimeFlag.Second % 1 == 0)
+            {
+                var connectStatus = GetInternetConStatus.GetNetConStatus(Env.ServerIP.Value);
+                ServerConncetStatus = (connectStatus == 2 || connectStatus == 3) ? true : false;
             }
         }
 
@@ -135,10 +178,165 @@ namespace SCADA_DAQ.Customer.Machines
         {
             base.RegReadValueChanged(sender, e);
             var reg = (RegInfo)sender;
-
+            if (reg.Comment.Variable == "计数开关" && reg.Value == 1)
+            {
+                if (NumberOfTimes == 0)
+                {
+                    NumberOfTimes++;
+                    FirstTime = DateTime.Now;
+                }
+                else if (NumberOfTimes == 1 && (int)(DateTime.Now - FirstTime).TotalSeconds < 7 && ServerConncetStatus)
+                {
+                    var taskInfoItem = DAL.APP_TaiLing_TaskManagement.GetData<TaskManagementModel>()?
+                   .Where(t => t.WorkShopId == CustomerEnv.ProductionLineConfigValue.WorkShopId
+                       && t.ProductionLineId == CustomerEnv.ProductionLineConfigValue.ProductionLineId && t.OrderState != "已生产")
+                   .OrderBy(t => t.ID).FirstOrDefault();
+                    if (taskInfoItem == null) { return; }
+                    else
+                    {
+                        taskInfoItem.CurrentProduction++;
+                        if (taskInfoItem.WorkStartTime == new DateTime() && taskInfoItem.OrderState == "待生产")
+                        {
+                            taskInfoItem.WorkStartTime = DateTime.Now;
+                            DAL.APP_TaiLing_TaskManagement.Update($"ID={taskInfoItem.ID}",
+                           new UpdateItem(SqlSchema.DBNames.T_Col_Name.APP_TaiLing_TaskManagement.OrderState_Str, "生产中"),
+                           new UpdateItem(SqlSchema.DBNames.T_Col_Name.APP_TaiLing_TaskManagement.WorkStartTime_Dt, taskInfoItem.WorkStartTime.Format()),
+                           new UpdateItem(SqlSchema.DBNames.T_Col_Name.APP_TaiLing_TaskManagement.CurrentProduction_Int, taskInfoItem.CurrentProduction)
+                             );
+                            Log.Info($"订单{taskInfoItem.OrderId}开始生产");
+                        }
+                        else if (taskInfoItem.CurrentProduction >= taskInfoItem.ScheduledProduction)
+                        {
+                            // 结束生产 
+                            var dateDiff = (DateTime.Now - taskInfoItem.WorkStartTime).TotalSeconds.ToInt();  // 时间差/s
+                            DAL.APP_TaiLing_TaskManagement.Update($"ID={taskInfoItem.ID}",
+                                new UpdateItem(SqlSchema.DBNames.T_Col_Name.APP_TaiLing_TaskManagement.WorkEndTime_Dt, DateTime.Now.Format()),
+                                new UpdateItem(SqlSchema.DBNames.T_Col_Name.APP_TaiLing_TaskManagement.DurationSec_Int, dateDiff),
+                                new UpdateItem(SqlSchema.DBNames.T_Col_Name.APP_TaiLing_TaskManagement.OrderState_Str, "已生产"),
+                                new UpdateItem(SqlSchema.DBNames.T_Col_Name.APP_TaiLing_TaskManagement.CurrentProduction_Int, taskInfoItem.CurrentProduction)
+                            );
+                            Log.Info($"订单{taskInfoItem.OrderId}生产完成");
+                        }
+                        else if(taskInfoItem.WorkStartTime != new DateTime() && taskInfoItem.OrderState == "生产中")
+                        {
+                            DAL.APP_TaiLing_TaskManagement.Update($"ID={taskInfoItem.ID}",
+                          new UpdateItem(SqlSchema.DBNames.T_Col_Name.APP_TaiLing_TaskManagement.CurrentProduction_Int, taskInfoItem.CurrentProduction)
+                            );
+                        }
+                        NumberOfTimes = 0;
+                    }
+                }
+                else if (NumberOfTimes == 1 && (int)(DateTime.Now - FirstTime).TotalSeconds > 5)
+                {
+                    NumberOfTimes = 0;
+                    FirstTime = DateTime.Now;
+                }
+            }
             //log.Debug($"{DeviceConfig.DeviceID} {reg.RegName} Value Changed:{reg.OldValue}->{reg.Value}");
         }
 
+        /// <summary>
+        /// 网络连接,判断是否与服务器连接
+        /// </summary>
+        public class GetInternetConStatus
+        {
+            private const int INTERNET_CONNECTION_MODEM = 1;
+            private const int INTERNET_CONNECTION_LAN = 2;
+            [DllImport("winInet.dll")]
+            private static extern bool InternetGetConnectedState(
+            ref int dwFlag,
+            int dwReserved
+            );
+
+            /// <summary>
+            /// 判断网络的连接状态
+            /// </summary>
+            /// <returns>
+            /// 网络状态(1-->未联网;2-->采用调治解调器上网;3-->采用网卡上网)
+            ///</returns>
+            public static int GetNetConStatus(string strNetAddress)
+            {
+                int iNetStatus = 0;
+                System.Int32 dwFlag = new int();
+                if (!InternetGetConnectedState(ref dwFlag, 0))
+                {
+                    //没有能连上互联网
+                    iNetStatus = 1;
+                }
+                else if ((dwFlag & INTERNET_CONNECTION_MODEM) != 0)
+                {
+                    //采用调治解调器上网,需要进一步判断能否登录具体网站
+                    if (PingNetAddress(strNetAddress))
+                    {
+                        //可以ping通给定的网址,网络OK
+                        iNetStatus = 2;
+                    }
+                    else
+                    {
+                        //不可以ping通给定的网址,网络不OK
+                        iNetStatus = 4;
+                    }
+                }
+
+                else if ((dwFlag & INTERNET_CONNECTION_LAN) != 0)
+                {
+                    //采用网卡上网,需要进一步判断能否登录具体网站
+                    if (PingNetAddress(strNetAddress))
+                    {
+                        //可以ping通给定的网址,网络OK
+                        iNetStatus = 3;
+                    }
+                    else
+                    {
+                        //不可以ping通给定的网址,网络不OK
+                        iNetStatus = 5;
+                    }
+                }
+
+                return iNetStatus;
+            }
+
+            private static bool PingNetAddress(string strNetAdd)
+            {
+                bool Flage = false;
+                Ping ping = new Ping();
+                try
+                {
+                    PingReply pr = ping.Send(strNetAdd, 3000);
+                    if (pr.Status == IPStatus.TimedOut)
+                    {
+                        Flage = false;
+                    }
+                    if (pr.Status == IPStatus.Success)
+                    {
+                        Flage = true;
+                    }
+                    else
+                    {
+                        Flage = false;
+                    }
+                }
+                catch
+                {
+                    Flage = false;
+                }
+                return Flage;
+            }
+        }
+
+        /// <summary>
+        /// 初始化获取工单任务
+        /// </summary>
+        //private void Init()
+        //{
+        //    var taskInfoItem = DAL.APP_TaiLing_TaskManagement.GetData<TaskManagementModel>()?
+        //       .Where(t => t.WorkShopId == CustomerEnv.ProductionLineConfigValue.WorkShopId
+        //           && t.ProductionLineId == CustomerEnv.ProductionLineConfigValue.ProductionLineId && t.OrderState != "已生产")
+        //       .OrderBy(t => t.ID).FirstOrDefault();
+        //    if (taskInfoItem == null) return;
+        //    else TaskManagement = taskInfoItem;
+        //}
+
         /// <summary>
         /// 
         /// </summary>
@@ -148,7 +346,7 @@ namespace SCADA_DAQ.Customer.Machines
         {
             base.AlarmTriggered(sender, e);
             var reg = (e.Alarm as Alarm<RegInfo>).Tag;
-            Application.Current.Dispatcher.BeginInvoke(new Action(() =>
+            System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() =>
             {
                 AlarmCollection.Add(e.Alarm);
             }));
@@ -169,7 +367,7 @@ namespace SCADA_DAQ.Customer.Machines
         protected override void AlarmReset(object sender, AlarmTriggedEventArgs e)
         {
             base.AlarmReset(sender, e);
-            Application.Current.Dispatcher.BeginInvoke(new Action(() =>
+            System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() =>
             {
                 AlarmCollection.Remove(e.Alarm);
             }));
@@ -182,6 +380,13 @@ namespace SCADA_DAQ.Customer.Machines
             //  new UpdateItem(T_Col_Name.Base_AlarmLog.IsClosed_Bit, e.Alarm.IsClosed));
         }
 
-
+        /// <summary>
+        /// 释放非托管资源
+        /// </summary>
+        public override void Dispose()
+        {
+            Env.Schedual.DateTimeChanged -= Schedual_DateTimeChanged;  // 销毁定时器
+            base.Dispose();
+        }
     }
 }

+ 7 - 7
SCADA_DAQ/Customer/Service/CustomerService.cs

@@ -418,13 +418,13 @@ namespace SCADA_DAQ.Customer.Service
             var sql = $"DECLARE @W VARCHAR(10), @P VARCHAR(10);" +
                 $"SET @W = '{CustomerEnv.ProductionLineConfigValue.WorkShopId}'; " + 
                 $"SET @P = '{CustomerEnv.ProductionLineConfigValue.ProductionLineId}'; " +
-                $"SELECT WorkShopId_Str AS 车间号, ProductionLineId_Str AS 产线号, COUNT(*) AS 总产量, " +
-                $"(SELECT COUNT(*) FROM APP_TaiLing_ProductionRecord " +
-                $"WHERE DATEDIFF(mm, EndTime_Dt, GETDATE())=0 AND WorkShopId_Str=@W AND ProductionLineId_Str=@P) AS 月产量, " +
-                $"(SELECT COUNT(*) FROM APP_TaiLing_ProductionRecord " +
-                $"WHERE DATEDIFF(dd, EndTime_Dt, GETDATE())=0 AND WorkShopId_Str=@W AND ProductionLineId_Str=@P) AS 日产量 " +
-                $"FROM APP_TaiLing_ProductionRecord " +
-                $"WHERE WorkShopId_Str=@W AND ProductionLineId_Str=@P AND EndTime_Dt IS NOT NULL " +
+                $"SELECT WorkShopId_Str AS 车间号, ProductionLineId_Str AS 产线号, SUM(CurrentProduction_Int) AS 总产量, " +
+                $"(SELECT SUM(CurrentProduction_Int) FROM APP_TaiLing_TaskManagement " +
+                $"WHERE DATEDIFF(mm, WorkStartTime_Dt, GETDATE())=0 AND WorkShopId_Str=@W AND ProductionLineId_Str=@P) AS 月产量, " +
+                $"(SELECT SUM(CurrentProduction_Int) FROM APP_TaiLing_TaskManagement " +
+                $"WHERE DATEDIFF(dd, WorkStartTime_Dt, GETDATE())=0 AND WorkShopId_Str=@W AND ProductionLineId_Str=@P) AS 日产量 " +
+                $"FROM APP_TaiLing_TaskManagement " +
+                $"WHERE WorkShopId_Str=@W AND ProductionLineId_Str=@P AND WorkStartTime_Dt IS NOT NULL " +
                 $"GROUP BY WorkShopId_Str, ProductionLineId_Str;";
             var dt = DAL.ReadDataTable(sql);
 

+ 1 - 1
SCADA_DAQ/Env.cs

@@ -29,7 +29,7 @@ namespace SCADA_DAQ
         public static DILDB DAL = null;
         public static LocalDB.DIL.DILDB LocalDAL = null; // 系统数据库
 
-        public static AutoSaveParameterItem<string> ServerIP { get; set; } = AutoSaveParameterItem.Create("TailgServerIP", "10.255.254.250");
+        public static AutoSaveParameterItem<string> ServerIP { get; set; } = AutoSaveParameterItem.Create("TailgServerIP", "127.0.0.1");
 
         //SQL Server
         //public static SqlSchema.DIL.DILDB SgIDAL = new SqlSchema.DIL.DILDB("127.0.0.1", "TL_DB", "sa","M+123456");