private Task RouteMessage(Temperature signal, Routing mode)
        {
            switch (mode)
            {
            case Routing.Train:
                return(Training.PublishAsync(signal));

            case Routing.Detect:
                return(Detection.PublishAsync(signal));

            case Routing.Visualize:
                return(Visualization.PublishAsync(signal));
            }
            return(null);
        }
        private async Task BroadcastMessage(Temperature signal, OrchestratorTwin twin)
        {
            Console.WriteLine($"Orchestrator.BroadcastMessage : {JsonConvert.SerializeObject(signal)}");

            List <Task> messages = new List <Task>();

            foreach (Routing item in Enum.GetValues(typeof(Routing)))
            {
                if (twin.RoutingMode.HasFlag(item))
                {
                    switch (item)
                    {
                    case Routing.Train:
                        messages.Add(Training.PublishAsync(signal));
                        break;

                    case Routing.Detect:
                        messages.Add(Detection.PublishAsync(signal));
                        break;

                    case Routing.VisualizeSource:
                        messages.Add(Visualization.PublishAsync(new GraphData()
                        {
                            CorrelationID = "Source",
                            Values        = new double[1][] { new double[2] {
                                                                  signal.TimeStamp, signal.Value
                                                              } }
                        }
                                                                ));
                        break;

                    default:
                        continue;
                    }
                }
            }

            if (messages.Count > 0)
            {
                await Task.WhenAll(messages).ConfigureAwait(false);
            }
        }
        private async Task BroadcastAggregate(Reference <DataAggregate> aggregate, OrchestratorTwin twin)
        {
            List <Task> messages = new List <Task>();

            foreach (Routing item in Enum.GetValues(typeof(Routing)))
            {
                if (twin.RoutingMode.HasFlag(item))
                {
                    switch (item)
                    {
                    case Routing.VisualizeFeature:
                        messages.Add(Visualization.PublishAsync(new GraphData()
                        {
                            CorrelationID = "Feature",
                            Values        = aggregate.Message.Values
                        }
                                                                ));
                        break;

                    case Routing.FeatureExtraction:
                        messages.Add(FeatureExtraction.PublishAsync(new DataAggregate()
                        {
                            CorrelationID = "Feature",
                            Values        = aggregate.Message.Values
                        }));
                        break;

                    default:
                        continue;
                    }
                }
            }

            if (messages.Count > 0)
            {
                await Task.WhenAll(messages);
            }
        }
예제 #4
0
        public Orchestrator(ITemperatureSensor temperatureProxy, IDataAggregator aggregatorProxy)
        {
            temperatureProxy.Temperature.Subscribe(this, async signal =>
            {
                var twin = Twin.LastKnownTwin;
                if (twin != null)
                {
                    Preprocess(signal, twin);
                    List <Task> messages = new List <Task>();
                    foreach (Routing item in Enum.GetValues(typeof(Routing)))
                    {
                        if (twin.RoutingMode.HasFlag(item))
                        {
                            switch (item)
                            {
                            case Routing.Sampling:
                                Console.WriteLine("Aggregating");
                                messages.Add(Sampling.PublishAsync(signal));
                                break;

                            case Routing.Detect:
                                messages.Add(Detection.PublishAsync(signal));
                                break;

                            default:
                                continue;
                            }
                        }
                    }

                    if (messages.Count > 0)
                    {
                        await Task.WhenAll(messages);
                    }
                }
                return(MessageResult.Ok);
            });


            aggregatorProxy.Aggregate.Subscribe(this, async aggregate =>
            {
                if (aggregate == null)
                {
                    return(MessageResult.Ok);
                }

                var twin = Twin.LastKnownTwin;
                Console.WriteLine(twin);
                if (twin != null)
                {
                    List <Task> messages = new List <Task>();
                    foreach (Routing item in Enum.GetValues(typeof(Routing)))
                    {
                        if (twin.RoutingMode.HasFlag(item))
                        {
                            switch (item)
                            {
                            case Routing.Visualize:
                                Console.WriteLine("Visualizing");
                                messages.Add(Visualization.PublishAsync(
                                                 new GraphData()
                                {
                                    CorrelationID = aggregate.Message.CorrelationID,
                                    Values        = aggregate.Message.Values
                                }
                                                 ));
                                break;

                            case Routing.FeatureExtraction:
                                //messages.Add(FeatureExtraction.PublishAsync(new DataAggregate()
                                //{
                                //    CorrelationID = aggregate.Message.CorrelationID,
                                //    Values = aggregate.Message.Values
                                //}));
                                break;

                            default:
                                continue;
                            }
                        }
                    }

                    if (messages.Count > 0)
                    {
                        await Task.WhenAll(messages);
                    }
                }
                return(MessageResult.Ok);
            });

            Twin.Subscribe(async twin =>
            {
                System.Console.WriteLine($"Preprocessor: new routing : { twin.RoutingMode.ToString()}");
                await Twin.ReportAsync(twin);
                return(TwinResult.Ok);
            });
        }