Exemplo n.º 1
0
        public void StartInit()
        {
            log = new Logger(Path.Combine(Directory.GetCurrentDirectory(), "Log"));
            /// <summary>
            /// 登录实例
            /// </summary>
            LoginForm _LoginForm = null;

            /// <summary>
            /// 获取调用实例
            /// </summary>
            CaijiCommHelper _CaijiCommHelper = null;

            /// <summary>
            /// 试验初始化配置信息
            /// </summary>
            SXCJModule _Module = null;

            try
            {
                _Module          = CalHelper.LoadModule();
                _CaijiCommHelper = new CaijiCommHelper(log, _Module);

                _LoginForm = new LoginForm(log, _CaijiCommHelper, _Module);
ReLogin:
                if (DialogResult.OK == _LoginForm.ShowDialog())
                {
                    if (_LoginForm.IsUpdate && !IsRuningProcess("JZUpgradeAgent"))
                    {
                        JZUpgrade.UpdateAlert ua = new JZUpgrade.UpdateAlert(2);
                        if (ua.ShowDialog() != DialogResult.OK)
                        {
                            goto ReLogin;
                        }

                        ProcessStartInfo Info = new ProcessStartInfo();
                        Info.CreateNoWindow  = false;
                        Info.UseShellExecute = true;
                        Info.FileName        = Path.Combine(Application.StartupPath, "JZUpgrade.exe");
                        Info.Arguments       = "\"5\"";
                        Process.Start(Info);
                        _LoginForm.Enabled = false;
                        _LoginForm.lblUpdateStatus.Text = "正在更新...";
                        goto ReLogin;
                    }

                    #region 用户登录
                    if (_LoginForm.bNetWorkIsConnected)
                    {
                        #region 在线登陆
                        ShowForm();
                        SetText("用户登录中.......");
                        try
                        {
                            string msg = _CaijiCommHelper.Login(_LoginForm.cmbUserName.Text, _LoginForm.txtUserPwd.Text, _Module.SpecialSetting.MachineCode, true);
                            if (msg.ToLower().Trim() != "true")
                            {
                                CloseFrom();
                                if (MessageBox.Show(msg, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.OK)
                                {
                                    goto ReLogin;
                                }
                                else
                                {
                                    Application.Exit();
                                    return;
                                }
                            }
                            else
                            {
                                #region 网络登录成功

                                _LoginForm.timerUpdate.Stop();

                                #region  步本地用户

                                SetText("同步本地用户.......");
                                _CaijiCommHelper.SysUserInfo(Yqun.Common.ContextCache.ApplicationContext.Current.UserName,
                                                             Yqun.Common.ContextCache.ApplicationContext.Current.Password,
                                                             Yqun.Common.ContextCache.ApplicationContext.Current.UserCode,
                                                             Yqun.Common.ContextCache.ApplicationContext.Current.InTestRoom.Code);
                                #endregion

                                #region  步本地模板
                                SetText("初始化用户配置信息.......");

                                SetText("同步本地模板.......");
                                _CaijiCommHelper.UpdateLocalModelInfo(_Module.SpecialSetting.TestRoomCode);
                                #endregion

                                if (!Yqun.Common.ContextCache.ApplicationContext.Current.IsAdministrator)
                                {
                                    #region  步本地配置信息

                                    try
                                    {
                                        SetText("检查本地配置信息状态.......");
                                        switch (_CaijiCommHelper.GetClientConfigStatus(_Module.SpecialSetting.MachineCode))
                                        {
                                        case -1:
                                            SetText("本地配置信息状态检查失败,请联系管理员.......");
                                            break;

                                        case 0:
                                            //SetText("同步本地配置信息.......");
                                            //_CaijiCommHelper.UploadClientConfig(_Module.SpecialSetting.TestRoomCode, _Module.SpecialSetting.MachineCode, Newtonsoft.Json.JsonConvert.SerializeObject(_Module.SpecialSetting));
                                            MessageBox.Show("本地设备信息未在服务上注册,请联系管理员!", "本地配置提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                                            CloseFrom();
                                            goto ReLogin;

                                        case 1:
                                            SetText("同步本地配置信息.......");
                                            _CaijiCommHelper.UpDateClientConfig(_Module.SpecialSetting.MachineCode, Newtonsoft.Json.JsonConvert.SerializeObject(_Module.SpecialSetting));
                                            break;

                                        case 2:
                                            SetText("同步本地配置信息.......");
                                            _CaijiCommHelper.GetClientConfig(ref _Module, _Module.SpecialSetting.MachineCode);
                                            break;
                                        }
                                        SetText("同步本地配置信息完成.......");
                                    }
                                    catch (Exception exSysConfig)
                                    {
                                        log.WriteLog(exSysConfig.Message + System.Environment.NewLine + exSysConfig.TargetSite, true, true);
                                    }

                                    #endregion
                                }
                                #region   本地数据

                                if (!Yqun.Common.ContextCache.ApplicationContext.Current.IsAdministrator)
                                {
                                    SetText("上传本地数据中.......");
                                    if (_CaijiCommHelper.UpdateLocalDateToServer())
                                    {
                                        SetText("上传本地数据成功.......");
                                    }
                                    else
                                    {
                                        SetText("上传本地数据失败,下次启动将继续上传.......");
                                    }
                                }

                                #endregion

                                #region 记录历史登陆
                                _LoginForm.MemberUser();
                                #endregion

                                #endregion
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("网络连接失败!已经转至本地服务登录!" + ex.Message, "登录提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                            Yqun.Common.ContextCache.ApplicationContext.Current.ISLocalService = true;
                        }
                        #endregion
                    }
                    else
                    {
                        #region 本地用户登录
                        ShowForm();
                        SetText("用户登录中.......");
                        SetText("读取本地登录用户信息.......");
                        DataTable _DataTable = new DataTable();

                        _DataTable = _CaijiCommHelper.GetUserInfo(_LoginForm.cmbUserName.Text, _LoginForm.txtUserPwd.Text);

                        if (_DataTable != null && _DataTable.Rows.Count > 0)
                        {
                            Yqun.Common.ContextCache.ApplicationContext.Current.UserName        = _LoginForm.cmbUserName.Text;
                            Yqun.Common.ContextCache.ApplicationContext.Current.Password        = _LoginForm.txtUserPwd.Text;
                            Yqun.Common.ContextCache.ApplicationContext.Current.UserCode        = _DataTable.Rows[0]["UserCode"].ToString();
                            Yqun.Common.ContextCache.ApplicationContext.Current.InTestRoom.Code = _DataTable.Rows[0]["UserTestCode"].ToString();
                            Yqun.Common.ContextCache.ApplicationContext.Current.ISLocalService  = true;
                            _Module = CalHelper.LoadModule();
                        }
                        else
                        {
                            if (MessageBox.Show("用户名或密码错误,请查证后在登录!", "登录错误", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.OK)
                            {
                                goto ReLogin;
                            }
                            else
                            {
                                Application.Exit();
                                return;
                            }
                        }
                        #endregion
                    }

                    #region 释放资源
                    if (_SplashScreen != null)
                    {
                        CloseFrom();
                    }
                    #endregion


                    #endregion

                    Application.EnableVisualStyles();
                    //Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new MainForm(log, _Module, _CaijiCommHelper));
                }
            }
            catch (Exception ex)
            {
                log.WriteLog(ex.StackTrace, true, false);
            }
        }
Exemplo n.º 2
0
        public static void DoMain()
        {
            try
            {
                #region 更新或执行更新\工管中心MD5验证
                Thread _Thread = new Thread(new ThreadStart(InitUpdate));
                _Thread.Start();
                #endregion

                Logger        log        = new Logger(Path.Combine(Directory.GetCurrentDirectory(), "Log"));
                StringBuilder sql_select = new StringBuilder();
                sql_select.Append("select top 1 * from sys_login order by loginId");

                DataTable Data = Agent.CallLocalService("Yqun.BO.BOBase.dll", "GetDataTable", new object[] { sql_select.ToString() }) as DataTable;
                if (Data != null)
                {
                    logger.Info(string.Format("发现{0}条登录记录", Data.Rows.Count));

                    DataRow Row = null;
                    if (Data.Rows.Count > 0)
                    {
                        Row = Data.Rows[0];
                    }
                    else
                    {
                        Row = Data.NewRow();
                        Data.Rows.Add(Row);
                    }

                    String  UserName = Row["username"].ToString();
                    String  Password = Row["password"].ToString();
                    Boolean r        = Agent.LoginProcess(UserName, Password);
                    int     Result   = (r ? 1 : 0);

                    logger.Info(string.Format("用户‘{0}’登录{1}", UserName, r ? "成功" : "失败"));

                    Row["loginresult"] = Result;

                    object o = Agent.CallLocalService("Yqun.BO.BOBase.dll", "Update", new object[] { Data });
                    r = (Convert.ToInt32(o) == 1);
                    logger.Info(string.Format("保存用户‘{0}’登录状态{1}", UserName, r ? "成功" : "失败"));

                    if (!ProcessHelper.IsRuningProcess("JZUpgradeAgent"))
                    {
                        ProcessStartInfo Info = new ProcessStartInfo();
                        Info.CreateNoWindow  = false;
                        Info.UseShellExecute = true;
                        Info.FileName        = Path.Combine(Application.StartupPath, "JZUpgrade.exe");
                        Info.Arguments       = "\"9\"";
                        //1 管理系统文件+不执行,2 采集系统文件+不执行,3 管理系统数据+不执行,
                        //4 管理系统文件+数据+执行;
                        //5 采集系统文件+执行
                        //6 管理系统执行
                        //7 采集系统执行
                        //8 管理系统文件+执行
                        //9 电液伺服系统执行
                        Process.Start(Info);
                    }
                    CaijiCommHelper _CaijiCommHelper = new CaijiCommHelper(log);
                    SXCJModule      _Module          = new SXCJModule();
                    _Module.SpecialSetting.MachineType = Convert.ToInt32(ConfigurationManager.ConfigurationManager.AppSettings["type"]);
                    switch (_CaijiCommHelper.GetClientConfigStatus(Convert.ToString(ConfigurationManager.ConfigurationManager.AppSettings["Machinecode"])))
                    {
                    case -1:
                        break;

                    case 0:
                        _CaijiCommHelper.UploadClientConfig(Convert.ToString(ConfigurationManager.ConfigurationManager.AppSettings["testcode"]), Convert.ToString(ConfigurationManager.ConfigurationManager.AppSettings["Machinecode"]), Newtonsoft.Json.JsonConvert.SerializeObject(_Module.SpecialSetting));
                        break;

                    case 1:
                        _CaijiCommHelper.UpDateClientConfig(Convert.ToString(ConfigurationManager.ConfigurationManager.AppSettings["Machinecode"]), Newtonsoft.Json.JsonConvert.SerializeObject(_Module.SpecialSetting));
                        break;

                    case 2:
                        _CaijiCommHelper.GetClientConfig(ref _Module, Convert.ToString(ConfigurationManager.ConfigurationManager.AppSettings["Machinecode"]));
                        break;
                    }


                    #region OldCode
                    //if (Internet.IsWanAlive())
                    //{
                    //    logger.Info("正在更新试验项目...");
                    //    sql_select = new StringBuilder();
                    //    sql_select.Append("select * from sys_testitem");
                    //    DataTable networkData = Agent.CallRemoteService("Yqun.BO.BOBase.dll", "GetDataTable", new object[] { sql_select.ToString() }) as DataTable;
                    //    DataTable localData = Agent.CallLocalService("Yqun.BO.BOBase.dll", "GetDataTable", new object[] { sql_select.ToString() }) as DataTable;
                    //    if (networkData != null && localData != null)
                    //    {
                    //        foreach (DataRow testRow in networkData.Rows)
                    //        {
                    //            String Index = testRow["ID"].ToString();
                    //            DataRow[] DataRows = localData.Select("ID='" + Index + "'");
                    //            if (DataRows.Length == 0)
                    //            {
                    //                DataRow localRow = localData.NewRow();
                    //                localRow["ID"] = testRow["ID"];
                    //                localRow["ItemName"] = testRow["ItemName"];
                    //                localRow["TestCount"] = testRow["TestCount"];
                    //                localRow["Type"] = testRow["Type"];
                    //                localData.Rows.Add(localRow);
                    //            }
                    //            else
                    //            {
                    //                DataRow localRow = DataRows[0];
                    //                localRow["ID"] = testRow["ID"];
                    //                localRow["ItemName"] = testRow["ItemName"];
                    //                localRow["TestCount"] = testRow["TestCount"];
                    //                localRow["Type"] = testRow["Type"];
                    //            }
                    //        }

                    //        object rt = Agent.CallLocalService("Yqun.BO.BOBase.dll", "Update", new object[] { localData });
                    //        r = (Convert.ToInt32(o) == 1);
                    //        logger.Info(string.Format("更新用户‘{0}’的试验项目{1}", UserName, r ? "成功" : "失败"));
                    //    }
                    //    else if (networkData == null)
                    //    {
                    //        logger.Info("获得服务端的试验项目失败");
                    //        logger.Info(string.Format("更新用户‘{0}’的试验项目失败", UserName));
                    //    }
                    //    else if (localData == null)
                    //    {
                    //        logger.Info("获得本机的试验项目表结构失败");
                    //        logger.Info(string.Format("更新用户‘{0}’的试验项目失败", UserName));
                    //    }
                    //}
                    //else
                    //{
                    //    logger.Info("无法访问Internet,更新试验项目失败");
                    //}
                    #endregion
                }
            }
            catch (Exception ex)
            {
                logger.Error(string.Format("应用出错:{0}", ex.Message));
            }
        }