Beispiel #1
0
        private void AdapterOnCommandReceived(ReceivedCommandData receivedCommandData)
        {
            Guard.DebugAssertArgumentNotNull(receivedCommandData, nameof(receivedCommandData));

            NooliteSensorInfo info;

            if (!_channelToConfig.Value.TryGetValue(receivedCommandData.Channel, out info))
            {
                throw new InvalidConfigurationException($"Could not locate Noolite channel #{receivedCommandData.Channel} in the configuration.");
            }

            switch (receivedCommandData.Cmd)
            {
            case CommandOn:
                _eventSender.SendEvent(new SensorActivatedEvent(info.SensorId));
                _log.Information("Noolite sensor activated: {SensorId}", info.SensorId);

                break;

            case CommandOff:
                _eventSender.SendEvent(new SensorDeactivatedEvent(info.SensorId));
                _log.Information("Noolite sensor deactivated: {SensorId}", info.SensorId);

                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(receivedCommandData.Cmd));
            }
        }
Beispiel #2
0
        public async Task ProcessSensorActivation(SensorId sensorId)
        {
            var switchesToTurnOn = (await GetState()).GetAutomatedSwitches(sensorId);

            foreach (var switchId in switchesToTurnOn)
            {
                _eventSender.SendEvent(new TurnOnEvent(switchId));
            }
        }
        private void ProcessRequest(ConfigurationRequestEvent request)
        {
            Guard.DebugAssertArgumentNotNull(request, nameof(request));

            _log.Debug("Processing configuration request: {ConfigurationKey}, {ReplyAddress}", request.ConfigurationKey, request.ReplyAddress);

            if (string.IsNullOrEmpty(request.ConfigurationKey))
            {
                _log.Debug("Invalid configuration key.");
                return;
            }

            var key           = request.ConfigurationKey;
            var configuration = _configurationProvider.GetConfiguration(key);

            if (string.IsNullOrEmpty(configuration))
            {
                _log.Warning("No configuration found for {ConfigurationKey}, requester: {ReplyAddress}", request.ConfigurationKey, request.ReplyAddress);
                return;
            }

            var reply = new ConfigurationResponseEvent
            {
                Configuration = configuration,
                Address       = request.ReplyAddress
            };

            _eventSender.SendEvent(reply);
            _log.Debug("Configuration request processed.");
        }
 public IndexModule(IEventSender eventSender)
 {
     Get("/", args =>
     {
         eventSender.SendEvent(DateTime.UtcNow.ToString(CultureInfo.InvariantCulture));
         return("Hello World, it's Event Producer service. Every request here will produce event with messege contains current date time.");
     });
 }
Beispiel #5
0
        private static Task SendSessionReadyEvent(IEventSender eventSender, string ownerUri, bool success,
                                                  string message)
        {
            var sessionReadyParams = new EditSessionReadyParams
            {
                OwnerUri = ownerUri,
                Message  = message,
                Success  = success
            };

            return(eventSender.SendEvent(EditSessionReadyEvent.Type, sessionReadyParams));
        }
Beispiel #6
0
        public async Task <TConfiguration> Load(string configKey)
        {
            Guard.DebugAssertArgumentNotNull(configKey, nameof(configKey));

            _log.Debug("Loading configuration {ConfigurationKey}", configKey);

            var request = new ConfigurationRequestEvent()
            {
                ConfigurationKey = configKey,
                ReplyAddress     = _serviceName
            };
            var response = _source.ReceiveEvents <ConfigurationResponseEvent>().FirstAsync();

            _sender.SendEvent(request);

            var config = (await response).Configuration;

            _log.Verbose("Got response, deserializing...");

            var serializerSettings = new JsonSerializerSettings
            {
                TypeNameHandling = TypeNameHandling.Auto,
                Converters       = _converters
            };

            try
            {
                var result = JsonConvert.DeserializeObject <TConfiguration>(config, serializerSettings);
                _log.Debug("Configuration {ConfigurationKey} loaded successfully.", configKey);
                return(result);
            }
            catch (JsonException ex)
            {
                throw new InvalidConfigurationException(ex, $"Error reading json from response.");
            }
        }
 /// <summary>
 /// Sends a JSON-RPC event.
 /// </summary>
 private void SendEvent <TParams>(IEventSender requestContext, EventType <TParams> eventType, TParams parameters)
 {
     Task.Run(async() => await requestContext.SendEvent(eventType, parameters));
 }
