public JsonResult SendCommand(WellCommandModel model) { bool res = false; string msg = ""; if (model.io.Trim() == "") { return(null); } IOParaModel para = ParaBll.GetByWhere(" where IO_SERVER_ID='" + model.io.Split(',')[0] + "' and IO_COMM_ID='" + model.io.Split(',')[1] + "' and IO_DEVICE_ID='" + model.io.Split(',')[2] + "' and IO_ID='" + model.io.Split(',')[3] + "' ").First(); IO_COMMANDS command = new IO_COMMANDS(); command.COMMAND_DATE = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); command.IO_SERVER_ID = model.io.Split(',')[0].Trim(); command.IO_COMM_ID = model.io.Split(',')[1].Trim(); command.IO_DEVICE_ID = model.io.Split(',')[2].Trim(); command.IO_ID = model.io.Split(',')[3].Trim(); if (Operator == null) { command.COMMAND_SEND_USER = "******"; command.COMMAND_SEND_USERNAME = "******"; } else { command.COMMAND_SEND_USER = base.Operator.UserId.ToString(); command.COMMAND_SEND_USERNAME = base.Operator.Account; } if (para != null) { try { res = SendNetMQ(command); msg = res ? "命令下置成功" : "下置命令失败"; command.IO_NAME = para.IO_NAME; command.IO_LABEL = para.IO_LABEL; command.COMMAND_ID = Guid.NewGuid().ToString(); command.COMMAND_RESULT = res.ToString().ToLower(); command.COMMAND_USER = ""; command.COMMAND_VALUE = model.writevalue; //写入下置命令日志 mWebInfluxDbManager.DbWrite_CommandPoints(new List <IO_COMMANDS> { command }, DateTime.Now).GetAwaiter(); } catch (Exception emx) { res = false; msg = "命令下置失败," + emx.Message; } } var result = new { result = res, msg = msg }; return(Json(result, JsonRequestBehavior.AllowGet)); }
/// <summary> /// 发送一个下置命令消息到消息列表 /// </summary> /// <param name="command"></param> private bool SendNetMQ(IO_COMMANDS command) { try { ///获取本地的IP地址 IPAddress AddressIP = IPAddress.Any; foreach (IPAddress _IPAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList) { if (_IPAddress.AddressFamily.ToString() == "InterNetwork") { AddressIP = _IPAddress; } } TcpData tcpData = new TcpData(); byte[] datas = tcpData.StringToTcpByte(command.GetCommandString(), ScadaTcpOperator.置命令); AsyncTcpClient Client = new AsyncTcpClient { IPAddress = AddressIP, Port = int.Parse(Configs.GetValue("Port")), AutoReconnect = true, ScadaClientType = ScadaClientType.WebSystem, ConnectedCallback = async(c, isReconnected) => { if (!c.IsClosing) { await c.WaitAsync(); // 等待服务器 //连接到服务器后向服务器发送心跳握手数据 } }, ConnectedTimeoutCallback = (c, isReconnected) => { }, ClosedCallback = (c, isReconnected) => { }, ReceivedCallback = (c, count) => { return(Task.CompletedTask); } }; Client.RunAsync().GetAwaiter(); Client.Send(new ArraySegment <byte>(datas)); return(true); } catch { return(false); } }
private Point CreateCommandPoint(string serverid, string communicationid, IO_COMMANDS command, DateTime?time) { Point point = new Point(); point.Fields = this.CreateCommandFields(command); point.Tags = this.CreateCommandTags(serverid, communicationid, command); point.Timestamp = time; point.Name = _fakeMeasurementPrefix + "_" + _CommandsMeasurement; return(point); }
/// <summary> /// 创建实时数据表中的Fields /// </summary> /// <param name="rnd"></param> /// <returns></returns> private Dictionary <string, object> CreateCommandFields(IO_COMMANDS command) { Dictionary <string, object> dict = new Dictionary <string, object>(); dict.Add("field_command_date", (string)command.COMMAND_DATE); dict.Add("field_command_id", command.COMMAND_ID.ToString()); dict.Add("field_command_result", (string)command.COMMAND_RESULT); dict.Add("field_command_user", (string)command.COMMAND_USER); dict.Add("field_command_value", (string)command.COMMAND_VALUE.ToString()); dict.Add("field_label", (string)command.IO_LABEL.ToString()); dict.Add("field_send_user", (string)command.COMMAND_SEND_USER.ToString()); dict.Add("field_send_username", (string)command.COMMAND_SEND_USERNAME.ToString()); return(dict); }
private void ucBtnSend_BtnClick(object sender, EventArgs e) { if (comboIOPara.SelectedItem == null) { FrmDialog.ShowDialog(this, "请选择要设置的参数"); return; } double dvalue = 0; if (tbValue.Text.Trim() == "") { FrmDialog.ShowDialog(this, "请输入下置的值"); return; } if (!double.TryParse(tbValue.Text.Trim(), out dvalue)) { FrmDialog.ShowDialog(this, "请输入下置的数值,不能是文本"); return; } if (FrmDialog.ShowDialog(this, "您确定要下置命令吗?", "提醒", true, true, true, true) == DialogResult.OK) { IO_COMMANDS cmmd = new IO_COMMANDS(); cmmd.COMMAND_DATE = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); cmmd.COMMAND_ID = GUIDTo16.GuidToLongID().ToString(); cmmd.COMMAND_RESULT = "false"; cmmd.COMMAND_USER = IOCenterManager.IOProject.ServerConfig.User; cmmd.COMMAND_VALUE = tbValue.Text.Trim(); cmmd.IO_COMM_ID = Communication.IO_COMM_ID; cmmd.IO_DEVICE_ID = Device.IO_DEVICE_ID; cmmd.IO_SERVER_ID = Server.SERVER_ID; cmmd.IO_LABEL = ((IO_PARA)comboIOPara.SelectedItem).IO_LABEL; cmmd.IO_NAME = ((IO_PARA)comboIOPara.SelectedItem).IO_NAME; cmmd.IO_ID = ((IO_PARA)comboIOPara.SelectedItem).IO_ID; Command = cmmd; this.ucStep.StepIndex = 0; try { Scada.AsyncNetTcp.TcpData tcpData = new Scada.AsyncNetTcp.TcpData(); byte[] datas = tcpData.StringToTcpByte(cmmd.GetCommandString(), Scada.AsyncNetTcp.ScadaTcpOperator.置命令); this.ucStep.StepIndex = 1; } catch (Exception ex) { FrmDialog.ShowDialog(this, ex.Message); } } }
/// <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 }
public void AddCommand(System.Net.EndPoint ep, string server, string communication, string device, string para, IO_COMMANDS command) { if (IOCenterManager.IsBackRun) { return; } if (ucbSendCommand.Checked) { if (listViewSendCommand.IsHandleCreated) { listViewSendCommand.BeginInvoke(new EventHandler(delegate { try { ListViewItem lvi = new ListViewItem(command.COMMAND_DATE); lvi.SubItems.Add(server); lvi.SubItems.Add(communication); lvi.SubItems.Add(device); lvi.SubItems.Add(para); lvi.SubItems.Add(command.COMMAND_VALUE); if (command.COMMAND_RESULT == "true") { lvi.SubItems.Add("成功"); } else { lvi.SubItems.Add("失败"); } lvi.SubItems.Add(command.COMMAND_USER); this.listViewSendCommand.Items.Insert(0, lvi); if (this.listViewSendCommand.Items.Count > int.Parse(this.cbSendCommandSize.SelectedValue)) { this.listViewSendCommand.Items.RemoveAt(this.listViewSendCommand.Items.Count - 1); } Scada.Logger.Logger.GetInstance().Info(command.COMMAND_DATE + " " + command.GetCommandString()); } catch { } })); } } }
/// <summary> /// 在ListView中显示下置命令 /// </summary> /// <param name="server"></param> /// <param name="communication"></param> /// <param name="device"></param> /// <param name="para"></param> /// <param name="command"></param> public void InsertMonitorCommandListView(IO_SERVER server, IO_COMMUNICATION communication, IO_DEVICE device, IO_PARA para, IO_COMMANDS command) { if (IOMonitorManager.IsBackRun) { return; } if (ucbSendCommand.Checked) { if (this.IsHandleCreated && listViewSendCommand.InvokeRequired) { listViewSendCommand.BeginInvoke(new EventHandler(delegate { ListViewItem lvi = new ListViewItem(command.COMMAND_DATE); lvi.SubItems.Add(command.COMMAND_RESULT); lvi.SubItems.Add(command.COMMAND_VALUE); lvi.SubItems.Add(server.SERVER_NAME); lvi.SubItems.Add(communication.IO_COMM_NAME + "[" + communication.IO_COMM_LABEL + "]"); lvi.SubItems.Add(device.IO_DEVICE_NAME + "[" + device.IO_DEVICE_LABLE + "]"); if (para != null) { lvi.SubItems.Add(para.IO_NAME + "[" + para.IO_LABEL + "]"); } else { lvi.SubItems.Add("未知IO参数"); } listViewSendCommand.Items.Insert(0, lvi); if (this.ucLateCommandSize.SelectedValue == "") { this.ucLateCommandSize.SelectedValue = "100"; } if (listViewSendCommand.Items.Count > int.Parse(this.ucLateCommandSize.SelectedValue)) { listViewSendCommand.Items.RemoveAt(listViewSendCommand.Items.Count - 1); } })); } } }
/// /// <summary> /// 创建实时数据表中的Tags /// </summary> /// <param name="rnd"></param> /// <returns></returns> private Dictionary <string, object> CreateCommandTags(string serverid, string communicationid, IO_COMMANDS command) { Dictionary <string, object> dict = new Dictionary <string, object>(); try { dict.Add("tag_did", command.IO_DEVICE_ID.ToString()); dict.Add("tag_cid", communicationid.ToString()); dict.Add("tag_sid", serverid.ToString()); dict.Add("tag_ioid", command.IO_ID.ToString()); } catch (Exception ex) { DisplayException(new Exception("ERR51013" + ex.Message)); } return(dict); }