123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- 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.DataBase;
- namespace parkMonitor.server.CoreThread
- {
- class CoreThreadTest2 : IEquipments, ICoreThreadDoWorking
- {
- int equipmentStatus_address;//设备总控状态地址
- //获取PLC句柄
- IEquipments PLC;
- //获取队列句柄
- IEquipments queuingThread;
- //获取号牌句柄
- IEquipments NumMachine;
- int localGarageId = 0;
- bool isClosing;
- string[] equipNames = new string[] { EquipmentName.PLC, EquipmentName.NumMachine, EquipmentName.Web, EquipmentName.Queue };
- public CoreThreadTest2()
- {
- try
- {
- equipmentStatus_address = Convert.ToInt32(ConfigurationManager.AppSettings["equipmentStatus_address"]);
- localGarageId = Convert.ToInt32(ConfigurationManager.AppSettings["localGarageId"]);
- DBConnection.Init();
- }
- catch (Exception) { }
- }
- public void Start()
- {
- for (int i = 0; i < equipNames.Length; ++i)
- {
- IEquipments equip = EquipmentSimpleFactory.ins.CreateEquipment(equipNames[i]);
- if (equip != null)
- {
- equip.Start();
- }
- }
- PLC = EquipmentSimpleFactory.ins.FindEquipment(EquipmentName.PLC);
- queuingThread = EquipmentSimpleFactory.ins.FindEquipment(EquipmentName.Queue);
- NumMachine = EquipmentSimpleFactory.ins.FindEquipment(EquipmentName.NumMachine);
- //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;
- }
- public void BeginWorking()
- {
- Task.Factory.StartNew(() =>
- {
- while (!isClosing)
- {
- Func<AbstractMessage> func = new Func<AbstractMessage>(GetExecuteNumberPlate);
- TaskFactory taskFactory = new TaskFactory();
- List<Task> tasklist = new List<Task>();
- IAsyncResult result = func.BeginInvoke(null, null);
- Action act1 = new Action(() =>
- {
- GetTotalStatus();
- });
- tasklist.Add(taskFactory.StartNew(act1));
- Task.WaitAll(tasklist.ToArray());
- Command queueCmd = (Command)func.EndInvoke(result);
- Task.Factory.StartNew(() =>
- {
- SimpleCMDFactory simpleCMDFactory = new SimpleCMDFactory();
- AbstractCmd abstractCmd = simpleCMDFactory.createCmd(queueCmd);
- abstractCmd.executeCmd(queueCmd);
- });
- Thread.Sleep(100);
- }
- });
- //同步数据库
- //Task.Factory.StartNew(() =>
- //{
- // while (true)
- // {
- // try
- // {
- // //更新车库表车位数
- // int freeSpaceCount = oper.getGarageFreeSpace("SqlConnectionLocation",localGarageId);
- // oper.UpdateGarageFreeSpace("SqlConnectionStr",freeSpaceCount, localGarageId);
- // //更新车位状态
- // //int[] parkingSpace = oper.GetAllParkingSpaceID(localGarageId);
- // Dictionary<int, Parking_Space> lps = oper.GetAllParkingSpace("SqlConnectionLocation", localGarageId);
- // if (lps != null)
- // {
- // //Dictionary<>
- // for (int i = 1; i <= lps.Count; i++)
- // {
- // oper.UpdateParkingSpaceState("SqlConnectionStr", lps[i].parkingSpaceID,lps[i].parkingSpaceState);
- // }
- // }
- // }
- // catch (Exception)
- // {
- // Console.WriteLine("无同步数据");
- // }
- // Thread.Sleep(8000);
- // }
- //});
- }
- /// <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)
- {
- UILogServer.ins.log("设备总状态正常");
- break;
- }
- Thread.Sleep(100);
- }
- }
- /// <summary>
- /// 获取可执行号牌
- /// </summary>
- private AbstractMessage GetExecuteNumberPlate()
- {
- AbstractMessage msg = null;
- while (!isClosing)
- {
- msg = queuingThread.GetMessage();
- if (msg != null)
- {
- break;
- }
- }
- return msg;
- }
- }
- }
|