Beispiel #8
0
        private static void ExecuteAndCompleteQuery(string ownerUri, Query query,
                                                    IEventSender eventSender,
                                                    Query.QueryAsyncEventHandler querySuccessCallback,
                                                    Query.QueryAsyncErrorEventHandler queryFailureCallback)
        {
            // Setup the callback to send the complete event
            Query.QueryAsyncEventHandler completeCallback = async q =>
            {
                // Send back the results
                QueryCompleteParams eventParams = new QueryCompleteParams
                {
                    OwnerUri       = ownerUri,
                    BatchSummaries = q.BatchSummaries
                };

                await eventSender.SendEvent(QueryCompleteEvent.Type, eventParams);
            };

            // Setup the callback to send the complete event
            Query.QueryAsyncErrorEventHandler failureCallback = async(q, e) =>
            {
                // Send back the results
                QueryCompleteParams eventParams = new QueryCompleteParams
                {
                    OwnerUri       = ownerUri,
                    BatchSummaries = q.BatchSummaries
                };

                await eventSender.SendEvent(QueryCompleteEvent.Type, eventParams);
            };
            query.QueryCompleted += completeCallback;
            query.QueryFailed    += failureCallback;

            // Add the callbacks that were provided by the caller
            // If they're null, that's no problem
            query.QueryCompleted += querySuccessCallback;
            query.QueryFailed    += queryFailureCallback;

            // Setup the batch callbacks
            Batch.BatchAsyncEventHandler batchStartCallback = async b =>
            {
                BatchEventParams eventParams = new BatchEventParams
                {
                    BatchSummary = b.Summary,
                    OwnerUri     = ownerUri
                };

                await eventSender.SendEvent(BatchStartEvent.Type, eventParams);
            };
            query.BatchStarted += batchStartCallback;

            Batch.BatchAsyncEventHandler batchCompleteCallback = async b =>
            {
                BatchEventParams eventParams = new BatchEventParams
                {
                    BatchSummary = b.Summary,
                    OwnerUri     = ownerUri
                };

                await eventSender.SendEvent(BatchCompleteEvent.Type, eventParams);
            };
            query.BatchCompleted += batchCompleteCallback;

            Batch.BatchAsyncMessageHandler batchMessageCallback = async m =>
            {
                MessageParams eventParams = new MessageParams
                {
                    Message  = m,
                    OwnerUri = ownerUri
                };
                await eventSender.SendEvent(MessageEvent.Type, eventParams);
            };
            query.BatchMessageSent += batchMessageCallback;

            // Setup the ResultSet completion callback
            ResultSet.ResultSetAsyncEventHandler resultCallback = async r =>
            {
                ResultSetEventParams eventParams = new ResultSetEventParams
                {
                    ResultSetSummary = r.Summary,
                    OwnerUri         = ownerUri
                };
                await eventSender.SendEvent(ResultSetCompleteEvent.Type, eventParams);
            };
            query.ResultSetCompleted += resultCallback;

            // Launch this as an asynchronous task
            query.Execute();
        }
