| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- <%@ WebHandler Language="C#" Class="AddCheckBarcode_WCF" %>
- using System.Web;
- using System.Web.SessionState;
- using System.Web.Configuration;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using System.Collections;
- using Curtain.DataAccess;
- using Curtain.Log;
- using DK.XuWei.WebMes;
- using Curtain.Net.Sockets.PLC;
- using Curtain.Net.Sockets.PLC.Model.Siemens;
- using System.Data;
- using System;
- /// <summary>
- /// 从WCF接口获取 成检保存
- /// xuwei 2019-11-11
- /// </summary>
- public class AddCheckBarcode_WCF : IHttpHandler, IRequiresSessionState
- {
- public void ProcessRequest(HttpContext context)
- {
- //处理提交数据
- if(context.Request["productionDataEntitys"] is object)
- {
- //提交到接口
- WCF wcf = new WCF();
- wcf.Para = wcf.LoadParaBySession();
- wcf.Para.Add(new JProperty("procedureID", context.Request["procedureID"]));
- wcf.Para.Add(new JProperty("productionDataEntitys",JArray.Parse(context.Request["productionDataEntitys"])));
- string jsonStr = wcf.Post("/DKService/PDAModuleService/AddCheckBarcode");
- //Logger.Debug(jsonStr);
- if(jsonStr != "")
- {
- string barCode = context.Request["barCode"].ToString();
- string procedureID = context.Request["procedureID"].ToString();
- string goodsRepair = context.Request["goodsRepair"].ToString();
- string userCode = context.Request["userCode"] is object ? context.Request["userCode"].ToString() : "";
- //操作成功 保存返修路径 只有官塘厂适用 xuwei 2022-04-18 ===========
- if(context.Request["goodsRepair"] is object)
- {
- updateGoodsRepair(barCode, procedureID, goodsRepair);
- }
- //=================================================================
- if (userCode != "")
- //写PLC数据给测漏机===============================================
- jsonStr = plcWrite(userCode, 1, barCode);
- else
- //不写PLC
- jsonStr = ("{'success':true,'message':'操作成功!','rows':'" + jsonStr + "'}").Replace("'","\"");
- }
- else
- {
- jsonStr = ("{'success':false,'message':'操作失败!','rows':'" + jsonStr + "'}").Replace("'","\"");
- }
- context.Response.Write(jsonStr);
- }
- else
- {
- string jsonStr = ("{'success':false,'message':'操作失败!','rows':''}").Replace("'","\"");
- context.Response.Write(jsonStr);
- }
- }
- //保存返修路径数据
- private int updateGoodsRepair(string barCode,string procedureID,string goodsRepair)
- {
- using (IDataAccess conn = DataAccess.Create())
- {
- int result = 0;
- result = conn.ExecuteNonQuery(@"
- UPDATE TP_PM_GOODSREPAIR
- SET
- VALUEFLAG = 0,
- UPDATETIME = SYSDATE
- WHERE
- BARCODE = @BARCODE@
- ",
- new CDAParameter("BARCODE", barCode)
- );
- result = conn.ExecuteNonQuery(@"
- INSERT INTO TP_PM_GOODSREPAIR
- (BARCODE,PROCEDUREID,REPAIRPATH,ACCOUNTID,CREATEUSERID,UPDATEUSERID)
- VALUES
- (@BARCODE@,@PROCEDUREID@,@REPAIRPATH@,@ACCOUNTID@,@CREATEUSERID@,@UPDATEUSERID@)
- ",
- new CDAParameter("BARCODE", barCode),
- new CDAParameter("PROCEDUREID", procedureID),
- new CDAParameter("REPAIRPATH",goodsRepair),
- new CDAParameter("ACCOUNTID",HttpContext.Current.Session["accountId"]),
- new CDAParameter("CREATEUSERID",HttpContext.Current.Session["userId"]),
- new CDAParameter("UPDATEUSERID",HttpContext.Current.Session["userId"])
- );
- return result;
- }
- }
- /// <summary>
- /// 写PLC xuwei add 2022-05-06
- /// </summary>
- /// <param name="conn"></param>
- /// <param name="userCode"></param>
- /// <param name="workStationType"></param>
- /// <param name="plcPara"></param>
- /// <returns></returns>
- private static string plcWrite(string userCode, int workStationType,string barCode)
- {
- DataTable dataTable = null;
- DataTable paraTable = null;
- using (IDataAccess conn = DataAccess.Create())
- {
- //读取工位配置信息
- dataTable = conn.ExecuteDatatable(@"
- SELECT
- 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)
- );
- //读取产品信息
- paraTable = conn.ExecuteDatatable(@"
- SELECT
- G.SEATCOVERCODE AS SEATCOVER,
- P.BARCODE,
- GL.GOODSLEVELTYPEID AS GOODSLEVEL,
- 1 AS FLAG
- FROM
- TP_PM_GROUTINGDAILYDETAIL P
- LEFT JOIN TP_MST_GOODS G ON G.GOODSID = P.GOODSID
- LEFT JOIN TP_MST_GOODSLEVEL GL ON P.GOODSLEVELTYPEID = GL.GOODSLEVELTYPEID
- WHERE
- P.VALUEFLAG = '1'
- AND P.BARCODE = @BARCODE@
- ",
- new CDAParameter("BARCODE", barCode)
- );
- }
- if (dataTable == null || dataTable.Rows.Count == 0)
- {
- return new JsonResult(JsonStatus.success) { message = "操作成功!当前工号无PLC工位!" }.ToJson();
- }
- //是否启用PLC对接
- if (dataTable.Columns.Contains("PLC_FLAG"))
- {
- string plc_flag = dataTable.Rows[0]["PLC_FLAG"] + "";
- if (plc_flag != "1")
- {
- return new JsonResult(JsonStatus.success) { message = "操作成功!当前工号未启用PLC工位!" }.ToJson();
- }
- }
- string ip = dataTable.Rows[0]["PLCIP"] + "";
- if (string.IsNullOrWhiteSpace(ip))
- {
- return new JsonResult(JsonStatus.success) { message = "操作成功!当前工位没有配置PLC地址!" }.ToJson();
- }
- int port = Convert.ToInt32(dataTable.Rows[0]["PLCPORT"]);
- JArray plcObj = JArray.Parse(dataTable.Rows[0]["PLCOBJECT"].ToString());
- //写入PLC
- try
- {
- string plcResult = "【BARCODE】" + barCode + "【USERCODE】" + userCode + "【PLC】" + ip + " ";
- using (SocketClient<SiemensS7_1200Model> plc = new SocketClient<SiemensS7_1200Model>())
- {
- plc.Connect(ip, port);
- for (int i = 0; i < plcObj.Count; i++)
- {
- if (plcObj[i]["type"].ToString().ToLower() == "string")
- plc.Write<string>(plcObj[i]["code"].ToString(), plcObj[i]["number"].ToString(), paraTable.Rows[0][plcObj[i]["name"].ToString()].ToString());
- else if (plcObj[i]["type"].ToString().ToLower() == "int" || plcObj[i]["type"].ToString().ToLower() == "short")
- plc.Write<short>(plcObj[i]["code"].ToString(), plcObj[i]["number"].ToString(), Convert.ToInt16(paraTable.Rows[0][plcObj[i]["name"].ToString()]));
- plcResult += "【" +plcObj[i]["code"].ToString() + plcObj[i]["number"].ToString() + "】" + paraTable.Rows[0][plcObj[i]["name"].ToString()] + " ";
- }
- }
- Logger.Debug(plcResult);
- //PLC操作成功
- return new JsonResult(JsonStatus.success) { message = "操作成功!PLC数据写入成功!"}.ToJson();
- }
- catch(Exception ex)
- {
- //PLC操作失败
- Logger.Error(ex, "条码:" + barCode);
- Logger.Error(ex);
- return new JsonResult(JsonStatus.error) { message = "操作失败!PLC数据写入失败!" }.ToJson();
- }
- }
- public bool IsReusable
- {
- get
- {
- return false;
- }
- }
- }
|