/// <summary>
        /// Initializes the configuration control with default settings.
        /// </summary>
        /// <param name="environment"></param>
        public void Initialize(PluginEnvironment environment)
        {
            _data = new LowLevelConfigActivityData();

            assetSelectionControl.Initialize(AssetAttributes.None);

            partialClean_ComboBox.DataSource     = _NVRAMOptions.PartialCleanOptions;
            executionMode_ComboBox.DataSource    = _NVRAMOptions.ExecutionModeOptions;
            jdimfgReset_ComboBox.DataSource      = _NVRAMOptions.JDIOptions;
            rebootCrashMode_ComboBox.DataSource  = _NVRAMOptions.RebootCrashModeOptions;
            saveRecoverFlags_ComboBox.DataSource = _NVRAMOptions.SaveRecoverFlagsOptions;
            language_ComboBox.DataSource         = _NVRAMOptions.LanguageOptions;
            suppressUsed_ComboBox.DataSource     = _NVRAMOptions.SuppressUsedOptions;
            crDefSleep_ComboBox.DataSource       = _NVRAMOptions.CRDefSleepOptions;
        }
Example #2
0
        /// <summary>
        /// Validates the given metadata against the LowLevelConfig Activity data.
        /// </summary>
        /// <param name="configurationData">The configuration data.</param>
        /// <returns>true if valid</returns>
        public bool ValidateMetadata(ref PluginConfigurationData configurationData)
        {
            bool validData = true;
            LowLevelConfigActivityData activityData = null;

            try
            {
                activityData = configurationData.GetMetadata <LowLevelConfigActivityData>();
            }
            catch
            {
                activityData = new LowLevelConfigActivityData();
                validData    = false;
            }

            configurationData = new PluginConfigurationData(activityData, LowLevelConfigConfigurationControl.Version);

            return(validData);
        }
        /// <summary>
        /// Initializes the configuration control with the specified settings.
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="environment"></param>
        public void Initialize(PluginConfigurationData configuration, PluginEnvironment environment)
        {
            Initialize(environment);
            _data = configuration.GetMetadata <LowLevelConfigActivityData>();

            assetSelectionControl.Initialize(configuration.Assets, AssetAttributes.None);

            //commandType_Checkbox.Checked = _data.EnableFW;


            partialClean_ComboBox.SelectedIndex     = _NVRAMOptions.PartialCleanOptions.FindIndex(x => x == _data.PartialClean);
            executionMode_ComboBox.SelectedIndex    = _NVRAMOptions.ExecutionModeOptions.FindIndex(x => x == _data.ExecutionMode);
            jdimfgReset_ComboBox.SelectedIndex      = _NVRAMOptions.JDIOptions.FindIndex(x => x == _data.JDIMfgReset);
            rebootCrashMode_ComboBox.SelectedIndex  = _NVRAMOptions.RebootCrashModeOptions.FindIndex(x => x == _data.RebootCrashMode);
            saveRecoverFlags_ComboBox.SelectedIndex = _NVRAMOptions.SaveRecoverFlagsOptions.FindIndex(x => x == _data.SaveRecoverFlags);
            language_ComboBox.SelectedIndex         = _NVRAMOptions.LanguageOptions.FindIndex(x => x == _data.Language);
            suppressUsed_ComboBox.SelectedIndex     = _NVRAMOptions.SuppressUsedOptions.FindIndex(x => x == _data.SuppressUsed);
            crDefSleep_ComboBox.SelectedIndex       = _NVRAMOptions.CRDefSleepOptions.FindIndex(x => x == _data.CRDefSleep);

            //fileLocation_TextBox.Text = _data.FimBundle;
            modelNumber_CheckBox.Checked  = "True" == _data.ModelNumberBool;
            serialNumber_CheckBox.Checked = "True" == _data.SerialNumberBool;
        }
        /// <summary>
        /// Execute the task of the LowLevelConfig activity.
        /// </summary>
        /// <param name="executionData"></param>
        /// <returns></returns>
        public PluginExecutionResult Execute(PluginExecutionData executionData)
        {
            //PluginExecutionResult result = new PluginExecutionResult(PluginResult.Failed, "Failed to complete successfully");
            bool result = false;

            _webServiceURL = executionData.Environment.PluginSettings["FalconWebServiceURL"];
            _executionData = executionData;
            _activityData  = executionData.GetMetadata <LowLevelConfigActivityData>();
            _mappedData    = new NVRAMMapping();
            Dictionary <string, string> _FWMapping = new Dictionary <string, string>();
            TimeSpan lockTimeout = TimeSpan.FromMinutes(30);
            TimeSpan holdTimeout = TimeSpan.FromMinutes(30);
            ConcurrentDictionary <string, bool> results = new ConcurrentDictionary <string, bool>();

            if (_executionData.Assets.OfType <IDeviceInfo>().Count() == 0)
            {
                return(new PluginExecutionResult(PluginResult.Failed, $"There were no assets retrieved.  If this is a count-based run, your reservation in asset inventory may have expired.", "DeviceInfo Asset error"));
            }


            ///1. If a path exists, check the folder structure for folder names of the devices with FW in them.
            ///2. If any errors occur, throw with the product type that we failed to find fw for
            ///3. Consider Mapping the device to to the folder path and grabbing it within the parallel loop. Alternatively we can just look for its folder knowing the base path.


            //Code saved as it may be necessary to use Falcon fim client to update FW in the future.

            //if (!string.IsNullOrWhiteSpace(_activityData.FimBundle) && !_activityData.FimBundle.Contains(".bdl"))
            //{
            //    try
            //    {
            //        DirectoryInfo info = new DirectoryInfo(_activityData.FimBundle);
            //        DirectoryInfo[] Folders = info.GetDirectories();

            //        var productNames = _executionData.Assets.OfType<IDeviceInfo>().Select(x => x.ProductName.ToLower()).Distinct();
            //        HashSet<string> DevicesWithNoFW = new HashSet<string>();

            //        foreach (var product in productNames)
            //        {
            //            var associatedFolder = Folders.FirstOrDefault(n => product.Contains(n.Name.ToLower()));
            //            if (null != associatedFolder)
            //            {
            //                string[] files = Directory.GetFiles(associatedFolder.FullName, "*.bdl");
            //                if (files.Count() == 0)
            //                {
            //                    throw new FileNotFoundException($"Unable to find a .bdl file of device of type {product} in folder {associatedFolder}");
            //                }

            //                _FWMapping.Add(product, files.FirstOrDefault());
            //            }
            //            else
            //            {
            //                throw new FileNotFoundException($"Unable to match {product} with a FW folder");
            //            }
            //        }

            //    }
            //    catch (DirectoryNotFoundException)
            //    {
            //        return new PluginExecutionResult(PluginResult.Failed, "Unable to find folder Directory");
            //    }
            //    catch (FileNotFoundException e)
            //    {
            //        return new PluginExecutionResult(PluginResult.Failed, e.Message);
            //    }
            //    catch
            //    {
            //        throw;
            //    }
            //}



            //List<AssetLockToken> assetTokens = _executionData.Assets.OfType<IDeviceInfo>().Select(n => new AssetLockToken(n, TimeSpan.FromMinutes(5), TimeSpan.FromMinutes(5))).ToList();

            ExecutionServices.SystemTrace.LogDebug("Entering the abyss");
            try
            {
                Parallel.ForEach(_executionData.Assets.OfType <IDeviceInfo>(), new ParallelOptions {
                    MaxDegreeOfParallelism = 10
                },
                                 asset =>
                {
                    try
                    {
                        //bool done = false;
                        ExecutionServices.SystemTrace.LogDebug($"Processing { (object)asset.AssetId} on thread { (object)Thread.CurrentThread}");
                        JediDevice device = SetDefaultPassword(asset.Address, asset.AdminPassword);

                        //Keeping as a list because critical section requires it

                        AssetLockToken assetToken = new AssetLockToken(asset, lockTimeout, holdTimeout);

                        ExecutionServices.CriticalSection.Run(assetToken, () =>
                        {
                            ExecutionServices.SystemTrace.LogDebug($"Performing update on device {asset.AssetId} at address {asset.Address}");
                            result = UpdateDevice(asset, _activityData);
                            results.AddOrUpdate(asset.AssetId, result, (key, oldValue) => UpdateDevice(asset, _activityData));

                            //Wait for status to be done


                            //if (!string.IsNullOrEmpty(_activityData.FimBundle))
                            //{
                            //    string json = string.Empty;
                            //    if (_FWMapping.ContainsKey(asset.ProductName.ToLower()))
                            //    {
                            //        json = @"{""printer"":""" + $@"{asset.Address}""" + $@", ""bundle_file_path"": ""{_FWMapping[asset.ProductName.ToLower()].Replace(@"\", @"\\")}"",""username"":""admin"", ""password"":" + $@"""{device.AdminPassword}""" + "}";
                            //    }
                            //    else if(_activityData.FimBundle.Contains(".bdl"))
                            //    {
                            //        json = @"{""printer"":""" + $@"{asset.Address}""" + $@", ""bundle_file_path"": ""{_activityData.FimBundle.Replace(@"\", @"\\")}"",""username"":""admin"", ""password"":" + $@"""{device.AdminPassword}""" + "}";

                            //    }

                            //    results.AddOrUpdate(asset.AssetId, result, (key, oldValue) => (PostFim(asset.AssetId, json)));

                            //}
                        });
                    }
                    catch (Exception e)
                    {
                        //Update DIctionary, Log Error, proceed
                        ExecutionServices.SystemTrace.LogDebug(e);
                        results.AddOrUpdate(asset.AssetId, result, (key, oldValue) => false);
                    }
                });
            }
            catch
            {
                return(new PluginExecutionResult(PluginResult.Error, "Error during NVRAM Setup"));
            }

            foreach (var item in results)
            {
                string passFail = item.Value ? "Passed" : "Failed";
                UpdateStatus($"Device {item.Key}: Result: {passFail}");
            }

            //CheckResults from dictionary
            PluginExecutionResult finalResult;

            if (results.Select(x => x.Value).Contains(false))
            {
                finalResult = new PluginExecutionResult(PluginResult.Failed);
            }
            else
            {
                finalResult = new PluginExecutionResult(PluginResult.Passed);
            }

            return(finalResult);
        }
        public string CreateJson(PrintDeviceInfoInternal device, LowLevelConfigActivityData data, ref string validationCall, ref Dictionary <string, string> validationValues)
        {
            string jsonString = @"{""printer"": """ + device.Address + @""", ""nvps"":[";

            validationCall = jsonString;
            Type type = typeof(LowLevelConfigActivityData);


            Dictionary <string, string> properties = new Dictionary <string, string>();

            //Reflection Fun?
            foreach (PropertyInfo prop in type.GetProperties())
            {
                if (!prop.Name.Equals("EnableFW"))
                {
                    properties.Add(prop.Name, (string)prop.GetValue(data));
                }
            }

            var loopValues = properties.Where(x => !string.IsNullOrWhiteSpace(x.Value)).Where(x => x.Value != "False");

            if (loopValues.Count() == 0)
            {
                return(string.Empty);
            }

            foreach (var item in properties)
            {
                if (!string.IsNullOrEmpty(item.Value))
                {
                    switch (item.Key)
                    {
                    case "SerialNumberBool":
                        if (item.Value == "False" || string.IsNullOrWhiteSpace(item.Value))
                        {
                            continue;
                        }
                        else
                        {
                            jsonString     += _mappedData.GetNVRAM(item.Key, device.SerialNumber);
                            validationCall += _mappedData.GetValidation("SerialNumber");
                            validationCall += @",";
                            validationValues.Add("SerialNumber", device.SerialNumber);
                        }

                        break;

                    case "ModelNumberBool":
                        if (item.Value == "False" || string.IsNullOrWhiteSpace(item.Value))
                        {
                            continue;
                        }
                        else
                        {
                            jsonString     += _mappedData.GetNVRAM(item.Key, device.ModelNumber);
                            validationCall += _mappedData.GetValidation("ModelNumber");
                            validationCall += @",";
                            validationValues.Add("ModelNumber", device.ModelNumber);
                        }

                        break;

                    case "CRDefSleep":
                        jsonString     += _mappedData.GetCRSleep(item.Value);
                        validationCall += _mappedData.GetValidation(item.Key);
                        validationValues.Add(item.Key, item.Value);
                        validationCall += @",";
                        break;

                    case "RebootCrashMode":
                        jsonString     += _mappedData.keyMapping[item.Key][item.Value];
                        validationCall += _mappedData.GetValidation(item.Key);
                        switch (item.Value)
                        {
                        case "Enable":
                            validationValues.Add(item.Key, "01000000");
                            break;

                        case "Disable":
                            validationValues.Add(item.Key, "00000000");
                            break;

                        default:
                            throw new Exception($@"Unhandled {item.Key} setting detected");
                        }
                        validationCall += @",";
                        break;


                    case "JDIMfgReset":
                        jsonString     += _mappedData.keyMapping[item.Key][item.Value];
                        validationCall += _mappedData.GetValidation(item.Key);
                        switch (item.Value)
                        {
                        case "True":
                            validationValues.Add(item.Key, "01000000");
                            break;

                        case "False":
                            validationValues.Add(item.Key, "00000000");
                            break;

                        default:
                            throw new Exception($@"Unhandled {item.Key} setting detected");
                        }
                        validationCall += @",";
                        break;

                    case "SaveRecoverFlags":
                        jsonString     += _mappedData.keyMapping[item.Key][item.Value];
                        validationCall += _mappedData.GetValidation(item.Key);
                        switch (item.Value)
                        {
                        case "None":
                            validationValues.Add(item.Key, "0000");
                            break;

                        case "Save":
                            validationValues.Add(item.Key, "0100");
                            break;

                        case "Recover":
                            validationValues.Add(item.Key, "0001");
                            break;

                        case "SaveRecover":
                            validationValues.Add(item.Key, "0101");
                            break;

                        default:
                            throw new Exception($@"Unhandled {item.Key} setting detected");
                        }
                        validationCall += @",";
                        break;

                    case "Language":
                        jsonString     += _mappedData.keyMapping[item.Key][item.Value];
                        validationCall += _mappedData.GetValidation(item.Key);
                        switch (item.Value)
                        {
                        case "English":
                            validationValues.Add(item.Key, "09040000");
                            break;

                        case "Korean":
                            validationValues.Add(item.Key, "12040000");
                            break;

                        default:
                            throw new Exception($@"Unhandled {item.Key} setting detected");
                        }
                        validationCall += @",";
                        break;

                    case "SuppressUsed":
                        jsonString     += _mappedData.keyMapping[item.Key][item.Value];
                        validationCall += _mappedData.GetValidation(item.Key);
                        switch (item.Value)
                        {
                        case "True":
                            validationValues.Add(item.Key, "01");
                            break;

                        case "False":
                            validationValues.Add(item.Key, "00");
                            break;

                        default:
                            throw new Exception($@"Unhandled {item.Key} setting detected");
                        }
                        validationCall += @",";
                        break;

                    case "ExecutionMode":
                        jsonString     += _mappedData.keyMapping[item.Key][item.Value];
                        validationCall += _mappedData.GetValidation(item.Key);
                        validationValues.Add(item.Key, item.Value);
                        validationCall += @",";
                        break;

                    case "PartialClean":
                        jsonString += _mappedData.GetPartialClean();
                        //validationCall += _mappedData.GetValidation(item.Key);
                        validationValues.Add(item.Key, "True");
                        break;

                    default:
                        continue;
                    }
                    jsonString += @",";
                }
            }
            jsonString      = jsonString.Remove(jsonString.Length - 1);
            validationCall  = validationCall.Remove(validationCall.Length - 1);
            validationCall += @"]}";

            if (jsonString.Contains("ModelNumber") || jsonString.Contains("SerialNumber"))
            {
                jsonString += @"], ""username"":""admin"", ""password"": " + $@"""{device.AdminPassword}""" + "}";
            }
            else
            {
                jsonString += @",[""845E3285-C67C-4f4b-9AA4-0AE91BD35089"",""LLDataStoreBootAction"",""hex"", ""01000000""],[""845E3285-C67C-4f4b-9AA4-0AE91BD35089"",""PMDataStoreBootAction"",""hex"", ""01000000""]], ""username"":""admin"", ""password"": " + $@"""{device.AdminPassword}""" + "}";
            }

            return(jsonString);
        }
        //Updating status during the parallel for each will lock the UI thread
        private bool UpdateDevice(IDeviceInfo device, LowLevelConfigActivityData data)
        {
            PrintDeviceInfoInternal printDevice = device as PrintDeviceInfoInternal;
            string validationCall = string.Empty;
            Dictionary <string, string> validationValues = new Dictionary <string, string>();


            var json = CreateJson(printDevice, data, ref validationCall, ref validationValues);

            if (json == string.Empty)// || !string.IsNullOrEmpty(data.FimBundle))
            {
                return(true);
            }
            ExecutionServices.SystemTrace.LogDebug(json);


            try
            {
                string callLocation = @"/jedi/nvpconfig";
                string jobId        = POST(_webServiceURL, callLocation, json);

                string status = GET(_webServiceURL, callLocation, jobId);
                //UpdateStatus($"Service Call Successful on device {device.Address}, response: {status}");
                DateTime startTime = DateTime.Now;


                while (status.ToUpper().Contains("PENDING") && startTime + TimeSpan.FromMinutes(15) > DateTime.Now)
                {
                    Thread.Sleep(TimeSpan.FromSeconds(10));
                    status = GET(_webServiceURL, callLocation, jobId);
                }

                if (!status.ToUpper().Contains("SUCCESS"))
                {
                    ExecutionServices.SystemTrace.LogDebug($"Device: {device.AssetId} failed to update NVRAM: Issue{status}");
                    if (status.ToUpper().Contains("PENDING"))
                    {
                        throw new Exception($"Device Unresponsive Exception, please check on device {device.AssetId}");
                    }
                    return(false);
                }
            }
            catch
            {
                throw;
            }


            //Validate here
            if (validationValues.ContainsKey("PartialClean"))
            {
                //We can't validate partial cleans.
                return(true);
            }
            else
            {
                //Otherwise send the nvram call to get the values of the current requested changes.
                string   endurl    = @"/jedi/nvpget";
                string   id        = POST(_webServiceURL, endurl, validationCall);
                string   temp      = GET(_webServiceURL, endurl, id);
                DateTime startTime = DateTime.Now;
                try
                {
                    while (temp.Contains("PENDING") && startTime + TimeSpan.FromMinutes(15) > DateTime.Now)
                    {
                        temp = GET(_webServiceURL, endurl, id);
                        Thread.Sleep(2000);
                        Console.WriteLine(temp);
                    }

                    //JavaScriptSerializer javascriptSerializer = new JavaScriptSerializer();
                    //temp = temp.Substring(1, temp.Length - 2);
                    var items = JsonConvert.DeserializeObject <List <NVRamPackageData> >(temp);

                    foreach (var item in items)
                    {
                        switch (item.Name)
                        {
                        case "SerialNumber":
                        case "ModelNumber":
                        case "ExecutionMode":
                        case "CRDefSleep":
                            byte[] hexval    = FromHex(item.HexValue);
                            string stringRep = Encoding.ASCII.GetString(hexval);

                            if (stringRep == validationValues[item.Name])
                            {
                                continue;
                            }
                            else
                            {
                                return(false);
                            }

                        case "JDIMfgReset":
                        case "SaveRecoverFlags":
                        case "Language":
                        case "RebootCrashMode":
                        case "SuppressUsed":
                            if (item.HexValue == validationValues[item.Name])
                            {
                                continue;
                            }
                            else
                            {
                                return(false);
                            }


                        //break;
                        //case "PartialClean":

                        //    break;
                        default:
                            continue;
                        }
                    }
                }
                catch (Exception ex)
                {
                    ExecutionServices.SystemTrace.LogDebug(ex);
                    throw;
                }

                return(true);
            }
        }