/*******************************************************************************
* Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
* 类的信息:
* 1.程序名称:PLCModuleLogic.cs
* 2.功能描述:发送信息到智能设备。
* 编辑履历:
* 作者 日期 版本 修改内容
* 陈晓野 2018/10/18 1.00 新建
*******************************************************************************/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Net.Sockets;
using System.Text;
using Curtain.Core;
using Curtain.DataAccess;
using Curtain.Framework.Json;
using Curtain.Net.Sockets.PLC;
using Curtain.Net.Sockets.PLC.Model.Siemens;
using Dongke.IBOSS.PRD.Basics.BaseResources;
using Dongke.IBOSS.PRD.Basics.DataAccess;
using Dongke.IBOSS.PRD.Service.DataModels;
using Dongke.IBOSS.PRD.WCF.DataModels;
using Microsoft.Win32;
using Newtonsoft.Json.Linq;
namespace Dongke.IBOSS.PRD.Service.CMNModuleService
{
///
/// PLC相关处理
///
public class PLCModuleLogic
{
#region 恒洁三水三车间工位对应PLC操作
///
/// 恒洁三水三车间工位对应PLC操作 xuwei modify 2020-06-29
///
///
///
public static ServiceResultEntity DoPLCWorkByUser_HEGII_S3(SUserInfo sUserInfo, string barcode,
int procedureid, int workStationType = 0, decimal weight = 0)
{
using (IDataAccess conn = DataAccess.Create())
{
//调用PLC的参数集合
Hashtable plcPara = new Hashtable();
try
{
//按当前登录用户在工位表中的当前工号查找工位类型
//workStationType = getWorkStationType(conn, sUserInfo.UserCode);
DataTable dataTable = GetWorkStationInfo(conn, sUserInfo.UserCode);
if (dataTable == null || dataTable.Rows.Count == 0)
{
return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Other, Message = "当前工号无工位信息" };
}
if (dataTable.Columns.Contains("PLC_FLAG"))
{
string plc_flag = dataTable.Rows[0]["PLC_FLAG"] + "";
if (plc_flag != "1")
{
return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Success, Message = "" /*"PLC对接未启用"*/ };
}
}
workStationType = Convert.ToInt32(dataTable.Rows[0]["WORKSTATIONTYPEID"]);
//成检交接3211
if (workStationType == 3211)
{
//读取产品型号
object goodsModel = conn.ExecuteScalar(@"
SELECT
g.SEATCOVERCODE
FROM
TP_PM_GROUTINGDAILYDETAIL p
LEFT JOIN TP_MST_GOODS g ON g.GOODSID = p.GOODSID
WHERE
p.VALUEFLAG = '1'
AND p.BARCODE = @BARCODE@
",
new CDAParameter("BARCODE", barcode)
);
plcPara.Add("usercode", sUserInfo.UserCode);
plcPara.Add("barcode", barcode);
plcPara.Add("procedureid", procedureid);
plcPara.Add("goodsmodel", goodsModel);
plcPara.Add("finishflag", 1);
return plcWrite(conn, sUserInfo.UserCode, workStationType, plcPara);
}
// 成检二检3212
// 读取plc重量 add by fubin 2020-7-20
if (workStationType == 3212)
{
if (weight > 0)
{
try
{
conn.ExecuteNonQuery(@"
INSERT INTO tp_pm_goodsweighing
(barcode
,groutingdailydetailid
,smartdevicepointcode
,procedureid
,goodsweight
,accountid
,createuserid)
SELECT @barcode@
,gd.groutingdailydetailid
,'3_2_2'
,@procedureid@
,@goodsweight@
,@accountid@
,@userid@
FROM tp_pm_groutingdailydetail gd
WHERE gd.barcode = @barcode@",
new CDAParameter("barcode", barcode),
new CDAParameter("procedureid", procedureid),
new CDAParameter("goodsweight", weight),
new CDAParameter("accountid", sUserInfo.AccountID),
new CDAParameter("userid", sUserInfo.UserID)
);
}
catch (Exception)
{
return new ServiceResultEntity()
{
Status = Constant.ServiceResultStatus.Other,
Message = "重量写入失败!"
};
}
plcPara.Add("finishflag", 1);
return plcWrite(conn, sUserInfo.UserCode, workStationType, plcPara);
}
return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Success };
}
// 3#上水3320
if (workStationType == 3320)
{
//读取产品型号
object goodsModel = conn.ExecuteScalar(@"
SELECT
g.SEATCOVERCODE
FROM
TP_PM_GROUTINGDAILYDETAIL p
LEFT JOIN TP_MST_GOODS g ON g.GOODSID = p.GOODSID
WHERE
p.VALUEFLAG = '1'
AND p.BARCODE = @BARCODE@
",
new CDAParameter("BARCODE", barcode)
);
if (goodsModel.ToInt32() == 0)
{
plcPara.Add("goodsmodel", 0);
plcPara.Add("finishflag", 1);
plcPara.Add("error", 1);
}
else
{
plcPara.Add("goodsmodel", goodsModel);
plcPara.Add("finishflag", 1);
plcPara.Add("error", 0);
}
return plcWrite(conn, sUserInfo.UserCode, workStationType, plcPara);
}
return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Other, Message = "当前工号无工位信息" };
}
catch (Exception ex)
{
Curtain.Log.Logger.Error(ex);
string plcParaStr = "";
foreach (string key in plcPara.Keys) plcParaStr += " " + key + ":" + plcPara[key].ToString() + " ";
#if DEBUG
Curtain.Log.Logger.Debug("PLC写入失败!" + plcParaStr);
#endif
return new ServiceResultEntity()
{
Status = Constant.ServiceResultStatus.Other,
Message = "PLC写入失败!" + plcParaStr
};
}
finally
{
conn?.Close();
}
}
}
///
/// 恒洁三水三车间成检二检获取PLC重量 fubin add 2020-07-20
///
///
///
public static ServiceResultEntity ReadPLCWeightByUser_HEGII_S3(SUserInfo sUserInfo)
{
using (IDataAccess conn = DataAccess.Create())
{
//调用PLC的参数集合
Hashtable plcPara = new Hashtable();
try
{
//按当前登录用户在工位表中的当前工号查找工位类型
int workStationType = getWorkStationType(conn, sUserInfo.UserCode);
// 成检二检3212
// 读取plc重量 add by fubin 2020-7-20
if (workStationType == 3212)
{
return plcRead(conn, sUserInfo.UserCode, workStationType);
}
return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Other, Result = 0 };
}
catch (Exception ex)
{
Curtain.Log.Logger.Error(ex);
return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Other, Result = 0 };
}
finally
{
conn?.Close();
}
}
}
///
/// 获取工位类型ID xuwei add 2020-06-29
///
///
///
///
private static int getWorkStationType(IDataAccess conn, string userCode)
{
return Convert.ToInt32(conn.ExecuteScalar(@"
SELECT
t.WORKSTATIONTYPEID
FROM
TP_MST_WORKSTATION w
LEFT JOIN TP_SYS_WORKSTATIONTYPE t ON w.WORKSTATIONTYPEID = t.WORKSTATIONTYPEID
WHERE
w.USERCODE = @USERCODE@
",
new CDAParameter("USERCODE", userCode)
));
}
private static DataTable GetWorkStationInfo(IDataAccess conn, string userCode)
{
DataTable dataTable = conn.ExecuteDatatable(@"
SELECT
t.workstationid,
t.workstationtypeid,
t.workstationname,
t.procedureid,
t.groutinglineid,
t.usercode,
t.pccode,
t.plcip,
t.plcport,
t.plcobject,
t.valueflag,
t.plcobjectread,
t.plc_flag
FROM
TP_MST_WORKSTATION t
WHERE
t.USERCODE =@USERCODE@
",
//new CDAParameter("WORKSTATIONTYPE", workStationType),
new CDAParameter("USERCODE", userCode)
);
return dataTable;
}
///
/// 写PLC xuwei add 2020-06-29
///
///
///
///
///
///
private static ServiceResultEntity plcWrite(IDataAccess conn, string userCode, int workStationType, Hashtable plcPara)
{
//读取工位配置信息
DataTable dataTable = conn.ExecuteDatatable(@"
SELECT --t.*
t.workstationtypeid,
t.workstationname,
t.procedureid,
t.groutinglineid,
t.usercode,
t.pccode,
t.plcip,
t.plcport,
t.plcobject,
t.valueflag,
t.plcobjectread,
t.plc_flag
FROM
TP_MST_WORKSTATION t
WHERE
t.WORKSTATIONTYPEID = @WORKSTATIONTYPE@
AND t.USERCODE =@USERCODE@
",
new CDAParameter("WORKSTATIONTYPE", workStationType),
new CDAParameter("USERCODE", userCode)
);
if (dataTable == null || dataTable.Rows.Count == 0)
{
return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Other, Message = "当前工号无工位信息" };
}
if (dataTable.Columns.Contains("PLC_FLAG"))
{
string plc_flag = dataTable.Rows[0]["PLC_FLAG"] + "";
if (plc_flag != "1")
{
return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Success, Message = "" /*"PLC对接未启用"*/ };
}
}
string ip = dataTable.Rows[0]["PLCIP"] + "";
if (string.IsNullOrWhiteSpace(ip))
{
return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Success, Message = "" /*"PLC对接未启用"*/ };
}
int port = Convert.ToInt32(dataTable.Rows[0]["PLCPORT"]);
JArray plcObj = JArray.Parse(dataTable.Rows[0]["PLCOBJECT"].ToString());
//写入PLC
using (SocketClient plc = new SocketClient())
{
//测试静态数据 测试成功
//plc.Connect(ip, port);
//plc.Write("D", "102.2", "2020070101");
//plc.Write("D", "102.14", Convert.ToInt16(10));
//手动写
//plc.Connect(ip, port);
//plc.Write("D", "102.2", plcPara["barcode"].ToString());
//plc.Write("D", "102.14", Convert.ToInt16(plcPara["goodsmodel"]));
//自动按配置写
plc.Connect(ip, port);
for (int i = 0; i < plcObj.Count; i++)
{
if (plcObj[i]["type"].ToString().ToLower() == "string")
plc.Write(plcObj[i]["code"].ToString(), plcObj[i]["number"].ToString(), plcPara[plcObj[i]["name"].ToString()].ToString());
else if (plcObj[i]["type"].ToString().ToLower() == "int")
plc.Write(plcObj[i]["code"].ToString(), plcObj[i]["number"].ToString(), Convert.ToInt16(plcPara[plcObj[i]["name"].ToString()]));
}
}
#if DEBUG
//写入操作成功日志
string plcParaStr = "";
foreach (string key in plcPara.Keys) plcParaStr += " " + key + ":" + plcPara[key].ToString() + " ";
Curtain.Log.Logger.Debug("PLC写入成功!" + plcParaStr);
#endif
//PLC操作成功
return new ServiceResultEntity()
{
Status = Constant.ServiceResultStatus.Success,
Message = "" //一定不可以有值,为空时才是提交成功
};
}
///
/// 读取PLC
///
///
///
///
private static ServiceResultEntity plcRead(IDataAccess conn, string userCode, int workStationType)
{
try
{
//读取工位配置信息
DataTable dataTable = conn.ExecuteDatatable(@"
SELECT --t.*
t.workstationtypeid,
t.workstationname,
t.procedureid,
t.groutinglineid,
t.usercode,
t.pccode,
t.plcip,
t.plcport,
t.plcobject,
t.valueflag,
t.plcobjectread,
t.plc_flag
FROM
TP_MST_WORKSTATION t
WHERE
t.WORKSTATIONTYPEID = @WORKSTATIONTYPE@
AND t.USERCODE =@USERCODE@
",
new CDAParameter("WORKSTATIONTYPE", workStationType),
new CDAParameter("USERCODE", userCode)
);
if (dataTable == null || dataTable.Rows.Count == 0)
{
return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Other, Message = "当前工号无工位信息" };
}
if (dataTable.Columns.Contains("PLC_FLAG"))
{
string plc_flag = dataTable.Rows[0]["PLC_FLAG"] + "";
if (plc_flag != "1")
{
return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Success, Message = "" /*"PLC对接未启用"*/ };
}
}
string ip = dataTable.Rows[0]["PLCIP"] + "";
if (string.IsNullOrWhiteSpace(ip))
{
return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Success, Message = "" /*"PLC对接未启用"*/ };
}
int port = Convert.ToInt32(dataTable.Rows[0]["PLCPORT"]);
JArray plcObj = JArray.Parse(dataTable.Rows[0]["PLCOBJECTREAD"].ToString());
// 读取PLC
using (SocketClient plc = new SocketClient())
{
//自动按配置写
plc.Connect(ip, port);
for (int i = 0; i < plcObj.Count; i++)
{
if (plcObj[i]["type"].ToString().ToLower() == "float")
{
PLCResult result = plc.Read(plcObj[i]["code"].ToString(), plcObj[i]["number"].ToString());
plcObj[i]["weight"] = result.Data;
}
}
}
#if DEBUG
//写入操作成功日志
Curtain.Log.Logger.Debug("PLC读取成功!" + plcObj.ToJson());
#endif
//PLC操作成功
return new ServiceResultEntity()
{
Status = Constant.ServiceResultStatus.Success,
Message = "", //一定不可以有值,为空时才是提交成功
Result = plcObj
};
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
///
/// 写PLC2
///
///
///
///
///
public static ServiceResultEntity PlcWrite2(int workStationType, Hashtable plcPara, string userCode = null)
{
using (IDataAccess conn = DataAccess.Create())
{
List paras = new List();
#region 读取工位配置信息
string sql =
"SELECT --t.*\n" + @"
t.workstationtypeid,
t.workstationname,
t.procedureid,
t.groutinglineid,
t.usercode,
t.pccode,
t.plcip,
t.plcport,
t.plcobject,
t.valueflag,
t.plcobjectread,
t.plc_flag" +
" FROM tp_mst_workstation t\n" +
" WHERE t.workstationtypeid = @workstationtype@";
paras.Add(new CDAParameter("workstationtype", workStationType));
if (!string.IsNullOrEmpty(userCode))
{
sql += " AND t.usercode = @usercode@";
paras.Add(new CDAParameter("usercode", userCode));
}
DataTable dataTable = conn.ExecuteDatatable(sql, paras.ToArray());
if (dataTable == null || dataTable.Rows.Count == 0)
{
return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Other, Message = "无当前工位信息" };
}
if (dataTable.Columns.Contains("PLC_FLAG"))
{
string plc_flag = dataTable.Rows[0]["PLC_FLAG"] + "";
if (plc_flag != "1")
{
return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Success, Message = "" /*"PLC对接未启用"*/ };
}
}
#endregion
string ip = dataTable.Rows[0]["PLCIP"] + "";
if (string.IsNullOrWhiteSpace(ip))
{
return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Success, Message = "" /*"PLC对接未启用"*/ };
}
int port = Convert.ToInt32(dataTable.Rows[0]["PLCPORT"]);
JArray plcObj = JArray.Parse(dataTable.Rows[0]["PLCOBJECT"].ToString());
//写入PLC
using (SocketClient plc = new SocketClient())
{
//自动按配置写
plc.Connect(ip, port);
for (int i = 0; i < plcObj.Count; i++)
{
if (plcObj[i]["type"].ToString().ToLower() == "string")
plc.Write(plcObj[i]["code"].ToString(), plcObj[i]["number"].ToString(), plcPara[plcObj[i]["name"].ToString()].ToString());
else if (plcObj[i]["type"].ToString().ToLower() == "int")
plc.Write(plcObj[i]["code"].ToString(), plcObj[i]["number"].ToString(), Convert.ToInt16(plcPara[plcObj[i]["name"].ToString()]));
else if (plcObj[i]["type"].ToString().ToLower() == "bool")
plc.Write(plcObj[i]["code"].ToString(), plcObj[i]["number"].ToString(), Convert.ToBoolean(plcPara[plcObj[i]["name"].ToString()]));
}
}
#if DEBUG
//写入操作成功日志
string plcParaStr = "";
foreach (string key in plcPara.Keys)
{
plcParaStr += " " + key + ":" + plcPara[key].ToString() + " ";
}
Curtain.Log.Logger.Debug($"PLC写入成功!{plcParaStr} {ip} {userCode} {plcObj.ToString()}");
#endif
//PLC操作成功
return new ServiceResultEntity()
{
Status = Constant.ServiceResultStatus.Success,
Message = "" //一定不可以有值,为空时才是提交成功
};
}
}
///
/// 写PLC 成型线放行
///
///
///
///
///
public static ServiceResultEntity PlcWrite_GL(int workStationType, int gl_id,
Hashtable plcPara, string userCode = null)
{
using (IDataAccess conn = DataAccess.Create())
{
List paras = new List();
#region 读取工位配置信息
string sql =
"SELECT --t.*\n" + @"
t.workstationtypeid,
t.workstationname,
t.procedureid,
t.groutinglineid,
t.usercode,
t.pccode,
t.plcip,
t.plcport,
t.plcobject,
t.valueflag,
t.plcobjectread,
t.plc_flag" +
" FROM tp_mst_workstation t\n" +
" WHERE t.workstationtypeid = @workstationtype@\n" +
" AND t.GROUTINGLINEID = @GROUTINGLINEID@\n"
;
paras.Add(new CDAParameter("workstationtype", workStationType));
paras.Add(new CDAParameter("GROUTINGLINEID", gl_id));
if (!string.IsNullOrEmpty(userCode))
{
sql += " AND t.usercode = @usercode@";
paras.Add(new CDAParameter("usercode", userCode));
}
DataTable dataTable = conn.ExecuteDatatable(sql, paras.ToArray());
if (dataTable == null || dataTable.Rows.Count == 0)
{
return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Other, Message = "无当前工位信息" };
}
if (dataTable.Columns.Contains("PLC_FLAG"))
{
string plc_flag = dataTable.Rows[0]["PLC_FLAG"] + "";
if (plc_flag != "1")
{
return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Success, Message = "" /*"PLC对接未启用"*/ };
}
}
#endregion
string ip = dataTable.Rows[0]["PLCIP"] + "";
if (string.IsNullOrWhiteSpace(ip))
{
return new ServiceResultEntity() { Status = Constant.ServiceResultStatus.Success, Message = "" /*"PLC对接未启用"*/ };
}
int port = Convert.ToInt32(dataTable.Rows[0]["PLCPORT"]);
JArray plcObj = JArray.Parse(dataTable.Rows[0]["PLCOBJECT"].ToString());
//写入PLC
using (SocketClient plc = new SocketClient())
{
//自动按配置写
plc.Connect(ip, port);
for (int i = 0; i < plcObj.Count; i++)
{
if (plcObj[i]["type"].ToString().ToLower() == "string")
plc.Write(plcObj[i]["code"].ToString(), plcObj[i]["number"].ToString(), plcPara[plcObj[i]["name"].ToString()].ToString());
else if (plcObj[i]["type"].ToString().ToLower() == "int")
plc.Write(plcObj[i]["code"].ToString(), plcObj[i]["number"].ToString(), Convert.ToInt16(plcPara[plcObj[i]["name"].ToString()]));
else if (plcObj[i]["type"].ToString().ToLower() == "bool")
plc.Write(plcObj[i]["code"].ToString(), plcObj[i]["number"].ToString(), Convert.ToBoolean(plcPara[plcObj[i]["name"].ToString()]));
}
}
#if DEBUG
//写入操作成功日志
string plcParaStr = "";
foreach (string key in plcPara.Keys)
{
plcParaStr += " " + key + ":" + plcPara[key].ToString() + " ";
}
Curtain.Log.Logger.Debug($"PLC写入成功!{plcParaStr} {ip} {userCode} {plcObj.ToString()}");
#endif
//PLC操作成功
return new ServiceResultEntity()
{
Status = Constant.ServiceResultStatus.Success,
Message = "" //一定不可以有值,为空时才是提交成功
};
}
}
}
}