예제 #1
0
 public CheckDevices(ILogger <CheckDevices> logger, IServiceScopeFactory scopeFactor, IMqttServerEx serverEx
                     , IOptions <AppSettings> options)
 {
     _mcsetting   = options.Value.MqttClient;
     _logger      = logger;
     _scopeFactor = scopeFactor;
     _serverEx    = serverEx;
 }
예제 #2
0
파일: PushData.cs 프로젝트: neoayi/IoTSharp
 public PushData(ILogger <PushData> logger, IServiceScopeFactory scopeFactor
                 , IOptions <AppSettings> options, IMsgQueue queue
                 )
 {
     _mcsetting = options.Value.MqttClient;
     _logger    = logger;
     scope      = scopeFactor.CreateScope();
     _queue     = queue;
 }
예제 #3
0
 public MQTTServerHandler(ILogger <MQTTServerHandler> logger, IServiceScopeFactory scopeFactor, IMqttServerEx serverEx
                          , IOptions <AppSettings> options
                          )
 {
     _mcsetting = options.Value.MqttClient;
     _logger    = logger;
     scope      = scopeFactor.CreateScope();
     _dbContext = scope.ServiceProvider.GetRequiredService <ApplicationDbContext>();
     _serverEx  = serverEx;
 }
예제 #4
0
 public CheckDevices(ILogger <CheckDevices> logger, IServiceScopeFactory scopeFactor, IMqttServerEx serverEx
                     , IOptions <AppSettings> options, IEasyCachingProviderFactory factory)
 {
     _mcsetting   = options.Value.MqttClient;
     _logger      = logger;
     _scopeFactor = scopeFactor;
     _factory     = factory;
     _serverEx    = serverEx;
     _device      = _factory.GetCachingProvider("iotsharp");
 }
예제 #5
0
 public MQTTServerHandler(ILogger <MQTTServerHandler> logger, IServiceScopeFactory scopeFactor, IMqttServerEx serverEx
                          , IOptions <AppSettings> options, ICapPublisher queue, IEasyCachingProviderFactory factory
                          )
 {
     _mcsetting   = options.Value.MqttClient;
     _logger      = logger;
     _scopeFactor = scopeFactor;
     _factory     = factory;
     _serverEx    = serverEx;
     _queue       = queue;
 }
예제 #6
0
 public MQTTServerHandler(ILogger <MQTTServerHandler> logger, IServiceScopeFactory scopeFactor, MqttServer serverEx
                          , IOptions <AppSettings> options, ICapPublisher queue, IEasyCachingProviderFactory factory, FlowRuleProcessor flowRuleProcessor
                          )
 {
     _mcsetting         = options.Value.MqttClient;
     _settings          = options.Value;
     _logger            = logger;
     _scopeFactor       = scopeFactor;
     _factory           = factory;
     _serverEx          = serverEx;
     _queue             = queue;
     _flowRuleProcessor = flowRuleProcessor;
     _caching           = factory.GetCachingProvider("iotsharp");
 }
예제 #7
0
        public MQTTServerHandler(ILogger <MQTTServerHandler> logger, IServiceScopeFactory scopeFactor, IMqttServerEx serverEx, DiagnosticsService diagnosticsService,
                                 RuntimeStatusHandler systemStatusService, IOptions <AppSettings> options
                                 )
        {
            _mcsetting      = options.Value.MqttClient;
            _logger         = logger;
            scope           = scopeFactor.CreateScope();
            _dbContext      = scope.ServiceProvider.GetRequiredService <ApplicationDbContext>();
            _serverEx       = serverEx;
            InboundCounter  = diagnosticsService.CreateOperationsPerSecondCounter("mqtt.inbound_rate");
            OutboundCounter = diagnosticsService.CreateOperationsPerSecondCounter("mqtt.outbound_rate");

            systemStatusService.Set("mqtt.subscribers_count", () => Subscribers.Count);
            systemStatusService.Set("mqtt.incoming_messages_count", () => IncomingMessages.Count);
            systemStatusService.Set("mqtt.inbound_rate", () => InboundCounter.Count);
            systemStatusService.Set("mqtt.outbound_rate", () => OutboundCounter.Count);
            systemStatusService.Set("mqtt.connected_clients_count", () => serverEx.GetClientStatusAsync().GetAwaiter().GetResult().Count);
        }
