yanjinhui
2025-05-15 bdc88e08328c360c2bddeedde744d858a7fd7e3d
project/WCS/WIDESEAWCS_Server/WIDESEAWCS_TelescopicService/ParametersService.cs
@@ -10,6 +10,7 @@
using WIDESEAWCS_Core.BaseRepository;
using WIDESEAWCS_Core.BaseServices;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_DTO.Telescopic;
using WIDESEAWCS_ITelescopicService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
@@ -50,23 +51,23 @@
                if (para == null)
                    return new WebResponseContent { Status = false, Message = "未找到参数配置" };
                int ext = (int)para.ExtendSpeed;
                int ret = (int)para.RetractionSpeed;
                int ext = (int)para.ExtendSpeed;//左PLC自动伸出缩回速度
                int ret = (int)para.RetractionSpeed;//右PLC自动伸出缩回速度
                if (ExtendedState == "伸出")
                {
                    devices.Value.left.Communicator.Write<int>("D500", ext);//伸出速度
                    devices.Value.left.Communicator.Write<bool>("M100", true);//伸出按钮
                    devices.Value.right.Communicator.Write<int>("D500", ext);//伸出速度
                    devices.Value.right.Communicator.Write<bool>("M100", true);//伸出按钮
                    devices.Value.left.Communicator.Write<int>("D1001", ext);//左PLC自动伸出缩回速度
                    devices.Value.left.Communicator.Write<bool>("M102", true);//伸出按钮
                    devices.Value.right.Communicator.Write<int>("D1001", ret);//右PLC自动伸出缩回速度
                    devices.Value.right.Communicator.Write<bool>("M102", true);//伸出按钮
                    Console.WriteLine($"自动伸缩杆伸出 {ext}");
                }
                else if (ExtendedState == "缩回")
                {
                    devices.Value.left.Communicator.Write<int>("D501", ext);//伸出速度
                    devices.Value.left.Communicator.Write<bool>("M101", true);//伸出按钮
                    devices.Value.right.Communicator.Write<int>("D501", ext);//伸出速度
                    devices.Value.right.Communicator.Write<bool>("M101", true);//伸出按钮
                    devices.Value.left.Communicator.Write<int>("D1001", ext);//左PLC自动伸出缩回速度
                    devices.Value.left.Communicator.Write<bool>("M103", true);//缩回按钮
                    devices.Value.right.Communicator.Write<int>("D1001", ret);//右PLC自动伸出缩回速度
                    devices.Value.right.Communicator.Write<bool>("M103", true);//缩回按钮
                    Console.WriteLine($"自动伸缩杆缩回 {ret}");
                }
                else
