示例#1
0
        private async Task HandlePropertyChange(PropertyChangedEvent propertyChanged)
        {
            if (_componentState.IsStateProvidingAdapter(propertyChanged.MessageSource, propertyChanged.PropertyChangedName) &&
                _componentState.TryUpdateState(propertyChanged.PropertyChangedName, propertyChanged.NewValue, out var oldValue))
            {
                var translator = _translators.FirstOrDefault(e => e.Type == MessageType.Event && e.From.Equals(propertyChanged));

                if (translator != null)
                {
                    await MessageBroker.PublishWithTranslate(propertyChanged, translator.To, Uid).ConfigureAwait(false);
                }
                else
                {
                    await MessageBroker.Publish(PropertyChangedEvent.Create(Uid, propertyChanged.PropertyChangedName, oldValue, propertyChanged.NewValue), Uid).ConfigureAwait(false);
                }
            }
        }
示例#2
0
        private async Task FetchState()
        {
            var stopwatch = Stopwatch.StartNew();

            var newState = await ReadFromBus();

            stopwatch.Stop();

            if (!_driver.TrySaveState(newState, out var oldState))
            {
                return;
            }

            var oldStateBits = new BitArray(oldState);
            var newStateBits = new BitArray(newState);

            Logger.LogTrace($"[{Uid}] fetched different state ({oldState.ToBinaryString()}->{newState.ToBinaryString()})");

            for (int pinNumber = 0; pinNumber < oldStateBits.Length; pinNumber++)
            {
                var  oldPinState    = oldStateBits.Get(pinNumber);
                var  newPinState    = newStateBits.Get(pinNumber);
                bool pinInWriteMode = IsPinInWriteMode(pinNumber);

                // When state is the same or change is in port that are set to WRITE we skip event generation
                if (oldPinState == newPinState || pinInWriteMode)
                {
                    continue;
                }

                var properyChangeEvent = PropertyChangedEvent.Create(Uid, PowerState.StateName, oldPinState, newPinState, new Dictionary <string, string>()
                {
                    [MessageProperties.PinNumber] = pinNumber.ToString()
                });

                await MessageBroker.Publish(properyChangeEvent, Uid);

                Logger.LogTrace($"[{Uid}] Pin [{pinNumber}] state changed {oldPinState}->{newPinState}");
            }

            if (stopwatch.ElapsedMilliseconds > _poolDurationWarning)
            {
                Logger.LogWarning($"Polling device '{Uid}' took {stopwatch.ElapsedMilliseconds}ms.");
            }
        }
示例#3
0
        protected async Task <T> UpdateState <T>(string stateName, T oldValue, T newValue, IDictionary <string, string> additionalProperties = null)
        {
            if (newValue == null || EqualityComparer <T> .Default.Equals(oldValue, newValue))
            {
                return(oldValue);
            }

            if (_requierdProperties.Count > 0)
            {
                if (additionalProperties == null || additionalProperties.Count != _requierdProperties.Count || !_requierdProperties.SequenceEqual(additionalProperties.Keys))
                {
                    throw new ArgumentException($"Update state on component {Uid} should be invoked with required properties: {string.Join(",", _requierdProperties)}");
                }
            }

            await MessageBroker.Publish(PropertyChangedEvent.Create(Uid, stateName, oldValue?.ToString(), newValue.ToString(), additionalProperties), Uid);

            return(newValue);
        }
示例#4
0
        private async Task HandlePropertyChange(PropertyChangedEvent propertyChanged)
        {
            if (_componentState.IsStateProvidingAdapter(propertyChanged.MessageSource, propertyChanged.PropertyChangedName) &&
                _componentState.TryUpdateState(propertyChanged.PropertyChangedName, propertyChanged.NewValue, out var oldValue))
            {
                var translator = _translators.FirstOrDefault(e => e.Type == MessageType.Event && e.From.Equals(propertyChanged));

                if (translator != null)
                {
                    var eventPublished = await MessageBroker.PublishWithTranslate(propertyChanged, translator.To, Uid);

                    Logger.Log(LogLevel.Information, $"<@{Uid}> {eventPublished}");
                }
                else if (RelayNotTranslatedEvents)
                {
                    var eventPublished = PropertyChangedEvent.Create(Uid, propertyChanged.PropertyChangedName, oldValue, propertyChanged.NewValue);
                    await MessageBroker.Publish(eventPublished, Uid);

                    Logger.Log(LogLevel.Information, $"<@{Uid}> {eventPublished}");
                }
            }
        }
        public override async Task RunTask(int taskId)
        {
            if (!pinNumber.HasValue)
            {
                ConsoleEx.WriteWarning("Write PIN number:");
                pinNumber = int.Parse(Console.ReadLine());
            }

            Command cmd = null;

            switch (taskId)
            {
            case 0:
                cmd = new TurnOnCommand();
                break;

            case 1:
                cmd = new TurnOffCommand();
                break;

            case 2:
                cmd          = new RefreshCommand();
                cmd.LogLevel = nameof(Microsoft.Extensions.Logging.LogLevel.Information);
                break;

            case 3:
                cmd = new SwitchPowerStateCommand();
                break;

            case 4:
                var inputUid           = "HSPE16InputOnly_2";
                var properyChangeEvent = PropertyChangedEvent.Create(inputUid, PowerState.StateName, false, true, new Dictionary <string, string>()
                {
                    [MessageProperties.PinNumber] = 0.ToString()
                });

                await MessageBroker.Publish(properyChangeEvent, inputUid);

                return;

            case 5:
                var inputUid2           = "HSPE16InputOnly_2";
                var properyChangeEvent2 = PropertyChangedEvent.Create(inputUid2, PowerState.StateName, true, false, new Dictionary <string, string>()
                {
                    [MessageProperties.PinNumber] = 0.ToString()
                });

                await MessageBroker.Publish(properyChangeEvent2, inputUid2);

                return;
            }

            if (pinNumber.HasValue && pinNumber.Value < 10)
            {
                cmd.SetProperty(MessageProperties.PinNumber, pinNumber.Value);
            }

            MessageBroker.Send(cmd, Uid);

            //return Task.CompletedTask;
        }