public IO_DEVICE Copy() { IO_DEVICE copyDevice = new IO_DEVICE() { DEVICE_DRIVER_ID = this.DEVICE_DRIVER_ID, IO_COMM_ID = this.IO_COMM_ID, IO_DEVICE_ADDRESS = this.IO_DEVICE_ADDRESS, IO_DEVICE_ID = this.IO_DEVICE_ID, IO_DEVICE_LABLE = this.IO_DEVICE_LABLE, IO_DEVICE_NAME = this.IO_DEVICE_NAME, IO_DEVICE_OVERTIME = this.IO_DEVICE_OVERTIME, IO_DEVICE_PARASTRING = this.IO_DEVICE_PARASTRING, IO_DEVICE_REMARK = this.IO_DEVICE_REMARK, IO_DEVICE_STATUS = this.IO_DEVICE_STATUS, GetedValueDate = this.GetedValueDate, IO_DEVICE_UPDATECYCLE = this.IO_DEVICE_UPDATECYCLE, IO_SERVER_ID = this.IO_SERVER_ID, Tag = this.Tag, ReceiveBytes = this.ReceiveBytes, DriverInfo = this.DriverInfo == null ? null : this.DriverInfo.Copy() }; copyDevice.IOParas = new List <IO_PARA>(); for (int i = 0; i < this.IOParas.Count; i++) { copyDevice.IOParas.Add(this.IOParas[i].Copy()); } return(copyDevice); }
/// <summary> /// 获得数据列表 /// </summary> public List <Scada.Model.IO_DEVICE> DataTableToList(DataTable dt, string strWhere) { List <Scada.Model.SCADA_DEVICE_DRIVER> drivers = driverBLL.GetModelList(""); List <Scada.Model.IO_PARA> allParas = paraBll.GetModelList(strWhere); List <Task> tasks = new List <Task>(); List <Scada.Model.IO_DEVICE> modelList = new List <Scada.Model.IO_DEVICE>(); int rowsCount = dt.Rows.Count; if (rowsCount > 0) { int sid = dt.Rows.Count % 100 == 0 ? (dt.Rows.Count / 100) : (dt.Rows.Count / 100 + 1); for (int a = 1; a <= sid; a++) { object aa = a.ToString() + "," + sid.ToString(); tasks.Add(Task.Run(() => { int startindex = Convert.ToInt32(aa.ToString().Split(',')[0]); int limitstep = Convert.ToInt32(aa.ToString().Split(',')[1]); for (int i = (startindex > 1 ? ((startindex - 1) * 100) : 0); i < (startindex == limitstep ? (dt.Rows.Count) : startindex * 100); i++) { Scada.Model.IO_DEVICE model = dal.DataRowToModel(dt.Rows[i], drivers); if (model != null) { model.IOParas = allParas.FindAll(x => x != null && x.IO_DEVICE_ID == model.IO_DEVICE_ID && x.IO_COMM_ID == model.IO_COMM_ID && x.IO_SERVER_ID == model.IO_SERVER_ID); lock (modelList) { modelList.Add(model); } lock (allParas) { for (int c = 0; c < model.IOParas.Count; c++) { allParas.Remove(model.IOParas[c]);//删除已经搜索到的 } } } } })); } } Task.WaitAll(tasks.ToArray()); tasks.Clear(); return(modelList); }
/// <summary> /// 刷新树状态 /// </summary> public static void RefreshIOStatus(Scada.Model.IO_DEVICE device) { if (device == null) { return; } if (mediator.IOStatusForm.IsHandleCreated) { mediator.IOStatusForm.IoTreeStatus.BeginInvoke(new EventHandler(delegate { if (mediator.IOStatusForm.IoTreeStatus.Nodes.Find(device.IO_DEVICE_ID, true).Length > 0) { IoDeviceTreeNode treeNode = (IoDeviceTreeNode)mediator.IOStatusForm.IoTreeStatus.Nodes.Find(device.IO_DEVICE_ID, true).First(); treeNode.ChangedStatus(device.IO_DEVICE_STATUS); } })); } }
/// <summary> /// 传入的是实际的数据包,并不包含头标识和尾标识 /// </summary> /// <param name="receivebytes"></param> /// <param name="count"></param> /// <param name="c"></param> private void SplitReceivePack(byte[] receivebytes, int count, AsyncTcpClient c) { //将实际字节转化成字符串 string message = Encoding.UTF8.GetString(receivebytes); //心跳包,不做处理 if (message == c.HeartBeat) { return; } #region 处理实际数据体 byte opeartor = receivebytes[0]; ScadaTcpOperator operatorEnum = (ScadaTcpOperator)opeartor; if (c.IsOperator(opeartor)) { switch (operatorEnum) { case ScadaTcpOperator.登录反馈: { try { #region 处理采集器端登录 TcpData tcpData = new TcpData(); byte[] narra = new byte[count - 1]; System.Array.Copy(receivebytes, 1, narra, 0, count - 1); tcpData.BytesToTcpItem(narra); if (tcpData == null) { if (OnUserLogined != null) { OnUserLogined(c, false, "登录失败"); } return; } STATION_TCP_INFO loginInfo = new STATION_TCP_INFO(); try { loginInfo.IO_SERVER_ID = tcpData.GetItemValue("IO_SERVER_ID"); loginInfo.IO_SERVER_IP = tcpData.GetItemValue("IO_SERVER_IP"); loginInfo.PASSWROD = tcpData.GetItemValue("PASSWROD"); loginInfo.USER = tcpData.GetItemValue("USER"); loginInfo.RESULT = tcpData.GetItemValue("RESULT"); loginInfo.MSG = tcpData.GetItemValue("MSG"); loginInfo.FUNCTION = tcpData.GetItemValue("FUNCTION"); //判断是否已经存在有发布的采集站工程 if (loginInfo.RESULT == "true") { if (OnUserLogined != null) { OnUserLogined(c, true, loginInfo.MSG); } } else { if (OnUserLogined != null) { OnUserLogined(c, false, loginInfo.MSG); } } } catch { if (OnUserLogined != null) { OnUserLogined(c, false, "登录失败"); } return; } tcpData.Dispose(); #endregion } catch (Exception ex) { ThrowExceptionToMain(new Exception("ERR10024" + ex.Message)); } } break; case ScadaTcpOperator.更新采集站报警: //接收到服务器端更新采集站中报警配置通知 { try { #region 更新采集站报警 TcpData tcpData = new TcpData(); byte[] narra = new byte[count - 1]; System.Array.Copy(receivebytes, 1, narra, 0, count - 1); tcpData.BytesToTcpItem(narra); if (tcpData == null) { //解析字符串失败 return; } IO_ALARM_CONFIG alarmConfig = new IO_ALARM_CONFIG(); try { alarmConfig.IO_ALARM_LEVEL = tcpData.GetItemValue("IO_ALARM_LEVEL"); alarmConfig.IO_ALARM_NUMBER = int.Parse(tcpData.GetItemValue("IO_ALARM_NUMBER")); alarmConfig.IO_ALARM_TYPE = tcpData.GetItemValue("IO_ALARM_TYPE"); alarmConfig.IO_COMM_ID = tcpData.GetItemValue("IO_COMM_ID"); alarmConfig.IO_CONDITION = tcpData.GetItemValue("IO_CONDITION"); alarmConfig.IO_DEVICE_ID = tcpData.GetItemValue("IO_DEVICE_ID"); alarmConfig.IO_ENABLE_MAX = int.Parse(tcpData.GetItemValue("IO_ENABLE_MAX")); alarmConfig.IO_ENABLE_MAXMAX = int.Parse(tcpData.GetItemValue("IO_ENABLE_MAXMAX")); alarmConfig.IO_ENABLE_MIN = int.Parse(tcpData.GetItemValue("IO_ENABLE_MIN")); alarmConfig.IO_ENABLE_MINMIN = int.Parse(tcpData.GetItemValue("IO_ENABLE_MINMIN")); alarmConfig.IO_ID = tcpData.GetItemValue("IO_ID"); alarmConfig.IO_MAXMAX_TYPE = tcpData.GetItemValue("IO_MAXMAX_TYPE"); alarmConfig.IO_MAXMAX_VALUE = int.Parse(tcpData.GetItemValue("IO_MAXMAX_VALUE")); alarmConfig.IO_MAX_TYPE = tcpData.GetItemValue("IO_MAX_TYPE"); alarmConfig.IO_MAX_VALUE = int.Parse(tcpData.GetItemValue("IO_MAX_VALUE")); alarmConfig.IO_MINMIN_TYPE = tcpData.GetItemValue("IO_MINMIN_TYPE"); alarmConfig.IO_MINMIN_VALUE = int.Parse(tcpData.GetItemValue("IO_MINMIN_VALUE")); alarmConfig.IO_MIN_TYPE = tcpData.GetItemValue("IO_MIN_TYPE"); alarmConfig.IO_MIN_VALUE = int.Parse(tcpData.GetItemValue("IO_MIN_VALUE")); alarmConfig.IO_SERVER_ID = tcpData.GetItemValue("IO_SERVER_ID"); alarmConfig.UPDATE_DATE = ""; alarmConfig.UPDATE_RESULT = ""; alarmConfig.UPDATE_UID = ""; } catch (Exception ex) { ThrowExceptionToMain(new Exception("ERROR20102 " + ex.Message)); alarmConfig = null; } if (alarmConfig != null) { if (alarmconfigBll.Update(alarmConfig)) { byte[] sendbytes = tcpData.StringToTcpByte(tcpData.TcpItemToString(), ScadaTcpOperator.更新采集站报警反馈); this.Send(new ArraySegment <byte>(sendbytes)); IO_DEVICE device = MonitorDataBaseModel.IODevices.Find(x => x.IO_DEVICE_ID == alarmConfig.IO_DEVICE_ID); if (device != null) { IO_PARA para = device.IOParas.Find(x => x.IO_ID == alarmConfig.IO_ID); if (para != null) { para.AlarmConfig = alarmConfig; AddLogToMainLog("管理员更新" + device.IO_DEVICE_NAME + "[" + device.IO_DEVICE_LABLE + "]//" + para.IO_NAME + "[" + para.IO_LABEL + "]" + "报警配置成功! "); } } } else { AddLogToMainLog("管理员更新" + alarmConfig.IO_ID + "报警配置失败! "); } } tcpData.Dispose(); #endregion } catch (Exception ex) { ThrowExceptionToMain(new Exception("ERR10029" + ex.Message)); } } break; case ScadaTcpOperator.置命令: //接收到用户要下置命令 { try { byte[] contentbytes = new byte[receivebytes.Length - 1]; System.Array.Copy(receivebytes, 1, contentbytes, 0, receivebytes.Length - 1); TcpData tcpData = new TcpData(); tcpData.BytesToTcpItem(contentbytes); IO_COMMANDS command = new IO_COMMANDS() { COMMAND_DATE = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), COMMAND_ID = tcpData.GetItemValue("COMMAND_ID"), COMMAND_RESULT = tcpData.GetItemValue("COMMAND_RESULT"), COMMAND_USER = tcpData.GetItemValue("COMMAND_USER"), COMMAND_VALUE = tcpData.GetItemValue("COMMAND_VALUE"), IO_COMM_ID = tcpData.GetItemValue("IO_COMM_ID"), IO_DEVICE_ID = tcpData.GetItemValue("IO_DEVICE_ID"), IO_ID = tcpData.GetItemValue("IO_ID"), IO_LABEL = tcpData.GetItemValue("IO_LABEL"), IO_NAME = tcpData.GetItemValue("IO_NAME"), IO_SERVER_ID = tcpData.GetItemValue("IO_SERVER_ID") }; Scada.Model.IO_COMMUNICATION _COMMUNICATION = MonitorDataBaseModel.IOCommunications.Find(x => x.IO_COMM_ID == command.IO_COMM_ID && command.IO_SERVER_ID == x.IO_SERVER_ID); Scada.Model.IO_DEVICE device = MonitorDataBaseModel.IODevices.Find(x => x.IO_COMM_ID == command.IO_COMM_ID && command.IO_SERVER_ID == x.IO_SERVER_ID && x.IO_DEVICE_ID == command.IO_DEVICE_ID); if (_COMMUNICATION != null && device != null) { Scada.Model.IO_PARA para = device.IOParas.Find(x => x.IO_COMM_ID == command.IO_COMM_ID && command.IO_SERVER_ID == x.IO_SERVER_ID && x.IO_DEVICE_ID == command.IO_DEVICE_ID && x.IO_ID == command.IO_ID); if (para == null) { AddLogToMainLog(device.IO_DEVICE_NAME + "[" + device.IO_DEVICE_LABLE + "] 设备下参数 " + para.IO_ID + " " + para.IO_LABEL + " " + para.IO_NAME + " 参数不存在"); return; } if (_COMMUNICATION.DriverInfo == null) { AddLogToMainLog("请在采集站中设置该通讯通道驱动!"); return; } try { if (_COMMUNICATION.CommunicateDriver == null) { AddLogToMainLog("请在采集站中设置该通讯通道驱动!"); return; } else { ((ScadaCommunicateKernel)_COMMUNICATION.CommunicateDriver).IsCreateControl = false; } ScadaCommunicateKernel driverDll = (ScadaCommunicateKernel)_COMMUNICATION.CommunicateDriver; driverDll.SetUIParameter(_COMMUNICATION.IO_COMM_PARASTRING); driverDll.IsCreateControl = false; driverDll.InitKernel(MonitorDataBaseModel.IOServer, _COMMUNICATION, _COMMUNICATION.Devices, _COMMUNICATION.DriverInfo); try { driverDll.SendCommand(MonitorDataBaseModel.IOServer, _COMMUNICATION, device, para, command.COMMAND_VALUE); AddLogToMainLog(device.IO_DEVICE_NAME + "[" + device.IO_DEVICE_LABLE + "] 设备下参数 " + para.IO_ID + " " + para.IO_LABEL + " " + para.IO_NAME + " 已经下置成功,下置值" + command.COMMAND_VALUE); } catch (Exception ex) { ThrowExceptionToMain(new Exception("ERROR600002" + ex.Message)); } } catch (Exception ex) { ThrowExceptionToMain(new Exception("ERROR600001" + ex.Message)); } } } catch (Exception ex) { ThrowExceptionToMain(new Exception("ERR10030" + ex.Message)); } } break; case ScadaTcpOperator.重新启动: break; } } #endregion }
private void RunSimulator() { Task.Run(() => { try { for (int d = 0; d < Devices.Count; d++) { try { Scada.Model.IO_DEVICE Device = Devices[d]; TaskManager.Add( Task.Run(delegate { //将新值写入数据库 while (IsSimulator) { Task.Run(() => { Device.GetedValueDate = DateTime.Now; ///重新赋值 for (int i = 0; i < Device.IOParas.Count; i++) { Device.IOParas[i].IORealData = new Scada.IOStructure.IOData() { ParaName = Device.IOParas[i].IO_NAME, ParaValue = random.Next(5000, 9000).ToString(), QualityStamp = Scada.IOStructure.QualityStamp.GOOD, Date = Device.GetedValueDate.Value }; } ///将接收到的数据存储到当前缓存 IOCenterManager.InfluxDbManager.DbWrite_RealPoints(new List <Scada.Model.IO_DEVICE>() { Device }, DateTime.Now); if (SimulatorForm != null) { SimulatorForm.AddLog(Device.IO_DEVICE_NAME + " IO点插入了模拟数据成功"); } if (IsMakeAlarm) { IODeviceParaMaker paraMaker = new IODeviceParaMaker(); List <IO_PARAALARM> alarms = new List <IO_PARAALARM>(); for (int i = 0; i < Device.IOParas.Count; i++) { try { IO_PARAALARM alarm = paraMaker.MakeAlarm(Device.IOParas, Device.IOParas[i], Device.IOParas[i].IORealData, Device.IO_DEVICE_LABLE); if (alarm != null) { IOCenterManager.InfluxDbManager.DbWrite_AlarmPoints(alarm.IO_SERVER_ID, alarm.IO_COMM_ID, alarm, DateTime.Now); } } catch (Exception ex) { this.SimulatorForm.AddLog("ERROR20003" + ex.Message); } } } }); if (SimulatorForm != null && SimulatorForm.IsCustomTimer) { Thread.Sleep(Interval * 1000); } else { Thread.Sleep(Device.IO_DEVICE_UPDATECYCLE * 1000); } } })); } catch (Exception ex) { if (SimulatorForm != null) { SimulatorForm.AddLog(ex.Message); } } } } catch (Exception ex) { SimulatorForm.AddLog(ex.Message); ColseSimulator(); } }); }
public static void InitMonitor() { commDriverBll = new Scada.Business.SCADA_DRIVER(); //开启日志保存服务 MonitorLogRun(); #region 创建通讯和设备驱动 int num = MonitorDataBaseModel.ProgressMaxNum + 4; //执行方法 #region 读取当前采集站工程数据 try { TaskOperator = TaskOperator.关闭; IOServer = MonitorDataBaseModel.IOServer; //创建驱动模块 for (int i = 0; i < MonitorDataBaseModel.IOCommunications.Count; i++) { if (MonitorDataBaseModel.IOCommunications[i].DriverInfo == null) { AddLogToMainLog("创建通道" + MonitorDataBaseModel.IOCommunications[i].IO_COMM_NAME.ToString() + @"[" + MonitorDataBaseModel.IOCommunications[i].IO_COMM_LABEL + @"]驱动失败,请在采集站中设置该通讯通道驱动!"); continue; } try { if (MonitorDataBaseModel.IOCommunications[i].CommunicateDriver == null) { continue; } else { ((ScadaCommunicateKernel)MonitorDataBaseModel.IOCommunications[i].CommunicateDriver).IsCreateControl = false; } AddLogToMainLog("创建通道" + MonitorDataBaseModel.IOCommunications[i].IO_COMM_NAME.ToString() + @"[" + MonitorDataBaseModel.IOCommunications[i].IO_COMM_LABEL + @"]驱动成功!"); ScadaCommunicateKernel driverDll = (ScadaCommunicateKernel)MonitorDataBaseModel.IOCommunications[i].CommunicateDriver; driverDll.SetUIParameter(MonitorDataBaseModel.IOCommunications[i].IO_COMM_PARASTRING); driverDll.IsCreateControl = false; driverDll.InitKernel(MonitorDataBaseModel.IOServer, MonitorDataBaseModel.IOCommunications[i], MonitorDataBaseModel.IOCommunications[i].Devices, MonitorDataBaseModel.IOCommunications[i].DriverInfo); driverDll.CommunctionClose += CDriverDll_CommunctionClose; driverDll.CommunctionContinue += CDriverDll_CommunctionContinue; driverDll.CommunctionPause += CDriverDll_CommunctionPause; driverDll.CommunctionStart += CDriverDll_CommunctionStart; driverDll.CommunctionStop += CDriverDll_CommunctionStop; driverDll.DeviceSended += CDriverDll_DeviceSended; driverDll.DeviceStatusChanged += CDriverDll_DeviceStatusChanged; driverDll.Exception += CDriverDll_Exception; driverDll.OnDataReceived += CDriverDll_OnDataReceived; driverDll.OnShowFormLog += CDriverDll_OnShowFormLog; AddLogToMainLog("准备创建该通道下的设备驱动....."); for (int d = 0; d < MonitorDataBaseModel.IOCommunications[i].Devices.Count; d++) { Scada.Model.IO_DEVICE device = MonitorDataBaseModel.IOCommunications[i].Devices[d]; try { if (MonitorDataBaseModel.IOCommunications[i].Devices[d].DriverInfo == null) { AddLogToMainLog("创建设备" + device.IO_DEVICE_LABLE.ToString() + @"[" + device.IO_DEVICE_NAME + @"]驱动失败,请在采集站中设置该设备驱动!"); continue; } ((ScadaDeviceKernel)device.DeviceDrive).IsCreateControl = false; ((ScadaDeviceKernel)device.DeviceDrive).ExceptionEvent += CDriverDll_ExceptionEvent; ((ScadaDeviceKernel)device.DeviceDrive).InitKernel(MonitorDataBaseModel.IOServer, MonitorDataBaseModel.IOCommunications[i], device, null, device.DriverInfo); } catch (Exception ex) { ThrowExceptionToMain(new Exception("创建设备" + MonitorDataBaseModel.IOCommunications[i].Devices[d].IO_DEVICE_LABLE.ToString() + @"[" + MonitorDataBaseModel.IOCommunications[i].Devices[d].IO_DEVICE_NAME + @"]驱动失败,!错误原因:" + ex.Message)); } } } catch (Exception ex) { ThrowExceptionToMain(new Exception("ERROR600001" + ex.Message)); } } #endregion //创建垃圾定时回收 ClearMemoryTimer = new System.Threading.Timer(delegate { ClearMemory(); }, null, 1000, 30000); } catch (Exception ex) { ThrowExceptionToMain(new Exception("ERROR600002" + ex.Message)); } #endregion receiveRealCache = new ReceiveRealCache(100, 3000); //批量上传实时数据 receiveRealCache.WillUpload = (List <ReceiveCacheObject> result) => { try { //定时从缓存区上传数据 var analysisTask = Task.Run(() => { RealDataDBUtility.UploadReal(result); }); return(analysisTask); } catch { return(null); } }; ///批量上传报警数据 receiveRealCache.WillUploadAlarm = (List <AlarmCacheObject> result) => { try { //定时从缓存区上传数据 var analysisTask = Task.Run(() => { RealDataDBUtility.UploadAlarm(result); }); return(analysisTask); } catch { return(null); } }; receiveRealCache.Read(); Start(); }
/// <summary> /// 设置采集点的采集值 /// </summary> /// <returns></returns> public void SetIOValue(Scada.Model.IO_SERVER server, Scada.Model.IO_COMMUNICATION comm, Scada.Model.IO_DEVICE device) { try { ChangedBinds(server, comm, device.Copy()); } catch (Exception ex) { MonitorFormManager.DisplyException(ex); } }
/// <summary> /// 显示更新报警界面的数据 /// </summary> /// <param name="server"></param> /// <param name="comm"></param> /// <param name="device"></param> public static void MonitorIODataAlarmShowView(Scada.Model.IO_SERVER server, Scada.Model.IO_COMMUNICATION comm, Scada.Model.IO_DEVICE device, IO_PARAALARM alarm, string uploadresult) { if (mediator.IOMonitorForm.IsHandleCreated) { mediator.IOMonitorForm.BeginInvoke(new EventHandler(delegate { mediator.IOMonitorForm.InsertMonitorAlarmListView(server, comm, device, alarm, uploadresult); })); } }
/// <summary> /// 显示上传到数据中心结果日志的显示 /// </summary> /// <param name="server"></param> /// <param name="communication"></param> /// <param name="device"></param> /// <param name="uploadresult"></param> public static void ShowMonitorUploadListView(Scada.Model.IO_SERVER server, Scada.Model.IO_COMMUNICATION communication, Scada.Model.IO_DEVICE device, string uploadresult) { if (mediator.IOMonitorForm.IsHandleCreated) { mediator.IOMonitorForm.ShowMonitorUploadListView(server, communication, device, uploadresult); } }
public static void MonitorIODataShowView(Scada.Model.IO_SERVER server, Scada.Model.IO_COMMUNICATION comm, Scada.Model.IO_DEVICE device) { if (mediator.IOMonitorForm.IsHandleCreated) { mediator.IOMonitorForm.BeginInvoke(new EventHandler(delegate { mediator.IOMonitorForm.SetIOValue(server, comm, device); //清空接收的数据 device.ClearCollectDatas(); })); } }
/// <summary> /// 显示最近产生的报警 /// </summary> /// <param name="msg"></param> /// <returns></returns> #region 加载IO属性 public static void SetIOPara(Scada.Model.IO_SERVER server, Scada.Model.IO_COMMUNICATION comm, Scada.Model.IO_DEVICE device, Scada.Model.IO_PARA para) { if (mediator.IOMonitorForm.IsHandleCreated) { mediator.IOMonitorForm.BeginInvoke(new EventHandler(delegate { mediator.IOPropertiesForm.SetPara(server, comm, device, para); })); } }
public static void AppendSendCommand(Scada.Model.IO_SERVER server, Scada.Model.IO_COMMUNICATION communication, Scada.Model.IO_DEVICE device, Scada.Model.IO_PARA para, Scada.Model.IO_COMMANDS command) { if (mediator == null) { return; } mediator.IOMonitorForm.InsertMonitorCommandListView(server, communication, device, para, command); Scada.Logger.Logger.GetInstance().Info(command.GetCommandString()); }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(Scada.Model.IO_DEVICE model) { return(dal.Update(model)); }
/// <summary> /// 增加一条数据 /// </summary> public bool Add(Scada.Model.IO_DEVICE model) { return(dal.Add(model)); }