private async void RunSimulator() { //创建驱动 for (int i = 0; i < this.Communications.Count; i++) { await Task.Run(() => { if (this.Communications[i].CommunicateDriver == null) { MonitorDataBaseModel.IOCommunications[i].CommunicateDriver = DriverAssembly.CreateCommunicateDriver(MonitorDataBaseModel.IOCommunications[i].DriverInfo); } ScadaCommunicateKernel driverDll = (ScadaCommunicateKernel)MonitorDataBaseModel.IOCommunications[i].CommunicateDriver; driverDll.IsCreateControl = false; driverDll.InitKernel(this.Servers.Find(x => x.SERVER_ID == this.Communications[i].IO_SERVER_ID), this.Communications[i], Communications[i].Devices, Communications[i].DriverInfo); driverDll.Simulator(Interval, false); driverDll.SimulatorStart(); driverDll.SimulatorLog += DriverDll_SimulatorLog; driverDll.CommunctionClose += IOMonitorManager.CDriverDll_CommunctionClose; driverDll.CommunctionContinue += IOMonitorManager.CDriverDll_CommunctionContinue; driverDll.CommunctionPause += IOMonitorManager.CDriverDll_CommunctionPause; driverDll.CommunctionStart += IOMonitorManager.CDriverDll_CommunctionStart; driverDll.CommunctionStop += IOMonitorManager.CDriverDll_CommunctionStop; driverDll.DeviceSended += IOMonitorManager.CDriverDll_DeviceSended; driverDll.DeviceStatusChanged += IOMonitorManager.CDriverDll_DeviceStatusChanged; driverDll.Exception += IOMonitorManager.CDriverDll_Exception; driverDll.OnDataReceived += IOMonitorManager.CDriverDll_OnDataReceived; driverDll.OnShowFormLog += IOMonitorManager.CDriverDll_OnShowFormLog; }); } }
public int Interval = 3; //默认是3秒 /// <summary> /// 启动模拟器 /// </summary> /// <param name="form"></param> public void InitSimulator(int mInterval) { Task.Run(() => { Scada.Business.SCADA_DRIVER DriverBll = new Scada.Business.SCADA_DRIVER(); Scada.Business.SCADA_DEVICE_DRIVER DeviceDriverBll = new Scada.Business.SCADA_DEVICE_DRIVER(); Scada.Business.IO_DEVICE deviceBll = new Scada.Business.IO_DEVICE(); Scada.Business.IO_COMMUNICATION commBll = new Scada.Business.IO_COMMUNICATION(); Scada.Business.IO_SERVER serverBll = new Scada.Business.IO_SERVER(); List <Scada.Model.SCADA_DRIVER> CommDrivers = null; List <Scada.Model.SCADA_DEVICE_DRIVER> DeviceDrivers = null; Interval = mInterval;//获取时间间隔 Servers = serverBll.GetModelList(""); Communications = commBll.GetModelList(""); Devices = deviceBll.GetModelList(""); CommDrivers = DriverBll.GetModelList(""); DeviceDrivers = DeviceDriverBll.GetModelList(""); for (int i = 0; i < Communications.Count; i++) { Communications[i].DriverInfo = CommDrivers.Find(x => x.Id == Communications[i].IO_COMM_DRIVER_ID); if (Communications[i].DriverInfo != null) { Communications[i].CommunicateDriver = DriverAssembly.CreateCommunicateDriver(Communications[i].DriverInfo); } Communications[i].Devices = Devices.FindAll(x => x.IO_COMM_ID == Communications[i].IO_COMM_ID && x.IO_SERVER_ID == Communications[i].IO_SERVER_ID); for (int j = 0; j < Communications[i].Devices.Count; j++) { Communications[i].Devices[j].DriverInfo = DeviceDrivers.Find(x => x.Id == Communications[i].Devices[j].DEVICE_DRIVER_ID); if (Communications[i].Devices[j].DriverInfo != null) { Communications[i].Devices[j].DeviceDrive = DriverAssembly.CreateDeviceDrive(Communications[i].Devices[j].DriverInfo); } } } }); }
/// <summary> /// 结束模拟器 /// </summary> public void ColseSimulator() { Task.WaitAll(); IsSimulator = false; for (int i = 0; i < this.Communications.Count; i++) { try { if (this.Communications[i].CommunicateDriver == null) { MonitorDataBaseModel.IOCommunications[i].CommunicateDriver = DriverAssembly.CreateCommunicateDriver(MonitorDataBaseModel.IOCommunications[i].DriverInfo); } ScadaCommunicateKernel driverDll = (ScadaCommunicateKernel)MonitorDataBaseModel.IOCommunications[i].CommunicateDriver; driverDll.SimulatorClose(); } catch { continue; } } GC.Collect(); }
public static void InitBaseModel() { if (ServerID == "") { return; } try { DbHelperSQLite.connectionString = "Data Source=" + Application.StartupPath + "\\IOProject\\Station.station"; IO_SERVER serverBll = new IO_SERVER(); AddLogToMainLog("读取采集站信息......"); IOServer = serverBll.GetModel(mServerID); if (IOServer == null) { return; } mServerID = IOServer.SERVER_ID; IOServer.SERVER_IP = LocalIp.GetLocalIp(); serverBll.Update(IOServer); //加载通道 AddLogToMainLog("读取采集站通道信息......"); IO_COMMUNICATION commBll = new IO_COMMUNICATION(); SCADA_DRIVER DriverBll = new SCADA_DRIVER(); SCADA_DEVICE_DRIVER DeviceDriverBll = new SCADA_DEVICE_DRIVER(); CommDrivers = DriverBll.GetModelList(""); DeviceDrivers = DeviceDriverBll.GetModelList(""); IOCommunications = commBll.GetModelList(" IO_SERVER_ID='" + IOServer.SERVER_ID + "'"); AddLogToMainLog("读取采集站通道下的所有设备信息......"); IO_DEVICE deviceBll = new IO_DEVICE(); IODevices = deviceBll.GetModelList(" IO_SERVER_ID='" + IOServer.SERVER_ID + "'"); AddLogToMainLog("数据处理中....."); for (int i = 0; i < IOCommunications.Count; i++) { IOCommunications[i].DriverInfo = CommDrivers.Find(x => x.Id == IOCommunications[i].IO_COMM_DRIVER_ID); if (IOCommunications[i].DriverInfo != null) { IOCommunications[i].CommunicateDriver = DriverAssembly.CreateCommunicateDriver(IOCommunications[i].DriverInfo); } AddLogToMainLog("处理 " + IOCommunications[i].IO_COMM_NAME + "[" + IOCommunications[i].IO_COMM_LABEL + "]"); IOCommunications[i].Devices = IODevices.FindAll(x => x.IO_COMM_ID == IOCommunications[i].IO_COMM_ID && x.IO_SERVER_ID == IOCommunications[i].IO_SERVER_ID); for (int j = 0; j < IOCommunications[i].Devices.Count; j++) { IOCommunications[i].Devices[j].DriverInfo = DeviceDrivers.Find(x => x.Id == IOCommunications[i].Devices[j].DEVICE_DRIVER_ID); if (IOCommunications[i].Devices[j].DriverInfo != null) { IOCommunications[i].Devices[j].DeviceDrive = DriverAssembly.CreateDeviceDrive(IOCommunications[i].Devices[j].DriverInfo); } } } AddLogToMainLog("正在创建驱动....."); AddLogToMainLog("读取工程完成!"); ProgressMaxNum = IOCommunications.Count + IODevices.Count; } catch (Exception ex) { ThrowExceptionToMain(ex); } }