123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522 |
- 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<LaserMessage> laserMsgList { get; set; }
- public List<LaserHandler> 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;
- }
- }
- }
- /// <summary>
- /// 激光数据记录与处理类
- /// </summary>
- class LaserProcessUnit
- {
- /// <summary>
- /// 激光id
- /// </summary>
- 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<int> laser_heartbeat = new HashSet<int>();
- /// <summary>
- /// 激光消息,用于保存激光数据、状态等信息
- /// </summary>
- public LaserMessage laserMsg = new LaserMessage();
- /// <summary>
- /// 激光处理单元构造函数,初始化各属性值
- /// </summary>
- /// <param name="id">编号</param>
- /// <param name="park_command_address">停车指令地址</param>
- /// <param name="laser_status_address">激光状态地址</param>
- /// <param name="laser_rescan_count">激光重测次数</param>
- /// <param name="laser_heartbeat_period">激光心跳时间窗口</param>
- 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, "激光设备配置文件错误");
- }
- }
- /// <summary>
- /// 更新激光状态
- /// </summary>
- /// <param name="addr">地址</param>
- /// <param name="value">值</param>
- public void UpdateLaserStatus(int addr, int value)
- {
- }
- /// <summary>
- /// 激光状态监测
- /// </summary>
- /// <param name="addr">地址</param>
- /// <param name="value">值</param>
- 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; }
- }
- /// <summary>
- /// 激光数据记录
- /// </summary>
- /// <param name="plist">plc数据列表</param>
- public void LaserRecord(List<PLCNode> 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("摆扫激光测量数据已记录");
- // }
- // }
- //}
- }
- }
- }
|