using System; using System.Collections.Generic; using System.DirectoryServices; using System.Linq; using System.Text; using System.Threading.Tasks; using AutoMapper; using OfficeOpenXml.FormulaParsing.Excel.Functions.Text; using SqlSugar; using WIDESEAWCS_Core; using WIDESEAWCS_Core.BaseRepository; using WIDESEAWCS_Core.BaseServices; using WIDESEAWCS_Core.Helper; using WIDESEAWCS_ITelescopicService; using WIDESEAWCS_Model.Models; using WIDESEAWCS_QuartzJob; namespace WIDESEAWCS_TelescopicService { public class ParametersService : ServiceBase>, IParametersService { public IRepository Repository => BaseDal; private readonly IRepository _user; public ParametersService(IRepository BaseDal, IRepository user) : base(BaseDal) { _user = user; } /// /// 自动控制伸缩杆的伸出和缩回速度(一次性控制所有的) /// /// 伸/出状态 /// public WebResponseContent automation(string extendedState) { var messages = new List(); //string message = ""; try { for (int deptId = 1; deptId <= 5; deptId++) { try { var devices = GetDevicesByDeptId(deptId); if (devices == null) { messages.Add($"DeptId={deptId} 未找到设备,跳过"); continue; } var para = BaseDal.QueryData() .Where(x => x.Deptid == deptId) .OrderByDescending(x => x.CreateDate) .FirstOrDefault(); if (para == null) { messages.Add($"DeptId={deptId} 未找到参数配置,跳过"); continue; } int ext = (int)para.ExtendSpeed; int ret = (int)para.RetractionSpeed; int liftPos = (int)para.LeftPosition; int rightPos = (int)para.RightPosition; // 独立判断左/右是否可执行 bool canLeft = true, canRight = true; try { var leftOrigin = devices.Value.left.Communicator.Read("M115");//到达原点 if (leftOrigin) { messages.Add($"{deptId}轨道左PLC已到原点"); canLeft = false; } } catch (Exception) { } try { var rightOrigin = devices.Value.right.Communicator.Read("M115"); if (rightOrigin) { messages.Add($"{deptId}轨道右PLC已到原点"); canRight = false; } } catch (Exception) { } try { var lefttarget = devices.Value.left.Communicator.Read("M116");//左plc到达目标位置 if (lefttarget) { messages.Add($"{deptId}轨道左plc到达目标位置"); canLeft = false; } } catch (Exception) { } try { var righttarget = devices.Value.right.Communicator.Read("M116");//右plc到达目标位置 if (righttarget) { messages.Add($"{deptId}轨道右plc到达目标位置"); canRight = false; } } catch (Exception) { } if (extendedState == "伸出") { if (canLeft) { try { devices.Value.left.Communicator.Write("D1001", ext); devices.Value.left.Communicator.Write("M102", true); devices.Value.left.Communicator.Write("D1002", liftPos); } catch (Exception) { } } if (canRight) { try { devices.Value.right.Communicator.Write("D1001", ret); devices.Value.right.Communicator.Write("M102", true); devices.Value.right.Communicator.Write("D1002", rightPos); } catch (Exception) { } } } else if (extendedState == "缩回") { if (canLeft) { try { devices.Value.left.Communicator.Write("D1001", ext); devices.Value.left.Communicator.Write("M103", true); } catch (Exception) { } } if (canRight) { try { devices.Value.right.Communicator.Write("D1001", ret); devices.Value.right.Communicator.Write("M103", true); } catch (Exception) { } } } else { return new WebResponseContent { Status = false, Message = "未知操作命令" }; } } catch (Exception exDept) { messages.Add($"DeptId={deptId} 异常:{exDept.Message}"); } } return new WebResponseContent { Status = true, Message = messages.Count == 0 ? "执行成功" : "部分执行失败:" + string.Join(";", messages), Data = messages }; } catch (Exception ex) { return new WebResponseContent { Status = false, Message = $"自动控制异常:{ex.Message}" }; } } /// /// 手动控制,伸缩杆的缩回和伸出速度 /// /// 伸缩杆的位置(左右) /// 伸/缩状态 /// 股道号 /// public WebResponseContent ManualOperation(string position, string ExtendedState,int DeptId) { try { var devices = GetDevicesByDeptId(DeptId); if (devices == null) return new WebResponseContent { Status = false, Message = "设备未找到" }; var para = BaseDal.QueryData(x=>x.Deptid== DeptId).OrderByDescending(x => x.CreateDate).FirstOrDefault(); if (para == null) return new WebResponseContent { Status = false, Message = "参数未配置" }; int ext = (int)para.ManualExtend;//左PLC手动伸出缩回速度 int ret = (int)para.ManualRetraction;//右PLC手动伸出缩回速度 int liftPosition = (int)para.LeftPosition;//左伸出位置 int rigtpostition = (int)para.RightPosition;//右伸出位置 bool canLeft = true, canRight = true; string message = ""; try { var leftOrigin = devices.Value.left.Communicator.Read("M115"); if (leftOrigin) { message += "左PLC已到原点;"; canLeft = false; } } catch (Exception) { } try { var rightOrigin = devices.Value.right.Communicator.Read("M115"); if (rightOrigin) { message += "右PLC已到原点;"; canRight = false; } } catch (Exception) { } try { var lefttarget = devices.Value.left.Communicator.Read("M116");//左plc到达目标位置 if (lefttarget) { message += "左plc到达目标位置;"; canLeft = false; } } catch (Exception) { } try { var righttarget = devices.Value.right.Communicator.Read("M116");//右plc到达目标位置 if (righttarget) { message += "右plc到达目标位置;"; canRight = false; } } catch (Exception) { } if (canLeft) { if (position == "左" && ExtendedState == "伸出") { devices.Value.left.Communicator.Write("D1000", ext);//左PLC手动伸出缩回速度 devices.Value.left.Communicator.Write("M100", true);//伸出按钮 Thread.Sleep(100); // 等待100ms(模拟按钮按下) devices.Value.left.Communicator.Write("M100", false); // 释放按钮 devices.Value.left.Communicator.Write("D1002", liftPosition);//左伸出位置 Console.WriteLine($"{position}伸缩杆手动伸出{ext}"); } else if (position == "左" && ExtendedState == "缩回") { devices.Value.left.Communicator.Write("D1000", ext);//左PLC手动伸出缩回速度 devices.Value.left.Communicator.Write("M101", true); Thread.Sleep(100); // 等待100ms(模拟按钮按下) devices.Value.left.Communicator.Write("M101", false); // 释放按钮 Console.WriteLine($"{position}伸缩杆手动缩回{ext}"); } } if (canRight) { if (position == "右" && ExtendedState == "伸出") { devices.Value.right.Communicator.Write("D1000", ret);//右PLC手动伸出缩回速度 devices.Value.right.Communicator.Write("M100", true); Thread.Sleep(100); // 等待100ms(模拟按钮按下) devices.Value.right.Communicator.Write("M100", false); // 释放按钮 devices.Value.right.Communicator.Write("D1002", liftPosition);//左伸出位置 Console.WriteLine($"{position}伸缩杆手动伸出{ret}"); } else if (position == "右" && ExtendedState == "缩回") { devices.Value.right.Communicator.Write("D1000", ret);//右PLC手动伸出缩回速度 devices.Value.right.Communicator.Write("M101", true); Thread.Sleep(100); // 等待100ms(模拟按钮按下) devices.Value.right.Communicator.Write("M101", false); // 释放按钮 Console.WriteLine($"{position}伸缩杆手动缩回{ret}"); } } else { return new WebResponseContent { Status = false, Message = "状态参数错误" }; } return new WebResponseContent { Status = true, Message = string.IsNullOrEmpty(message) ? "执行成功" : $"执行失败:{message}", Data = new { ManualExtend = ext, ManualRetraction = ret, message } }; } catch (Exception ex) { return new WebResponseContent { Status = false, Message = ex.Message }; } } /// ///当自动伸出需要暂停时,暂停按钮 /// /// public WebResponseContent PauseButton(int deptid) { try { //根据int.TryParse(AppSettings.Get("DepartmentID")读取到当前是轨道几,所有人都只能操作当前 var devices = GetDevicesByDeptId(deptid); try { devices.Value.left.Communicator.Write("M104", true); } catch (Exception) { throw; } try { devices.Value.right.Communicator.Write("M104", true); } catch (Exception) { throw; } return new WebResponseContent { Status = true, Message = "暂停成功", Data = devices }; } catch (Exception ex) { return new WebResponseContent { Status = false, Message = ex.Message }; } } /// /// 伸缩杆当前位置(这个没有用) /// 轨道站号 public WebResponseContent CurrentLocation(int deptid) { try { var devices = GetDevicesByDeptId(deptid); if (devices == null) return new WebResponseContent { Status = false, Message = "设备未找到" }; var deviceslift = devices.Value.left.Communicator.Read("D80"); var devicesright = devices.Value.right.Communicator.Read("D80"); Console.WriteLine($"当前左右伸缩杆返回速度分别为为{deviceslift}"); return new WebResponseContent { Status = true, Data = new { lift = deviceslift, Right = devicesright } }; } catch (Exception ex) { return new WebResponseContent { Status=false,Message=ex.Message}; } } /// /// 获取设备号 /// /// /// public (OtherDevice left, OtherDevice right)? GetDevicesByDeptId(int deptId) { // 左设备号:1 -> 001,2 -> 003,3 -> 005... int baseCode = 1 + (deptId - 1) * 2; string leftCode = $"SSG{baseCode.ToString("D3")}"; //右设备:1->002 ,2->004 string rightCode = $"SSG{(baseCode + 1).ToString("D3")}"; var left = (OtherDevice)Storage.Devices.Find(x => x.DeviceCode == leftCode); var right = (OtherDevice)Storage.Devices.Find(x => x.DeviceCode == rightCode); if (left == null || right == null) return null; return (left, right); } } }