public void SignRequest(SignRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("SignRequest data can not be null.");
            }
            _signTcpClient = new TcpHelper();
            if (string.IsNullOrWhiteSpace(Const.Locator.ParameterSetting.SdcUrl))
            {
                MessageBoxEx.Show("EFD URL can not be null.", MessageBoxButton.OK);
                return;
            }
            string[] sdc = Const.Locator.ParameterSetting.SdcUrl.Split(':');
            if (sdc != null && sdc.Count() != 2)
            {
                MessageBoxEx.Show("EFD URL is not in the right format.", MessageBoxButton.OK);
                return;
            }
            bool isConn = _signTcpClient.Connect(IPAddress.Parse(sdc[0]), int.Parse(sdc[1]));

            if (!isConn)
            {
                MessageBoxEx.Show("Failed to connect to EFD.", MessageBoxButton.OK);
                return;
            }
            string requestString = JsonConvert.SerializeObject(request);

            _signTcpClient.Complated -= _signTcpClient_Complated;
            _signTcpClient.Complated += _signTcpClient_Complated;
            _signTcpClient.Send(0x02, requestString);
            _signTcpClient.ReciveAsync();
        }
예제 #2
0
        public static void StatueRequest(string requestString)
        {
            _statusTcpClient = new TcpHelper();
            if (string.IsNullOrWhiteSpace(Const.Locator.ParameterSetting.SdcUrl))
            {
                MessageBoxEx.Show("EFD URL can not be null.", MessageBoxButton.OK);
                return;
            }
            string[] sdc = Const.Locator.ParameterSetting.SdcUrl.Split(':');
            if (sdc != null && sdc.Count() != 2)
            {
                MessageBoxEx.Show("EFD URL is not in the right format.", MessageBoxButton.OK);
                return;
            }
            bool isConn = _statusTcpClient.Connect(IPAddress.Parse(sdc[0]), int.Parse(sdc[1]));

            if (!isConn)
            {
                MessageBoxEx.Show("Failed to connect to EFD.", MessageBoxButton.OK);
                return;
            }
            _statusTcpClient.Complated -= _statusTcpClient_Complated;
            _statusTcpClient.Complated += _statusTcpClient_Complated;
            _statusTcpClient.Send(0x01, requestString);
            _statusTcpClient.ReciveAsync();
        }
예제 #3
0
        public static void SignRequest(SignRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("SignRequest data can not be null.");
            }
            string requestString = JsonConvert.SerializeObject(request);

            if (!TcpClient.IsConnected)
            {
                if (string.IsNullOrWhiteSpace(Const.Locator.ParameterSetting.SdcUrl))
                {
                    MessageBoxEx.Show("E-SDC URL can not be null.", MessageBoxButton.OK);
                    return ;
                }
                string[] sdc = Const.Locator.ParameterSetting.SdcUrl.Split(':');
                if (sdc != null && sdc.Count() != 2)
                {
                    MessageBoxEx.Show("E-SDC URL is not in the right format.", MessageBoxButton.OK);
                    return ;
                }
                TcpClient.Connect(IPAddress.Parse(sdc[0]), int.Parse(sdc[1]));
            }
            TcpClient.Send(0x02, requestString);

            //MessageModel messageModel = TcpClient.Recive();
            TcpClient.Recive();

            //request.Hash = CaclBase64Md5Hash(requestString);
            //requestString = JsonConvert.SerializeObject(request);

            //HttpHelper httpHelper = new HttpHelper();
            //HttpItem httpItem = new HttpItem();
            //httpItem.Method = "POST";
            //httpItem.URL = Const.SignUri;
            //httpItem.Postdata = Convert.ToString(requestString);

            //httpItem.ResultType = ResultType.String;
            //HttpResult html = httpHelper.GetHtml(httpItem);
            //if (html.StatusCode != HttpStatusCode.OK)
            //{
            //    throw new Exception(string.IsNullOrEmpty(html.Html) ? (string.IsNullOrEmpty(html.StatusDescription) ? "Post Data Error!" : html.StatusDescription) : html.Html);
            //}

            //return JsonConvert.DeserializeObject<SignResponse>(messageModel.Message);
        }
예제 #4
0
        private void CheckIsOnline()
        {
            try
            {
                switch (Const.Locator.ParameterSetting.CommModel)
                {
                case CommModel.NetPort:
                    TcpHelper helper = new TcpHelper();
                    string[]  sdc    = Const.Locator.ParameterSetting.SdcUrl.Split(':');
                    if (sdc != null && sdc.Count() != 2)
                    {
                        _logger.Info("EFD URL is not in the right format.");
                        SetIsOnline(false);
                        return;
                    }
                    helper.Connect(IPAddress.Parse(sdc[0]), int.Parse(sdc[1]));
                    SetIsOnline(helper.IsConnected);
                    break;

                case CommModel.SerialPort:
                    string[] ports = SerialPort.GetPortNames();
                    if (ports != null && ports.Count() > 0)
                    {
                        if (ports.Contains(Const.Locator.ParameterSetting.SelectedPort))
                        {
                            SetIsOnline(true);
                        }
                        else
                        {
                            SetIsOnline(false);
                        }
                    }
                    else
                    {
                        SetIsOnline(false);
                    }
                    break;

                default:
                    break;
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex.Message);
            }
        }
