123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558 |
- using allInOneMachine;
- using BroadcastModule;
- using centralController.advert;
- using db;
- using Monitor;
- using MySql.Data.MySqlClient;
- using NumMachine;
- using parkMonitor.language;
- using parkMonitor.model;
- using PLCS7;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Diagnostics;
- using System.Management;
- using System.Net;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using WebServer;
- namespace Monitor
- {
- public class Monitor : IMonitor
- {
- /// <summary>
- /// 监控模块单例
- /// </summary>
- public static Monitor ins { get; set; }
- /// <summary>
- /// 中控系统总状态
- /// </summary>
- public static bool globalStatus = false;
- /// <summary>
- /// 初始化步骤
- /// </summary>
- public static int initializeState = 0;
- /// <summary>
- /// PLC对象句柄
- /// </summary>
- public static AbstractPLCLinker PLC = null;
- public static string plcIPAddr { get; set; }
- public static int plcRack { get; set; }
- public static int plcSlot { get; set; }
- public static string[] plcDatablockConfig { get; set; }
- public static int plcTerminalCount { get; set; }
- public static int plcParkingSpaceCount { get; set; }
- public static int plcRefreshInterval { get; set; }
- public static MainBlockStru mainBlockInfo { get; set; }
- public static List<ParkingSpaceStru> parkingSpaceInfo { get; set; }
- /// <summary>
- /// 远程数据库操作句柄
- /// </summary>
- public static DBOperation remoteDBOper;
- /// <summary>
- /// 本地数据库操作句柄
- /// </summary>
- public static DBOperation localDBOper;
- /// <summary>
- /// 显示板操作对象句柄
- /// </summary>
- public static BroadcastLinker allInOneMachine;
- public static string allInOneMachineIP { get; set; }
- public static int allInOneMachinePort { get; set; }
- /// <summary>
- /// 号牌机操作句柄
- /// </summary>
- public static NumMachineLinker numMachineLinker;
- public static IntPtr flpHandle;
- /// <summary>
- /// 本地web操作句柄
- /// </summary>
- public static IWebServer webServer;
- public static int webPort { get; set; }
- /// <summary>
- /// 广告路径
- /// </summary>
- public static string advertPath { get; set; }
- public static AdvertManager advertMgr;
- /// <summary>
- /// 系统初始化器句柄
- /// </summary>
- internal static SystemInitializer sysInitializer;
- /// <summary>
- /// 系统关闭状态
- /// </summary>
- bool isClosing;
- /// <summary>
- /// 车库ID
- /// </summary>
- public static int garageID;
- /// <summary>
- /// 将显示在界面的提示字符串
- /// </summary>
- private static Queue<string> notificationQueue = new Queue<string>();
- private const int MAXLINES = 50;
- private void PLCUpdate()
- {
- int linkCount = 0;
- bool disconnected = false;
- while (!isClosing)
- {
- if (PLC != null)
- {
- if (PLC.isConnected)
- {
- if (disconnected) {disconnected = false; SetNotification("PLC已重新连接连接"); }
- linkCount = 0;
- List<object> received = PLC.ReadFromPLC(PLCDataType.terminal, 0);
- //首先获取所有终端信息
- try
- {
- //终端总数相同
- if (Terminal.Terminal.terminalInfo.Count == plcTerminalCount)
- {
- for (int i = 0; i < plcTerminalCount; i++)
- {
- //一旦发现差异立刻更新
- if (!Terminal.Terminal.terminalInfo[i].Equals(received[i]))
- {
- Terminal.Terminal.terminalInfo[i] = (TerminalStru)received[i];
- }
- }
- }
- else
- {
- //初始化终端信息列表
- Terminal.Terminal.terminalInfo.Clear();
- for (int i = 0; i < plcTerminalCount; i++)
- {
- Terminal.Terminal.terminalInfo.Add((TerminalStru)received[i]);
- Terminal.Terminal.termUsedMap.Add(((TerminalStru)received[i]).terminalID, false);
- }
- }
- }
- catch (Exception e) { Console.WriteLine("PLC监控终端数据," + e.Message); }
- //接下来获取中控监控信息
- try
- {
- received = PLC.ReadFromPLC(PLCDataType.central, 0);
- if (received.Count > 0 && !mainBlockInfo.Equals(received[0]))
- {
- mainBlockInfo = (MainBlockStru)received[0];
- }
- }
- catch (Exception e) { Console.WriteLine("PLC监控中控数据," + e.Message); }
- //最后获得所有车位信息
- try
- {
- received = PLC.ReadFromPLC(PLCDataType.parkingSpace, 0);
- //Console.WriteLine(parkingSpaceInfo.Count+","+ plcParkingSpaceCount);
- //车位总数相同
- if (parkingSpaceInfo.Count == plcParkingSpaceCount)
- {
- for (int i = 0; i < plcParkingSpaceCount; i++)
- {
- //一旦发现差异立刻更新
- if (!parkingSpaceInfo[i].Equals(received[i]))
- {
- parkingSpaceInfo[i] = (ParkingSpaceStru)received[i];
- }
- }
- }
- else
- {
- parkingSpaceInfo.Clear();
- for (int i = 0; i < plcParkingSpaceCount; i++)
- {
- parkingSpaceInfo.Add((ParkingSpaceStru)received[i]);
- }
- }
- }
- catch (Exception e) { Console.WriteLine("PLC监控车位数据," + e.Message); }
- }
- else
- {
- linkCount += 1;
- if (linkCount == 1)
- {
- disconnected = true;
- SetNotification("PLC掉线,请检查连接");
- }
- }
- }
- Thread.Sleep(500);
- }
- }
- /// <summary>
- /// CPU名
- /// </summary>
- /// <returns></returns>
- private static string getCPUName()
- {
- try
- {
- string str = string.Empty;
- ManagementClass mcCPU = new ManagementClass("Win32_Processor");
- ManagementObjectCollection mocCPU = mcCPU.GetInstances();
- foreach (ManagementObject m in mocCPU)
- {
- string name = m["Name"].ToString();
- return name;
- }
- }
- catch { }
- return "";
- }
- /// <summary>
- /// 操作系统版本
- /// </summary>
- private static string getOsVersion()
- {
- string str = "Windows 10";
- try
- {
- string hdId = string.Empty;
- ManagementClass hardDisk = new ManagementClass("Win32_OperatingSystem");
- ManagementObjectCollection hardDiskC = hardDisk.GetInstances();
- foreach (ManagementObject m in hardDiskC)
- {
- str = m["Name"].ToString().Split('|')[0].Replace("Microsoft", "").Trim();
- break;
- }
- }
- catch
- {
- }
- return str;
- }
- /// <summary>
- /// 显卡名
- /// </summary>
- private static string getGPUName()
- {
- string result = "";
- try
- {
- ManagementClass hardDisk = new ManagementClass("Win32_VideoController");
- ManagementObjectCollection hardDiskC = hardDisk.GetInstances();
- foreach (ManagementObject m in hardDiskC)
- {
- result = m["VideoProcessor"].ToString();
- break;
- }
- }
- catch
- {
- }
- return result;
- }
- /// <summary>
- /// 获取系统内存大小
- /// </summary>
- private static string getMenSize()
- {
- ManagementObjectSearcher searcher = new ManagementObjectSearcher(); //用于查询一些如系统信息的管理对象
- searcher.Query = new SelectQuery("Win32_PhysicalMemory", "", new string[] { "Capacity" });//设置查询条件
- ManagementObjectCollection collection = searcher.Get(); //获取内存容量
- ManagementObjectCollection.ManagementObjectEnumerator em = collection.GetEnumerator();
- long capacity = 0;
- while (em.MoveNext())
- {
- ManagementBaseObject baseObj = em.Current;
- if (baseObj.Properties["Capacity"].Value != null)
- {
- try
- {
- capacity += long.Parse(baseObj.Properties["Capacity"].Value.ToString());
- }
- catch
- {
- return "-GB";
- }
- }
- }
- int gb = 1024 * 1024 * 1024;
- return ((double)capacity / gb).ToString("0.0") + "GB";
- }
- //************************************ 公有方法 **********************************
- public Monitor(IntPtr flpHandle)
- {
- Monitor.flpHandle = flpHandle;
- parkingSpaceInfo = new List<ParkingSpaceStru>();
- mainBlockInfo = new MainBlockStru();
- }
- /// <summary>
- /// 获取提示信息
- /// </summary>
- /// <returns></returns>
- public string GetNotification()
- {
- StringBuilder notificationStr = new StringBuilder();
- lock (notificationQueue)
- {
- string[] strs = notificationQueue.ToArray();
- Array.Reverse(strs);
- for (int i = 0; i < strs.Length; i++)
- {
- notificationStr.Append(strs[i]);
- }
- //Queue<string>.Enumerator notiEnumer = notificationQueue.GetEnumerator();
- //while (notiEnumer.MoveNext())
- //{
- // notificationStr.Append(notiEnumer.Current);
- //}
- }
- return notificationStr.ToString();
- }
- /// <summary>
- /// 添加提示信息
- /// </summary>
- /// <param name="notification"></param>
- public static void SetNotification(string notification)
- {
- if (notification == "clear")
- {
- lock (notificationQueue)
- {
- notificationQueue.Clear();
- }
- return;
- }
- else
- {
- string time = DateTime.Now + "\r\n";
- string notificationStr = time + notification + "\r\n";
- lock (notificationQueue)
- {
- int count = notificationQueue.Count;
- if (count >= MAXLINES)
- {
- notificationQueue.Dequeue();
- }
- notificationQueue.Enqueue(notificationStr);
- }
- }
- }
- /// <summary>
- /// 返回系统信息字符串
- /// </summary>
- /// <returns></returns>
- public static string GetSysInfo()
- {
- string info = "";
- Language lng = Language.ins;
- try
- {
- string endl = "\r\n";
- info += endl + lng.autoPackSys + " " + lng.centralPort + endl + endl;
- info += lng.version + ":" + SysConst.version() + endl + endl;
- info += lng.hostNmae + ":" + Dns.GetHostName() + endl + endl;
- info += lng.os + ":" + getOsVersion() + endl + endl;
- info += lng.cpu + ":" + getCPUName() + endl + endl;
- info += lng.mem + ":" + getMenSize() + endl + endl;
- info += lng.gpu + ":" + getGPUName() + endl + endl;
- }
- catch (Exception) { }
- return info;
- }
- /// <summary>
- /// 返回停车记录信息
- /// </summary>
- /// <returns></returns>
- public static List<object[]> GetParkingRecords(string license = "", string startTime = "", string endTime = "")
- {
- DateTime now = DateTime.Now;
- List<object[]> result = new List<object[]>();
- string getParkingRecordsSql = "";
- if (startTime == "" || endTime == "")
- {
- DateTime yesterday = DateTime.Now - (new TimeSpan(1, 0, 0, 0));
- DateTime twoDaysAgo = DateTime.Now - (new TimeSpan(2, 0, 0, 0));
- getParkingRecordsSql = "select parkingRecordsID,userID,numberPlate,parkingSpaceID,realParkTime,realGetTime,receiptNum,parkingPrice,paymentStatus " +
- "from parkingrecords where numberPlate " + (license == "" ? "like '%" : "= '" + license) + "' and (realParkTime like '" + now.ToString("yyyy-MM-dd") + "%' or realParkTime like '" + yesterday.ToString("yyyy-MM-dd") + "%' or realParkTime like '" + twoDaysAgo.ToString("yyyy-MM-dd") + "%');";
- }
- else
- {
- getParkingRecordsSql = "select parkingRecordsID,userID,numberPlate,parkingSpaceID,realParkTime,realGetTime,receiptNum,parkingPrice,paymentStatus " +
- "from parkingrecords where numberPlate " + (license == "" ? "like '%" : "= '" + license) + "' and realParkTime >= '" + startTime + "' and realParkTime <= '" + endTime + "';";
- }
- if (localDBOper != null)
- {
- lock (localDBOper)
- {
- MySqlDataReader reader = localDBOper.Query(getParkingRecordsSql);
- try
- {
- //MySqlDataAdapter adapter = localDBOper.Display(getParkingRecordsSql);
- //DataSet ds = new DataSet();
- //adapter.Fill(ds);
- //DataTable dt = ds.Tables[0];
- //for (int i = 0; i < dt.Rows.Count; i++)
- //{
- // object[] objArray = new object[10];
- // for (int j = 0; j < dt.Columns.Count; j++)
- // {
- // objArray[j] = dt.Rows[i][j];
- // }
- // result.Add(objArray);
- //}
- //adapter.Dispose();
- while (reader != null && reader.Read())
- {
- if (reader.HasRows)
- {
- object[] temp = new object[reader.FieldCount];
- reader.GetValues(temp);
- result.Add(temp);
- }
- }
- }
- catch (Exception e) { Console.WriteLine(e.Message); }
- try
- {
- if (reader != null)
- {
- reader.Close();
- reader.Dispose();
- }
- }
- catch (Exception e) { Console.WriteLine(e.Message); }
- }
- }
- result.Reverse();
- return result;
- }
- /// <summary>
- /// 返回预约记录信息
- /// </summary>
- /// <returns></returns>
- public static List<object[]> GetOrderRecords()
- {
- return null;
- }
- /// <summary>
- /// 返回空闲正常车位数
- /// </summary>
- /// <param name="state">状态:0.空闲与保留,1.空闲,2.保留</param>
- /// <returns></returns>
- public int GetFreeSpaceCount(int state)
- {
- int freeSpaceCount = 0;
- int reservedSpaceCount = 0;
- if (parkingSpaceInfo != null)
- {
- foreach (ParkingSpaceStru psStru in parkingSpaceInfo)
- {
- if (psStru.spaceStatus != 1 && psStru.spaceStatus != 3)
- {
- freeSpaceCount++;
- if (psStru.spaceStatus == 2)
- reservedSpaceCount++;
- }
- }
- }
- switch (state)
- {
- case 0:
- return freeSpaceCount;
- case 1:
- return freeSpaceCount - reservedSpaceCount;
- case 2:
- return reservedSpaceCount;
- default:
- return freeSpaceCount;
- }
- }
- /// <summary>
- /// 暂未使用,查询本地数据库返回可预约车位数
- /// </summary>
- /// <returns></returns>
- public int GetBookableSpaceCount()
- {
- int bookableSpaceCount = 0;
- string bookableSpaceSql = "select currentBookableSpace from garageproperties where garageID = " + garageID + ";";
- if (localDBOper != null)
- {
- MySqlDataReader reader = localDBOper.Query(bookableSpaceSql);
- if (reader != null)
- {
- try
- {
- if (reader.Read() && reader.HasRows)
- {
- bookableSpaceCount = reader.GetInt32("currentBookableSpace");
- }
- }
- catch { }
- try
- {
- reader.Close();
- reader.Dispose();
- }
- catch { }
- }
- }
- return bookableSpaceCount;
- }
- /// <summary>
- /// 系统初始化,启动plc监控
- /// </summary>
- public void Start()
- {
- if (flpHandle != IntPtr.Zero)
- {
- //初始化系统
- if (sysInitializer == null)
- {
- sysInitializer = new SystemInitializer();
- }
- Task.Factory.StartNew(() =>
- {
- sysInitializer.Init(flpHandle);
- });
- //更新PLC数据
- Task.Factory.StartNew(() =>
- {
- PLCUpdate();
- });
- }
- }
- /// <summary>
- /// 系统停止
- /// </summary>
- public void Stop()
- {
- sysInitializer.Stop();
- }
- }
- //public class ParkingRecord
- //{
- // int parkingRecordsID;
- // int userID;
- // string numberPlate;
- // int parkingSpaceID;
- //}
- }
|