diff --git a/IoTSharp/AppSettings.cs b/IoTSharp/AppSettings.cs index 369653a4953946d620bc82b66fee2c3d25894638..daa8990e035422e8ac10be64ea109a12337db9e5 100644 --- a/IoTSharp/AppSettings.cs +++ b/IoTSharp/AppSettings.cs @@ -29,8 +29,8 @@ namespace IoTSharp /// built-in or IP、HostName /// public string MqttBroker { get; set; } = "built-in"; - public string UserName { get; set; } = "root"; - public string Password { get; set; } = "kissme"; + public string UserName { get; set; } = ""; + public string Password { get; set; } = ""; public int Port { get; set; } = 1883; } public class MqttBrokerSetting diff --git a/IoTSharp/Controllers/SampleDataController.cs b/IoTSharp/Controllers/SampleDataController.cs deleted file mode 100644 index b9e4121987c97699977fbe7b77da74c6c7712301..0000000000000000000000000000000000000000 --- a/IoTSharp/Controllers/SampleDataController.cs +++ /dev/null @@ -1,44 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc; - -namespace IoTSharp.Controllers -{ - [Route("api/[controller]")] - public class SampleDataController : Controller - { - private static string[] Summaries = new[] - { - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" - }; - - [HttpGet("[action]")] - public IEnumerable WeatherForecasts() - { - var rng = new Random(); - return Enumerable.Range(1, 5).Select(index => new WeatherForecast - { - DateFormatted = DateTime.Now.AddDays(index).ToString("d"), - TemperatureC = rng.Next(-20, 55), - Summary = Summaries[rng.Next(Summaries.Length)] - }); - } - - public class WeatherForecast - { - public string DateFormatted { get; set; } - public int TemperatureC { get; set; } - public string Summary { get; set; } - - public int TemperatureF - { - get - { - return 32 + (int)(TemperatureC / 0.5556); - } - } - } - } -} diff --git a/IoTSharp/Extensions/MqttExtension.cs b/IoTSharp/Extensions/MqttExtension.cs index b791ba68aea961c8876bf97b0bf477ce62502592..c707c0289a852d0381ee33932bb0bc8463b0296f 100644 --- a/IoTSharp/Extensions/MqttExtension.cs +++ b/IoTSharp/Extensions/MqttExtension.cs @@ -142,6 +142,6 @@ namespace IoTSharp }; } - + } } diff --git a/IoTSharp/Handlers/MqttEventsHandler.cs b/IoTSharp/Handlers/MqttEventsHandler.cs index 6fecfe2ae5cd1df38f67b3ee63b835cc21368ff0..040319deb063f3806dccc9f557ac6d9d92670a42 100644 --- a/IoTSharp/Handlers/MqttEventsHandler.cs +++ b/IoTSharp/Handlers/MqttEventsHandler.cs @@ -17,11 +17,13 @@ namespace IoTSharp.Handlers public class MqttEventsHandler { ILogger Logger { get; set; } - ApplicationDbContext _dbContext; - public MqttEventsHandler(ILogger _logger, ApplicationDbContext dbContext) + readonly ApplicationDbContext _dbContext; + readonly IMqttServerEx _serverEx; + public MqttEventsHandler(ILogger _logger, ApplicationDbContext dbContext,IMqttServerEx serverEx ) { Logger = _logger; _dbContext = dbContext; + _serverEx = serverEx; } static long clients = 0; @@ -29,7 +31,7 @@ namespace IoTSharp.Handlers { Logger.LogInformation($"Client [{e.ClientId}] connected"); clients++; - Task.Run(() => ((IMqttServer)sender).PublishAsync("$SYS/broker/clients/total", clients.ToString())); + Task.Run(() => _serverEx.PublishAsync("$SYS/broker/clients/total", clients.ToString())); } static DateTime uptime = DateTime.MinValue; internal void Server_Started(object sender, EventArgs e) @@ -50,7 +52,7 @@ namespace IoTSharp.Handlers if (!lstTopics.ContainsKey(e.ApplicationMessage.Topic)) { lstTopics.Add(e.ApplicationMessage.Topic, 1); - Task.Run(() => ((IMqttServer)sender).PublishAsync("$SYS/broker/subscriptions/count", lstTopics.Count.ToString())); + Task.Run(() => _serverEx.PublishAsync("$SYS/broker/subscriptions/count", lstTopics.Count.ToString())); } else { @@ -68,17 +70,17 @@ namespace IoTSharp.Handlers { var mename = typeof(MqttEventsHandler).Assembly.GetName(); var mqttnet = typeof(MqttClientSubscribedTopicEventArgs).Assembly.GetName(); - Task.Run(() => ((IMqttServer)sender).PublishAsync("$SYS/broker/version", $"{mename.Name}V{mename.Version.ToString()},{mqttnet.Name}.{mqttnet.Version.ToString()}")); + Task.Run(() => _serverEx.PublishAsync("$SYS/broker/version", $"{mename.Name}V{mename.Version.ToString()},{mqttnet.Name}.{mqttnet.Version.ToString()}")); } else if (e.TopicFilter.Topic.StartsWith("$SYS/broker/uptime")) { - Task.Run(() => ((IMqttServer)sender).PublishAsync("$SYS/broker/uptime", uptime.ToString())); + Task.Run(() => _serverEx.PublishAsync("$SYS/broker/uptime", uptime.ToString())); } } else { Subscribed++; - Task.Run(() => ((IMqttServer)sender).PublishAsync("$SYS/broker/subscriptions/count", Subscribed.ToString())); + Task.Run(() => _serverEx.PublishAsync("$SYS/broker/subscriptions/count", Subscribed.ToString())); } @@ -90,7 +92,7 @@ namespace IoTSharp.Handlers if (!e.TopicFilter.StartsWith("$SYS/")) { Subscribed--; - Task.Run(() => ((IMqttServer)sender).PublishAsync("$SYS/broker/subscriptions/count", Subscribed.ToString())); + Task.Run(() => _serverEx.PublishAsync("$SYS/broker/subscriptions/count", Subscribed.ToString())); } } public static string MD5Sum(string text) => BitConverter.ToString(MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(text))).Replace("-", ""); diff --git a/IoTSharp/IoTSharp.csproj b/IoTSharp/IoTSharp.csproj index ccfb1d6f7ac1bbda1c94385207ba24392d972bdf..04029513d5e19ebbe0ab15eb454eef269a9b7d76 100644 --- a/IoTSharp/IoTSharp.csproj +++ b/IoTSharp/IoTSharp.csproj @@ -76,6 +76,8 @@ + +