Exemple #1
0
        //this is a temp depedency, until we get the feature extraction module
        public Orchestrator(ITemperatureSensor temperatureProxy, IModelTraining trainerProxy)
        {
            temperatureProxy.Temperature.Subscribe(this, async signal =>
            {
                OrchestratorTwin twin;

                lock (_twinLock)
                {
                    twin = _lastTwin;
                }

                if (twin == null)
                {
                    return(MessageResult.Ok);
                }
                if (twin.Scale == TemperatureScale.Celsius)
                {
                    signal.Value = signal.Value * 9 / 5 + 32;
                }

                await BroadcastMessage(signal, twin);

                return(MessageResult.Ok);
            });

            trainerProxy.Model.Subscribe(this, async model =>
            {
                Logger.LogInformation(
                    $"New Trained Model for {model.Algorithm}. Updating the model in {model.Algorithm}");
                await Model.PublishAsync(model);
                return(MessageResult.Ok);
            });

            Twin.Subscribe(async twin =>
            {
                lock (_twinLock)
                {
                    _lastTwin = twin;
                }

                Logger.LogInformation($"Twin update. Routing  = {twin.RoutingMode.ToString()}");
                await Twin.ReportAsync(twin);
                return(TwinResult.Ok);
            });
        }
        public AnomalyDetection(IOrchestrator orcherstratorProxy, IModelTraining modelTrainingProxy)
        {
            orcherstratorProxy.Detection.Subscribe(this, async signal =>
            {
                try
                {
                    int cluster = 0;
                    if (_kMeansScoring != null)
                    {
                        lock (_syncClustering)
                            if (_kMeansScoring != null)
                            {
                                cluster = _kMeansScoring.Score(new double[] { signal.Value });
                            }
                    }

                    if (cluster < 0)
                    {
                        await Anomaly.PublishAsync(new Anomaly()
                        {
                            Temperature = signal
                        });
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
                return(MessageResult.Ok);
            });

            modelTrainingProxy.Model.Subscribe(this, async(model) =>
            {
                //if the messages has been stored and forwarded, but the file has been deleted (e.g. a restart)
                //then the message can be empty (null)
                if (model == null)
                {
                    return(MessageResult.Ok);
                }

                try
                {
                    lock (_syncClustering)

                        switch (model.Algorithm)
                        {
                        case ThermostatApplication.Algorithm.kMeans:
                            _kMeansScoring = new KMeansScoring(_numClusters);
                            _kMeansScoring.DeserializeModel(model.DataJson);
                            break;

                        case ThermostatApplication.Algorithm.LSTM:
                            break;

                        default:
                            break;
                        }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }

                return(MessageResult.Ok);
            });
        }