public PluginResponse InvokeMetrics(MetricsRequest metricsRequest,string metricsType)
 {
     PluginResourceAction pluginResourceAction = new PluginResourceAction(this.ConnectString);
     List<PluginMap> pluginMaps = pluginResourceAction.RetrievePluginMapByClassAndType(CentralConnectMetrics, metricsType);
     if (pluginMaps.Count != 1)
     {
         return new PluginResponse() { IsSuccess = false, Message = Enum.GetName(typeof(CentralConnectCode), CentralConnectCode.PluginMapNotExist) };
     }
     Logger.Write("PluginMaps", pluginMaps[0]);
     PluginMap pluginMap = pluginMaps[0];
     Resource resource = pluginResourceAction.RetrieveResourceByTypeAndFileName(PluginResource, pluginMap.FileName);
     if(string.IsNullOrEmpty(resource.FileName))
     {
         return new PluginResponse() { IsSuccess = false, Message = Enum.GetName(typeof(CentralConnectCode), CentralConnectCode.ResourceFileNotExist) };
     }
     System.Reflection.Assembly assembly = null;
     if (!LoadPluginResourceFile(resource, out assembly) || assembly == null)
     {
         return new PluginResponse() { IsSuccess = false, Message = Enum.GetName(typeof(CentralConnectCode), CentralConnectCode.LoadResourceFileFail) };
     }
     IMetrics iMetrics = (IMetrics)assembly.CreateInstance(pluginMap.ClassFullName);
     if (iMetrics == null)
     {
         return new PluginResponse() { IsSuccess = false, Message = Enum.GetName(typeof(CentralConnectCode), CentralConnectCode.CreateMetricsInstanceFail) };
     }
     DictionaryAction dictionaryAction = new DictionaryAction(this.ConnectString);
     string workspace = dictionaryAction.RetrieveValueByTypeAndKey("Settings", "Workspace");
     metricsRequest.Workspace = System.IO.Path.Combine(workspace, CentralConnectMetrics);
     return iMetrics.GenerateMetrics(metricsRequest);
 }
        public CentralConnectCode CreateAndSendMetrics(MetricsEnhancement metricsEnhancement)
        {
            //Client
            ClientAction clientAction = new ClientAction(this.ConnectString);
            List<Client> clients = clientAction.RetrieveClientByClientNameAndDomain(metricsEnhancement.ClientName, metricsEnhancement.Domain);
            if (clients.Count != 1)
            {
                return CentralConnectCode.ClientNotFound;
            }
            Logger.Write("Client", clients[0]);
            Client client = clients[0];
            //Contact
            ContactAction contactAction = new ContactAction(this.ConnectString);
            List<Contact> primaryContacts = contactAction.RetrieveContactByFullName(client.PrimaryContact);
            if (primaryContacts.Count != 1)
            {
                return CentralConnectCode.PrimaryContactNotFound;
            }
            Logger.Write("Retrieve PrimaryContacts", primaryContacts[0]);
            Contact primaryContact = primaryContacts[0];
            List<Contact> secondaryContacts = contactAction.RetrieveContactByFullName(client.SecondaryContact);
            if (secondaryContacts.Count != 1)
            {
                return CentralConnectCode.SecondaryContactNotFound;
            }
            Logger.Write("Retrieve SecondaryContact", secondaryContacts[0]);
            Contact secondaryContact = secondaryContacts[0];
            //Company
            CompanyAction companyAction = new CompanyAction(this.ConnectString);
            List<Company> companys = companyAction.RetrieveCompanyByCompanyName(client.CompanyName);
            if (companys.Count != 1)
            {
                return CentralConnectCode.CompanyNotFound;
            }
            Logger.Write("Retrieve Company", companys[0]);
            Company company = companys[0];
            //registerClientMessages
            List<RegisterClientMessage> registerClientMessages = this.RetrieveRegisterClientMessageByEnvironmentAndClientNameAndDomain(metricsEnhancement.Environment, metricsEnhancement.ClientName, metricsEnhancement.Domain);
            if (registerClientMessages.Count != 1)
            {
                return CentralConnectCode.RegisterClientMessageNotExist;
            }
            Logger.Write("Retrieve registerClientMessages", registerClientMessages[0]);
            RegisterClientMessage registerClientMessage = registerClientMessages[0];
            //devices
            List<Device> devices = this.RetrieveRegisterDeviceByEnvironmentAndClientNameAndClientDomain(metricsEnhancement.Environment, metricsEnhancement.ClientName, metricsEnhancement.Domain);
            if (devices.Count == 0)
            {
                return CentralConnectCode.ClientNotFoundAssociatedDevice;
            }
            Logger.Write("Retrieve devices", devices);

            MetricsRequest metricsRequest = new MetricsRequest();
            //ClientInformation
            ClientInformation clientInformation = new ClientInformation();
            clientInformation.ClientName = metricsEnhancement.ClientName;
            clientInformation.Domain = metricsEnhancement.Domain;
            clientInformation.ProductNumber = client.ProductNumber;
            clientInformation.SerialNumber = client.SerialNumber;
            clientInformation.CoreSystemModel = client.CoreSystemModel;
            clientInformation.MAC = client.MAC;
            clientInformation.OSName = client.OSName;
            clientInformation.OSVersion = client.OSVersion;
            clientInformation.IPAddress = client.IPAddress;
            clientInformation.HPPID = registerClientMessage.HPPID;
            clientInformation.Password = registerClientMessage.Password;
            clientInformation.CSID = registerClientMessage.CSID;
            clientInformation.OSGDID = registerClientMessage.OSGDID;
            clientInformation.HWGDID = registerClientMessage.HWGDID;
            clientInformation.RegistrationToken = registerClientMessage.RegistrationToken;
            clientInformation.CompanyName = client.CompanyName;
            clientInformation.CountryCode = company.CountryCode;
            clientInformation.StateProvince = company.StateProvince;
            clientInformation.City = company.City;
            clientInformation.Street = company.Street;
            clientInformation.PostalCode = company.PostalCode;
            clientInformation.PrimaryContact = client.PrimaryContact;
            clientInformation.PrimaryFirstName = primaryContact.FirstName;
            clientInformation.PrimaryLastName = primaryContact.LastName;
            clientInformation.PrimaryEmailAddress = primaryContact.EmailAddress;
            clientInformation.PrimaryPhone = primaryContact.Phone;
            clientInformation.SecondaryContact = client.SecondaryContact;
            clientInformation.SecondaryFirstName = secondaryContact.FirstName;
            clientInformation.SecondaryLastName = secondaryContact.LastName;
            clientInformation.SecondaryEmailAddress = secondaryContact.EmailAddress;
            clientInformation.SecondaryPhone = secondaryContact.Phone;
            clientInformation.SupportPartnerId = metricsEnhancement.SupportPartnerId;
            clientInformation.ServicePartnerId = metricsEnhancement.ServicePartnerId;
            metricsRequest.ClientInformation = clientInformation;
            //deviceinformation
            DeviceAction deviceAction = new DeviceAction(this.ConnectString);
            List<DeviceInformation> deviceInformations = new List<DeviceInformation>();
            foreach (Device item in devices)
            {
                DeviceInformation deviceInformation = new DeviceInformation();
                deviceInformation.ClientName = client.ClientName;
                deviceInformation.ClientDomain = client.Domain;
                deviceInformation.DeviceName = item.DeviceName;
                deviceInformation.Domain = item.Domain;
                deviceInformation.ProductNumber = item.ProductNumber;
                deviceInformation.SerialNumber = item.SerialNumber;
                deviceInformation.CoreSystemModel = item.CoreSystemModel;
                deviceInformation.MAC = item.MAC;
                deviceInformation.OSName = item.OSName;
                deviceInformation.OSVersion = item.OSVersion;
                deviceInformation.IPAddress = item.IPAddress;
                List<RegisterDeviceMessage> registerDeviceMessages = deviceAction.RetrieveRegisterDeviceMessageByEnvironmentAndDeviceNameAndDomain(metricsEnhancement.Environment, item.DeviceName, item.Domain);
                if (registerDeviceMessages.Count != 1)
                {
                    return CentralConnectCode.RegisterDeviceMessageNotExist;
                }
                RegisterDeviceMessage registerDeviceMessage = registerDeviceMessages[0];
                deviceInformation.OSGDID = registerDeviceMessage.OSGDID;
                deviceInformation.OSGDIDUsn = registerDeviceMessage.OSGDIDUsn;
                deviceInformation.HWGDID = registerDeviceMessage.HWGDID;
                deviceInformation.HWGDIDUsn = registerDeviceMessage.HWGDIDUsn;
                deviceInformation.CollectionProblem = registerDeviceMessage.CollectionProblem;
                deviceInformation.MonitoringProblem = registerDeviceMessage.MonitoringProblem;
                deviceInformations.Add(deviceInformation);
            }
            metricsRequest.DeviceInformations = deviceInformations;
            //enhancementArguments
            Dictionary<string, string> enhancementArguments = new Dictionary<string, string>();
            if (metricsEnhancement.Arguments != null)
            {
                foreach (Argument item in metricsEnhancement.Arguments)
                {
                    if (!enhancementArguments.ContainsKey(item.key))
                    {
                        enhancementArguments.Add(item.key, item.value);
                    }
                }
            }
            metricsRequest.EnhancementArguments = enhancementArguments;
            //InvokeMetrics Plugin
            EnhancementAction enhancementAction = new EnhancementAction(this.ConnectString);
            PluginResponse result = enhancementAction.InvokeMetrics(metricsRequest,metricsEnhancement.Type);
            if (!result.IsSuccess)
            {
                Logger.Write("CreateMetricsFail-" + result.Message);
                return CentralConnectCode.CreateMetricsFail;
            }
            //SendMetrics
            DictionaryAction dictionaryAction = new DictionaryAction(this.ConnectString);
            string serviceUrl = string.Format("{0}/{1}", dictionaryAction.RetrieveServiceUrl(metricsEnhancement.Environment), "DataPackageReceiver/DataPackageReceiver.asmx");
            DataPackageSubmissionResult dataPackageSubmissionResult =this.SendMetricsRequest(serviceUrl,
                registerClientMessage.CSID,
                registerClientMessage.HWGDID,
                registerClientMessage.OSGDID,
                registerClientMessage.RegistrationToken,
                result.FileName,
                result.FileContent);
            if (!dataPackageSubmissionResult.IsSuccess)
            {
                if (dataPackageSubmissionResult.Error.IsClientError)
                {
                    return CentralConnectCode.SendMetricsClientError;
                }
                else if (dataPackageSubmissionResult.Error.IsReRegisterError)
                {
                    return CentralConnectCode.SendMetricsReRegisterError;
                }
                else if (dataPackageSubmissionResult.Error.IsTransientError)
                {
                    return CentralConnectCode.SendMetricsTransientError;
                }
            }
            MetricsMessage metricsMessage = new MetricsMessage();
            metricsMessage.Environment = metricsEnhancement.Environment;
            metricsMessage.ClientName = metricsEnhancement.ClientName;
            metricsMessage.Domain = metricsEnhancement.Domain;
            metricsMessage.PackageName = result.FileName;
            metricsMessage.ServicePartnerId = metricsEnhancement.ServicePartnerId;
            metricsMessage.SupportPartnerId = metricsEnhancement.SupportPartnerId;
            metricsMessage.CreateBy = "TBD";

            if (this.AddMetricsMessage(metricsMessage) != 1)
            {
                return CentralConnectCode.AddMetricsMessageFail;
            }
            return CentralConnectCode.Success;
        }