Beispiel #9
0
        private static void ExecuteAndCompleteQuery(string ownerUri, Query query,
                                                    IEventSender eventSender,
                                                    Query.QueryAsyncEventHandler querySuccessCallback,
                                                    Query.QueryAsyncErrorEventHandler queryFailureCallback)
        {
            // Setup the callback to send the complete event
            Query.QueryAsyncEventHandler completeCallback = async q =>
            {
                // Send back the results
                QueryCompleteParams eventParams = new QueryCompleteParams
                {
                    OwnerUri       = ownerUri,
                    BatchSummaries = q.BatchSummaries
                };

                Logger.Write(TraceEventType.Information, $"Query:'{ownerUri}' completed");
                await eventSender.SendEvent(QueryCompleteEvent.Type, eventParams);
            };

            // Setup the callback to send the failure event
            Query.QueryAsyncErrorEventHandler failureCallback = async(q, e) =>
            {
                // Send back the results
                QueryCompleteParams eventParams = new QueryCompleteParams
                {
                    OwnerUri       = ownerUri,
                    BatchSummaries = q.BatchSummaries
                };

                Logger.Write(TraceEventType.Error, $"Query:'{ownerUri}' failed");
                await eventSender.SendEvent(QueryCompleteEvent.Type, eventParams);
            };
            query.QueryCompleted += completeCallback;
            query.QueryFailed    += failureCallback;

            // Add the callbacks that were provided by the caller
            // If they're null, that's no problem
            query.QueryCompleted += querySuccessCallback;
            query.QueryFailed    += queryFailureCallback;

            // Setup the batch callbacks
            Batch.BatchAsyncEventHandler batchStartCallback = async b =>
            {
                BatchEventParams eventParams = new BatchEventParams
                {
                    BatchSummary = b.Summary,
                    OwnerUri     = ownerUri
                };

                Logger.Write(TraceEventType.Information, $"Batch:'{b.Summary}' on Query:'{ownerUri}' started");
                await eventSender.SendEvent(BatchStartEvent.Type, eventParams);
            };
            query.BatchStarted += batchStartCallback;

            Batch.BatchAsyncEventHandler batchCompleteCallback = async b =>
            {
                BatchEventParams eventParams = new BatchEventParams
                {
                    BatchSummary = b.Summary,
                    OwnerUri     = ownerUri
                };

                Logger.Write(TraceEventType.Information, $"Batch:'{b.Summary}' on Query:'{ownerUri}' completed");
                await eventSender.SendEvent(BatchCompleteEvent.Type, eventParams);
            };
            query.BatchCompleted += batchCompleteCallback;

            Batch.BatchAsyncMessageHandler batchMessageCallback = async m =>
            {
                MessageParams eventParams = new MessageParams
                {
                    Message  = m,
                    OwnerUri = ownerUri
                };

                Logger.Write(TraceEventType.Information, $"Message generated on Query:'{ownerUri}' :'{m}'");
                await eventSender.SendEvent(MessageEvent.Type, eventParams);
            };
            query.BatchMessageSent += batchMessageCallback;

            // Setup the ResultSet available callback
            ResultSet.ResultSetAsyncEventHandler resultAvailableCallback = async r =>
            {
                ResultSetAvailableEventParams eventParams = new ResultSetAvailableEventParams
                {
                    ResultSetSummary = r.Summary,
                    OwnerUri         = ownerUri
                };

                Logger.Write(TraceEventType.Information, $"Result:'{r.Summary} on Query:'{ownerUri}' is available");
                await eventSender.SendEvent(ResultSetAvailableEvent.Type, eventParams);
            };
            query.ResultSetAvailable += resultAvailableCallback;

            // Setup the ResultSet updated callback
            ResultSet.ResultSetAsyncEventHandler resultUpdatedCallback = async r =>
            {
                ResultSetUpdatedEventParams eventParams = new ResultSetUpdatedEventParams
                {
                    ResultSetSummary = r.Summary,
                    OwnerUri         = ownerUri
                };

                Logger.Write(TraceEventType.Information, $"Result:'{r.Summary} on Query:'{ownerUri}' is updated with additional rows");
                await eventSender.SendEvent(ResultSetUpdatedEvent.Type, eventParams);
            };
            query.ResultSetUpdated += resultUpdatedCallback;

            // Setup the ResultSet completion callback
            ResultSet.ResultSetAsyncEventHandler resultCompleteCallback = async r =>
            {
                ResultSetCompleteEventParams eventParams = new ResultSetCompleteEventParams
                {
                    ResultSetSummary = r.Summary,
                    OwnerUri         = ownerUri
                };

                Logger.Write(TraceEventType.Information, $"Result:'{r.Summary} on Query:'{ownerUri}' is complete");
                await eventSender.SendEvent(ResultSetCompleteEvent.Type, eventParams);
            };
            query.ResultSetCompleted += resultCompleteCallback;

            // Launch this as an asynchronous task
            query.Execute();
        }
        private static void ExecuteAndCompleteQuery(string ownerUri, IEventSender eventSender, Query query)
        {
            // Skip processing if the query is null
            if (query == null)
            {
                return;
            }

            // Setup the query completion/failure callbacks
            Query.QueryAsyncEventHandler callback = async q =>
            {
                // Send back the results
                QueryCompleteParams eventParams = new QueryCompleteParams
                {
                    OwnerUri       = ownerUri,
                    BatchSummaries = q.BatchSummaries
                };

                await eventSender.SendEvent(QueryCompleteEvent.Type, eventParams);
            };

            query.QueryCompleted += callback;
            query.QueryFailed    += callback;

            // Setup the batch callbacks
            Batch.BatchAsyncEventHandler batchStartCallback = async b =>
            {
                BatchEventParams eventParams = new BatchEventParams
                {
                    BatchSummary = b.Summary,
                    OwnerUri     = ownerUri
                };

                await eventSender.SendEvent(BatchStartEvent.Type, eventParams);
            };
            query.BatchStarted += batchStartCallback;

            Batch.BatchAsyncEventHandler batchCompleteCallback = async b =>
            {
                BatchEventParams eventParams = new BatchEventParams
                {
                    BatchSummary = b.Summary,
                    OwnerUri     = ownerUri
                };

                await eventSender.SendEvent(BatchCompleteEvent.Type, eventParams);
            };
            query.BatchCompleted += batchCompleteCallback;

            Batch.BatchAsyncMessageHandler batchMessageCallback = async m =>
            {
                MessageParams eventParams = new MessageParams
                {
                    Message  = m,
                    OwnerUri = ownerUri
                };
                await eventSender.SendEvent(MessageEvent.Type, eventParams);
            };
            query.BatchMessageSent += batchMessageCallback;

            // Setup the ResultSet completion callback
            ResultSet.ResultSetAsyncEventHandler resultCallback = async r =>
            {
                ResultSetEventParams eventParams = new ResultSetEventParams
                {
                    ResultSetSummary = r.Summary,
                    OwnerUri         = ownerUri
                };
                await eventSender.SendEvent(ResultSetCompleteEvent.Type, eventParams);
            };
            query.ResultSetCompleted += resultCallback;

            // Launch this as an asynchronous task
            query.Execute();
        }