diff --git a/projects/Application/Domain/Statistic.cs b/projects/Application/Domain/Statistic.cs index 054017de..fb6f37cd 100644 --- a/projects/Application/Domain/Statistic.cs +++ b/projects/Application/Domain/Statistic.cs @@ -21,22 +21,26 @@ namespace Application.Domain.Entities public void SetValue(object value) { this.UpdateAt = DateTime.UtcNow; - if (value is Int32) + if (this.Type == StatisticType.Int) { this.IntValue = Convert.ToInt32(value); this.Value = this.IntValue.ToString(); } - else if (value is Double) + else if (this.Type == StatisticType.Double) { this.DoubleValue = Convert.ToDouble(value); this.Value = this.DoubleValue.Value.ToString("f2"); } - else if (value is DateTime) + else if (this.Type == StatisticType.DateTime) { var timestamp = Convert.ToInt64(value, CultureInfo.InvariantCulture); this.DateTimeValue = DateTimeOffset.FromUnixTimeMilliseconds(timestamp).DateTime; this.Value = Convert.ToString(value); } + else + { + this.Value = value.ToString(); + } } } } \ No newline at end of file diff --git a/projects/IoTNode/DeviceServices/FBee/FBeeService.cs b/projects/IoTNode/DeviceServices/FBee/FBeeService.cs index 02e258ea..8790ff0c 100644 --- a/projects/IoTNode/DeviceServices/FBee/FBeeService.cs +++ b/projects/IoTNode/DeviceServices/FBee/FBeeService.cs @@ -604,7 +604,8 @@ namespace IoTNode.DeviceServices.FBee } else { - this.UpdateIoTData(device.Id, DataKeys.CurtainState, switchState); + var key = device.Name.Contains("电机") ? DataKeys.CurtainState : DataKeys.PowerState; + this.UpdateIoTData(device.Id, key, switchState); } } else diff --git a/projects/Platform/Controllers/HomeController.cs b/projects/Platform/Controllers/HomeController.cs index cf3e94b0..f4911154 100644 --- a/projects/Platform/Controllers/HomeController.cs +++ b/projects/Platform/Controllers/HomeController.cs @@ -25,14 +25,21 @@ namespace Platform.Controllers private readonly IRepository _organRepo; private readonly IRepository _buildingRepo; + private readonly IRepository _statisticRepo; - public HomeController(IUserService userService, IRepository userRepo, IRepository areaRepo, IRepository organRepo, IRepository buildingRepo) + public HomeController(IUserService userService, + IRepository userRepo, + IRepository areaRepo, + IRepository organRepo, + IRepository buildingRepo, + IRepository statisticRepo) { this._useService = userService; this._userRepo = userRepo; this._areaRepo = areaRepo; this._organRepo = organRepo; this._buildingRepo = buildingRepo; + this._statisticRepo = statisticRepo; } public IActionResult Index() @@ -54,12 +61,32 @@ namespace Platform.Controllers .Where(o => o.Parent == null) .ToList(); var model = new { - Organ= organ + Organ= organ, + MaxLight = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{userOrganId}]MaxLight").FirstOrDefault()?.Value, + MinLight = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{userOrganId}]MinLight").FirstOrDefault()?.Value, + MaxTemperature = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{userOrganId}]MaxTemperature").FirstOrDefault()?.Value, + MinTemperature = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{userOrganId}]MinTemperature").FirstOrDefault()?.Value, + MaxHumidity = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{userOrganId}]MaxHumidity").FirstOrDefault()?.Value, + MinHumidity = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{userOrganId}]MinHumidity").FirstOrDefault()?.Value, + DeviceOpenCount = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{userOrganId}]DeviceOpenCount").FirstOrDefault()?.Value, + DeviceCloseCount = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{userOrganId}]DeviceCloseCount").FirstOrDefault()?.Value, }; return Json(model); } - + public IActionResult GetBuilding(Guid id) + { + var model = new + { + Building = this._buildingRepo.ReadOnlyTable() + .Include(o=>o.Scenes) + .Include(o => o.IoTGateways) + .ThenInclude(o => o.Devices).ThenInclude(o => o.Data) + .ToList() + + }; + return Json(model); + } public IActionResult Building(HomeModel model) { diff --git a/projects/Platform/EventHandlers/UpateStatisticEventHandler.cs b/projects/Platform/EventHandlers/UpateStatisticEventHandler.cs index 9a67201f..35b4f150 100644 --- a/projects/Platform/EventHandlers/UpateStatisticEventHandler.cs +++ b/projects/Platform/EventHandlers/UpateStatisticEventHandler.cs @@ -124,6 +124,11 @@ namespace Platform.EventHandlers UpdateOrganDataValue(organId, DataKeys.Temperature.GetName(), StatisticType.Double); UpdateOrganDataValue(organId, DataKeys.Humidity.GetName(), StatisticType.Double); //用电器 + UpdateOrganPowerState(organId); + } + + private void UpdateOrganPowerState(Guid organId) + { var key = DataKeys.PowerState.GetName(); var query = this._dataRepo.ReadOnlyTable() .Where(o => o.IoTDevice.IoTGateway.BuildingId.HasValue) @@ -133,7 +138,7 @@ namespace Platform.EventHandlers var close = query.Count(o => o.IntValue == 0); var open = total - close; UpdateValue($"[{organId}]", "DeviceOpenCount", open, StatisticType.Int); - UpdateValue($"[{organId}]", "DeviceCloseCount", open, StatisticType.Int); + UpdateValue($"[{organId}]", "DeviceCloseCount", close, StatisticType.Int); } public void UpdateBuildingData(Guid organId, Guid buildingId) @@ -147,6 +152,11 @@ namespace Platform.EventHandlers UpdateBuildingDataValue(organId, buildingId, DataKeys.Temperature.GetName(), StatisticType.Double); UpdateBuildingDataValue(organId, buildingId, DataKeys.Humidity.GetName(), StatisticType.Double); //用电器 + UpdateBuildingPowerState(organId, buildingId); + } + + private void UpdateBuildingPowerState(Guid organId, Guid buildingId) + { var key = DataKeys.PowerState.GetName(); var query = this._dataRepo.ReadOnlyTable() .Where(o => o.IoTDevice.IoTGateway.BuildingId == buildingId) @@ -155,7 +165,7 @@ namespace Platform.EventHandlers var close = query.Count(o => o.IntValue == 0); var open = total - close; UpdateValue($"[{organId}][{buildingId}]", "DeviceOpenCount", open, StatisticType.Int); - UpdateValue($"[{organId}][{buildingId}]", "DeviceCloseCount", open, StatisticType.Int); + UpdateValue($"[{organId}][{buildingId}]", "DeviceCloseCount", close, StatisticType.Int); } private void UpdateOrganDataValue(Guid organId, string key, StatisticType type) @@ -217,21 +227,42 @@ namespace Platform.EventHandlers private void IoTDataEventHandle(BaseEvent message) { - var deviceId = message.Data.IoTDeviceId; - var buildingId = this._deviceRepo.ReadOnlyTable() - .Where(o => o.Id == deviceId) - .Where(o => o.IoTGateway.BuildingId.HasValue) - .Select(o => o.IoTGateway.BuildingId) - .FirstOrDefault(); - if (buildingId != null) + + if (message.Data.Key == DataKeys.Light.GetName() || + message.Data.Key == DataKeys.Temperature.GetName() || + message.Data.Key == DataKeys.Humidity.GetName() || + message.Data.Key == DataKeys.PowerState.GetName()) { - var organId = this.GetOrganId(buildingId.Value); - UpdateOrganDataValue(organId, DataKeys.Light.GetName(), StatisticType.Int); - UpdateOrganDataValue(organId, DataKeys.Temperature.GetName(), StatisticType.Double); - UpdateOrganDataValue(organId, DataKeys.Humidity.GetName(), StatisticType.Double); - UpdateBuildingDataValue(organId, buildingId.Value, DataKeys.Light.GetName(), StatisticType.Int); - UpdateBuildingDataValue(organId, buildingId.Value, DataKeys.Temperature.GetName(), StatisticType.Double); - UpdateBuildingDataValue(organId, buildingId.Value, DataKeys.Humidity.GetName(), StatisticType.Double); + var deviceId = message.Data.IoTDeviceId; + var buildingId = this._deviceRepo.ReadOnlyTable() + .Where(o => o.Id == deviceId) + .Where(o => o.IoTGateway.BuildingId.HasValue) + .Select(o => o.IoTGateway.BuildingId) + .FirstOrDefault(); + if (buildingId != null) + { + var organId = this.GetOrganId(buildingId.Value); + if (message.Data.Key == DataKeys.Light.GetName()) + { + UpdateOrganDataValue(organId, DataKeys.Light.GetName(), StatisticType.Int); + UpdateBuildingDataValue(organId, buildingId.Value, DataKeys.Light.GetName(), StatisticType.Int); + } + else if(message.Data.Key == DataKeys.Temperature.GetName()) + { + UpdateOrganDataValue(organId, DataKeys.Temperature.GetName(), StatisticType.Double); + UpdateBuildingDataValue(organId, buildingId.Value, DataKeys.Temperature.GetName(), StatisticType.Double); + } + else if(message.Data.Key == DataKeys.Humidity.GetName()) + { + UpdateOrganDataValue(organId, DataKeys.Humidity.GetName(), StatisticType.Double); + UpdateBuildingDataValue(organId, buildingId.Value, DataKeys.Humidity.GetName(), StatisticType.Double); + } + else if(message.Data.Key == DataKeys.PowerState.GetName()) + { + UpdateOrganPowerState(organId); + UpdateBuildingPowerState(organId, buildingId.Value); + } + } } } diff --git a/projects/Platform/wwwroot/components/views/home/index.html b/projects/Platform/wwwroot/components/views/home/index.html index 48372548..7ecc9f82 100644 --- a/projects/Platform/wwwroot/components/views/home/index.html +++ b/projects/Platform/wwwroot/components/views/home/index.html @@ -1,7 +1,7 @@