public void SaveExcel(TUpdateInfo info = null)
        {
            if (excelData == null || excelBytes == null || excelFilePath == null)
            {
                return;
            }

            if (info == null)
            {
                foreach (TUpdateInfo upinfo in dgUpdateList)
                {
                    UpdateRowData(upinfo);
                }
            }
            else
            {
                UpdateRowData(info);
            }

            try
            {
                byte[] fileBytes = ExcelHelper.FillData(excelData, excelBytes);
                File.WriteAllBytes(excelFilePath, fileBytes); //更新Excel文件
            }
            catch (Exception ex)
            {
                MessageBox.Show("写Excel文件出错,将无法保存更新记录!(不影响升级)\n" + ex.Message, "提示", MessageBoxButton.OK, MessageBoxImage.Stop);
            }
        }
 void UpdateRowData(TUpdateInfo info)
 {
     if (info != null && info.xlsIndex != -1 && info.xlsIndex != 0)
     {
         List <string> row = excelData[info.xlsIndex];
         if (row.Count == 1)
         {
             row.AddRange(new[] { String.Empty, String.Empty, String.Empty, String.Empty, String.Empty });
         }
         else if (row.Count == 2)
         {
             row.AddRange(new[] { String.Empty, String.Empty, String.Empty, String.Empty });
         }
         else if (row.Count == 3)
         {
             row.AddRange(new[] { String.Empty, String.Empty, String.Empty });
         }
         else if (row.Count == 4)
         {
             row.AddRange(new[] { String.Empty, String.Empty });
         }
         else if (row.Count == 5)
         {
             row.AddRange(new[] { String.Empty });
         }
         row[2] = info.oldver;
         row[3] = info.updatever;
         row[4] = info.down;
         row[5] = info.time;
     }
 }
        private void MenuItemStartUpdate_OnClick(object sender, RoutedEventArgs e)
        {
            TUpdateInfo item = GetSelectedItem();

            if (item != null)
            {
                if (item.AutoUpdate)
                {
                    if (item.PauseUpdate)
                    {
                        item.AutoUpdate = true;
                    }
                    else
                    {
                        MessageBox.Show(this, "升级已经开始,请勿重复指令!", "注意", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    }

                    return;
                }

                if (string.IsNullOrWhiteSpace(strUpdatefmt))
                {
                    MessageBox.Show(this, "升级服务还未启动,无法升级!", "注意", MessageBoxButton.OK, MessageBoxImage.Stop);
                    return;
                }

                item.AutoUpdate = true;
                item.updatever  = updateVer;
            }
        }
        private void MenuItemPauseUpdate_OnClick(object sender, RoutedEventArgs e)
        {
            TUpdateInfo item = GetSelectedItem();

            if (item != null)
            {
                item.PauseUpdate = true;
            }
        }
        private void MenuItemRemove_OnClick(object sender, RoutedEventArgs e)
        {
            TUpdateInfo item = GetSelectedItem(false);

            if (item != null)
            {
                item.Remove(true);
            }
        }
        private void MenuItemReadDynamicParameters_OnClick(object sender, RoutedEventArgs e)
        {
            TUpdateInfo item = GetSelectedItem();

            if (item != null)
            {
                item.client.GetDynamicParameters();
            }
        }
        private void MenuItemSetParameters_OnClick(object sender, RoutedEventArgs e)
        {
            TUpdateInfo item = GetSelectedItem();

            if (item != null)
            {
                var win = new SetParameters(item);
                win.Owner = this;
                win.ShowDialog();
            }
        }
        private void MenuItemReboot_OnClick(object sender, RoutedEventArgs e)
        {
            TUpdateInfo item = GetSelectedItem();

            if (item != null)
            {
                if (MessageBox.Show(this, "确定要重启设备?", "请选择", MessageBoxButton.OKCancel, MessageBoxImage.Question, MessageBoxResult.Cancel) == MessageBoxResult.OK)
                {
                    item.client.RebootDevice();
                }
            }
        }
        private void MenuItemStopDebug_OnClick(object sender, RoutedEventArgs e)
        {
            TUpdateInfo item = GetSelectedItem();

            if (item != null)
            {
                if (MessageBox.Show(this, "确定要断开远程远程诊断?", "请选择", MessageBoxButton.OKCancel, MessageBoxImage.Question, MessageBoxResult.Cancel) == MessageBoxResult.OK)
                {
                    item.client?.StopDebug();
                }
            }
        }
示例#10
0
 public RealTimeData(TUpdateInfo info)
 {
     _info = info;
     InitializeComponent();
     if (_info != null)
     {
         _info.MessageReceived += _info_MessageReceived;
         _info.PropertyChanged += _info_PropertyChanged;
         _info.Removed         += _info_Removed;
     }
     txtRec.MaxLines   = 500;
     txtRec.IsReadOnly = true;
     Title             = "在线维护[" + _info?.mac + "] " + _info?.ip_port;
     AppendText("开始监听实时数据...");
 }
        private TUpdateInfo GetSelectedItem(bool checkConn = true)
        {
            var         cell = dgUpdate.CurrentCell;
            TUpdateInfo item = cell.Item as TUpdateInfo;

            if (item != null)
            {
                if (checkConn && item.DevStatus == DevStatus.DisConnected)
                {
                    MessageBox.Show(this, "设备已断开远程调试,请重新连接设备后再进行操作!", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Stop);
                    item = null;
                }
            }
            return(item);
        }
        private void MenuItemDynamicParameters_OnClick(object sender, RoutedEventArgs e)
        {   //动态参数信息
            TUpdateInfo item = GetSelectedItem();

            if (item != null)
            {
                if (!string.IsNullOrWhiteSpace(item.dynamicParameters))
                {
                    MessageBox.Show(this, item.dynamicParameters, "动态参数-" + item.mac, MessageBoxButton.OK, MessageBoxImage.None);
                }
                else
                {
                    item.client.GetDynamicParameters();
                    MessageBox.Show(this, "正在查询相关信息,请稍后查看!", "请稍后", MessageBoxButton.OK, MessageBoxImage.Asterisk);
                }
            }
        }
示例#13
0
 public SetParameters(TUpdateInfo info)
 {
     _info = info;
     InitializeComponent();
     if (_info != null)
     {
         txtServerIP_R.DataContext   = _info;
         txtServerPort_R.DataContext = _info;
         txtApn_R.DataContext        = _info;
         txtMac_R.DataContext        = _info;
         txtMac.Text = _info.mac;
         txtSuperPhone1_R.DataContext = _info;
         txtSuperPhone2_R.DataContext = _info;
         txtSuperPhone3_R.DataContext = _info;
         txtSuperPhone4_R.DataContext = _info;
     }
 }
        private void dgUpdate_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            TUpdateInfo item = GetSelectedItem();

            if (item != null)
            {
                if (item.hasFeatures)
                {
                    MessageBox.Show(this, item.features, "版本特征-" + item.mac, MessageBoxButton.OK, MessageBoxImage.Information);
                }
                else
                {
                    item.client.GetParameters();
                    MessageBox.Show(this, "正在查询相关信息,请稍后查看!", "请稍后", MessageBoxButton.OK, MessageBoxImage.Asterisk);
                }
            }
        }
        private void MenuItemStopUpdate_OnClick(object sender, RoutedEventArgs e)
        {
            TUpdateInfo item = GetSelectedItem();

            if (item != null)
            {
                if (!item.AutoUpdate)
                {
                    MessageBox.Show(this, "升级已经停止,请勿重复指令!", "注意", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    return;
                }
                if (item.client != null)
                {
                    item.AutoUpdate = false;
                    item.client.StopUpdate();
                }
            }
        }
        private void MenuItemParameters_OnClick(object sender, RoutedEventArgs e)
        {   //配置信息
            TUpdateInfo item = GetSelectedItem();

            if (item != null)
            {
                if (item.hasFeatures)
                {
                    ParameterInfo pi = new ParameterInfo("配置信息-" + item.mac);
                    pi.Owner   = this;
                    pi.DicData = item.dicParameters;
                    pi.ShowDialog();
                }
                else
                {
                    item.client.GetParameters();
                    MessageBox.Show(this, "正在查询相关信息,请稍后查看!", "请稍后", MessageBoxButton.OK, MessageBoxImage.Asterisk);
                }
            }
        }
        private void ShowRealTimeDataWindow(TUpdateInfo info)
        {
            if (info == null)
            {
                return;
            }

            RealTimeData window = null;

            if (dicWidows.ContainsKey(info))
            {
                window = dicWidows[info];
            }
            else
            {
                window         = new RealTimeData(info);
                window.Closed += (sender, e) => { dicWidows.Remove(((RealTimeData)sender).Info); };
                dicWidows.Add(info, window);
            }
            window.Owner = this;
            window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            window.Show();
            window.Activate();
        }
        private void MenuItemViewRealData_OnClick(object sender, RoutedEventArgs e)
        {
            TUpdateInfo item = GetSelectedItem();

            ShowRealTimeDataWindow(item);
        }
示例#19
0
 private void _info_Removed(TUpdateInfo obj)
 {
     this.Dispatcher.BeginInvoke(new Action(Close));
 }
示例#20
0
        private void ReceiveCallback(IAsyncResult result)
        {
            Socket sok = (Socket)result.AsyncState;

            try
            {
                if (!sok.Connected)
                {
                    return;
                }

                int len = sok.EndReceive(result);
                result.AsyncWaitHandle.Close();

                if (len == 0)
                {   //连接取消
                    OnClientClosed("连接断开");
                    CloseSocket();
                    return;
                }

                if (updateInfo.DevStatus == DevStatus.DisConnected)
                {
                    updateInfo.DevStatus = DevStatus.ReConnect;
                }

                string str = Encoding.GetEncoding("GB2312").GetString(_recBuffer, 0, len);
                lastRecTime = DateTime.Now;

                if (!string.IsNullOrEmpty(str))
                {
                    //Debug.Write($"rec({len}):{str}");
                    MessageReceived?.BeginInvoke(str, null, null); //调用接收到消息事件

                    if (updateInfo.lastCmd != CmdId.NULL)
                    {
                        sb.Append(str); //附加接收到的数据
                    }
                    else
                    if (sb.Length > 0)
                    {
                        sb.Clear();
                    }

                    if (sb.Length > 0)
                    {
                        string sbStr = sb.ToString();
                        Match  mc00  = Match.Empty;

                        if (string.IsNullOrEmpty(mac) || updateInfo.lastCmd == CmdId.ReadPara)
                        {
                            #region 识别设备
                            bool nd = (sbStr.IndexOf("Features", StringComparison.OrdinalIgnoreCase) > 0 &&
                                       sbStr.IndexOf("Parameters", StringComparison.OrdinalIgnoreCase) > 0 &&
                                       sbStr.IndexOf("Supervisor", StringComparison.OrdinalIgnoreCase) > 0 &&
                                       sbStr.IndexOf("numbers", StringComparison.OrdinalIgnoreCase) > 0 &&
                                       sbStr.IndexOf("End", StringComparison.OrdinalIgnoreCase) > 0);
                            if (nd)
                            {
                                mc00 = Regex.Match(sbStr, @"-+\s*Features\s*-+\n+(.+)\n+-+\s*Parameters\s*-+\n+(.+)\n+-+\s*Supervisor\s*numbers\s*-+\n+(.+)\n+-+\s*End\s*-+", RegexOptions.Singleline | RegexOptions.IgnoreCase);
                            }
                            if (!mc00.Success)
                            {
                                if (GetParameters())
                                {
                                    sb.Clear();
                                }
                            }
                            else
                            {
                                sb.Clear();

                                string parameters = mc00.Groups[2].Value;
                                Dictionary <string, string> dicParameters = GetDictionaryFromParameters(parameters);
                                if (dicParameters.ContainsKey("VEID"))
                                {
                                    mac = dicParameters["VEID"];
                                }
                                if (string.IsNullOrWhiteSpace(mac) && dicParameters.ContainsKey("VEID(MOBNUMB)"))
                                {
                                    mac = dicParameters["VEID(MOBNUMB)"];
                                }

                                if (string.IsNullOrWhiteSpace(mac))
                                {
                                    updateInfo.SetCmdResult(CmdId.ReadPara, false);
                                    //未提取到识别码 继续查找识别码
                                    GetParameters();
                                }
                                else
                                {
                                    updateInfo.SetCmdResult(CmdId.ReadPara, true);
                                    if (string.IsNullOrWhiteSpace(updateInfo.mac))
                                    {
                                        //当前连接未识别
                                        TUpdateInfo oldInfo = main.dgUpdateList.FirstOrDefault(x => x.mac == mac);
                                        if (oldInfo != null)
                                        {
                                            //激活历史相同客户端
                                            oldInfo.features          = mc00.Groups[1].Value;
                                            oldInfo.parameters        = parameters;
                                            oldInfo.dicParameters     = dicParameters;
                                            oldInfo.supervisorNumbers = mc00.Groups[3].Value;

                                            if (dicParameters.ContainsKey("SW VER"))
                                            {
                                                string cver = dicParameters["SW VER"].TrimStart('S', 'W', 'V', 'E', 'R');
                                                oldInfo.ver = cver; //当前版本
                                            }

                                            if (oldInfo.lastCmd == CmdId.Reboot)
                                            {
                                                oldInfo.SetCmdResult(CmdId.Reboot, true);
                                            }
                                            else
                                            {
                                                //oldInfo.down = "-";
                                            }
                                            if (oldInfo.DevStatus != DevStatus.FlashRom)
                                            {
                                                oldInfo.DevStatus = DevStatus.ReConnect;
                                            }
                                            else
                                            {
                                                if (oldInfo.FoceUpdate)
                                                {
                                                    oldInfo.FoceUpdate = false;
                                                }
                                            }

                                            oldInfo.client.Stop(); //移除旧连接
                                            updateInfo.Remove();   //移除自己
                                            oldInfo.client = this; //将新连接赋值给已存在信息
                                            ReceiveNextData(sok);
                                            return;
                                        }
                                    }

                                    updateInfo.mac               = mac;
                                    updateInfo.features          = mc00.Groups[1].Value;
                                    updateInfo.parameters        = parameters;
                                    updateInfo.dicParameters     = dicParameters;
                                    updateInfo.supervisorNumbers = mc00.Groups[3].Value;

                                    if (dicParameters.ContainsKey("SW VER"))
                                    {
                                        string cver = dicParameters["SW VER"].TrimStart('S', 'W', 'V', 'E', 'R');
                                        updateInfo.ver = cver; //当前版本

                                        if (string.IsNullOrWhiteSpace(updateInfo.oldver))
                                        {
                                            updateInfo.oldver = cver; //旧版本为空 也赋值为当前版本
                                        }
                                    }

                                    if (updateInfo.DevStatus == DevStatus.UnKnown)
                                    {
                                        updateInfo.DevStatus = DevStatus.Connect;
                                    }
                                }
                            }
                            #endregion
                        }
                        else if (updateInfo.lastCmd == CmdId.ReadDynamicPara &&
                                 sbStr.IndexOf("Dynamic", StringComparison.OrdinalIgnoreCase) > 0 &&
                                 sbStr.IndexOf("parameters", StringComparison.OrdinalIgnoreCase) > 0)
                        {   //读取动态参数
                            mc00 = Regex.Match(sbStr, @"-+\s*Dynamic\s*parameters\s*-+\n+(.+?)\n+[^\s]", RegexOptions.Singleline | RegexOptions.IgnoreCase);
                            if (mc00.Success)
                            {
                                sb.Clear();
                                updateInfo.dynamicParameters = mc00.Groups[1].ToString();
                                updateInfo.SetCmdResult(CmdId.ReadDynamicPara, true);
                            }
                        }
                        else if (updateInfo.lastCmd == CmdId.Reboot)
                        {   //重启指令
                            mc00 = Regex.Match(sbStr, @"Rebooting|Reboot", RegexOptions.IgnoreCase);
                            if (mc00.Success)
                            {
                                sb.Clear();
                                updateInfo.DevStatus = DevStatus.Rebooting;
                            }
                        }
                        else if (updateInfo.lastCmd == CmdId.StartUpdate || updateInfo.lastCmd == CmdId.ContinueUpdate)
                        {
                            sb.Clear();
                            string[] lines = sbStr.Split('\n');
                            int      index = -1;
                            for (int i = 0; i < lines.Length; i++)
                            {
                                string line = lines[i];
                                if (string.IsNullOrEmpty(line))
                                {
                                    continue;
                                }

                                if ((index = line.IndexOf("start_download", StringComparison.OrdinalIgnoreCase)) >= 0)
                                {
                                    updateInfo.DevStatus = DevStatus.StartDownloadRom;
                                }
                                else if ((index = line.LastIndexOf("download:", StringComparison.OrdinalIgnoreCase)) >= 0)
                                {
                                    updateInfo.DevStatus = DevStatus.DownloadingRom;
                                    line = line.Substring(index);
                                    mc00 = Regex.Match(line, @"download:\s*((\d+)\s*\(.+\))", RegexOptions.IgnoreCase); //UPD download: 119808 (57.55%)
                                    if (mc00.Success)
                                    {
                                        string down = null;
                                        if (main.ProgressMode == "P")
                                        {
                                            down = mc00.Groups[1].Value;
                                        }
                                        else if (main.ProgressMode == "T")
                                        {
                                            down = mc00.Groups[2].Value;
                                        }
                                        if (updateInfo.down != down)
                                        {
                                            lastUpRecTime = DateTime.Now;
                                        }
                                        updateInfo.down = down;

                                        if (!updateInfo.IsUpdating)
                                        {
                                            updateInfo.IsUpdating = true;
                                        }

                                        updateInfo.time = "升级中...";
                                    }
                                }
                                else if (line.Contains("image") && line.Contains("success") && line.Contains("real") && line.Contains("update"))//11,H,upd_dl.c,289,upd_dlack_handler,Download image successful, delay do real update
                                {
                                    string down = null;
                                    if (main.ProgressMode == "P")
                                    {
                                        down = main.fileStream.Length + " (100%)";
                                    }
                                    else if (main.ProgressMode == "T")
                                    {
                                        down = main.fileStream.Length.ToString();
                                    }

                                    if (updateInfo.down != down)
                                    {
                                        lastUpRecTime = DateTime.Now;
                                    }
                                    updateInfo.down = down;

                                    updateInfo.DevStatus = DevStatus.FlashRom;
                                }
                                else if (line.Contains("upd_task") && line.Contains("download") && line.Contains("timeout"))
                                {
                                    double lastDualSec = (DateTime.Now - lastUpRecTime).TotalSeconds;
                                    if (lastDualSec >= 1800) //距离上次发送时间超过半个小时
                                    {
                                        RebootDevice();      //重启车机 超过半小时
                                        lastUpRecTime = DateTime.Now;
                                    }
                                }
                            }
                        }
                        else if (updateInfo.lastCmd == CmdId.SetPara)
                        {
                            sb.Clear();
                            mc00 = Regex.Match(sbStr, "!!!.+!!!", RegexOptions.IgnoreCase);
                            if (mc00.Success)
                            {
                                updateInfo.SetCmdResult(CmdId.SetPara, false);
                            }
                            else
                            {
                                mc00 = Regex.Match(sbStr, "reboot", RegexOptions.IgnoreCase);
                                if (mc00.Success)
                                {
                                    updateInfo.SetCmdResult(CmdId.SetPara, true);
                                    updateInfo.DevStatus = DevStatus.Rebooting;
                                }
                            }
                        }
                        else if (updateInfo.lastCmd == CmdId.StopUpdate)
                        {
                            sb.Clear();
                            mc00 = Regex.Match(sbStr, "lmt_stop_update", RegexOptions.IgnoreCase);
                            if (mc00.Success)
                            {
                                updateInfo.time      = DateTime.Now.ToFormatDateTimeStr();
                                updateInfo.DevStatus = DevStatus.StopUpdate;
                                updateInfo.SetCmdResult(CmdId.StopUpdate, true);
                            }
                        }
                    }

                    CheckUpdate(str);  //每次检查更新
                    CheckErrorLink();  //检查错误连接
                    CheckCmdExecState();
                }
            }
            catch (Exception e)
            {
                if (e is SocketException)
                {
                    Debug.WriteLine("recErro:" + e.Message);
                    OnClientClosed("连接中断");
                    if (string.IsNullOrEmpty(mac))
                    {
                        updateInfo.Remove(true); //移除自己
                    }
                    return;
                }
                else if (e is ObjectDisposedException)
                {
                    OnClientClosed("连接中断");
                    return;
                }
                else
                {
                    Debug.WriteLine(e.Message);
                }
            }

            ReceiveNextData(sok);
        }
示例#21
0
        public void Start(Delegate startSuccessCallBack)
        {
            if (thread != null && thread.IsAlive)
            {
                return;
            }

            thread = new Thread(new ThreadStart(() =>
            {
                IPAddress ip;
                if (IPAddress.TryParse(main.serverIP, out ip))
                {
                    try
                    {
                        IPEndPoint ipep = new IPEndPoint(IPAddress.Any, main.serverPort); //7920
                        server          = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                        server.Bind(ipep);
                        server.Listen(10);
                        listening = true;
                        startSuccessCallBack?.DynamicInvoke();
                        while (listening)
                        {
                            Socket sok = server.Accept();
                            try
                            {
                                ClientThread client = new ClientThread(this, sok, main);
                                listClient.Add(client);
                                TUpdateInfo info = new TUpdateInfo(client);
                                main.dgUpdate.Dispatcher.Invoke(new Action(() => main.dgUpdateList.Add(info)));
                                client.Start();
                            }
                            catch (Exception ex)
                            {
                                if (ex is SocketException || ex is ObjectDisposedException)
                                {
                                }
                                else
                                {
                                    LogHelper.WriteError(ex, "ServerThread");
                                }
                                sok.Close();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        if (ex is SocketException)
                        {
                            SocketException sex = (SocketException)ex;
                            if (sex.ErrorCode == 10004)
                            {
                                return;
                            }
                        }
                        main.Dispatcher.BeginInvoke(
                            new Action(() => MessageBox.Show(this.main, "服务启动失败,请检查端口是否被占用!\r\n错误消息:" + ex.Message, "提示",
                                                             MessageBoxButton.OK, MessageBoxImage.Error)), null);
                    }
                }
                else
                {
                    main.Dispatcher.BeginInvoke(
                        new Action(() => MessageBox.Show(this.main, "服务器IP地址不正确,启动失败!", "提示", MessageBoxButton.OK,
                                                         MessageBoxImage.Stop)), null);
                }
                listening = false;
            }));
            thread.Name         = "ServerThread";
            thread.IsBackground = true;
            thread.Start();
        }