| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- <%@ WebHandler Language="C#" Class="GetBarcodeByScan" %>
- using System;
- using System.Web;
- using System.Data;
- using DK.XuWei.WebMes;
- using System.Web.SessionState;
- using System.Web.Configuration;
- using Newtonsoft.Json.Linq;
- using Curtain.DataAccess;
- using System.Collections.Generic;
- using Fleck;
- using System.Linq;
- using Curtain.Log;
- /// <summary>
- /// 获取 缺陷扣罚
- /// QQ 2024-03-26
- /// </summary>
- public class GetBarcodeByScan : IHttpHandler, IReadOnlySessionState
- {
- public static class PLC
- {
- public static string dbbarResult = "1000.0"; //纸箱码状态(int)
- public static string dbidnrkResult = "1000.2"; //纸箱码状态(int)
- }
- public static S7 s7=new S7();
- public JArray result = new JArray();
- public static List<IWebSocketConnection> allScokets;
- public static WebSocketServer server;
- public void ProcessRequest(HttpContext context)
- {
- context.Response.ContentType = "text/plain";
- //启动服务
- if (context.Request["start"] is object)
- {
- string barcodeip = context.Request["barcodeip"].ToString();
- if (allScokets == null)
- {
- start(barcodeip);
- }
- context.Response.Write("服务启动成功!");
- }
- if (context.Request["barcode"] is object)
- {
- try
- {
- //当扫描条码后,35服务器传输对应的条码及端口,需要根据端口打开对应常连接服务
- string barCode = context.Request["barcode"].ToString();
- string scanport = context.Request["scanport"].ToString();
- if (allScokets == null)
- {
- start("ws://172.18.32.32:9102");
- }
- foreach (var socket in allScokets.ToList())
- {
- socket.Send(scanport+"#"+barCode);
- }
- Logger.Info(barCode);
- context.Response.Write(new JsonResult(barCode).ToJson());
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- if (context.Request["scanFlag"] is object)
- {
- try
- {
- var scanflag = Convert.ToInt16(context.Request["scanFlag"]);
- var plcip = context.Request["plcip"].ToString();
- s7 = new S7(plcip);
- if (scanflag == 0)
- {
- scanflag = 3;
- }
- if (scanflag == 1)
- {
- scanflag = 2;
- }
- bool successflag = s7.Write<short>(PLC.dbbarResult, Convert.ToInt16(scanflag));
- if (successflag)
- {
- result.Add(new JObject(new JProperty("TRUE;PLC写入", PLC.dbbarResult + " = " + scanflag + ";")));
- result.Add(new JObject(new JProperty("PLC读出(核对)", PLC.dbbarResult + " = " + s7.Read<short>(PLC.dbbarResult) + ";")));
- Logger.Info(result.ToString());
- context.Response.Write(new JsonResult(JsonStatus.success) { rows = result }.ToJson());
- }
- else
- {
- s7.IS_OPEN = false;
- s7.Write<short>(PLC.dbbarResult, Convert.ToInt16(scanflag));
- result.Add(new JObject(new JProperty("FALSE;PLC写入", PLC.dbbarResult + " = " + scanflag + ";")));
- result.Add(new JObject(new JProperty("PLC读出(核对)", PLC.dbbarResult + " = " + s7.Read<short>(PLC.dbbarResult) + ";")));
- Logger.Info(result.ToString());
- context.Response.Write(new JsonResult(JsonStatus.success) { rows = result }.ToJson());
- }
- }
- catch (Exception ex)
- {
- s7.IS_OPEN = false;
- var scanflag = Convert.ToInt16(context.Request["scanFlag"]);
- if (scanflag == 0)
- {
- scanflag = 3;
- }
- if (scanflag == 1)
- {
- scanflag = 2;
- }
- s7.Write<short>(PLC.dbbarResult, Convert.ToInt16(scanflag));
- result.Add(new JObject(new JProperty("CATCH;PLC写入", PLC.dbbarResult + " = " + scanflag + ";")));
- result.Add(new JObject(new JProperty("PLC读出(核对)", PLC.dbbarResult + " = " + s7.Read<short>(PLC.dbbarResult) + ";")));
- Logger.Info(result.ToString());
- context.Response.Write(new JsonResult(JsonStatus.success) { rows = result }.ToJson());
- throw ex;
- }
- }
- if (context.Request["scanBomFlag"] is object)
- {
- try
- {
- var scanBomFlag = Convert.ToInt16(context.Request["scanBomFlag"]);
- var plcip = context.Request["plcip"].ToString();
- s7 = new S7(plcip);
- if (scanBomFlag == 0)
- {
- scanBomFlag = 5;
- }
- if (scanBomFlag == 1)
- {
- scanBomFlag = 4;
- }
- bool successflag = s7.Write<short>(PLC.dbidnrkResult, Convert.ToInt16(scanBomFlag));
- if (successflag)
- {
- result.Add(new JObject(new JProperty("TRUE;PLC写入", PLC.dbidnrkResult + " = " + scanBomFlag + ";")));
- result.Add(new JObject(new JProperty("PLC读出(核对)", PLC.dbidnrkResult + " = " + s7.Read<short>(PLC.dbidnrkResult) + ";")));
- Logger.Info(result.ToString());
- context.Response.Write(new JsonResult(JsonStatus.success) { rows = result }.ToJson());
- }
- else
- {
- s7.IS_OPEN = false;
- s7.Write<short>(PLC.dbidnrkResult, Convert.ToInt16(scanBomFlag));
- result.Add(new JObject(new JProperty("FALSE;PLC写入", PLC.dbidnrkResult + " = " + scanBomFlag + ";")));
- result.Add(new JObject(new JProperty("PLC读出(核对)", PLC.dbidnrkResult + " = " + s7.Read<short>(PLC.dbidnrkResult) + ";")));
- Logger.Info(result.ToString());
- context.Response.Write(new JsonResult(JsonStatus.success) { rows = result }.ToJson());
- }
- }
- catch (Exception ex)
- {
- s7.IS_OPEN = false;
- var scanBomFlag = Convert.ToInt16(context.Request["scanBomFlag"]);
- if (scanBomFlag == 0)
- {
- scanBomFlag = 5;
- }
- if (scanBomFlag == 1)
- {
- scanBomFlag = 4;
- }
- s7.Write<short>(PLC.dbidnrkResult, Convert.ToInt16(scanBomFlag));
- result.Add(new JObject(new JProperty("CATCH;PLC写入", PLC.dbidnrkResult + " = " + scanBomFlag + ";")));
- result.Add(new JObject(new JProperty("PLC读出(核对)", PLC.dbidnrkResult + " = " + s7.Read<short>(PLC.dbidnrkResult) + ";")));
- Logger.Info(result.ToString());
- context.Response.Write(new JsonResult(JsonStatus.success) { rows = result }.ToJson());
- throw ex;
- }
- }
- }
- //启动服务
- public static void start(string ip)
- {
- allScokets = new List<IWebSocketConnection>();
- server = new WebSocketServer(ip);
- server.Start(scoket =>
- {
- scoket.OnOpen = () => { allScokets.Add(scoket); };
- scoket.OnClose = () => { allScokets.Remove(scoket); };
- scoket.OnMessage = message => { allScokets.ToList().ForEach(s => s.Send(message)); };
- });
- }
- public bool IsReusable
- {
- get
- {
- return false;
- }
- }
- }
|