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<Dt_Parameters, IRepository<Dt_Parameters>>, IParametersService
|
{
|
public IRepository<Dt_Parameters> Repository => BaseDal;
|
private readonly IRepository<Sys_User> _user;
|
|
public ParametersService(IRepository<Dt_Parameters> BaseDal, IRepository<Sys_User> user) : base(BaseDal)
|
{
|
_user = user;
|
}
|
|
|
/// <summary>
|
/// 自动控制伸缩杆的伸出和缩回速度(一次性控制所有的)
|
/// </summary>
|
/// <param name="ExtendedState">伸/出状态</param>
|
/// <returns></returns>
|
public WebResponseContent automation(string extendedState)
|
{
|
try
|
{
|
for (int deptId = 1; deptId <= 5; deptId++)
|
{
|
var devices = GetDevicesByDeptId(deptId);
|
if (devices == null)
|
{
|
Console.WriteLine($"DeptId={deptId} 未找到设备,跳过");
|
continue;
|
}
|
|
var para = BaseDal.QueryData()
|
.Where(x => x.Deptid == deptId)
|
.OrderByDescending(x => x.CreateDate)
|
.FirstOrDefault();
|
|
if (para == null)
|
{
|
Console.WriteLine($"DeptId={deptId} 未找到参数配置,跳过");
|
continue;
|
}
|
|
int ext = (int)para.ExtendSpeed;//左PLC自动伸出缩回速度
|
int ret = (int)para.RetractionSpeed;//右PLC自动伸出缩回速度
|
int liftPosition = (int)para.LeftPosition;//左伸出位置
|
int rigtpostition = (int)para.RightPosition;//右伸出位置
|
|
try
|
{
|
var leftorigin = devices.Value.left.Communicator.Read<bool>("M115");//左plc到原点位置信号
|
var rightorigin = devices.Value.right.Communicator.Read<bool>("M115");//右plc到达原点位置信号
|
if (leftorigin == true || rightorigin == true)
|
{
|
return new WebResponseContent { Status = false, Message = "已到原始点" };
|
}
|
}
|
catch (Exception)
|
{
|
|
|
}
|
try
|
{
|
var lefttarget = devices.Value.left.Communicator.Read<bool>("M116");//左plc到达目标位置
|
var righttarget = devices.Value.right.Communicator.Read<bool>("M116");//右plc到达目标位置
|
if (lefttarget == true || righttarget == true)
|
{
|
return new WebResponseContent { Status = false, 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)
|
{
|
|
throw;
|
}
|
|
|
|
Console.WriteLine($"DeptId={deptId} ,左速度={ext},右速度={ret}");
|
}
|
else if (extendedState == "缩回")
|
{
|
try
|
{
|
devices.Value.left.Communicator.Write<int>("D1001", ext);//缩回速度
|
devices.Value.left.Communicator.Write<bool>("M103", true);//缩回按钮
|
}
|
catch (Exception)
|
{
|
|
throw;
|
}
|
|
try
|
{
|
devices.Value.right.Communicator.Write<int>("D1001", ret);
|
devices.Value.right.Communicator.Write<bool>("M103", true);
|
}
|
catch (Exception)
|
{
|
|
throw;
|
}
|
Console.WriteLine($"DeptId={deptId} 缩回操作,左速度={ext},右速度={ret}");
|
}
|
else
|
{
|
return new WebResponseContent { Status = false, Message = "未知的操作命令" };
|
}
|
}
|
|
return new WebResponseContent { Status = true, Message = "全部设备操作完成",
|
};
|
}
|
catch (Exception ex)
|
{
|
return new WebResponseContent { Status = false, Message = $"操作异常:{ex.Message}" };
|
}
|
}
|
|
|
|
|
|
/// <summary>
|
/// 手动控制,伸缩杆的缩回和伸出速度
|
/// </summary>
|
/// <param name="position">伸缩杆的位置(左右)</param>
|
/// <param name="ExtendedState">伸/缩状态</param>
|
/// <param name="DeptId">股道号</param>
|
/// <returns></returns>
|
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手动伸出缩回速度
|
try
|
{
|
var leftorigin = devices.Value.left.Communicator.Read<bool>("M115");//左plc到原点位置信号
|
var rightorigin = devices.Value.right.Communicator.Read<bool>("M115");//右plc到达原点位置信号
|
if (leftorigin == true || rightorigin == true)
|
{
|
return new WebResponseContent { Status = false, Message = "已到原始点" };
|
}
|
}
|
catch (Exception)
|
{
|
|
|
}
|
try
|
{
|
var lefttarget = devices.Value.left.Communicator.Read<bool>("M116");//左plc到达目标位置
|
var righttarget = devices.Value.right.Communicator.Read<bool>("M116");//右plc到达目标位置
|
if (lefttarget == true || righttarget == true)
|
{
|
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}左PLC自动伸出缩回速度{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}左PLC自动伸出缩回速度{ext}");
|
}
|
else if (position == "右" && ExtendedState == "伸出")
|
{
|
devices.Value.right.Communicator.Write<int>("D1000", ret);//右PLC手动伸出缩回速度
|
devices.Value.right.Communicator.Write<bool>("M100", true);
|
Console.WriteLine($"{position}右PLC自动伸出缩回速度{ext}");
|
}
|
else if (position == "右" && ExtendedState == "缩回")
|
{
|
devices.Value.right.Communicator.Write<int>("D1000", ret);//右PLC手动伸出缩回速度
|
devices.Value.right.Communicator.Write<bool>("M101", true);
|
Console.WriteLine($"{position}右PLC自动伸出缩回速度{ret}");
|
}
|
|
else
|
{
|
return new WebResponseContent { Status = false, 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>
|
/// <returns></returns>
|
public WebResponseContent PauseButton(int deptid)
|
{
|
try
|
{
|
//根据int.TryParse(AppSettings.Get("DepartmentID")读取到当前是轨道几,所有人都只能操作当前
|
var devices = GetDevicesByDeptId(deptid);
|
try
|
{
|
devices.Value.left.Communicator.Write<bool>("M104", true);
|
}
|
catch (Exception)
|
{
|
|
throw;
|
}
|
|
try
|
{
|
devices.Value.right.Communicator.Write<bool>("M104", true);
|
}
|
catch (Exception)
|
{
|
|
throw;
|
}
|
return new WebResponseContent { Status = true, Message = "暂停成功", Data = devices };
|
}
|
catch (Exception ex)
|
{
|
|
return new WebResponseContent { Status = false, Message = ex.Message };
|
}
|
}
|
|
|
/// <summary>
|
/// 伸缩杆当前位置(这个没有用)
|
/// <param name="deptid">轨道站号</param>
|
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<float>("D80");
|
var devicesright = devices.Value.right.Communicator.Read<float>("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};
|
}
|
}
|
|
|
/// <summary>
|
/// 获取设备号
|
/// </summary>
|
/// <param name="deptId"></param>
|
/// <returns></returns>
|
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);
|
}
|
|
|
}
|
|
}
|