예제 #5
0
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="data"></param>
        /// <param name="encod"></param>
        /// <returns></returns>
        public TResult Send <TResult>(string data, Encoding encod) where TResult : Result, new()
        {
            if (!tcp.Connect())
            {
                return(GetErrorResult <TResult>("请求处理失败"));
            }
            string Message = tcp.SendData(data, encod);

            if (LoggerProvider != null)
            {
                LoggerProvider(string.Format("ip={0}&prot={1}", tcp.ServerIP, tcp.SPort), data, Message);
            }
            if (Message.Contains("Fail-EX:"))
            {
                return(GetErrorResult <TResult>("请求处理失败"));
            }

            //解析成结果对象
            return(Message.Replace(">", "").ConvertToResult <TResult>());
        }
예제 #6
0
        private void Send()
        {
            switch (Const.Locator.ParameterSetting.CommModel)
            {
            case Commom.CommModel.NetPort:    //网口通讯
                if (string.IsNullOrWhiteSpace(Cmd))
                {
                    throw new Exception("Request cmd can not be null.");
                }
                if (string.IsNullOrWhiteSpace(Request))
                {
                    throw new Exception("Request can not be null.");
                }

                _netClient = new TcpHelper();
                if (string.IsNullOrWhiteSpace(Const.Locator.ParameterSetting.SdcUrl))
                {
                    MessageBoxEx.Show("EFD URL can not be null.", MessageBoxButton.OK);
                    return;
                }
                string[] sdc = Const.Locator.ParameterSetting.SdcUrl.Split(':');
                if (sdc != null && sdc.Count() != 2)
                {
                    MessageBoxEx.Show("EFD URL is not in the right format.", MessageBoxButton.OK);
                    return;
                }
                bool isConn = _netClient.Connect(IPAddress.Parse(sdc[0]), int.Parse(sdc[1]));
                if (!isConn)
                {
                    MessageBoxEx.Show("Failed to connect to EFD.", MessageBoxButton.OK);
                    return;
                }

                _netClient.Complated -= _client_Complated;
                _netClient.Complated += _client_Complated;
                switch (Cmd)
                {
                case "Status":
                    _netClient.Send(0x01, Request);
                    break;

                case "Sign":
                    _netClient.Send(0x02, Request);
                    break;

                case "Report":
                    _netClient.Send(0x04, Request);
                    break;

                default:
                    break;
                }
                _netClient.ReciveAsync();
                break;

            case Commom.CommModel.SerialPort:    //串口通讯
                if (string.IsNullOrWhiteSpace(Const.Locator.ParameterSetting.SelectedPort))
                {
                    throw new Exception("Port can not be null.");
                }
                if (string.IsNullOrWhiteSpace(Const.Locator.ParameterSetting.SelectedDataBits))
                {
                    throw new Exception("DataBits can not be null.");
                }
                if (string.IsNullOrWhiteSpace(Const.Locator.ParameterSetting.SelectedBaudRate))
                {
                    throw new Exception("BaudRate can not be null.");
                }
                if (string.IsNullOrWhiteSpace(Const.Locator.ParameterSetting.SelectedParity))
                {
                    throw new Exception("Parity can not be null.");
                }
                if (string.IsNullOrWhiteSpace(Const.Locator.ParameterSetting.SelectedStopBits))
                {
                    throw new Exception("StopBits can not be null.");
                }
                if (string.IsNullOrWhiteSpace(Cmd))
                {
                    throw new Exception("Request cmd can not be null.");
                }
                if (string.IsNullOrWhiteSpace(Request))
                {
                    throw new Exception("Request can not be null.");
                }
                _client = new SerialClient(Const.Locator.ParameterSetting.SelectedPort,
                                           int.Parse(Const.Locator.ParameterSetting.SelectedBaudRate),
                                           (Parity)Enum.Parse(typeof(Parity), Const.Locator.ParameterSetting.SelectedParity),
                                           int.Parse(Const.Locator.ParameterSetting.SelectedDataBits),
                                           (StopBits)Enum.Parse(typeof(StopBits), Const.Locator.ParameterSetting.SelectedStopBits));
                _client.Open();
                _isCanSend         = false;
                _client.Complated += _client_Complated;
                switch (Cmd)
                {
                case "Status":
                    _client.Send(0x01, Request);
                    break;

                case "Sign":
                    _client.Send(0x02, Request);
                    break;

                case "Report":
                    _client.Send(0x04, Request);
                    break;

                default:
                    break;
                }
                break;

            default:
                break;
            }
        }