|
|
@@ -0,0 +1,401 @@
|
|
|
+<%@ WebHandler Language="C#" Class="plc_shiyou3228" %>
|
|
|
+
|
|
|
+using System;
|
|
|
+using System.Web;
|
|
|
+using DK.XuWei.WebMes;
|
|
|
+using Curtain.DataAccess;
|
|
|
+using Curtain.Log;
|
|
|
+using System.Data;
|
|
|
+using Newtonsoft.Json.Linq;
|
|
|
+
|
|
|
+
|
|
|
+/// <summary>
|
|
|
+/// 对接机械施釉(水箱)(二期)
|
|
|
+/// qq
|
|
|
+/// 2024-07-15
|
|
|
+/// </summary>
|
|
|
+public class plc_shiyou3228 : IHttpHandler
|
|
|
+{
|
|
|
+ public static class PLC
|
|
|
+ {
|
|
|
+ public static string plcIp = "172.19.24.65";
|
|
|
+ public static int plcPort = 102;
|
|
|
+ public static string dbResult = "1000.0"; //结果;扫码结果(1:通过,2:条码错误,3:产品不可到达,4:MES异常,0:复位)
|
|
|
+ public static string dbGoodsCode = "1000.2";//产品编码(数值写入,产品型号)
|
|
|
+ public static string dbGoodsType = "1000.4";//产品类型(1:智能、2:连体)
|
|
|
+ public static string dbBarcode = "1000.8"; //产品条码
|
|
|
+ }
|
|
|
+
|
|
|
+ //输入参数 【条码】
|
|
|
+ public string barCode = "";
|
|
|
+
|
|
|
+ public JArray result = new JArray();
|
|
|
+ public static S7 s7 = new S7();
|
|
|
+
|
|
|
+ //打开PLC
|
|
|
+ public int PlcOpen()
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ s7.Open(PLC.plcIp, PLC.plcPort);
|
|
|
+ result.Add(new JObject(new JProperty("PLC连接", PLC.plcIp + ":" + PLC.plcPort + " 连接成功!")));
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ result.Add(new JObject(new JProperty("PLC连接", PLC.plcIp + ":" + PLC.plcPort + " 连接失败!")));
|
|
|
+ result.Add(new JObject(new JProperty("PLC错误信息", e.Message)));
|
|
|
+ }
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ //关闭PLC
|
|
|
+ public int PlcClose()
|
|
|
+ {
|
|
|
+ s7.Close();
|
|
|
+ result.Add(new JObject(new JProperty("PLC关闭", PLC.plcIp + ":" + PLC.plcPort + " 关闭成功!")));
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static class API
|
|
|
+ {
|
|
|
+ public static string accountCode = "dongke";
|
|
|
+ public static string userId = "8";
|
|
|
+ public static string userCode = "DK03";
|
|
|
+ public static string userName = "东科刘世奇";
|
|
|
+ public static string userPassword = "5D3EF95E2716E4FB83E2639DC385A58B";
|
|
|
+ public static string sessionKey = "CD806C4D-D4EC-4F6A-B4F4-720F8F780FF0";
|
|
|
+ public static int procedureID = 80; //1#机械施釉(水箱)
|
|
|
+ }
|
|
|
+
|
|
|
+ //主程序入口
|
|
|
+ public void ProcessRequest(HttpContext context)
|
|
|
+ {
|
|
|
+ context.Response.ContentType = "text/plain";
|
|
|
+ //输入产品条码参数
|
|
|
+ barCode = context.Request["barcode"] is object ? context.Request["barcode"].ToString() : "";
|
|
|
+
|
|
|
+ DataTable infoDt = new DataTable();
|
|
|
+ //写入PLC
|
|
|
+ //正式
|
|
|
+ //SiemensS7.Open("172.19.24.65", 102);
|
|
|
+
|
|
|
+ if (context.Request["barcode"] is object)
|
|
|
+ {
|
|
|
+ //string barCode = context.Request["barcode"].ToString();
|
|
|
+ if (PlcOpen() == 1)
|
|
|
+ {
|
|
|
+ //读取工号
|
|
|
+ string userCode = "JSB";
|
|
|
+ if (userCode != "")
|
|
|
+ {
|
|
|
+ GetUserinfo(userCode);
|
|
|
+
|
|
|
+ //接口验证可到达
|
|
|
+ int check = ApiCheckBarcode(barCode);
|
|
|
+
|
|
|
+ if (check == 1)
|
|
|
+ {
|
|
|
+ int addWorkPiece = ApiAddWorkPiece(barCode);
|
|
|
+
|
|
|
+ if (addWorkPiece == 1)
|
|
|
+ {
|
|
|
+ DataTable goodsType = GetGoodinfo(barCode);
|
|
|
+
|
|
|
+ if (goodsType != null && goodsType.Rows.Count > 0)
|
|
|
+ {
|
|
|
+ bool goodscodeflag = PlcWriteRepeat<short>(PLC.dbGoodsCode, Convert.ToInt16(goodsType.Rows[0]["SEATCOVERCODE"].ToString()));
|
|
|
+ bool goodstypeflag = PlcWriteRepeat<short>(PLC.dbGoodsType, Convert.ToInt16(goodsType.Rows[0]["GOODSTYPENAME"].ToString()));
|
|
|
+ bool barcodeflag = PlcWriteRepeat<string>(PLC.dbBarcode, barCode);
|
|
|
+ bool resultflag = PlcWriteRepeat<short>(PLC.dbResult, Convert.ToInt16(1));
|
|
|
+ result.Add(new JObject(new JProperty("PLC读出(核对)", PLC.dbResult + " = " + s7.Read<short>(PLC.dbResult) + ";" + PLC.dbGoodsCode + " = " + s7.Read<short>(PLC.dbGoodsCode) + ";"+ PLC.dbGoodsType + " = " + s7.Read<short>(PLC.dbGoodsType) + ";" + PLC.dbBarcode + " = " + s7.Read<string>(PLC.dbBarcode, 11))));
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ s7.Write<short>(PLC.dbResult, Convert.ToInt16(2));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ s7.Write<short>(PLC.dbResult, Convert.ToInt16(4));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ s7.Write<short>(PLC.dbResult, Convert.ToInt16(3));
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ context.Response.Write(new JsonResult(JsonStatus.success) { rows = result }.ToJson());
|
|
|
+
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ context.Response.Write(new JsonResult(JsonStatus.error) { rows = new JObject(new JProperty("MES读取(条码信息:ERR)", "无条码")) }.ToJson());
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ //SiemensS7.Close();
|
|
|
+ }
|
|
|
+
|
|
|
+ //xuwei add begin 2024-01-19 增加多次写入容错方法===============
|
|
|
+ public bool PlcWriteRepeat<T>(string db,object value,int repeat = 5)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ bool isSuccess = false;
|
|
|
+ isSuccess = s7.Write<T>(db, (T)(value));
|
|
|
+ result.Add(new JObject(new JProperty("PLC写入数据成功标识:", isSuccess)));
|
|
|
+ result.Add(new JObject(new JProperty("PLC写入数据:", db + "=" + value.ToString())));
|
|
|
+ int i = 0;
|
|
|
+ while ( !isSuccess && i<repeat )
|
|
|
+ {
|
|
|
+ s7.IS_OPEN = false;
|
|
|
+ isSuccess = s7.Write<T>(db, (T)(value));
|
|
|
+ result.Add(new JObject(new JProperty("PLC写入数据(重写第" + i.ToString() + "次):", db + "=" + value.ToString())));
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+
|
|
|
+ return isSuccess;
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+
|
|
|
+ throw;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //读取用户信息
|
|
|
+ public DataRow GetUserinfo(string userCode)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ using (IDataAccess conn = DataAccess.Create())
|
|
|
+ {
|
|
|
+ DataTable dt = conn.ExecuteDatatable(@"
|
|
|
+ SELECT
|
|
|
+ u.ACCOUNTCODE,
|
|
|
+ u.USERID,
|
|
|
+ u.USERCODE,
|
|
|
+ u.USERNAME,
|
|
|
+ u.PASSWORD,
|
|
|
+ l.SESSIONKEY
|
|
|
+ FROM
|
|
|
+ TP_MST_USER u
|
|
|
+ LEFT JOIN TP_MST_USERLOGIN l ON u.USERID = l.USERID
|
|
|
+ WHERE
|
|
|
+ u.USERCODE = @USERCODE@
|
|
|
+ ",
|
|
|
+ new CDAParameter("USERCODE", userCode)
|
|
|
+ );
|
|
|
+ if (dt.Rows.Count > 0)
|
|
|
+ {
|
|
|
+ API.accountCode = dt.Rows[0]["ACCOUNTCODE"].ToString();
|
|
|
+ API.userId = dt.Rows[0]["USERID"].ToString();
|
|
|
+ API.userCode = dt.Rows[0]["USERCODE"].ToString();
|
|
|
+ API.userName = dt.Rows[0]["USERNAME"].ToString();
|
|
|
+ API.userPassword = dt.Rows[0]["PASSWORD"].ToString();
|
|
|
+ API.sessionKey = dt.Rows[0]["SESSIONKEY"].ToString();
|
|
|
+ JObject obj = new JObject();
|
|
|
+ obj.Add(new JProperty("accountCode", dt.Rows[0]["ACCOUNTCODE"].ToString()));
|
|
|
+ obj.Add(new JProperty("userId", dt.Rows[0]["USERID"].ToString()));
|
|
|
+ obj.Add(new JProperty("userCode", dt.Rows[0]["USERCODE"].ToString()));
|
|
|
+ obj.Add(new JProperty("userName", dt.Rows[0]["USERNAME"].ToString()));
|
|
|
+ obj.Add(new JProperty("userPassword", dt.Rows[0]["PASSWORD"].ToString()));
|
|
|
+ obj.Add(new JProperty("sessionKey", dt.Rows[0]["SESSIONKEY"].ToString()));
|
|
|
+ result.Add(new JObject(new JProperty("MES读取(用户信息:OK)", obj)));
|
|
|
+ return dt.Rows[0];
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ result.Add(new JObject(new JProperty("MES读取(用户信息:ERR)", "用户未找到!")));
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ result.Add(new JObject(new JProperty("系统异常:ERROR", e.Message)));
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //读取产品信息
|
|
|
+ public DataTable GetGoodinfo(string barcode)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ using (IDataAccess conn = DataAccess.Create())
|
|
|
+ {
|
|
|
+ DataTable dt = conn.ExecuteDatatable(@"
|
|
|
+ SELECT G.SEATCOVERCODE
|
|
|
+ ,CASE
|
|
|
+ WHEN INSTR(GT.GOODSTYPENAME, '智能') = 1 THEN 1
|
|
|
+ WHEN INSTR(GT.GOODSTYPENAME, '连体') = 1 THEN 2
|
|
|
+ END AS GOODSTYPENAME
|
|
|
+ ,GDD.BARCODE
|
|
|
+ ,G.GOODSCODE
|
|
|
+ FROM TP_PM_GROUTINGDAILYDETAIL GDD
|
|
|
+ LEFT JOIN TP_MST_GOODS G
|
|
|
+ ON GDD.GOODSID = G.GOODSID
|
|
|
+ LEFT JOIN TP_MST_GOODSTYPE GT
|
|
|
+ ON GT.GOODSTYPEID = G.GOODSTYPEID
|
|
|
+ WHERE GDD.BARCODE = @BARCODE@
|
|
|
+ ",
|
|
|
+ new CDAParameter("BARCODE", barcode)
|
|
|
+ );
|
|
|
+ if (dt.Rows.Count > 0)
|
|
|
+ {
|
|
|
+
|
|
|
+ result.Add(new JObject(new JProperty("MES读取(产品信息:OK)", barcode + ";产品类型:" + dt.Rows[0]["GOODSTYPENAME"].ToString())));
|
|
|
+ return dt;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ result.Add(new JObject(new JProperty("MES读取(用户信息:ERR)", "产品未找到!")));
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ result.Add(new JObject(new JProperty("系统异常:ERROR", e.Message)));
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //加载默认参数
|
|
|
+ public static JObject ApiLoadDefaultPara()
|
|
|
+ {
|
|
|
+ JObject userPara = new JObject();
|
|
|
+ userPara.Add(new JProperty("accountCode", API.accountCode));
|
|
|
+ userPara.Add(new JProperty("userCode", API.userCode));
|
|
|
+ userPara.Add(new JProperty("userPassword", API.userPassword));
|
|
|
+ userPara.Add(new JProperty("sessionKey", API.sessionKey));
|
|
|
+ return userPara;
|
|
|
+ }
|
|
|
+
|
|
|
+ public int ApiCheckBarcode(string barCode)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ WCF wcf = new WCF();
|
|
|
+ wcf.Para = ApiLoadDefaultPara();
|
|
|
+ wcf.Para.Add(new JProperty("procedureID", API.procedureID));
|
|
|
+ wcf.Para.Add(new JProperty("barcode", barCode));
|
|
|
+
|
|
|
+ string jsonStr = wcf.Post("/DKService/PDAModuleService/CheckBarcode");
|
|
|
+
|
|
|
+ JObject json = JObject.Parse(jsonStr);
|
|
|
+ JArray jsonArray = JArray.Parse(json["d"]["Result"].ToString());
|
|
|
+ if (json["d"]["Status"].ToString() == "0" && jsonArray[0]["out_errMsg"].ToString() == "")
|
|
|
+ {
|
|
|
+ result.Add(new JObject(
|
|
|
+ new JProperty("API调用(条码验证:OK)", "操作成功!"),
|
|
|
+ new JProperty("接口", "/DKService/PDAModuleService/CheckBarcode"),
|
|
|
+ new JProperty("参数(procedureID)", API.procedureID),
|
|
|
+ new JProperty("参数(barcode)", barCode)
|
|
|
+ //,new JProperty("结果", jsonStr)
|
|
|
+ )
|
|
|
+ );
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ result.Add(new JObject(
|
|
|
+ new JProperty("API调用(条码验证:ERR)", json["d"]["Message"].ToString()),
|
|
|
+ new JProperty("接口", "/DKService/PDAModuleService/CheckBarcode"),
|
|
|
+ new JProperty("参数(procedureID)", API.procedureID),
|
|
|
+ new JProperty("参数(barcode)", barCode)
|
|
|
+ , new JProperty("结果", jsonStr)
|
|
|
+ )
|
|
|
+ );
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ result.Add(new JObject(
|
|
|
+ new JProperty("API调用(条码验证:ERROR)", "系统异常!"),
|
|
|
+ new JProperty("接口", "/DKService/PDAModuleService/CheckBarcode"),
|
|
|
+ new JProperty("参数(procedureID)", API.procedureID),
|
|
|
+ new JProperty("参数(barcode)", barCode),
|
|
|
+ new JProperty("异常", e.Message)
|
|
|
+ )
|
|
|
+ );
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public int ApiAddWorkPiece(string barCode)
|
|
|
+ {
|
|
|
+ JArray productionDataEntitys = new JArray();
|
|
|
+
|
|
|
+ JObject obj = new JObject(
|
|
|
+ new JProperty("UserID", API.userId),
|
|
|
+ new JProperty("UserCode", API.userCode),
|
|
|
+ new JProperty("UserName", API.userName),
|
|
|
+ new JProperty("Barcode", barCode)
|
|
|
+ );
|
|
|
+ productionDataEntitys.Add(obj);
|
|
|
+
|
|
|
+ try
|
|
|
+ {
|
|
|
+ WCF wcf = new WCF();
|
|
|
+ wcf.Para = ApiLoadDefaultPara();
|
|
|
+ wcf.Para.Add(new JProperty("procedureID", API.procedureID));
|
|
|
+ wcf.Para.Add(new JProperty("productionDataEntitys", productionDataEntitys));
|
|
|
+
|
|
|
+ //正式用
|
|
|
+ string jsonStr = wcf.Post("/DKService/PDAModuleService/AddWorkPiece");
|
|
|
+
|
|
|
+ JObject json = JObject.Parse(jsonStr);
|
|
|
+ JArray jsonArray = JArray.Parse(json["d"]["Result"].ToString());
|
|
|
+ if (json["d"]["Status"].ToString() == "0" && (jsonArray[0]["out_errMsg"].ToString() == ""))
|
|
|
+ {
|
|
|
+ result.Add(new JObject(
|
|
|
+ new JProperty("API调用(1#机械施釉(水箱):OK)", "操作成功!"),
|
|
|
+ new JProperty("接口", "/DKService/PDAModuleService/AddWorkPiece"),
|
|
|
+ new JProperty("参数(procedureID)", API.procedureID),
|
|
|
+ new JProperty("参数(productionDataEntitys)", productionDataEntitys)
|
|
|
+ )
|
|
|
+ );
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ result.Add(new JObject(
|
|
|
+ new JProperty("API调用(1#机械施釉(水箱):ERR)", json["d"]["Message"].ToString()),
|
|
|
+ new JProperty("接口", "/DKService/PDAModuleService/AddWorkPiece"),
|
|
|
+ new JProperty("参数(procedureID)", API.procedureID),
|
|
|
+ new JProperty("参数(productionDataEntitys)", productionDataEntitys)
|
|
|
+ , new JProperty("结果", jsonStr)
|
|
|
+ )
|
|
|
+ );
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ result.Add(new JObject(
|
|
|
+ new JProperty("API调用(1#机械施釉(水箱):ERROR)", "系统异常!"),
|
|
|
+ new JProperty("接口", "/DKService/PDAModuleService/AddWorkPiece"),
|
|
|
+ new JProperty("参数(procedureID)", API.procedureID),
|
|
|
+ new JProperty("参数(productionDataEntitys)", productionDataEntitys),
|
|
|
+ new JProperty("异常", e.Message)
|
|
|
+ )
|
|
|
+ );
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public bool IsReusable
|
|
|
+ {
|
|
|
+ get
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|