Esempio n. 1
0
        protected override ModelMetadata CreateMetadata(System.Collections.Generic.IEnumerable <System.Attribute> attributes, System.Type containerType, System.Func <object> modelAccessor, System.Type modelType, string propertyName)
        {
            var metadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);

            var renderModeAttribute = attributes.OfType <RenderModeAttribute>();

            if (renderModeAttribute.Any())
            {
                var renderMode = renderModeAttribute.First().RenderMode;
                switch (renderMode)
                {
                case RenderMode.DisplayModeOnly:
                    metadata.ShowForDisplay = true;
                    metadata.ShowForEdit    = false;
                    break;

                case RenderMode.EditModeOnly:
                    metadata.ShowForDisplay = false;
                    metadata.ShowForEdit    = true;
                    break;

                case RenderMode.None:
                    metadata.ShowForDisplay = false;
                    metadata.ShowForEdit    = false;
                    break;

                case RenderMode.Any:
                    metadata.ShowForDisplay = true;
                    metadata.ShowForEdit    = true;
                    break;
                }
            }

            return(metadata);
        }
#pragma warning disable RCS1213 // Remove unused member declaration.
        private static async Task Test3()
#pragma warning restore RCS1213 // Remove unused member declaration.
        {
            using (var client = new SmartHomeClient())
            {
                client.Start();

                await Task.Delay(1000).ConfigureAwait(false);

                System.Collections.Generic.IEnumerable <IDevice> devices = client
                                                                           .GetDevices();
                Plug bulb = devices.OfType <Plug>().First();

                while (true)
                {
                    Console.Write($"State ({bulb.RelayState}): ");
                    string value = Console.ReadLine();
                    if (string.Equals(value, "exit", StringComparison.CurrentCultureIgnoreCase))
                    {
                        break;
                    }

                    if (bool.TryParse(value, out bool flag))
                    {
                        await bulb.SetRelayStateAsync(flag?SwitchState.On : SwitchState.Off).ConfigureAwait(false);
                    }
                    Console.Clear();
                }

                await bulb.SetRelayStateAsync(SwitchState.Off).ConfigureAwait(false);

                Console.WriteLine("Press any key to exit...");
                Console.Read();
            }
        }
#pragma warning disable RCS1213 // Remove unused member declaration.
        private static async Task Test2()
#pragma warning restore RCS1213 // Remove unused member declaration.
        {
            using (var client = new SmartHomeClient())
            {
                client.Start();

                await Task.Delay(1000).ConfigureAwait(false);

                System.Collections.Generic.IEnumerable <IDevice> devices = client
                                                                           .GetDevices();
                LightBulb bulb = devices.OfType <LightBulb>().First();

                while (true)
                {
                    Console.Write($"Brightness ({bulb.State.Brightness}): ");
                    string value = Console.ReadLine();
                    if (string.Equals(value, "exit", StringComparison.CurrentCultureIgnoreCase))
                    {
                        break;
                    }

                    await bulb.TransitionStateAsync(new RequestedBulbState()
                    {
                        PowerState = SwitchState.On,
                        Brightness = int.Parse(value)
                    }).ConfigureAwait(false);

                    Console.Clear();
                }

                await bulb.TransitionStateAsync(SwitchState.Off).ConfigureAwait(false);

                Console.WriteLine("Press any key to exit...");
                Console.Read();
            }
        }
        protected override ModelMetadata CreateMetadata(System.Collections.Generic.IEnumerable <System.Attribute> attributes, System.Type containerType, System.Func <object> modelAccessor, System.Type modelType, string propertyName)
        {
            ModelMetadata             metadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
            PropertyEditableAttribute editable = attributes.OfType <PropertyEditableAttribute>().FirstOrDefault <PropertyEditableAttribute>();

            if (editable == null)
            {
                editable = PropertyEditableAttribute.Default;
            }
            metadata.AdditionalValues.Add(MyDataAnnotationsModelMetadataProvider.EditableWhenCreate_Key, editable.WhenCreate);
            metadata.AdditionalValues.Add(MyDataAnnotationsModelMetadataProvider.EditableWhenEdit_Key, editable.WhenEdit);
            metadata.AdditionalValues.Add(MyDataAnnotationsModelMetadataProvider.Icon_Prefix, (
                                              from i in attributes.OfType <IconAttribute>()
                                              select i.File).FirstOrDefault <string>());
            metadata.AdditionalValues.Add(MyDataAnnotationsModelMetadataProvider.RealSort_Key, (
                                              from s in attributes.OfType <RealSortAttribute>()
                                              select s.Expression).FirstOrDefault <string>());
            TypeConverterAttribute typeConverterAttribute = attributes.OfType <TypeConverterAttribute>().FirstOrDefault <TypeConverterAttribute>();

            if (typeConverterAttribute != null && typeConverterAttribute.ConverterTypeName != null)
            {
                metadata.AdditionalValues.Add(MyDataAnnotationsModelMetadataProvider.ConverterTypeName_Key, typeConverterAttribute.ConverterTypeName);
            }
            foreach (KeyValueInfoAttribute item in attributes.OfType <KeyValueInfoAttribute>())
            {
                metadata.AdditionalValues.Add(MyDataAnnotationsModelMetadataProvider.KeyValueInfoAttribute_Prefix + item.Key, item.Value);
            }
            DataFormatAttribute dataFormatAttribute = attributes.OfType <DataFormatAttribute>().FirstOrDefault <DataFormatAttribute>();

            if (dataFormatAttribute != null)
            {
                metadata.EditFormatString         = dataFormatAttribute.EditFormatStringWithBraces;
                metadata.DisplayFormatString      = dataFormatAttribute.DiaplayFormatStringWithBraces;
                metadata.NullDisplayText          = dataFormatAttribute.NullDisplayText;
                metadata.ConvertEmptyStringToNull = dataFormatAttribute.ConvertEmptyStringToNull;
            }
            return(metadata);
        }