plc_led_data_print3124.ashx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <%@ WebHandler Language="C#" Class="plc_led_data_print3124" %>
  2. using System;
  3. using System.Web;
  4. using System.Collections.Generic;
  5. using Curtain.Net.Sockets;
  6. using DK.XuWei.WebMes;
  7. using Curtain.DataAccess;
  8. using Curtain.Log;
  9. using System.Data;
  10. using Newtonsoft.Json.Linq;
  11. using System.Collections;
  12. /// <summary>
  13. /// 对接LED看板,用于成检显示产品属性信息,打印包装2线
  14. /// xuwei
  15. /// 2026-08-19
  16. /// </summary>
  17. public class plc_led_data_print3124 : IHttpHandler
  18. {
  19. public struct LedData
  20. {
  21. public int goodscode;
  22. public int goodsspecification;
  23. public int goodstype;
  24. public string barcode;
  25. }
  26. //输入参数 【条码】
  27. public string barCode = "";
  28. string procedure_no;
  29. string fentiprocedure_no;
  30. string procedure_in;
  31. string road_no;
  32. string rack;
  33. string position;
  34. //返回失败提示
  35. public string message = "";
  36. public void ProcessRequest(HttpContext context)
  37. {
  38. try
  39. {
  40. context.Response.ContentType = "text/plain";
  41. //输入输出参数初始化 计件
  42. barCode = context.Request["barcode"] is object ? context.Request["barcode"] : "";
  43. procedure_no = context.Request["procedure_no"] is object ? context.Request["procedure_no"] : "1_1_3";
  44. fentiprocedure_no = "1_1_4";
  45. procedure_in = "1";
  46. road_no = "0";
  47. rack = "";
  48. position = "";
  49. DataTable infoDt = new DataTable();
  50. //写入PLC
  51. //正式
  52. SiemensS7.Open("172.19.26.152", 102);
  53. if (context.Request["barcode"] is object)
  54. {
  55. using (IDataAccess conn = DataAccess.Create())
  56. {
  57. infoDt = conn.ExecuteDatatable(@"
  58. SELECT g.seatcovercode
  59. ,CASE
  60. WHEN instr(g.goodsspecification, '305') = 1 THEN 1
  61. WHEN instr(g.goodsspecification, '400') = 1 THEN 2
  62. WHEN instr(g.goodsspecification, '地排') = 1 THEN 3
  63. WHEN instr(g.goodsspecification, '横排') = 1 THEN 4
  64. WHEN instr(g.goodsspecification, '295') = 1 THEN 5
  65. WHEN instr(g.goodsspecification, '220') = 1 THEN 6
  66. WHEN instr(g.goodsspecification, '300') = 1 THEN 7
  67. END AS goodsspecification
  68. ,CASE
  69. WHEN instr(gt.goodstypename, '连体') = 1 THEN 1
  70. WHEN instr(gt.goodstypename, '智能') = 1 THEN 2
  71. END AS goodstypename
  72. ,CASE WHEN g.goodsid in (178,177,164,163,166,150,149,182,183,190,191,186,187) THEN '分体'
  73. when g.goodsid in(158,159,167) and gdd.logoid in (7,14) THEN '分体'
  74. when g.goodsid in(188,189) and gdd.logoid in (7,17) THEN '分体'
  75. when g.goodsid in(146,147) and gdd.logoid in (1) THEN '分体'
  76. ELSE '大件' END AS pritetype
  77. ,gdd.barcode
  78. ,g.goodscode
  79. FROM tp_pm_groutingdailydetail gdd
  80. LEFT JOIN tp_mst_goods g
  81. ON gdd.goodsid = g.goodsid
  82. LEFT JOIN tp_mst_goodstype gt
  83. ON gt.goodstypeid = g.goodstypeid
  84. WHERE gdd.barcode = @barcode@
  85. ",
  86. new CDAParameter("barcode", context.Request["barcode"])
  87. );
  88. if (infoDt != null && infoDt.Rows.Count > 0 && infoDt.Rows[0] != null)
  89. {
  90. SiemensS7.Write<short>("31.2", Convert.ToInt16(infoDt.Rows[0]["seatcovercode"]));
  91. SiemensS7.Write<short>("31.4", Convert.ToInt16(infoDt.Rows[0]["goodsspecification"]));
  92. SiemensS7.Write<short>("31.6", Convert.ToInt16(infoDt.Rows[0]["goodstypename"]));
  93. SiemensS7.Write<string>("31.10", infoDt.Rows[0]["barcode"].ToString());
  94. SiemensS7.Write<string>("31.24", infoDt.Rows[0]["goodscode"].ToString());
  95. //写标识位-扫描结果
  96. //写标识位-扫描结果
  97. SiemensS7.Write<short>("31.0", Convert.ToInt16(1));
  98. }
  99. context.Response.Write(SiemensS7.Read<short>("31.0"));
  100. context.Response.Write(SiemensS7.Read<short>("31.2"));
  101. context.Response.Write(SiemensS7.Read<short>("31.4"));
  102. context.Response.Write(SiemensS7.Read<short>("31.6"));
  103. context.Response.Write(SiemensS7.Read<string>("31.24", 11));
  104. if (infoDt != null && infoDt.Rows.Count > 0 && infoDt.Rows[0] != null)
  105. {
  106. if (Convert.ToInt16(infoDt.Rows[0]["seatcovercode"]) != Convert.ToInt16(SiemensS7.Read<short>("31.2"))
  107. || Convert.ToInt16(infoDt.Rows[0]["goodsspecification"]) != Convert.ToInt16(SiemensS7.Read<short>("31.4"))
  108. || Convert.ToInt16(infoDt.Rows[0]["goodstypename"]) != Convert.ToInt16(SiemensS7.Read<short>("31.6"))
  109. || Convert.ToInt16(1) != Convert.ToInt16(SiemensS7.Read<short>("31.0")))
  110. {
  111. SiemensS7.Write<short>("31.2", Convert.ToInt16(infoDt.Rows[0]["seatcovercode"]));
  112. SiemensS7.Write<short>("31.4", Convert.ToInt16(infoDt.Rows[0]["goodsspecification"]));
  113. SiemensS7.Write<short>("31.6", Convert.ToInt16(infoDt.Rows[0]["goodstypename"]));
  114. SiemensS7.Write<string>("31.10", infoDt.Rows[0]["barcode"].ToString());
  115. SiemensS7.Write<string>("31.24", infoDt.Rows[0]["goodscode"].ToString());
  116. //写标识位-扫描结果
  117. SiemensS7.Write<short>("31.0", Convert.ToInt16(1));
  118. }
  119. }
  120. Logger.Info("写入数据完成!barcode:" + context.Request["barcode"].ToString() + "五位数据位:" + SiemensS7.Read<short>("31.0") + "," + SiemensS7.Read<short>("31.2")
  121. + "," + SiemensS7.Read<short>("31.4") + "," + SiemensS7.Read<short>("31.6") + "," + SiemensS7.Read<short>("31.10") + "," + SiemensS7.Read<string>("31.24", 11) + ";数据:" + new JsonResult(infoDt).ToJson());
  122. }
  123. //调用标准计件方法===============================
  124. if (infoDt != null && infoDt.Rows.Count > 0 && infoDt.Rows[0] != null)
  125. {
  126. if (infoDt.Rows[0]["pritetype"].ToString() == "分体")
  127. {
  128. procedure_no = fentiprocedure_no;
  129. }
  130. WCF wcf = new WCF();
  131. wcf.Para.Add(new JProperty("barcode", barCode.ToString()));
  132. wcf.Para.Add(new JProperty("procedure_no", procedure_no));
  133. wcf.Para.Add(new JProperty("road_no", road_no));
  134. wcf.Para.Add(new JProperty("rack", rack));
  135. wcf.Para.Add(new JProperty("position", position));
  136. wcf.Para.Add(new JProperty("procedure_in", procedure_in));
  137. string jsonStr = wcf.Get("/DKService/ExHGS3QR/AddWorkInfoHGS3_QR");
  138. //这个接口很小的机率会带出html标签===============================================
  139. if (jsonStr.IndexOf("<html>") > 0) jsonStr = jsonStr.Substring(0, jsonStr.IndexOf("<html>"));
  140. if (jsonStr.IndexOf("<!DOCTYPEhtml>") > 0) jsonStr = jsonStr.Substring(0, jsonStr.IndexOf("<!DOCTYPEhtml>"));
  141. //xuwei fix 2022-05-28 有时会带出 DOCTYPEhtml标签
  142. jsonStr = jsonStr.Replace("<!DOCTYPEhtml>", "");
  143. jsonStr = jsonStr.Replace("\\u000d\\u000a", "")
  144. .Replace("\\n", "").Replace("\\", "").Replace(" ", "")
  145. .Replace("产品编码:", " 产品编码 ")
  146. .Replace("当前工序:", " 当前工序 ")
  147. .Replace("可到工序:", " 可到工序 ")
  148. .Replace("{\"d\":\"", "")
  149. .Replace("\"}\"}", "\"}")
  150. .Replace("<!DOCTYPEhtml>", "")
  151. ;
  152. Curtain.Log.Logger.Debug(context.Request.UserHostAddress + "\r\n" + context.Request.Url + "\r\n" + jsonStr);
  153. string apiResult = "";
  154. //处理输出结果 OK开头成功 NOK开头失败
  155. JObject json = JObject.Parse(jsonStr);
  156. bool apiStatus = false;
  157. apiStatus = Convert.ToBoolean(json["success"]);
  158. apiResult = jsonStr;
  159. //HttpContext.Current.Response.Write(apiResult);
  160. //记录接口调用日志
  161. Hashtable ht = new Hashtable();
  162. ht.Add("barcode", barCode.ToString());
  163. ht.Add("procedure_no", procedure_no);
  164. ht.Add("road_no", road_no);
  165. ht.Add("rack", rack);
  166. ht.Add("position", position);
  167. ht.Add("procedure_in", procedure_in);
  168. int apiId = Convert.ToInt32(procedure_no.Replace("_", "") + road_no + Math.Abs(Convert.ToInt32(procedure_in)));
  169. barCode = barCode.Replace("2c%", ",");
  170. int apiBarcodeCount = barCode.Split(',').Length;
  171. string apiUrl = "http://" + HttpContext.Current.Request.Url.Host + ":" + HttpContext.Current.Request.Url.Port.ToString() + "/api/DKService/ExHGS3QR/AddWorkInfoHGS3_QR/?" + JsonClient.ParaToString(ht);
  172. ApiLog.WriteApiLog("PLC_扫码", apiUrl, apiStatus, apiResult, apiId, apiBarcodeCount);
  173. //===============================================
  174. }
  175. }
  176. else
  177. {
  178. //写标识位-扫描结果
  179. SiemensS7.Write<short>("31.0", Convert.ToInt16(2));
  180. context.Response.Write(SiemensS7.Read<short>("31.0"));
  181. Logger.Info("无条码!");
  182. }
  183. context.Response.Write(new JsonResult(infoDt).ToJson());
  184. SiemensS7.Close();
  185. }
  186. catch (Exception ex )
  187. {
  188. Logger.Info("异常!:"+ex);
  189. throw ex ;
  190. }
  191. }
  192. public bool IsReusable
  193. {
  194. get
  195. {
  196. return false;
  197. }
  198. }
  199. }