コード例 #1
0
        /// <summary>
        /// Sets a new value for the specified camera configuration.
        /// </summary>
        /// <param name="value">The new value that is to be set.</param>
        /// <exception cref="CameraConfiguration">
        /// If the value is not valid or anything goes wrong during configuration the value, then a <see cref="CameraConfiguration" /> exception is
        /// thrown.
        /// </exception>
        public async Task SetValueAsync(string value)
        {
            // Checks if the configuration has already been initialized, if so then the configuration does not need to be initialized again
            if (!this.isInitialized)
            {
                await this.InitializeAsync();
            }

            // Validates the value that is to be set, if the value could not be validated, then a camera exception is thrown
            bool isValueValid = false;

            switch (this.configurationType)
            {
            case CameraConfigurationType.Unknown:
                throw new CameraException("The value of the configuration could not be set, because its type is Unknown.");

            case CameraConfigurationType.Text:
                isValueValid = true;
                break;

            case CameraConfigurationType.Option:
                isValueValid = this.choices.Any(choice => choice == value);
                break;

            case CameraConfigurationType.Toggle:
                isValueValid = value == "0" || value == "1";
                break;

            case CameraConfigurationType.DateTime:
                isValueValid = value.All(character => char.IsDigit(character));
                break;
            }
            if (!isValueValid)
            {
                throw new CameraException("The value of the configuration could not be set, because the specified value is invalid for the camera configuration.");
            }

            // Executes the set value command on the camera
            await gPhoto2IpcWrapper.ExecuteInteractiveAsync(string.Format(CultureInfo.InvariantCulture, "set-config {0}={1}",
                                                                          this.Name, value));
        }
コード例 #2
0
        /// <summary>
        /// Iterates all configurations of the specified camera and initializes them.
        /// </summary>
        /// <param name="gPhoto2IpcWrapper">
        /// The IPC wrapper, which is to be used to interface with gPhoto2. The IPC wrapper must be injected, because the configurations should
        /// use the exact same IPC wrapper used by the camera (the IPC wrapper ensures that only one operation at a time is executed,
        /// which is important when interfacing with the camera). If two operations, e.g. configuration a value and capturing an image, would
        /// be performed at the same time, the program would crash, because gPhoto2 can only do one thing at a time).
        /// </param>
        /// <exception cref="CameraConfiguration">
        /// If anything goes wrong during the retrieval of the camera configurations, then a <see cref="CameraConfiguration" /> exception is thrown.
        /// </exception>
        /// <returns>Returns a read-only list containing all configurations of the specified camera.</returns>
        internal static async Task <IEnumerable <CameraConfiguration> > GetCameraConfigurationsAsync(GPhoto2IpcWrapper gPhoto2IpcWrapper)
        {
            // Gets all the configurations of the specified camera and returns them
            return(await gPhoto2IpcWrapper.ExecuteInteractiveAsync("list-config", output =>
            {
                // Creates a new result list for the camera configurations
                List <CameraConfiguration> CameraConfigurations = new List <CameraConfiguration>();

                // Creates a string reader, so that the output of gPhoto2 can be read line by line
                using (StringReader stringReader = new StringReader(output))
                {
                    // Cycles over the each line of the output and creates a new configuration (each line contains the name of the configuration)
                    string line;
                    while (!string.IsNullOrWhiteSpace(line = stringReader.ReadLine()))
                    {
                        CameraConfigurations.Add(new CameraConfiguration(line.Trim(), gPhoto2IpcWrapper));
                    }
                }

                // Returns all configurations that have been found by gPhoto2 for the specified camera
                return Task.FromResult(CameraConfigurations);
            }));
        }