GetBarcodeByScan.ashx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <%@ WebHandler Language="C#" Class="GetBarcodeByScan" %>
  2. using System;
  3. using System.Web;
  4. using System.Data;
  5. using DK.XuWei.WebMes;
  6. using System.Web.SessionState;
  7. using System.Web.Configuration;
  8. using Newtonsoft.Json.Linq;
  9. using Curtain.DataAccess;
  10. using System.Collections.Generic;
  11. using Fleck;
  12. using System.Linq;
  13. using Curtain.Log;
  14. /// <summary>
  15. /// 获取 缺陷扣罚
  16. /// QQ 2024-03-26
  17. /// </summary>
  18. public class GetBarcodeByScan : IHttpHandler, IReadOnlySessionState
  19. {
  20. public static class PLC
  21. {
  22. public static string dbbarResult = "1000.0"; //纸箱码状态(int)
  23. public static string dbidnrkResult = "1000.2"; //纸箱码状态(int)
  24. }
  25. public static S7 s7=new S7();
  26. public JArray result = new JArray();
  27. public static List<IWebSocketConnection> allScokets;
  28. public static WebSocketServer server;
  29. public void ProcessRequest(HttpContext context)
  30. {
  31. context.Response.ContentType = "text/plain";
  32. //启动服务
  33. if (context.Request["start"] is object)
  34. {
  35. string barcodeip = context.Request["barcodeip"].ToString();
  36. if (allScokets == null)
  37. {
  38. start(barcodeip);
  39. }
  40. context.Response.Write("服务启动成功!");
  41. }
  42. if (context.Request["barcode"] is object)
  43. {
  44. try
  45. {
  46. //当扫描条码后,35服务器传输对应的条码及端口,需要根据端口打开对应常连接服务
  47. string barCode = context.Request["barcode"].ToString();
  48. string scanport = context.Request["scanport"].ToString();
  49. if (allScokets == null)
  50. {
  51. start("ws://172.18.32.32:9102");
  52. }
  53. foreach (var socket in allScokets.ToList())
  54. {
  55. socket.Send(scanport+"#"+barCode);
  56. }
  57. Logger.Info(barCode);
  58. context.Response.Write(new JsonResult(barCode).ToJson());
  59. }
  60. catch (Exception ex)
  61. {
  62. throw ex;
  63. }
  64. }
  65. if (context.Request["scanFlag"] is object)
  66. {
  67. try
  68. {
  69. var scanflag = Convert.ToInt16(context.Request["scanFlag"]);
  70. var plcip = context.Request["plcip"].ToString();
  71. s7 = new S7(plcip);
  72. if (scanflag == 0)
  73. {
  74. scanflag = 3;
  75. }
  76. if (scanflag == 1)
  77. {
  78. scanflag = 2;
  79. }
  80. bool successflag = s7.Write<short>(PLC.dbbarResult, Convert.ToInt16(scanflag));
  81. if (successflag)
  82. {
  83. result.Add(new JObject(new JProperty("TRUE;PLC写入", PLC.dbbarResult + " = " + scanflag + ";")));
  84. result.Add(new JObject(new JProperty("PLC读出(核对)", PLC.dbbarResult + " = " + s7.Read<short>(PLC.dbbarResult) + ";")));
  85. Logger.Info(result.ToString());
  86. context.Response.Write(new JsonResult(JsonStatus.success) { rows = result }.ToJson());
  87. }
  88. else
  89. {
  90. s7.IS_OPEN = false;
  91. s7.Write<short>(PLC.dbbarResult, Convert.ToInt16(scanflag));
  92. result.Add(new JObject(new JProperty("FALSE;PLC写入", PLC.dbbarResult + " = " + scanflag + ";")));
  93. result.Add(new JObject(new JProperty("PLC读出(核对)", PLC.dbbarResult + " = " + s7.Read<short>(PLC.dbbarResult) + ";")));
  94. Logger.Info(result.ToString());
  95. context.Response.Write(new JsonResult(JsonStatus.success) { rows = result }.ToJson());
  96. }
  97. }
  98. catch (Exception ex)
  99. {
  100. s7.IS_OPEN = false;
  101. var scanflag = Convert.ToInt16(context.Request["scanFlag"]);
  102. if (scanflag == 0)
  103. {
  104. scanflag = 3;
  105. }
  106. if (scanflag == 1)
  107. {
  108. scanflag = 2;
  109. }
  110. s7.Write<short>(PLC.dbbarResult, Convert.ToInt16(scanflag));
  111. result.Add(new JObject(new JProperty("CATCH;PLC写入", PLC.dbbarResult + " = " + scanflag + ";")));
  112. result.Add(new JObject(new JProperty("PLC读出(核对)", PLC.dbbarResult + " = " + s7.Read<short>(PLC.dbbarResult) + ";")));
  113. Logger.Info(result.ToString());
  114. context.Response.Write(new JsonResult(JsonStatus.success) { rows = result }.ToJson());
  115. throw ex;
  116. }
  117. }
  118. if (context.Request["scanBomFlag"] is object)
  119. {
  120. try
  121. {
  122. var scanBomFlag = Convert.ToInt16(context.Request["scanBomFlag"]);
  123. var plcip = context.Request["plcip"].ToString();
  124. s7 = new S7(plcip);
  125. if (scanBomFlag == 0)
  126. {
  127. scanBomFlag = 5;
  128. }
  129. if (scanBomFlag == 1)
  130. {
  131. scanBomFlag = 4;
  132. }
  133. bool successflag = s7.Write<short>(PLC.dbidnrkResult, Convert.ToInt16(scanBomFlag));
  134. if (successflag)
  135. {
  136. result.Add(new JObject(new JProperty("TRUE;PLC写入", PLC.dbidnrkResult + " = " + scanBomFlag + ";")));
  137. result.Add(new JObject(new JProperty("PLC读出(核对)", PLC.dbidnrkResult + " = " + s7.Read<short>(PLC.dbidnrkResult) + ";")));
  138. Logger.Info(result.ToString());
  139. context.Response.Write(new JsonResult(JsonStatus.success) { rows = result }.ToJson());
  140. }
  141. else
  142. {
  143. s7.IS_OPEN = false;
  144. s7.Write<short>(PLC.dbidnrkResult, Convert.ToInt16(scanBomFlag));
  145. result.Add(new JObject(new JProperty("FALSE;PLC写入", PLC.dbidnrkResult + " = " + scanBomFlag + ";")));
  146. result.Add(new JObject(new JProperty("PLC读出(核对)", PLC.dbidnrkResult + " = " + s7.Read<short>(PLC.dbidnrkResult) + ";")));
  147. Logger.Info(result.ToString());
  148. context.Response.Write(new JsonResult(JsonStatus.success) { rows = result }.ToJson());
  149. }
  150. }
  151. catch (Exception ex)
  152. {
  153. s7.IS_OPEN = false;
  154. var scanBomFlag = Convert.ToInt16(context.Request["scanBomFlag"]);
  155. if (scanBomFlag == 0)
  156. {
  157. scanBomFlag = 5;
  158. }
  159. if (scanBomFlag == 1)
  160. {
  161. scanBomFlag = 4;
  162. }
  163. s7.Write<short>(PLC.dbidnrkResult, Convert.ToInt16(scanBomFlag));
  164. result.Add(new JObject(new JProperty("CATCH;PLC写入", PLC.dbidnrkResult + " = " + scanBomFlag + ";")));
  165. result.Add(new JObject(new JProperty("PLC读出(核对)", PLC.dbidnrkResult + " = " + s7.Read<short>(PLC.dbidnrkResult) + ";")));
  166. Logger.Info(result.ToString());
  167. context.Response.Write(new JsonResult(JsonStatus.success) { rows = result }.ToJson());
  168. throw ex;
  169. }
  170. }
  171. }
  172. //启动服务
  173. public static void start(string ip)
  174. {
  175. allScokets = new List<IWebSocketConnection>();
  176. server = new WebSocketServer(ip);
  177. server.Start(scoket =>
  178. {
  179. scoket.OnOpen = () => { allScokets.Add(scoket); };
  180. scoket.OnClose = () => { allScokets.Remove(scoket); };
  181. scoket.OnMessage = message => { allScokets.ToList().ForEach(s => s.Send(message)); };
  182. });
  183. }
  184. public bool IsReusable
  185. {
  186. get
  187. {
  188. return false;
  189. }
  190. }
  191. }