123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using parkMonitor.LOG;
- using parkMonitor.entity;
- using parkMonitor.model;
- using System.Configuration;
- using System.Threading;
- using parkMonitor.server.uiLogServer;
- using parkMonitor.tools;
- using parkMonitor.Database2;
- using MySql.Data.MySqlClient;
- using parkMonitor.server.CoreThread;
- namespace parkMonitor.server.CoreThread
- {
- class CoreThreadTest2 : IEquipments, ICoreThreadDoWorking
- {
- int equipmentStatus_address;//设备总控状态地址
- bool initialized = false;
- /// <summary>
- /// 获取PLC句柄
- /// </summary>
- IEquipments PLC;
- /// <summary>
- /// 获取队列句柄
- /// </summary>
- IEquipments queuingThread;
- /// <summary>
- /// 获取号牌句柄
- /// </summary>
- IEquipments NumMachine;
- bool isClosing;
- string[] equipNames = new string[] { EquipmentName.PLC, EquipmentName.NumMachine, EquipmentName.Web, EquipmentName.Queue };
- public CoreThreadTest2()
- {
- try
- {
- EntityForCore.ins.globalStatus = false;
- equipmentStatus_address = Convert.ToInt32(ConfigurationManager.AppSettings["equipmentStatus_address"]);
- }
- catch (Exception) { }
- }
- public void Start()
- {
- //创建所有实例
- for (int i = 0; i < equipNames.Length; ++i)
- {
- IEquipments equip = EquipmentSimpleFactory.ins.CreateEquipment(equipNames[i]);
- }
- //启动所有实例
- for (int i = 0; i < equipNames.Length; ++i)
- {
- IEquipments equip = EquipmentSimpleFactory.ins.FindEquipment(equipNames[i]);
- equip.Start();
- }
- PLC = EquipmentSimpleFactory.ins.FindEquipment(EquipmentName.PLC);
- queuingThread = EquipmentSimpleFactory.ins.FindEquipment(EquipmentName.Queue);
- NumMachine = EquipmentSimpleFactory.ins.FindEquipment(EquipmentName.NumMachine);
- if (!(EntityForCore.ins.globalStatus = EntityForCore.ins.Init()))
- {
- UILogServer.ins.error("系统初始化异常,请检查配置");
- }
- initialized = true;
- //BeginWorking();
- //监听总状态改变消息
- //由监控线程发出
- AsyncCmdServer.ins.listen(AsyncCmdType.TotalStatusChanged, (AlarmStatus type) =>
- {
- if (type != AlarmStatus.Normal)
- {
- hangUp();
- }
- });
- }
- public AbstractMessage GetMessage()
- {
- return null;
- }
- public void SetMessage(AbstractMessage message)
- {
- }
- public void Stop()
- {
- for (int i = 0; i < equipNames.Length; ++i)
- {
- IEquipments equip = EquipmentSimpleFactory.ins.FindEquipment(equipNames[i]);
- if (equip != null)
- {
- equip.Stop();
- }
- }
- AbstractCmd.isClosing = true;//停止运行中指令
- this.isClosing = true;
- }
- /// <summary>
- /// 核心线程工作
- /// </summary>
- public void BeginWorking()
- {
- Timer checkRemoteTimer = new System.Threading.Timer(CheckRemotePool, null, 3600000, 3600000);
- Timer killTimeoutConnection = new System.Threading.Timer(KillTimeoutConnection, null, 3600000, 3600000);
- //Timer checkLocalTimer = new System.Threading.Timer(CheckLocalPool, null, 10000, 300000);
- //Timer logTimer = new System.Threading.Timer(displayLog, null, 1000, 30000);
- while (!isClosing)
- {
- if (initialized && EntityForCore.ins.globalStatus)
- {
- Command queueCmd = null;
- //等待号牌资源
- Task getCmd = Task.Factory.StartNew(() =>
- {
- while (true)
- {
- if (EntityForCore.ins.globalStatus)
- {
- queueCmd = (Command)queuingThread.GetMessage();
- if (queueCmd != null)
- {
- //远端或本地异常,遇到相应指令则退回
- if ((!queueCmd.manual && ConnectionPoolManager.malfunctionRemote) || (queueCmd.manual && ConnectionPoolManager.malfunctionLocal))
- {
- queueCmd.returnedCount = 1;
- queuingThread.SetMessage((Command)queueCmd.Clone());
- Thread.Sleep(5000);
- }
- //判断当前计数是否允许线程创建,机械手资源争抢严重则将指令丢回队列
- else if (queueCmd.id / 6 + 1 == Robot.robot1.id)
- {
- if ((queueCmd.commandType == 's' && Robot.robot1.parkWaitCount >= 1) || (queueCmd.commandType == 'f' && Robot.robot1.fetchWaitCount >= 1))
- {
- Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "等待机械手1资源线程过多:" + Robot.robot1.parkWaitCount + "," + Robot.robot1.fetchWaitCount);
- queueCmd.returnedCount = 10000;
- queuingThread.SetMessage((Command)queueCmd.Clone());
- Thread.Sleep(15000);
- continue;
- }
- }
- else if (queueCmd.id / 6 + 1 == Robot.robot2.id)
- {
- if ((queueCmd.commandType == 's' && Robot.robot2.parkWaitCount >= 1) || (queueCmd.commandType == 'f' && Robot.robot2.fetchWaitCount >= 1))
- {
- Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "等待机械手2资源线程过多:" + Robot.robot2.parkWaitCount + "," + Robot.robot2.fetchWaitCount);
- queueCmd.returnedCount = 10000;
- queuingThread.SetMessage((Command)queueCmd.Clone());
- Thread.Sleep(15000);
- continue;
- }
- }
- break;
- }
- }Thread.Sleep(2000);
- }
- });
- Task.WaitAll(getCmd);
- //等待系统总状态
- Task getSysStatus = Task.Factory.StartNew(() =>
- {
- GetTotalStatus();
- });
- Task.WaitAll(getSysStatus);
- //对依赖同一机械手的线程计数
- if (queueCmd.id / 6 + 1 == Robot.robot1.id)
- {
- if (queueCmd.commandType == 's')
- {
- Robot.robot1.parkWaitCount += 1;
- }
- else if (queueCmd.commandType == 'f')
- {
- Robot.robot1.fetchWaitCount += 1;
- }
- }
- else if (queueCmd.id / 6 + 1 == Robot.robot2.id)
- {
- if (queueCmd.commandType == 's')
- {
- Robot.robot2.parkWaitCount += 1;
- }
- else if (queueCmd.commandType == 'f')
- {
- Robot.robot2.fetchWaitCount += 1;
- }
- }
- Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "---线程创建:" + queueCmd.LicenseNum + "准备,等待机械手1资源停取线程计数:" + Robot.robot1.parkWaitCount + "," + Robot.robot1.fetchWaitCount);
- Task t = Task.Factory.StartNew(() =>
- {
- Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "---线程创建:" + queueCmd.LicenseNum + "开始");
- SimpleCMDFactory simpleCMDFactory = new SimpleCMDFactory();
- AbstractCmd abstractCmd = simpleCMDFactory.createCmd(queueCmd);
- abstractCmd.executeCmd(queueCmd);
- Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "---线程创建:" + queueCmd.LicenseNum + "完成");
- });
- if (t != null)
- {
- Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "---线程创建:" + queueCmd.LicenseNum + "状态:" + t.Status);
- }
- else
- {
- Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "---线程创建:" + queueCmd.LicenseNum + "为空");
- }
- }
- Thread.Sleep(300);
- }
- }
- /// <summary>挂起</summary>
- public void hangUp()
- {
- }
- /// <summary>
- /// 获取PLC总控状态
- /// </summary>
- private void GetTotalStatus()
- {
- PLCMessage PLCMsg = null;
- int normalStatus = 0;
- while (!isClosing)
- {
- PLCMsg = (PLCMessage)PLC.GetMessage();
- if (PLCMsg != null && PLCMsg.originalPlcList != null && PLCMsg.originalPlcList.Count != 0)
- {
- normalStatus = Convert.ToInt32(PLCMsg.originalPlcList[equipmentStatus_address].Value);
- }
- //设备总控状态
- if (normalStatus == 1 && EntityForCore.ins.globalStatus)
- {
- UILogServer.ins.log("设备总状态正常");
- Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "设备总状态正常");
- break;
- }
- Thread.Sleep(200);
- }
- }
- /// <summary>
- /// 执行日志文件数据库操作
- /// </summary>
- /// <param name="o"></param>
- private void displayLog(object o)
- {
- while (!EntityForCore.ins.globalStatus)
- {
- Thread.Sleep(1000);
- }
- lock (Parking_Space.spaceLock)
- {
- string sqlStatus;
- string sqlMsg;
- int count;
- Log.ReadLog(out sqlStatus, out sqlMsg, out count);
- List<string> strs = new List<string>();
- strs.Add(sqlMsg);
- if (count > 0)
- {
- if (sqlStatus == "1")
- {
- int parkingRecordsID = 0;
- try
- {
- //Operation.MyTransaction(EntityForCore.remoteBQ,sqlMsg, out parkingRecordsID);
- parkingRecordsID = DBOperation.InsertParkingRecords(EntityForCore.remoteBQ, strs);
- if (parkingRecordsID == 0)
- {
- Log.WriteLog(LogType.DATABASE, sqlStatus + ":" + sqlMsg);
- }
- }
- catch
- {
- Log.WriteLog(LogType.DATABASE, sqlStatus + ":" + sqlMsg);
- //throw;
- }
- }
- if (sqlStatus == "0")
- {
- bool result;
- try
- {
- //Operation.MyTransaction(EntityForCore.remoteBQ,sqlMsg, out temp);
- result = DBOperation.UpdateTransaction(EntityForCore.remoteBQ, strs);
- if (!result)
- {
- Log.WriteLog(LogType.DATABASE, sqlStatus + ":" + sqlMsg);
- }
- }
- catch
- {
- Log.WriteLog(LogType.DATABASE, sqlStatus + ":" + sqlMsg);
- //throw;
- }
- }
- Log.ReadLog(out sqlStatus, out sqlMsg, out count);
- }
- }
- }
- private void CheckRemotePool(object o)
- {
- ConnectionPoolManager.CheckConnPooling(3, 10, ConnectionPoolManager.remoteConf, EntityForCore.remoteBQ);
- }
- private void CheckLocalPool(object o)
- {
- ConnectionPoolManager.CheckConnPooling(3, 10, ConnectionPoolManager.localConf, EntityForCore.localBQ);
- }
- private void KillTimeoutConnection(object o)
- {
- DBOperation.KillTimeOutConnection(null, 3600, EntityForCore.remoteBQ);
- }
- }
- }
|