public TerminalInformationUC(TerminalInformation ti)
        {
            InitializeComponent();

            DataContext = this;

            _ti = ti;
        }
예제 #2
0
        private void AddDTU_MenuItem_Click(object sender, RoutedEventArgs e)
        {
            if (TermInfoOc.Count >= 4)
            {
                MessageBox.Show("已经同时控制4个DTU.", "添加DTU失败.", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            Socket soc = null;
            try
            {
                soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPAddress local = IPAddress.Parse(ServerIP);
                IPEndPoint iep = new IPEndPoint(local, ServerPort);
                soc.SendTimeout = Consts.TERM_TIMEOUT;
                soc.ReceiveTimeout = Consts.TERM_TIMEOUT;
                soc.Connect(iep);
                if (soc.Connected)
                {
                    byte[] bytes = Helper.DoSendReceive(soc, Consts.TERM_GET_ALL_DTU);
                    if (bytes.Length < 1)
                    {
                        MessageBox.Show("失去与服务器的连接.", "添加DTU失败.", MessageBoxButton.OK, MessageBoxImage.Error);
                        Helper.SafeCloseSocket(soc);
                    }
                    else
                    {
                        Tuple<string, byte[], string, string> resp = Helper.ExtractSocketResponse(bytes, bytes.Length);
                        if (resp.Item1 != Consts.TERM_GET_ALL_DTU_OK)
                        {
                            MessageBox.Show("无法获得所有DTU信息 : " + resp.Item3, "添加DTU失败.", MessageBoxButton.OK, MessageBoxImage.Error);
                            Helper.SafeCloseSocket(soc);
                        }
                        else
                        {
                            Helper.FillDTUInfoOC(DTUInfoOC, resp.Item3);
                            SelectDTU sdtu = new SelectDTU(soc, DTUInfoOC);
                            bool? b = sdtu.ShowDialog();
                            if (b == true)
                            {
                                bool dupDtu = false;
                                TerminalInformation curTi = null;
                                foreach (TerminalInformation tii in TermInfoOc)
                                {
                                    if (string.Compare(tii.OldDTUID, _dtuInfoOc[sdtu.DTUSelectedIndex].DtuId, true) == 0)
                                    {
                                        if (tii.State == TerminalInformation.TiState.Connected)
                                        {
                                            MessageBox.Show("DTU(" + _dtuInfoOc[sdtu.DTUSelectedIndex].DtuId + ")已经被你控制.", "添加DTU失败.", MessageBoxButton.OK, MessageBoxImage.Error);
                                            Helper.SafeCloseSocket(soc);
                                            dupDtu = true;
                                        }
                                        else
                                            curTi = tii;

                                        break;
                                    }
                                }

                                if (dupDtu == false)
                                {
                                    _timerPulse.Change(Timeout.Infinite, Consts.TERM_TASK_TIMER_PULSE);
                                    bytes = Helper.DoSendReceive(soc, Consts.TERM_ADD_DTU + UserName + "\t" + _dtuInfoOc[sdtu.DTUSelectedIndex].DtuId);
                                    _timerPulse.Change(Consts.TERM_TASK_TIMER_PULSE, Consts.TERM_TASK_TIMER_PULSE);
                                    resp = Helper.ExtractSocketResponse(bytes, bytes.Length);
                                    if (resp.Item1 != Consts.TERM_ADD_DTU_OK)
                                    {
                                        MessageBox.Show("无法添加DTU(" + _dtuInfoOc[sdtu.DTUSelectedIndex].DtuId + ") : " + resp.Item3, "添加DTU失败.", MessageBoxButton.OK, MessageBoxImage.Error);
                                        Helper.SafeCloseSocket(soc);
                                    }
                                    else
                                    {
                                        //soc.SendTimeout = -1;
                                        soc.ReceiveTimeout = -1;

                                        IPAddress ipad = ((IPEndPoint)soc.RemoteEndPoint).Address;
                                        if (curTi != null)
                                        {
                                            Dispatcher.Invoke((ThreadStart)delegate()
                                            {
                                                curTi.TerminalSocket = soc;
                                                curTi.State = TerminalInformation.TiState.Connected;
                                                curTi.InitUI(true);
                                            }, null);
                                        }
                                        else
                                        {
                                            TerminalInformation ti = null;
                                            Dispatcher.Invoke((ThreadStart)delegate()
                                            {
                                                ti = new TerminalInformation()
                                                {
                                                    ServerIP = IPAddress.Parse(ServerIP),
                                                    ServerIPString = ServerIP,
                                                    TerminalIP = ipad,
                                                    CurrentDTU = DTUInfoOC[sdtu.DTUSelectedIndex],
                                                    TerminalSocket = soc,
                                                    State = TerminalInformation.TiState.Connected,
                                                };
                                                ti.InitUI();
                                                lock (_tiLock)
                                                {
                                                    TermInfoOc.Add(ti);
                                                }
                                                UpdateTerminalInforView(ti);
                                                tcTerminal.SelectedIndex = tcTerminal.Items.Count;
                                            }, null);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                Helper.SafeCloseSocket(soc);
                            }
                        }
                    }
                }
                else
                {
                    Helper.SafeCloseSocket(soc);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Socket错误 : " + ex.Message, "添加DTU失败", MessageBoxButton.OK, MessageBoxImage.Error);
                Helper.SafeCloseSocket(soc);
            }
        }
예제 #3
0
        private void TerminateDTUInfo(TerminalInformation ti)
        {
            try
            {
                ti.TerminalSocket.Shutdown(SocketShutdown.Both);
                ti.TerminalSocket.Disconnect(false);
                ti.TerminalSocket.Close();
                ti.TerminalSocket.Dispose();
            }
            catch (Exception)// ex)
            {
                //AddLog("Disconnect server error : " + ex.Message, ip, state: LogMessage.State.Error);
                Helper.SafeCloseSocket(ti.TerminalSocket);
            }
            if (ti.CurrentTvItem != null)
            {
                Dispatcher.Invoke((ThreadStart)delegate
                {
                    ti.State = TerminalInformation.TiState.Unknown;
                    foreach (TabItem tii in tcTerminal.Items)
                    {
                        StackPanel sp = tii.Header as StackPanel;
                        if (sp == null)
                            continue;
                        Image img = sp.Children[0] as Image;
                        Label lbl = sp.Children[1] as Label;
                        string s = lbl.Content as string;
                        if (s == null)
                            continue;
                        if (s == ti.TerminalIPString)
                        {
                            img.Source = new BitmapImage(new Uri("pack://application:,,,/managementsystem;component/resources/dtuunknown.ico"));
                            lbl.Foreground = Brushes.Red;
                            sp.ToolTip = ti.TerminalIPString + " : 未知";
                        }
                    }

                }, null);
            }
        }
예제 #4
0
        private void UpdateTerminalInforView(TerminalInformation ti, bool isAdd = true, bool noTv = false)
        {
            if (ti == null)
                return;

            if (isAdd == true)
            {
                if (noTv == false)
                {
                    if (ti.CurrentTvItem != null && tvTerminal.Items.Contains(ti.CurrentTvItem) == false)
                        tvTerminal.Items.Add(ti.CurrentTvItem);
                }
                if (ti.CurrentTabItem != null && tcTerminal.Items.Contains(ti.CurrentTabItem) == false)
                {
                    tcTerminal.Items.Add(ti.CurrentTabItem);
                    tcTerminal.SelectedIndex = tcTerminal.Items.Count - 1;
                    AdjustDTUEnable();
                }
            }
            else
            {
                if (noTv == false)
                {
                    if (ti.CurrentTvItem != null && tvTerminal.Items.Contains(ti.CurrentTvItem) == true)
                        tvTerminal.Items.Remove(ti.CurrentTvItem);
                }
                if (ti.CurrentTabItem != null && tcTerminal.Items.Contains(ti.CurrentTabItem) == true)
                {
                    tcTerminal.Items.Remove(ti.CurrentTabItem);
                    AdjustDTUEnable();
                }
            }
        }
예제 #5
0
        private void ProcessServerResponse(TerminalInformation ti, byte[] respBytes, int length)
        {
            Tuple<string, byte[], string, string> resp = Helper.ExtractSocketResponse(respBytes, length);

            switch (resp.Item1)
            {
                default:
                    AddLog("未知的响应 : " + resp, ti.CurrentDTU.DtuId, state: LogMessage.State.Fail, flow: LogMessage.Flow.Response);
                    break;
                //case Consts.TEST_CONNECTION_RESP:
                //    break;
                case Consts.TERM_INVALID_REQUEST:
                    AddLog("非法请求 : " + resp, ti.CurrentDTU.DtuId, state: LogMessage.State.Fail, flow: LogMessage.Flow.Response);
                    break;
                case Consts.TERM_ADD_DTU_OK:
                    AddLog("成功添加DTU : " + resp, ti.CurrentDTU.DtuId, state: LogMessage.State.OK, flow: LogMessage.Flow.Response);
                    Dispatcher.Invoke((ThreadStart)delegate
                    {
                        ti.State = TerminalInformation.TiState.Connected;
                        UpdateTerminalInforView(ti);
                    }, null);
                    //Dispatcher.Invoke((ThreadStart)delegate
                    //{
                    //    //TerminalInformationUC tiuc = new TerminalInformationUC(ti);
                    //    //TabItem tabi = new TabItem();
                    //    //StackPanel sp = new StackPanel();
                    //    //sp.Orientation = Orientation.Horizontal;
                    //    //sp.Margin = new Thickness(0, 0, 0, 0);//-4, -4, -4, -4);
                    //    //Label lbl = new Label();
                    //    //lbl.Content = ti.CurrentDTU.DtuId;
                    //    //sp.Children.Add(lbl);
                    //    //Image img = new Image();
                    //    //switch (ti.State)
                    //    //{
                    //    //    default:
                    //    //    case TerminalInformation.TiState.Unknown:
                    //    //        img.Source = new BitmapImage(new Uri("pack://application:,,,/managementsystem;component/resources/dtuunknown.ico"));
                    //    //        lbl.Foreground = Brushes.Red;
                    //    //        sp.ToolTip = ti.CurrentDTU.DtuId + " : Unknown";
                    //    //        break;
                    //    //    case TerminalInformation.TiState.Connected:
                    //    //        img.Source = new BitmapImage(new Uri("pack://application:,,,/managementsystem;component/resources/dtuok.ico"));
                    //    //        lbl.Foreground = Brushes.Black;
                    //    //        sp.ToolTip = ti.CurrentDTU.DtuId + " : Connected";
                    //    //        break;
                    //    //    case TerminalInformation.TiState.Disconnected:
                    //    //        img.Source = new BitmapImage(new Uri("pack://application:,,,/managementsystem;component/resources/dtuerror.ico"));
                    //    //        lbl.Foreground = Brushes.Red;
                    //    //        sp.ToolTip = ti.CurrentDTU.DtuId + " : Disconnected";
                    //    //        break;
                    //    //}
                    //    //img.Width = 16;
                    //    //img.Height = 16;
                    //    ////img.Opacity = 0.75;
                    //    //sp.Children.Insert(0, img);
                    //    //tabi.Header = sp;
                    //    //tabi.Content = tiuc;
                    //    //tcTerminal.Items.Add(tabi);
                    //    //tcTerminal.SelectedIndex = tcTerminal.Items.Count - 1;
                    //}, null);
                    break;
                case Consts.TERM_ADD_DTU_ERR:
                    AddLog("添加DTU错误 : " + resp, ti.CurrentDTU.DtuId, state: LogMessage.State.Fail, flow: LogMessage.Flow.Response);
                    Dispatcher.Invoke((ThreadStart)delegate()
                    {
                        TermInfoOc.Remove(ti);
                        UpdateTerminalInforView(ti, false);
                        Helper.SafeCloseSocket(ti.TerminalSocket);
                        ti.TerminalSocket = null;
                    }, null);
                    break;
                case Consts.TERM_PULSE_REQ_OK:
                    AddLog("脉搏成功 : " + resp, ti.CurrentDTU.DtuId, state: LogMessage.State.OK, flow: LogMessage.Flow.Response);
                    break;
            }
        }