using parkMonitor.entity; using parkMonitor.LOG; using parkMonitor.server.uiLogServer; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; namespace parkMonitor.server.LaserLinker { class LaserLinker : IEquipments { public List laserMsgList { get; set; } public List laserMgmtList { get; set; } private Socket listener; private int listen_port = 1080; private int LASER_RESCAN_COUNT, LASER_HEARTBEAT_PERIOD; public void ReceiveFromLaser() { } public AbstractMessage GetMessage() { throw new NotImplementedException(); } public void SetMessage(AbstractMessage message) { throw new NotImplementedException(); } public void Start() { try { LASER_RESCAN_COUNT = Int32.Parse(ConfigurationManager.AppSettings.Get("laser_rescan_count")); LASER_HEARTBEAT_PERIOD = Int32.Parse(ConfigurationManager.AppSettings.Get("laser_countdown")); } catch (Exception) { } ////激光管理 //for (int i = 1; i < 20; i++) //{ // try // { // if (ConfigurationManager.AppSettings.AllKeys.Contains("laser" + i + "_status_address")) // { // string laser = ConfigurationManager.AppSettings.Get("laser" + i + "_status_address"); // int laser_status_address = Int32.Parse(laser); // LaserHandler lh = new LaserHandler(i, new IPEndPoint(IPAddress.Parse("192.168.0.127"),1080)); // laserMgmtList.Add(lh); // } // else { break; } // } // catch (Exception) { } //} // Create a TCP/IP socket. listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse("192.168.111.254"), listen_port); // Bind the socket to the local endpoint and listen for incoming connections. listener.Bind(localEndPoint); listener.Listen(20); // Start an asynchronous socket to listen for connections. Console.WriteLine("Waiting for a connection..."); listener.BeginAccept( new AsyncCallback(AcceptCallback), listener); } public void AcceptCallback(IAsyncResult ar) { try { // Get the socket that handles the client request. Socket listener = (Socket)ar.AsyncState; listener.BeginAccept( new AsyncCallback(AcceptCallback), listener); Socket conn = listener.EndAccept(ar); IPEndPoint remoteIPE = (IPEndPoint)(conn.RemoteEndPoint); // Create the state object. LaserHandler handler = new LaserHandler(0, new IPEndPoint(remoteIPE.Address, 8000)); handler.connection = conn; handler.Start(); } catch (Exception e) { Console.WriteLine(e.ToString()); } } public void Stop() { throw new NotImplementedException(); } } class LaserHandler { //laser id public int id { get; set; } public bool writeAvailable { get; set; } public bool linkAvailable { get; set; } //remote ipe private EndPoint remotePE { get; set; } // Client socket. public Socket remote { get; set; } public Socket connection { get; set; } // Size of receive buffer. private const int BufferSize = 512; // remote receive buffer private byte[] sendBuffer = new byte[BufferSize]; // connection receive buffer private byte[] receiveBuffer = new byte[BufferSize]; // Received data string. private LaserJson lj = new LaserJson(); public LaserHandler(int id, EndPoint pe) { this.id = id; remotePE = pe; } public void Start() { try { Receive(); remote = new Socket(remotePE.AddressFamily, SocketType.Stream, ProtocolType.Tcp); // Connect to the remote endpoint. remote.BeginConnect(remotePE, new AsyncCallback(ConnectCallback), null); } catch (Exception e) { Console.WriteLine(e.ToString()); this.Close(); } } public void Close() { if (connection != null) { connection.Close(); } if (remote != null) { remote.Close(); } } private void ConnectCallback(IAsyncResult ar) { try { // Complete the connection. remote.EndConnect(ar); Console.WriteLine("Socket connected to {0}", remote.RemoteEndPoint.ToString()); writeAvailable = true; } catch (Exception e) { Console.WriteLine(e.ToString()); this.Close(); writeAvailable = false; } } private void Receive() { try { connection.BeginReceive(receiveBuffer, 0, BufferSize, 0, new AsyncCallback(ReceiveCallback), null); } catch (Exception e) { Console.WriteLine(e.ToString()); this.Close(); linkAvailable = false; } } private void Send() { receiveBuffer.CopyTo(sendBuffer, 0); try { remote.BeginSend(sendBuffer, 0, sendBuffer.Length, 0, new AsyncCallback(SendCallback), null); } catch (Exception e) { Console.WriteLine(e.ToString()); this.Close(); writeAvailable = false; } } private void ReceiveCallback(IAsyncResult ar) { try { int bytesRead = connection.EndReceive(ar); //获得信息则再次监听 if (bytesRead > 0) { Send(); connection.BeginReceive(receiveBuffer, 0, BufferSize, 0, new AsyncCallback(ReceiveCallback), null); } else { this.Close(); } } catch (Exception e) { Console.WriteLine("雷达" + id + "掉线\n" + e.ToString()); this.Close(); linkAvailable = false; } } private void SendCallback(IAsyncResult ar) { try { remote.EndSend(ar); } catch (Exception e) { Console.WriteLine(e.ToString()); this.Close(); writeAvailable = false; } } } /// /// 激光数据记录与处理类 /// class LaserProcessUnit { /// /// 激光id /// public int id { get; set; } private int LASER_RESCAN_COUNT, LASER_HEARTBEAT_PERIOD, laser_rescan_countdown, laser_heartbeat_countdown; private bool laser_record, laser_heartbeat_test, enable_status_check = true, disordered = false; private HashSet laser_heartbeat = new HashSet(); /// /// 激光消息,用于保存激光数据、状态等信息 /// public LaserMessage laserMsg = new LaserMessage(); /// /// 激光处理单元构造函数,初始化各属性值 /// /// 编号 /// 停车指令地址 /// 激光状态地址 /// 激光重测次数 /// 激光心跳时间窗口 public LaserProcessUnit(int id, int laser_rescan_count, int laser_heartbeat_period) { try { laserMsg.id = id; this.id = id; laserMsg.status = 6; LASER_RESCAN_COUNT = laser_rescan_count; LASER_HEARTBEAT_PERIOD = laser_heartbeat_period; laser_rescan_countdown = LASER_RESCAN_COUNT; laser_heartbeat_countdown = LASER_HEARTBEAT_PERIOD; } catch (Exception) { UILogServer.ins.error("激光设备配置文件错误"); Log.WriteLog(LogType.NOT_DATABASE, LogFile.ERROR, "激光设备配置文件错误"); } } /// /// 更新激光状态 /// /// 地址 /// 值 public void UpdateLaserStatus(int addr, int value) { } /// /// 激光状态监测 /// /// 地址 /// 值 public void LaserStatusChecking(int addr, int value) { //if (enable_status_check) //{ // int status_addr = laser_status_address; // if (addr == status_addr) // { // //after status 0, start to check laser heartbeat // if (value == 0) // { // laserMsg.abort_rescan = false;//就绪状态设置允许重测 // laser_heartbeat_test = true; // } // //after status 3 or 4, start to check laser heartbeat // if (value == 3 && !laserMsg.recorded && laserMsg.licenseNum != "") // { // if (plc == null) // { // plc = EquipmentSimpleFactory.ins.FindEquipment(EquipmentName.PLC); // } // if (plc != null) // { // PLCNode pn = new PLCNode(laser_start_address.ToString(), "0"); // plc.SetMessage(pn); // //停车指令置0 // MyTimer mt = new MyTimer(); // mt.StartTiming(); // while (laserMsg.status != 254 && laserMsg.status != 255) // { // Thread.Sleep(1000); // mt.EndTiming(); // int activationCount = 0; // if (mt.IsLonger(15, 1, false, out activationCount)) // { // if (activationCount == 1) // { // UILogServer.ins.info("记录数据前未获得心跳,继续等待"); // } // if (MyTimer.restart && !mt.rolledBack) // { // mt.rolledBack = true; // UILogServer.ins.error("记录数据前超时未获得心跳,请检查设备"); // Log.WriteLog(LogType.NOT_DATABASE, LogFile.ERROR, "记录数据前超时未获得心跳"); // return; // } // } // } // laser_record = true; // laser_rescan_countdown = LASER_RESCAN_COUNT; // laser_heartbeat_test = true; // } // } // else if (value == 4) // { // laser_record = false; // //启动重测指令 // if (laser_rescan_countdown > 0) // { // enable_status_check = false; // Task t = Task.Factory.StartNew(() => // { // Thread.Sleep(500); // if (plc == null) // { // plc = EquipmentSimpleFactory.ins.FindEquipment(EquipmentName.PLC); // } // if (plc != null) // { // laser_rescan_countdown--; // //停车指令置0 // PLCNode pn = new PLCNode(laser_start_address.ToString(), "0"); // plc.SetMessage(pn); // //未终止重测,车未开走,停车指令归零后置1 // if (!laserMsg.abort_rescan) // { // UILogServer.ins.error("激光" + laserMsg.id + "计算异常,重新测量"); // Log.WriteLog(LogType.NOT_DATABASE, LogFile.ERROR, "激光" + laserMsg.id + "计算异常,重新测量"); // //重测检测心跳 // Task rescan_wait_heartbeat = Task.Factory.StartNew(() => // { // MyTimer mt = new MyTimer(); // mt.StartTiming(); // while (laserMsg.status != 254 && laserMsg.status != 255) // { // Thread.Sleep(1000); // mt.EndTiming(); // int activationCount = 0; // if (mt.IsLonger(15, 1, false, out activationCount)) // { // if (activationCount == 1) // { // UILogServer.ins.info("重测前未获得心跳,继续等待"); // } // if (MyTimer.restart && !mt.rolledBack) // { // mt.rolledBack = true; // UILogServer.ins.error("发起重测前超时未能获取摆扫激光心跳,请检查设备"); // Log.WriteLog(LogType.NOT_DATABASE, LogFile.ERROR, "发起重测前超时未能获取摆扫激光心跳"); // return; // } // } // } // }); // Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "获得心跳,准备发起重测"); // rescan_wait_heartbeat.Wait(); // pn = new PLCNode(laser_start_address.ToString(), "1"); // plc.SetMessage(pn); // } // } // Thread.Sleep(500); // enable_status_check = true; // }); // } // else if (laser_rescan_countdown == 0) // { // //激光1异常 // laser_rescan_countdown = -1; // laser_heartbeat_test = false; // UILogServer.ins.error("激光" + laserMsg.id + "计算异常超过重测次数,请检查"); // Log.WriteLog(LogType.NOT_DATABASE, LogFile.ERROR, "激光" + laserMsg.id + "计算异常超过重测次数"); // } // } // //status 5, system error // if (value == 5) // { // lock (laserMsg) // { // laser_heartbeat_test = false; // if (!disordered) // { // UILogServer.ins.error("激光" + laserMsg.id + "系统异常,请检查"); // Log.WriteLog(LogType.NOT_DATABASE, LogFile.ERROR, "激光" + laserMsg.id + "系统异常"); // } // disordered = true; // } // } // //find the heartbeat of laser // if (laser_heartbeat_test && laser_heartbeat_countdown-- > 0 && value != 3 && value != 4) // { // if (value == 254 || value == 255) // laser_heartbeat.Add(value); // if (laser_heartbeat.Contains(254) && laser_heartbeat.Contains(255)) // { // lock (laserMsg) // { // laserMsg.disconnected = false; // } // laser_heartbeat_countdown = LASER_HEARTBEAT_PERIOD; // laser_heartbeat_test = false; // laser_heartbeat.Clear(); // } // //fail to check laser heartbeat // if (laser_heartbeat_countdown <= 0) // { // laser_heartbeat_countdown = LASER_HEARTBEAT_PERIOD; // lock (laserMsg) // { // if (laserMsg.status >= 0 && !laserMsg.disconnected) // { // UILogServer.ins.error("激光" + laserMsg.id + "心跳检测失败"); // Log.WriteLog(LogType.NOT_DATABASE, LogFile.ERROR, "激光" + laserMsg.id + "心跳检测失败"); // } // laserMsg.disconnected = true; // } // } // } // } //} //else { return; } } /// /// 激光数据记录 /// /// plc数据列表 public void LaserRecord(List plist) { //lock (laserMsg) //{ // //激光未掉线,记录数据写入plc // if (laserMsg.status != -1) // { // if (laser_record) // { // laser_record = false; // foreach (PLCNode p in plist) // { // int addr = Int32.Parse(p.Address); // int value = Int32.Parse(p.Value); // if (addr == laser_status_address + 1) // laserMsg.data.centerX = value; // else if (addr == laser_status_address + 2) // laserMsg.data.centerY = value; // else if (addr == laser_status_address + 3) // laserMsg.data.angleA = value; // else if (addr == laser_status_address + 4) // laserMsg.data.length = value; // else if (addr == laser_status_address + 5) // laserMsg.data.width = value; // else if (addr == laser_status_address + 6) // laserMsg.data.height = value; // } // laserMsg.recorded = true; // UILogServer.ins.info("摆扫激光测量数据已记录"); // } // } //} } } }