コード例 #1
0
ファイル: ResultView.cs プロジェクト: vscode1111/TagGenerator
        void workerImport_DoWork(object sender, DoWorkEventArgs e)
        {
            DateTime t0 = DateTime.Now;
            int      createSuccessCount = 0;
            int      updateSuccessCount = 0;
            int      faultCount         = 0;
            int      delay      = 50;
            bool     isUpdating = false;
            int      index      = 0;
            double   progress   = 0;
            int      count      = 0;

            try
            {
                //string nodeName = Environment.MachineName;
                string nodeName   = Global.Default.varXml.SystemPlatform.NodeName;
                string galaxyName = Global.Default.varXml.SystemPlatform.GalaxyName;

                // create GRAccessAppClass object
                GRAccessApp grAccess = new GRAccessAppClass();

                Report(string.Format("Подключение к Galaxy ({0}/{1})...\r\n", nodeName, galaxyName));

                // try to get galaxy
                IGalaxies gals = grAccess.QueryGalaxies(nodeName);
                if (gals == null || grAccess.CommandResult.Successful == false)
                {
                    Report(string.Format("Error!\r\nCustomMessage - {0}\r\nCommandResult - {1}\r\n", grAccess.CommandResult.CustomMessage, grAccess.CommandResult.Text));
                    return;
                }
                else
                {
                    Report("Подключен.\r\n");
                }
                IGalaxy galaxy = gals[galaxyName];

                string login    = Global.Default.varXml.SystemPlatform.Login;
                string password = Global.Default.varXml.SystemPlatform.Password;

                Report(string.Format("Авторизация {0}/{1}...\r\n", login, password));

                // log in
                galaxy.Login(login, password);
                ICommandResult cmd;
                cmd = galaxy.CommandResult;
                if (!cmd.Successful)
                {
                    Report("Ошибка авторизации:" + cmd.Text + " : " + cmd.CustomMessage + "\r\n");
                    return;
                }
                else
                {
                    Report("Авторизован.\r\n");
                }

                string                instanceName = "";
                IgObjects             queryResult;
                string[]              tagnames = new string[1];
                XPCollection <Object> objects  = new XPCollection <Object>();
                objects.Criteria = CriteriaOperator.Parse("[ArchestrAImport]");
                count            = objects.Count;
                foreach (Object obj in objects)
                {
                    index++;
                    try
                    {
                        if (obj.ObjectTypeID != null)
                        {
                            if (!string.IsNullOrEmpty(obj.ObjectTypeID.ArchestrATemplate))
                            {
                                instanceName = obj.Attribute;
                                tagnames[0]  = instanceName;

                                queryResult = galaxy.QueryObjectsByName(
                                    EgObjectIsTemplateOrInstance.gObjectIsInstance,
                                    ref tagnames);

                                IInstance instance = null;
                                cmd = galaxy.CommandResult;

                                try
                                {
                                    instance   = (IInstance)queryResult[1];
                                    isUpdating = true;
                                }
                                catch
                                {
                                    isUpdating = false;
                                }

                                if (isUpdating)
                                {
                                    updateSuccessCount++;
                                }
                                else
                                {
                                    // get the $UserDefined template
                                    tagnames[0] = obj.ObjectTypeID.ArchestrATemplate;

                                    queryResult = galaxy.QueryObjectsByName(
                                        EgObjectIsTemplateOrInstance.gObjectIsTemplate,
                                        ref tagnames);

                                    cmd = galaxy.CommandResult;
                                    if (!cmd.Successful)
                                    {
                                        meResultArchestrA.Text = "Ошибка инициализации шаблона:" + cmd.Text + " : " + cmd.CustomMessage + "\r\n";
                                    }

                                    ITemplate userDefinedTemplate = (ITemplate)queryResult[1];

                                    // create an instance
                                    instance = userDefinedTemplate.CreateInstance(instanceName, true);
                                    createSuccessCount++;
                                    isUpdating = false;
                                }

                                instance.CheckOut();
                                MxValue mxName = new MxValueClass();
                                mxName.PutString(obj.Chart);
                                instance.Attributes[Global.Default.varXml.SystemPlatform.NameField].SetValue(mxName);

                                MxValue mxDescription = new MxValueClass();
                                mxDescription.PutString(obj.Comment);
                                instance.Attributes[Global.Default.varXml.SystemPlatform.DescriptionField].SetValue(mxDescription);

                                if (obj.ObjectTypeID.IsRealType)
                                {
                                    MxValue mxPointNum = new MxValueClass();
                                    mxPointNum.PutInteger(obj.PointNum);
                                    instance.Attributes[Global.Default.varXml.SystemPlatform.PointNumField].SetValue(mxPointNum);

                                    MxValue mxUnit = new MxValueClass();
                                    mxUnit.PutString(obj.Unit);
                                    instance.Attributes[Global.Default.varXml.SystemPlatform.UnitField].SetValue(mxUnit);
                                }

                                instance.Save();
                                instance.CheckIn("Check in after adding.");

                                progress          = ((double)index / objects.Count * 1000);
                                progressPossition = (int)Math.Round(progress);

                                if (!isUpdating)
                                {
                                    Report(string.Format("Объект {0} создан удачно.\r\n", instanceName));
                                }
                                else
                                {
                                    Report(string.Format("Объект {0} обновлен удачно.\r\n", instanceName));
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Report(string.Format("У объекта {0} есть проблема создания :( [{1} -> {2}] \r\n", instanceName, ex.Message, ex.InnerException));
                        faultCount++;
                    }
                }

                galaxy.Logout();
                Thread.Sleep(delay);
                Report(string.Format("Отключение от галактики выполнено. \r\n"));
                Thread.Sleep(delay);
            }
            catch (Exception ex)
            {
                meResultArchestrA.Text = ex.Message + "->" + ex.InnerException + "\r\n";
            }
            TimeSpan diff = DateTime.Now - t0;

            Report(string.Format("Было потрачено времени: {0} c.\r\n", diff.ToString())); Thread.Sleep(delay);
            Report(string.Format("Среднее время на один объект: {0:0.#} c.\r\n", diff.TotalSeconds / count)); Thread.Sleep(delay);
            Report(string.Format("Удачно созданных: {0}.\r\n", createSuccessCount)); Thread.Sleep(delay);
            Report(string.Format("Удачно обновленных: {0}.\r\n", updateSuccessCount)); Thread.Sleep(delay);
            Report(string.Format("Неудачно выполненных команд: {0}. \r\n", faultCount)); Thread.Sleep(delay);
        }