public void Register(Type consumer)
        {
            if (consumer.GetInterfaces().Length == 0 || consumer.GetInterfaces().Any(x => x.GetInterface("IConsumer") != null))
            {
                throw new Exception("Consumer must be implement IConsumer.");
            }

            if (ConsumerList.Any(x => x.GetType() == consumer))
            {
                throw new Exception("Consumer already registered.");
            }

            var consumerObject = (IConsumer)Activator.CreateInstance(consumer);

            ConsumerList.Add(consumerObject);
        }
Пример #2
0
        private async Task Restart()
        {
            try
            {
                var nowTime = DateTime.UtcNow;
                restartStatisticalCount = restartStatisticalCount + ConsumerList.Where(consumer => consumer.Children.Any(c => c.NeedRestart)).Count();
                if (restartStatisticalCount > ConsumerList.Count / 3)
                {
                    ClientFactory.ReBuild();
                    restartStatisticalStartTime = nowTime;
                    restartStatisticalCount     = 0;
                }
                else if ((nowTime - restartStatisticalStartTime).Minutes > 10)
                {
                    restartStatisticalStartTime = nowTime;
                    restartStatisticalCount     = 0;
                }

                foreach (var consumer in ConsumerList)
                {
                    var needRestartChildren = consumer.Children.Where(child => child.NeedRestart || child.BasicConsumer == null || !child.BasicConsumer.IsRunning || child.Channel.Model.IsClosed).ToList();
                    if (needRestartChildren.Count > 0)
                    {
                        foreach (var child in needRestartChildren)
                        {
                            child.Close();
                            consumer.Children.Remove(child);
                            consumer.NowQos -= child.Qos;
                        }
                        if (consumer.NowQos < consumer.MinQos)
                        {
                            await StartSub(consumer, false);
                        }
                    }
                    else if ((nowTime - consumer.StartTime).TotalMinutes >= 5)
                    {
                        await ExpandQos(consumer);//扩容操作
                    }
                }
            }
            catch (Exception exception)
            {
                logger.LogError(exception.InnerException ?? exception, "消息队列守护线程发生错误");
            }
        }
Пример #3
0
        // Notes:
        //  - Device updates come from within the EweLinkAPI. The websocket receives a message
        //    for a device. It then calls 'Update' on the device, which the Consumer observes.

        public Model()
        {
            ConsumersList = new ConsumerList();
            Settings      = new SettingsData(SettingsData.Filepath);
            Shutdown      = new CancellationTokenSource();
            History       = new History();
            Fronius       = new FroniusAPI(Settings.SolarInverterIP, Shutdown.Token);
            Ewe           = new EweLinkAPI(Shutdown.Token);
            Solar         = new SolarData();
            Sched         = new Schedule();

            Sched.Add(new Schedule.Range("Monitor Active", Schedule.ERepeat.Daily,
                                         new DateTimeOffset(1, 1, 1, 8, 0, 0, TimeSpan.Zero),
                                         new DateTimeOffset(1, 1, 1, 18, 0, 0, TimeSpan.Zero)));

            Log.Write(ELogLevel.Info, "Model initialised");
            m_settings.NotifyAllSettingsChanged();

            // Prevent system sleep
            WinOS.SystemSleep(keep_awake: true);
        }
Пример #4
0
        public void Register(Type consumer)
        {
            if (consumer.GetInterfaces().Length == 0 || consumer.GetInterfaces().Any(x => x.GetInterface("IConsumer") != null))
            {
                throw new Exception("Consumer must be implement IConsumer.");
            }

            if (ConsumerList.Any(x => x.GetType() == consumer))
            {
                throw new Exception("Consumer already registered.");
            }

            try
            {
                var consumerObject = (IConsumer)Activator.CreateInstance(consumer);
                ConsumerList.Add(consumerObject);
            }
            catch (Exception e)
            {
                ElasticLogger.Instance.Error(e, $"ConsumerRegistry throw an error : {e.Message}");
            }
        }