AddCheckBarcode_WCF.ashx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <%@ WebHandler Language="C#" Class="AddCheckBarcode_WCF" %>
  2. using System.Web;
  3. using System.Web.SessionState;
  4. using System.Web.Configuration;
  5. using Newtonsoft.Json;
  6. using Newtonsoft.Json.Linq;
  7. using System.Collections;
  8. using Curtain.DataAccess;
  9. using Curtain.Log;
  10. using DK.XuWei.WebMes;
  11. using Curtain.Net.Sockets.PLC;
  12. using Curtain.Net.Sockets.PLC.Model.Siemens;
  13. using System.Data;
  14. using System;
  15. /// <summary>
  16. /// 从WCF接口获取 成检保存
  17. /// xuwei 2019-11-11
  18. /// </summary>
  19. public class AddCheckBarcode_WCF : IHttpHandler, IRequiresSessionState
  20. {
  21. public void ProcessRequest(HttpContext context)
  22. {
  23. //处理提交数据
  24. if(context.Request["productionDataEntitys"] is object)
  25. {
  26. //提交到接口
  27. WCF wcf = new WCF();
  28. wcf.Para = wcf.LoadParaBySession();
  29. wcf.Para.Add(new JProperty("procedureID", context.Request["procedureID"]));
  30. wcf.Para.Add(new JProperty("productionDataEntitys",JArray.Parse(context.Request["productionDataEntitys"])));
  31. string jsonStr = wcf.Post("/DKService/PDAModuleService/AddCheckBarcode");
  32. //Logger.Debug(jsonStr);
  33. if(jsonStr != "")
  34. {
  35. string barCode = context.Request["barCode"].ToString();
  36. string procedureID = context.Request["procedureID"].ToString();
  37. string goodsRepair = context.Request["goodsRepair"].ToString();
  38. string userCode = context.Request["userCode"] is object ? context.Request["userCode"].ToString() : "";
  39. //操作成功 保存返修路径 只有官塘厂适用 xuwei 2022-04-18 ===========
  40. if(context.Request["goodsRepair"] is object)
  41. {
  42. updateGoodsRepair(barCode, procedureID, goodsRepair);
  43. }
  44. //=================================================================
  45. if (userCode != "")
  46. //写PLC数据给测漏机===============================================
  47. jsonStr = plcWrite(userCode, 1, barCode);
  48. else
  49. //不写PLC
  50. jsonStr = ("{'success':true,'message':'操作成功!','rows':'" + jsonStr + "'}").Replace("'","\"");
  51. }
  52. else
  53. {
  54. jsonStr = ("{'success':false,'message':'操作失败!','rows':'" + jsonStr + "'}").Replace("'","\"");
  55. }
  56. context.Response.Write(jsonStr);
  57. }
  58. else
  59. {
  60. string jsonStr = ("{'success':false,'message':'操作失败!','rows':''}").Replace("'","\"");
  61. context.Response.Write(jsonStr);
  62. }
  63. }
  64. //保存返修路径数据
  65. private int updateGoodsRepair(string barCode,string procedureID,string goodsRepair)
  66. {
  67. using (IDataAccess conn = DataAccess.Create())
  68. {
  69. int result = 0;
  70. result = conn.ExecuteNonQuery(@"
  71. UPDATE TP_PM_GOODSREPAIR
  72. SET
  73. VALUEFLAG = 0,
  74. UPDATETIME = SYSDATE
  75. WHERE
  76. BARCODE = @BARCODE@
  77. ",
  78. new CDAParameter("BARCODE", barCode)
  79. );
  80. result = conn.ExecuteNonQuery(@"
  81. INSERT INTO TP_PM_GOODSREPAIR
  82. (BARCODE,PROCEDUREID,REPAIRPATH,ACCOUNTID,CREATEUSERID,UPDATEUSERID)
  83. VALUES
  84. (@BARCODE@,@PROCEDUREID@,@REPAIRPATH@,@ACCOUNTID@,@CREATEUSERID@,@UPDATEUSERID@)
  85. ",
  86. new CDAParameter("BARCODE", barCode),
  87. new CDAParameter("PROCEDUREID", procedureID),
  88. new CDAParameter("REPAIRPATH",goodsRepair),
  89. new CDAParameter("ACCOUNTID",HttpContext.Current.Session["accountId"]),
  90. new CDAParameter("CREATEUSERID",HttpContext.Current.Session["userId"]),
  91. new CDAParameter("UPDATEUSERID",HttpContext.Current.Session["userId"])
  92. );
  93. return result;
  94. }
  95. }
  96. /// <summary>
  97. /// 写PLC xuwei add 2022-05-06
  98. /// </summary>
  99. /// <param name="conn"></param>
  100. /// <param name="userCode"></param>
  101. /// <param name="workStationType"></param>
  102. /// <param name="plcPara"></param>
  103. /// <returns></returns>
  104. private static string plcWrite(string userCode, int workStationType,string barCode)
  105. {
  106. DataTable dataTable = null;
  107. DataTable paraTable = null;
  108. using (IDataAccess conn = DataAccess.Create())
  109. {
  110. //读取工位配置信息
  111. dataTable = conn.ExecuteDatatable(@"
  112. SELECT
  113. T.WORKSTATIONTYPEID,
  114. T.WORKSTATIONNAME,
  115. T.PROCEDUREID,
  116. T.GROUTINGLINEID,
  117. T.USERCODE,
  118. T.PCCODE,
  119. T.PLCIP,
  120. T.PLCPORT,
  121. T.PLCOBJECT,
  122. T.VALUEFLAG,
  123. T.PLCOBJECTREAD,
  124. T.PLC_FLAG
  125. FROM
  126. TP_MST_WORKSTATION T
  127. WHERE
  128. T.WORKSTATIONTYPEID = @WORKSTATIONTYPE@
  129. AND T.USERCODE =@USERCODE@
  130. ",
  131. new CDAParameter("WORKSTATIONTYPE", workStationType),
  132. new CDAParameter("USERCODE", userCode)
  133. );
  134. //读取产品信息
  135. paraTable = conn.ExecuteDatatable(@"
  136. SELECT
  137. G.SEATCOVERCODE AS SEATCOVER,
  138. P.BARCODE,
  139. GL.GOODSLEVELTYPEID AS GOODSLEVEL,
  140. 1 AS FLAG
  141. FROM
  142. TP_PM_GROUTINGDAILYDETAIL P
  143. LEFT JOIN TP_MST_GOODS G ON G.GOODSID = P.GOODSID
  144. LEFT JOIN TP_MST_GOODSLEVEL GL ON P.GOODSLEVELTYPEID = GL.GOODSLEVELTYPEID
  145. WHERE
  146. P.VALUEFLAG = '1'
  147. AND P.BARCODE = @BARCODE@
  148. ",
  149. new CDAParameter("BARCODE", barCode)
  150. );
  151. }
  152. if (dataTable == null || dataTable.Rows.Count == 0)
  153. {
  154. return new JsonResult(JsonStatus.success) { message = "操作成功!当前工号无PLC工位!" }.ToJson();
  155. }
  156. //是否启用PLC对接
  157. if (dataTable.Columns.Contains("PLC_FLAG"))
  158. {
  159. string plc_flag = dataTable.Rows[0]["PLC_FLAG"] + "";
  160. if (plc_flag != "1")
  161. {
  162. return new JsonResult(JsonStatus.success) { message = "操作成功!当前工号未启用PLC工位!" }.ToJson();
  163. }
  164. }
  165. string ip = dataTable.Rows[0]["PLCIP"] + "";
  166. if (string.IsNullOrWhiteSpace(ip))
  167. {
  168. return new JsonResult(JsonStatus.success) { message = "操作成功!当前工位没有配置PLC地址!" }.ToJson();
  169. }
  170. int port = Convert.ToInt32(dataTable.Rows[0]["PLCPORT"]);
  171. JArray plcObj = JArray.Parse(dataTable.Rows[0]["PLCOBJECT"].ToString());
  172. //写入PLC
  173. try
  174. {
  175. string plcResult = "【BARCODE】" + barCode + "【USERCODE】" + userCode + "【PLC】" + ip + " ";
  176. using (SocketClient<SiemensS7_1200Model> plc = new SocketClient<SiemensS7_1200Model>())
  177. {
  178. plc.Connect(ip, port);
  179. for (int i = 0; i < plcObj.Count; i++)
  180. {
  181. if (plcObj[i]["type"].ToString().ToLower() == "string")
  182. plc.Write<string>(plcObj[i]["code"].ToString(), plcObj[i]["number"].ToString(), paraTable.Rows[0][plcObj[i]["name"].ToString()].ToString());
  183. else if (plcObj[i]["type"].ToString().ToLower() == "int" || plcObj[i]["type"].ToString().ToLower() == "short")
  184. plc.Write<short>(plcObj[i]["code"].ToString(), plcObj[i]["number"].ToString(), Convert.ToInt16(paraTable.Rows[0][plcObj[i]["name"].ToString()]));
  185. plcResult += "【" +plcObj[i]["code"].ToString() + plcObj[i]["number"].ToString() + "】" + paraTable.Rows[0][plcObj[i]["name"].ToString()] + " ";
  186. }
  187. }
  188. Logger.Debug(plcResult);
  189. //PLC操作成功
  190. return new JsonResult(JsonStatus.success) { message = "操作成功!PLC数据写入成功!"}.ToJson();
  191. }
  192. catch(Exception ex)
  193. {
  194. //PLC操作失败
  195. Logger.Error(ex, "条码:" + barCode);
  196. Logger.Error(ex);
  197. return new JsonResult(JsonStatus.error) { message = "操作失败!PLC数据写入失败!" }.ToJson();
  198. }
  199. }
  200. public bool IsReusable
  201. {
  202. get
  203. {
  204. return false;
  205. }
  206. }
  207. }