Beispiel #1
0
        public void SetComponentStatusValue(string componentUid, string uid, object value)
        {
            if (componentUid == null)
            {
                throw new ArgumentNullException(nameof(componentUid));
            }
            if (uid == null)
            {
                throw new ArgumentNullException(nameof(uid));
            }

            var component = GetComponent(componentUid);

            var setStatusValueResult = component.SetStatusValue(uid, value);
            var oldValue             = setStatusValueResult.OldValue;
            var isAdd = setStatusValueResult.IsNewValue;

            var oldValueString = Convert.ToString(oldValue, CultureInfo.InvariantCulture);
            var newValueString = Convert.ToString(value, CultureInfo.InvariantCulture);
            var hasChanged     = isAdd || !string.Equals(oldValueString, newValueString, StringComparison.Ordinal);

            if (!hasChanged)
            {
                return;
            }

            _logger.LogDebug(
                "Component status value changed: [Component={0}] [Status UID={1}] [Value={2} -> {3}].",
                component.Uid,
                uid,
                oldValueString,
                newValueString);

            var componentStatusChangedEventArgs = new ComponentStatusChangedEventArgs
            {
                Timestamp = DateTime.UtcNow,
                Component = component,
                StatusUid = uid,
                OldValue  = oldValue,
                NewValue  = value
            };

            ComponentStatusChanged?.Invoke(this, componentStatusChangedEventArgs);

            _messageBusWrapper.PublishStatusChangedEvent(componentStatusChangedEventArgs);
        }
Beispiel #2
0
        public void PublishStatusChangedEvent(ComponentStatusChangedEventArgs eventArgs)
        {
            if (eventArgs is null)
            {
                throw new ArgumentNullException(nameof(eventArgs));
            }

            var message = new Dictionary <object, object>
            {
                ["type"]          = "component_registry.event.status_changed",
                ["timestamp"]     = eventArgs.Timestamp.ToString("O"),
                ["component_uid"] = eventArgs.Component.Uid,
                ["status_uid"]    = eventArgs.StatusUid,
                ["old_value"]     = eventArgs.OldValue,
                ["new_value"]     = eventArgs.NewValue
            };

            _messageBusService.Publish(message);
        }