@@ -74,7 +75,13 @@
                    return new WebResponseContent { Status = false, Message = "未知的操作命令" };
                }
                return new WebResponseContent { Status = true, Message = "成功" };
                return new WebResponseContent { Status = true, Message = "成功",
                    Data = new
                    {
                        ExtendSpeed = ext,//
                        RetractionSpeed = ret
                    }
                };
            }
            catch (Exception ex)
            {
@@ -106,42 +113,116 @@
                //俩个plc左右各一个
                //OtherDevice liftotherDevice = (OtherDevice)Storage.Devices.Find(x => x.DeviceCode == "SSG001");
                //OtherDevice rightotherDevice = (OtherDevice)Storage.Devices.Find(x => x.DeviceCode == "SSG002");
                //找到Dt_Parameters中的最新一条数据根据当前的deid
                var para = BaseDal.QueryData().Where(x => x.Deptid == deid).OrderByDescending(x => x.CreateDate).FirstOrDefault();
                int ext = (int)para.ManualExtend;//手动伸出速度
                int ret = (int)para.ManualRetraction;//手动缩回速度
                int ext = (int)para.ManualExtend;//左PLC手动伸出缩回速度
                int ret = (int)para.ManualRetraction;//右PLC手动伸出缩回速度
                if (position == "左" && ExtendedState == "伸出")
                {
                    devices.Value.left.Communicator.Write<int>("D500", ext);//伸出速度
                    devices.Value.left.Communicator.Write<int>("D1000", ext);//左PLC手动伸出缩回速度
                    devices.Value.left.Communicator.Write<bool>("M100", true);//伸出按钮
                    Console.WriteLine($"{position}伸缩杆手动伸出{ext}");
                    Console.WriteLine($"{position}伸缩杆手动伸出缩回{ext}");
                }
                else if (position == "左" && ExtendedState == "缩回")
                {
                    devices.Value.left.Communicator.Write<int>("D501", ret);//缩回速度
                    devices.Value.left.Communicator.Write<int>("D1000", ext);//左PLC手动伸出缩回速度
                    devices.Value.left.Communicator.Write<bool>("M101", true);
                    Console.WriteLine($"{position}伸缩杆手动缩回{ret}");
                    Console.WriteLine($"{position}伸缩杆手动伸出缩回{ext}");
                }
                else if (position == "右" && ExtendedState == "伸出")
                {
                    devices.Value.right.Communicator.Write<int>("D500", ext);
                    devices.Value.right.Communicator.Write<int>("D1000", ret);//右PLC手动伸出缩回速度
                    devices.Value.right.Communicator.Write<bool>("M100", true);
                    Console.WriteLine($"{position}伸缩杆手动伸出{ext}");
                    Console.WriteLine($"{position}伸缩杆手动伸出{ret}");
                }
                else if (position == "右" && ExtendedState == "缩回")
                {
                    devices.Value.right.Communicator.Write<int>("D501", ret);
                    devices.Value.right.Communicator.Write<int>("D1000", ret);//右PLC手动伸出缩回速度
                    devices.Value.right.Communicator.Write<bool>("M101", true);
                    Console.WriteLine($"{position}伸缩杆手动缩回{ret}");
                }
                return new WebResponseContent { Status = true, Message = "成功" };
                return new WebResponseContent { Status = true,
                    Message = "成功" ,
                    Data = new
                    {
                        ManualExtend = ext,
                        ManualRetraction = ret
                    }
                };//这里要加返回速度
            }
            catch (Exception ex)
            {
                return new WebResponseContent { Status = false, Message = ex.Message };
            }
        }
        /// <summary>
        /// 根据用户添加速度
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public  WebResponseContent AddSpeed(SpeedDTO entity)
        {
            try
            {
                // 获取当前登录用户
                var user = _user.QueryData(x => x.UserName == entity.account).FirstOrDefault();
               // 判断:如果不是管理员 && Dept_Id 为空,则提示错误
                if (user.UserName != "admin" && user.Dept_Id == null)
                {
                    return new WebResponseContent { Status = false, Message = "你的账号没有设置轨道站号,无法添加,请联系管理员" };
                }
                var dept = user.Dept_Id;
                //  获取配置文件中的 DepartmentID(管理员专用)
                if (!int.TryParse(AppSettings.Get("DepartmentID"), out int deid))
                {
                    return new WebResponseContent
                    {
                        Status = false,
                        Message = "配置项 DepartmentID 无法转换为整数"
                    };
                }
                // 判断是否为管理员账号
                bool isAdmin = user?.UserName?.Equals("admin") == true; //如果当前用户不为空,且用户名是 "admin",就认为这个用户是管理员
                // 根据是否是管理员决定使用哪个 Deptid
                var parmet = new Dt_Parameters
                {
                    ExtendSpeed = entity.ExtendSpeed,
                    RetractionSpeed = entity.RetractionSpeed,
                    ManualExtend = entity.ManualExtend,
                    ManualRetraction = entity.ManualRetraction,
                    Deptid = isAdmin ? deid : dept.GetValueOrDefault()//如果当前用户是管理员,就用配置文件中的 DepartmentID,否则用传入的 entity 参数中的 Deptid
                };
                // 添加数据
                AddData(parmet);
                return new WebResponseContent
                {
                    Status = true,
                    Message = "添加成功",
                    Data = parmet
                };
            }
            catch (Exception ex)
            {
                return new WebResponseContent { Status = false, Message = ex.Message };
            }
        }
        /// <summary>
@@ -165,6 +246,30 @@
            return (left, right);
        }
        public WebResponseContent BackfillSpeed()
        {
            try
            {
                if (!int.TryParse(AppSettings.Get("DepartmentID"), out int deid))
                {
                    return new WebResponseContent { Status = false, Message = "配置项 DepartmentID 无法转换为整数" };
                }
                var para = BaseDal.QueryData().Where(x => x.Deptid == deid).OrderByDescending(x => x.CreateDate).FirstOrDefault();
                if (para==null)
                {
                    return new WebResponseContent { Message = "你还没有配置速度" };
                }
                return new WebResponseContent { Status = true, Data = para };
            }
            catch (Exception ex)
            {
                return new WebResponseContent { Status = false, Message = ex.Message };
            }
        }
    }
}