yanjinhui
2025-06-09 9f3858dd723a69c56dbab909e0c7e6c5ef8b85a8
project/WCS/WIDESEAWCS_Server/WIDESEAWCS_TelescopicService/ParametersService.cs
@@ -15,6 +15,7 @@
using WIDESEAWCS_ITelescopicService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using static WIDESEAWCS_Common.Face.TH_Faces;
namespace WIDESEAWCS_TelescopicService
@@ -37,6 +38,7 @@
        /// <returns></returns>
        public WebResponseContent automation(string ExtendedState)
        {
            try
            {
@@ -48,90 +50,128 @@
                var devices = GetDevicesByDeptId(deid);
                //找到Dt_Parameters中的最新一条数据
                var para = BaseDal.QueryData().Where(x=>x.Deptid== deid).OrderByDescending(x => x.CreateDate).FirstOrDefault();
                var para = BaseDal.QueryData().Where(x => x.Deptid == deid).OrderByDescending(x => x.CreateDate).FirstOrDefault();
                if (para == null)
                    return new WebResponseContent { Status = false, Message = "未找到参数配置" };
                int ext = (int)para.ExtendSpeed;//左PLC自动伸出缩回速度
                int ret = (int)para.RetractionSpeed;//右PLC自动伸出缩回速度
                int liftPosition= (int)para.LeftPosition;//左伸出位置
                int rigtpostition= (int)para.RightPosition;//右伸出位置
                var leftorigin = devices.Value.left.Communicator.Read<bool>("M115");//左plc到原点位置信号
                var rightorigin = devices.Value.right.Communicator.Read<bool>("M115");//右plc到达原点位置信号
                var lefttarget = devices.Value.left.Communicator.Read<bool>("M116");//左plc到达目标位置
                var righttarget = devices.Value.right.Communicator.Read<bool>("M116");//右plc到达目标位置
                int liftPosition = (int)para.LeftPosition;//左伸出位置
                int rigtpostition = (int)para.RightPosition;//右伸出位置
                string message = "";
                if (leftorigin == true || rightorigin == true)
                try
                {
                    return new WebResponseContent { Status = false, Message = "已到原始点" };
                    if (devices.Value.left.Communicator.Read<bool>("M117"))
                    {
                        return new WebResponseContent { Status = true, Message = "现在正在进行中,等待操作完成" };
                    }
                }
                if (lefttarget == true || righttarget == true)
                catch (Exception)
                {
                    return new WebResponseContent { Status = false, Message = "已到最大值" };
                }
                try
                {
                    if (devices.Value.right.Communicator.Read<bool>("M117"))
                    {
                        return new WebResponseContent { Status = true, Message = "现在正在进行中,等待操作完成" };
                    }
                }
                catch (Exception)
                {
                }
                if (ExtendedState == "伸出")
                {
                    try
                    {
                        devices.Value.left.Communicator.Write<int>("D1001", ext);//左PLC自动伸出缩回速度
                        devices.Value.left.Communicator.Write<bool>("M102", true);//伸出按钮
                        devices.Value.left.Communicator.Write<int>("D1002", liftPosition);//左伸出位置
                    }
                    catch (Exception)
                    {
                    }
                    try
                    {
                        devices.Value.right.Communicator.Write<int>("D1001", ret);//右PLC自动伸出缩回速度
                        devices.Value.right.Communicator.Write<bool>("M102", true);//伸出按钮
                        devices.Value.right.Communicator.Write<int>("D1002", rigtpostition);//右伸出位置
                    }
                    catch (Exception)
                    {
                    }
                        try
                        {
                        if (devices.Value.left.Communicator.Read<float>("D80")>= liftPosition-5)//到达最大位置)
                        {
                            message += "左PLC已到最大位置;";
                        }
                        else
                        {
                            devices.Value.left.Communicator.Write<int>("D1022", liftPosition);//伸出位置
                            devices.Value.left.Communicator.Write<int>("D1002", ext);//左PLC自动动伸出缩回速度
                            devices.Value.left.Communicator.Write<bool>("M102", true);//伸出按钮
                        }
                        }
                        catch { }
                        try
                        {
                        if (devices.Value.right.Communicator.Read<float>("D80") >= rigtpostition-5)
                        {
                            message += "右PLC已到最大位置;";
                        }
                        else
                        {
                            devices.Value.right.Communicator.Write<int>("D1022", rigtpostition);
                            devices.Value.right.Communicator.Write<int>("D1002", ret);
                            devices.Value.right.Communicator.Write<bool>("M102", true);
                        }                        }
                        catch { }
                    
                    Console.WriteLine($"自动伸缩杆伸出 {ext},右伸出位置{liftPosition},右伸出位置{rigtpostition}");
                }
                else if (ExtendedState == "缩回")
                {
                    try
                    {
                        devices.Value.left.Communicator.Write<int>("D1001", ext);//左PLC自动伸出缩回速度
                        devices.Value.left.Communicator.Write<bool>("M103", true);//缩回按钮
                    }
                    catch (Exception)
                    {
                    }
                    try
                    {
                        devices.Value.right.Communicator.Write<int>("D1001", ret);//右PLC自动伸出缩回速度
                        devices.Value.right.Communicator.Write<bool>("M103", true);//缩回按钮
                    }
                    catch (Exception)
                    {
                    }
                    
                    Console.WriteLine($"自动伸缩杆缩回 {ret}");
                }
                        try
                        {
                        if (devices.Value.left.Communicator.Read<bool>("M115")) //到达原点位
                        {
                            message += "左伸缩杆已到原点;";
                        }
                        else
                        {
                            devices.Value.left.Communicator.Write<int>("D1022", liftPosition);
                            devices.Value.left.Communicator.Write<int>("D1002", ext);//左PLC自动伸=缩回出缩回速度
                            devices.Value.left.Communicator.Write<bool>("M103", true);//左plc缩回按钮
                        }
                    }
                        catch { }
                        try
                        {
                        if (devices.Value.right.Communicator.Read<bool>("M115"))
                        {
                            message += "右伸缩杆已到原点;";
                        }
                        else
                        {
                            devices.Value.right.Communicator.Write<int>("D1022", rigtpostition);
                            devices.Value.right.Communicator.Write<int>("D1002", ret);
                            devices.Value.right.Communicator.Write<bool>("M103", true);
                        }
                        }
                        catch { }
                }
                else
                {
                    return new WebResponseContent { Status = false, Message = "未知的操作命令" };
                }
                return new WebResponseContent { Status = true, Message = "成功",
                    Data = new
                    {
                        ExtendSpeed = ext,//
                        RetractionSpeed = ret,
                    }
                return new WebResponseContent
                {
                    Status = true,
                    Message = string.IsNullOrEmpty(message) ? "执行成功" : $"{message}",
                    Data = new { ExtendSpeed = ext, RetractionSpeed = ret,liftweiz= liftPosition,rightweiz= rigtpostition }
                };
            }
            catch (Exception ex)
            {
@@ -168,56 +208,124 @@
                var para = BaseDal.QueryData().Where(x => x.Deptid == deid).OrderByDescending(x => x.CreateDate).FirstOrDefault();
                int ext = (int)para.ManualExtend;//左PLC手动伸出缩回速度
                int ret = (int)para.ManualRetraction;//右PLC手动伸出缩回速度
                var leftorigin = devices.Value.left.Communicator.Read<bool>("M115");//左plc到原点位置信号
                var rightorigin = devices.Value.right.Communicator.Read<bool>("M115");//右plc到达原点位置信号
                var lefttarget = devices.Value.left.Communicator.Read<bool>("M116");//左plc到达目标位置
                var righttarget = devices.Value.right.Communicator.Read<bool>("M116");//右plc到达目标位置
                int liftPosition = (int)para.LeftPosition;//左伸出位置
                int rigtpostition = (int)para.RightPosition;//右伸出位置
                //bool canLeft = true,
                // canRight = true;
                string message = "";
                if (leftorigin==true|| rightorigin==true)
                try
                {
                    return new WebResponseContent { Status = false, Message = "已到原始点" };
                    if (devices.Value.left.Communicator.Read<bool>("M117")) //默认是flase
                    {
                        return new WebResponseContent { Status = true, Message = "现在正在进行中,等待操作完成" };
                    }
                }
                if(lefttarget==true|| righttarget == true)
                catch (Exception)
                {
                    return new WebResponseContent { Status = false, Message = "已到最大值" };
                }
                try
                {
                    if (devices.Value.right.Communicator.Read<bool>("M117"))
                    {
                        return new WebResponseContent { Status = false, Message = "现在正在进行中,等待操作完成" };
                    }
                }
                catch (Exception)
                {
                }
                if (position == "左" && ExtendedState == "伸出")
                {
                    devices.Value.left.Communicator.Write<int>("D1000", ext);//左PLC手动伸出缩回速度
                    devices.Value.left.Communicator.Write<bool>("M100", true);//伸出按钮
                    Console.WriteLine($"{position}伸缩杆手动伸出{ext}");
                    if (devices.Value.left.Communicator.Read<float>("D80") >= liftPosition - 5) //到达目标位置
                    {
                        message += "左伸缩杆到达目标位置;";
                    }
                    else
                    {
                        devices.Value.left.Communicator.Write<int>("D1004", liftPosition);//左伸出位置
                        devices.Value.left.Communicator.Write<int>("D1000", ext);//左PLC手动伸出缩回速度
                        devices.Value.left.Communicator.Write<bool>("M100", true);//伸出按钮
                        Thread.Sleep(100); // 等待100ms(模拟按钮按下)
                        devices.Value.left.Communicator.Write<bool>("M100", false); // 释放按钮
                        Console.WriteLine($"{position}伸缩杆手动伸出{ext}");
                    }
                }
                else if (position == "左" && ExtendedState == "缩回")
                {
                    devices.Value.left.Communicator.Write<int>("D1000", ext);//左PLC手动伸出缩回速度
                    devices.Value.left.Communicator.Write<bool>("M101", true);
                    Console.WriteLine($"{position}伸缩杆手动缩回{ext}");
                    if (devices.Value.left.Communicator.Read<bool>("M115"))//缩回的时候判断)
                    {
                        message += "左PLC已到原点;";
                    }
                    else
                    {
                        devices.Value.left.Communicator.Write<int>("D1004", liftPosition);//伸出位置
                        devices.Value.left.Communicator.Write<int>("D1000", ext);//左PLC手动伸出缩回速度
                        devices.Value.left.Communicator.Write<bool>("M101", true);
                        Thread.Sleep(100); // 等待100ms(模拟按钮按下)
                        devices.Value.left.Communicator.Write<bool>("M101", false); // 释放按钮
                        Console.WriteLine($"{position}伸缩杆手动缩回{ext}");
                    }
                }
                else if (position == "右" && ExtendedState == "伸出")
                if (position == "右" && ExtendedState == "伸出")
                {
                    devices.Value.right.Communicator.Write<int>("D1000", ret);//右PLC手动伸出缩回速度
                    devices.Value.right.Communicator.Write<bool>("M100", true);
                    Console.WriteLine($"{position}伸缩杆手动伸出{ret}");
                    if (devices.Value.right.Communicator.Read<float>("D80") >= rigtpostition - 5)
                    {
                        message += "右伸缩杆到达目标位置;";
                    }
                    else
                    {
                        devices.Value.right.Communicator.Write<int>("D1004", rigtpostition);//右伸出位置
                        devices.Value.right.Communicator.Write<int>("D1000", ret);//右PLC手动伸出缩回速度
                        devices.Value.right.Communicator.Write<bool>("M100", true);
                        Thread.Sleep(100); // 等待100ms(模拟按钮按下)
                        devices.Value.right.Communicator.Write<bool>("M100", false); // 释放按钮
                        Console.WriteLine($"{position}伸缩杆手动伸出{ret}");
                    }
                }
                else if (position == "右" && ExtendedState == "缩回")
                {
                    devices.Value.right.Communicator.Write<int>("D1000", ret);//右PLC手动伸出缩回速度
                    devices.Value.right.Communicator.Write<bool>("M101", true);
                    Console.WriteLine($"{position}伸缩杆手动缩回{ret}");
                    if (devices.Value.right.Communicator.Read<bool>("M115"))
                    {
                        message += "右伸缩杆已到原点;";
                    }
                    else
                    {
                        devices.Value.right.Communicator.Write<int>("D1004", rigtpostition);//伸出位置
                        devices.Value.right.Communicator.Write<int>("D1000", ret);//右PLC手动伸出缩回速度
                        devices.Value.right.Communicator.Write<bool>("M101", true);
                        Thread.Sleep(100); // 等待100ms(模拟按钮按下)
                        devices.Value.right.Communicator.Write<bool>("M101", false); // 释放按钮
                        Console.WriteLine($"{position}伸缩杆手动缩回{ret}");
                    }
                }
                return new WebResponseContent { Status = true,
                    Message = "成功" ,
                return new WebResponseContent
                {
                    Status = true,
                    Message = string.IsNullOrEmpty(message) ? "执行成功" : $"{message}",
                    Data = new
                    {
                        ManualExtend = ext,
                        ManualRetraction = ret
                        ManualRetraction = ret,
                        message
                    }
                };
            }
            catch (Exception ex)
            {
                return new WebResponseContent { Status = false, Message = ex.Message };