using Newtonsoft.Json;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using WIDESEA_Comm.LogInfo;
|
using WIDESEA_Core.FreeDB;
|
using WIDESEA_Core.Utilities;
|
using WIDESEA_WCS.WCSClient;
|
|
namespace WIDESEA_WCS.Services.WCS.DianJieChiServer
|
{
|
public class ParentServer
|
{
|
FreeDB freeDB = new FreeDB();
|
private static List<PLCInfo> plcInfos;
|
|
public ParentServer()
|
{
|
if (plcInfos == null)
|
{
|
string config = freeDB.DataBase.Ado.QuerySingle<string>("select config from dt_equipmentinfo where equipment_name='母车1'");
|
plcInfos = JsonConvert.DeserializeObject<List<PLCInfo>>(config);
|
}
|
}
|
|
/// <summary>
|
/// 获取PLC信息
|
/// </summary>
|
/// <returns></returns>
|
public WebResponseContent GetParentInfo()
|
{
|
WebResponseContent content = new WebResponseContent();
|
try
|
{
|
//长链接断开后,会自动默认使用短连接
|
var client = PLCClient.Clients.Where(t => t.PLCName == "母车1").FirstOrDefault();
|
if (client == null || !client.IsConnected)
|
{
|
return content.Error($"PLC{"母车1"}未连接");
|
}
|
GetParent(client);
|
content.Data = new { info = plcInfos };
|
content.OK();
|
}
|
catch (Exception ex)
|
{
|
content.Error(ex.Message);
|
}
|
return content;
|
}
|
|
/// <summary>
|
/// 获取母车信息
|
/// </summary>
|
/// <param name="client"></param>
|
private void GetParent(PLCClient client)
|
{
|
foreach (var item in plcInfos)
|
{
|
if (item.dbAddress == "V230" || item.dbAddress == "V270")
|
{
|
item.value = client.Read<short>(item.dbAddress);
|
continue;
|
}
|
if (item.showType.Contains("bool"))
|
{
|
item.value = client.Read<bool>(item.dbAddress);
|
}
|
else if (item.showType == "int")
|
{
|
item.value = client.Read<int>(item.dbAddress);
|
}
|
}
|
}
|
|
#region 操作母车
|
/// <summary>
|
/// 启动母车
|
/// </summary>
|
/// <returns></returns>
|
public WebResponseContent Run_Parent(int point)
|
{
|
WebResponseContent content = new WebResponseContent();
|
try
|
{
|
var client = PLCClient.Clients.Where(t => t.PLCName == "母车1").FirstOrDefault();
|
if (client == null || !client.IsConnected)
|
{
|
return content.Error($"PLC{"母车1"}未连接");
|
}
|
var move_error1 = client.Read<short>("V230");//伺服1报警
|
var move_error2 = client.Read<short>("V270");//伺服2报警
|
|
if (move_error1 != 0)
|
{
|
throw new Exception($"伺服1报警码{move_error1},无法启动");
|
}
|
if (move_error2 != 0)
|
{
|
throw new Exception($"伺服2报警码{move_error2},无法启动");
|
}
|
|
client.Write("V1010.0", point);//目的地址
|
Thread.Sleep(1500);
|
client.Write("V1006.0", true);//启动命令
|
|
WriteLog.Write_Log("手动操作", "母车", "启动母车" + point.ToString());
|
content.OK("启动成功!");
|
}
|
catch (Exception ex)
|
{
|
WriteLog.Write_Log("手动操作", "母车", "启动母车异常" + point.ToString(), ex.Message);
|
content.Error(ex.Message);
|
}
|
return content;
|
}
|
|
/// <summary>
|
/// 停止母车
|
/// </summary>
|
/// <returns></returns>
|
public WebResponseContent Stop_Parent()
|
{
|
WebResponseContent content = new WebResponseContent();
|
try
|
{
|
var client = PLCClient.Clients.Where(t => t.PLCName == "母车1").FirstOrDefault();
|
if (client == null || !client.IsConnected)
|
{
|
return content.Error($"PLC{"母车1"}未连接");
|
}
|
|
client.Write("V1006.0", false);//启动命令
|
|
WriteLog.Write_Log("手动操作", "母车", "停止母车");
|
content.OK("停止成功!");
|
}
|
catch (Exception ex)
|
{
|
WriteLog.Write_Log("手动操作", "母车", "停止母车异常", ex.Message);
|
content.Error(ex.Message);
|
}
|
return content;
|
}
|
|
/// <summary>
|
/// 复位母车伺服的报警
|
/// </summary>
|
/// <returns></returns>
|
public WebResponseContent ResetMotherMove()
|
{
|
WebResponseContent content = new WebResponseContent();
|
try
|
{
|
var client = PLCClient.Clients.Where(t => t.PLCName == "母车1").FirstOrDefault();
|
if (client == null || !client.IsConnected)
|
{
|
return content.Error($"PLC{"母车1"}未连接");
|
}
|
client.Write("V1006.2", true);
|
Thread.Sleep(2000);
|
client.Write("V1006.2", false);
|
|
WriteLog.Write_Log("手动操作", "母车", "复位伺服报警");
|
content.OK("伺服复位成功!");
|
}
|
catch (Exception ex)
|
{
|
WriteLog.Write_Log("手动操作", "母车", "复位伺服报警异常", ex.Message);
|
content.Error(ex.Message);
|
}
|
return content;
|
}
|
|
/// <summary>
|
/// 母车置零
|
/// </summary>
|
/// <returns></returns>
|
public WebResponseContent SetZeroToMotherCar()
|
{
|
WebResponseContent content = new WebResponseContent();
|
try
|
{
|
var client = PLCClient.Clients.Where(t => t.PLCName == "母车1").FirstOrDefault();
|
if (client == null || !client.IsConnected)
|
{
|
return content.Error($"PLC{"母车1"}未连接");
|
}
|
|
//一个置零信号,先发1、间隔3秒再发个0
|
client.Write("V1006.1", true);
|
WriteLog.Write_Log("手动操作", "母车", "母车置零,写入1");
|
Thread.Sleep(2000);
|
client.Write("V1006.1", false);
|
WriteLog.Write_Log("手动操作", "母车", "母车置零,写入0");
|
|
content.OK("母车置零成功!");
|
}
|
catch (Exception ex)
|
{
|
WriteLog.Write_Log("手动操作", "母车", "母车置零异常", ex.Message);
|
content.Error(ex.Message);
|
}
|
return content;
|
}
|
#endregion
|
}
|
}
|