/// <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);
            }
        }
Beispiel #2
0
        /// <summary>
        /// 准备开始发布流程,要发布的流程工程
        /// </summary>
        public static void PublishFlowStart(FlowProject project)
        {
            if (FlowDataBaseManager.IOServer == null)
            {
                AddLogToMainLog("发布流程失败,您当前所在的采集站没有采集站工程!");
                MessageBox.Show(FlowDesign, "发布流程失败,您当前所在的采集站没有采集站工程!");
                return;
            }
            TcpData tcpData    = new TcpData();
            string  commandStr = "PROJECTID:" + project.ProjectID + "#IO_SERVER_ID:" + FlowDataBaseManager.IOServer.SERVER_ID + "#RESULT:#MSG:工程发布请求";

            byte[] publishbytes = tcpData.StringToTcpByte(commandStr, Scada.AsyncNetTcp.ScadaTcpOperator.流程发布请求);
            //发送登录命令
            TcpClient.Send(new ArraySegment <byte>(publishbytes));
            AddLogToMainLog("发布流程命令请求已经下发到服务器,请耐心等待服务器进一步提示!");
        }
Beispiel #3
0
        /// <summary>
        /// 上传实时数据
        /// </summary>
        /// <param name="server"></param>
        /// <param name="comm"></param>
        /// <param name="device"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public static bool UploadReal(List <ReceiveCacheObject> receiveCaches)
        {
            bool   result     = false;
            string sendString = "";

            receiveCaches.ForEach(delegate(ReceiveCacheObject p) {
                sendString += p.DataString + "^";
            });

            try
            {
                TcpData tcpData = new TcpData();

                byte[] simuBytes = tcpData.StringToTcpByte(sendString, Scada.AsyncNetTcp.ScadaTcpOperator.实时值);
                if (IOMonitorManager.TcpClient != null && IOMonitorManager.TcpClient.IsClientConnected)
                {
                    IOMonitorManager.TcpClient.Send(new ArraySegment <byte>(simuBytes));
                    result = true;
                }
                else
                {
                    result = false;
                }
                return(result);
            }
            catch (Exception ex)
            {
                result = false;
                //写入错误日志,并将错误日志返回的日志窗体
                MonitorFormManager.DisplyException(ex);
            }
            sendString = "";
            receiveCaches.Clear();
            receiveCaches = null;
            return(result);
        }
Beispiel #4
0
        /// <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
        }
Beispiel #5
0
        private static void TcpClient_OnPublishProject(Scada.AsyncNetTcp.Net.AsyncTcpClient client, object sender, string msg, string ProjectID)
        {
            bool res = Convert.ToBoolean(sender);

            if (res)
            {
                PublishObject.Clear();
                var pubProject = Projects.Find(x => x.ProjectID == ProjectID);
                if (pubProject == null)
                {
                    AddLogToMainLog("未找到ID" + ProjectID + "的工程,无法发布!");
                    return;
                }
                bool isindex = false;
                for (int i = 0; i < pubProject.GraphList.Count; i++)
                {
                    isindex = pubProject.GraphList[i].Index;
                    if (isindex)
                    {
                        break;
                    }
                }
                if (isindex == false)
                {
                    AddLogToMainLog("您发布的工程没有创建主视图,无法发布。请选择主先设置主视图后在尝试发布");
                    return;
                }
                StringBuilder sb      = new StringBuilder();
                int           ViewNum = pubProject.GraphList.Count;//当前发布的视图数量
                sb.AppendLine("\r\n--PROJ #" + pubProject.ProjectID + "#" + pubProject.Title + "\r\n");
                sb.AppendLine(" ");
                for (int i = 0; i < ViewNum; i++)
                {
                    sb.AppendLine("\r\n--VIEW #" + pubProject.GraphList[i].GID + "#" + pubProject.GraphList[i].ViewTitle + "#" + pubProject.GraphList[i].Index.ToString() + "\r\n");
                    sb.AppendLine(" ");
                    pubProject.GraphList[i].Site.IsPublish = true;
                    sb.Append(pubProject.GraphList[i].Site.ExportSVG());
                    sb.AppendLine("");
                    sb.AppendLine("\r\n--ENDVIEW\r\n");
                }
                //工程包含的用户数据
                for (int i = 0; i < pubProject.FlowUsers.Count; i++)
                {
                    sb.AppendLine("\r\n--USER #" + pubProject.FlowUsers[i].Nickname + "#" + pubProject.FlowUsers[i].UserName + "#" + pubProject.FlowUsers[i].Password.ToString() + "#" + pubProject.FlowUsers[i].Read.ToString() + "#" + pubProject.FlowUsers[i].Write.ToString() + "\r\n");
                    sb.AppendLine("\r\n--ENDUSER\r\n");
                }

                byte[] byteArray = System.Text.Encoding.Default.GetBytes(sb.ToString());
                int    num       = byteArray.Length / 8192;
                if (byteArray.Length % 8192 != 0)
                {
                    num++;
                }
                List <byte> tempBytes = new List <byte>();
                for (int i = 0; i < num; i++)
                {
                    byte[] subBytes;
                    if (i == num - 1 && byteArray.Length % 8192 != 0)
                    {
                        subBytes = byteArray.Skip(i * 8192).Take(byteArray.Length % 8192).ToArray();
                    }
                    else
                    {
                        subBytes = byteArray.Skip(i * 8192).Take(8192).ToArray();
                    }
                    PublishObject.Add(i + 1, subBytes);
                    tempBytes.AddRange(subBytes);
                }

                TcpData tcpData = new TcpData();
                tcpData.Items.Add(new TcpDataItem()
                {
                    Key = "RELUST", Value = ""
                });
                tcpData.Items.Add(new TcpDataItem()
                {
                    Key = "MSG", Value = ""
                });
                tcpData.Items.Add(new TcpDataItem()
                {
                    Key = "IO_SERVER_ID", Value = FlowDataBaseManager.IOServer.SERVER_ID
                });
                tcpData.Items.Add(new TcpDataItem()
                {
                    Key = "NUMBER", Value = num.ToString()
                });
                tcpData.Items.Add(new TcpDataItem()
                {
                    Key = "BYTENUMBER", Value = byteArray.Length.ToString()
                });
                AddLogToMainLog("发布视图数量" + ViewNum);
                byte[] datas = tcpData.StringToTcpByte(tcpData.TcpItemToString(), Scada.AsyncNetTcp.ScadaTcpOperator.流程发布准备);
                TcpClient.Send(new ArraySegment <byte>(datas));
            }
            else
            {
                AddLogToMainLog(msg + ", 无法发布!");
            }
        }