예제 #8
0
        static void Main()
        {
            Debug.WriteLine("Setup Ethernet");
            SetupEnc28_MicroBus(SC20100.SpiBus.Spi3, SC20100.GpioPin.PD14, SC20100.GpioPin.PA8, SC20100.GpioPin.PD15, SC20100.GpioPin.Id);
            Thread.Sleep(50);

            Debug.WriteLine("Setup Date and Time from Internet");
            Debug.WriteLine("===================================");

            while
            ((DateTime.UtcNow.ToString("yyyy.MM.dd") == "1900.01.01") || (DateTime.UtcNow.ToString("yyyy.MM.dd") == "2017.01.01"))
            {
                SystemTime.SetTime(GetNetworkTime(2));
                Thread.Sleep(15000);
                // Debug.WriteLine("Try again after 15 sec");
            }

            Debug.WriteLine("Set Proper Time now is :" + DateTime.UtcNow.ToString("yyyy.MM.dd HH:mm:ss"));
            Debug.WriteLine("===================================");

            //test.mosquitto.org
            var caCertificate = new X509Certificate(Certificate());

            var mqttHost = "test.mosquitto.org";
            //var mqttHost = "broker.hivemq.com";

            var mqttPort = 1883;//1883 no ssl - 8883 with ssl
            var deviceId = "tiny_clr_device_001";

            string username = null;
            string password = null;

            string topic1 = "tinyclr/temp";
            string topic2 = "tinyclr/humidity";

            string subscribe1 = "tinyclr/light1";
            string subscribe2 = "tinyclr/light2";

            var clientSetting = new MqttClientSetting
            {
                BrokerName = mqttHost,
                BrokerPort = mqttPort,
                //  ClientCertificate = null,
                //  CaCertificate = caCertificate,
                //  SslProtocol = System.Security.Authentication.SslProtocols.Tls12
            };

            try
            {
                client = new Mqtt(clientSetting);
            }
            catch (Exception e)
            {
                Debug.WriteLine("Failure one");
                Debug.WriteLine(e.Message.ToString());
            }


            var connectSetting = new MqttConnectionSetting
            {
                ClientId         = deviceId,
                UserName         = username,
                Password         = password,
                KeepAliveTimeout = 60,
                LastWillQos      = QoSLevel.MostOnce

                                   /*
                                    * QOS(0) = MostOnce
                                    * QOS(1) = LeastOnce
                                    * QOS(2) = ExactlyOnce
                                    */
            };

            //Connect to host


            var returnCode = client.Connect(connectSetting);
            int packetId   = 1;

            client.PublishReceivedChanged += Client_PublishReceivedChanged;

            client.SubscribedChanged += Client_SubscribedChanged;



            // Subscribe to a topic
            client.Subscribe(new string[] { subscribe1, subscribe2 }, new QoSLevel[] { QoSLevel.MostOnce, QoSLevel.MostOnce }, (ushort)packetId++);  // there was no QoS Level for the second topic in your code


            while (true)
            {
                Debug.WriteLine("------------------------------");
                Debug.WriteLine(DateTime.UtcNow.ToString(">> yyyy.MM.dd HH:mm:ss.fff"));

                //Publish a topic
                client.Publish(topic1, GetRandomTemperature(), QoSLevel.MostOnce, false, (ushort)packetId);
                packetId++;



                //Publish a topic
                client.Publish(topic2, GetRandomHumidity(), QoSLevel.MostOnce, false, (ushort)packetId);
                packetId++;


                client.PublishedChanged += (a, b, c) =>
                {
                    Debug.WriteLine("Published " + a.ToString());
                };

                Debug.WriteLine("Wait next 15 sec");
                Thread.Sleep(15000);
            }
        }