diff --git a/OpenAuth.App/ServiceApp/AirportMaintenanceApp.cs b/OpenAuth.App/ServiceApp/AirportMaintenanceApp.cs index 2176515..8c5925e 100644 --- a/OpenAuth.App/ServiceApp/AirportMaintenanceApp.cs +++ b/OpenAuth.App/ServiceApp/AirportMaintenanceApp.cs @@ -1,22 +1,23 @@ -using OpenAuth.App.BaseApp.Base; -using OpenAuth.Repository.Domain; +using DocumentFormat.OpenXml.EMMA; +using DocumentFormat.OpenXml.Math; +using Infrastructure; +using Infrastructure.CloudSdk.minio; +using Infrastructure.Extensions; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Configuration; +using Newtonsoft.Json.Linq; +using NPOI.SS.Formula.Functions; +using OpenAuth.App.BaseApp.Base; +using OpenAuth.App.Interface; +using OpenAuth.App.ServiceApp.Response; using OpenAuth.Repository; +using OpenAuth.Repository.Domain; +using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -using OpenAuth.App.Interface; -using SqlSugar; -using Infrastructure; -using OpenAuth.App.ServiceApp.Response; -using DocumentFormat.OpenXml.EMMA; -using NPOI.SS.Formula.Functions; -using Infrastructure.Extensions; -using DocumentFormat.OpenXml.Math; -using Microsoft.Extensions.Configuration; -using Infrastructure.CloudSdk.minio; -using Microsoft.AspNetCore.Http; namespace OpenAuth.App.ServiceApp { @@ -405,10 +406,47 @@ namespace OpenAuth.App.ServiceApp }; } } - + /// + /// 添加无人机操作日志 + /// + /// + /// + public async Task AddOperationLog(LasaControlOperation info) + { + using (var db = UnitWork.CreateContext()) + { + var flag = await db.LasaControlOperation.InsertAsync(info); + if (db.Commit()) + return true; + else + return false; + } + } + /// + /// 获取无人机操作日志 + /// + /// + /// + /// + /// + public async Task>>> GetOperationLogList(string taskid, int page, int limit) + { + RefAsync totalCount = 0; + using (var db = UnitWork.CreateContext()) + { + var list = await db.LasaControlOperation.AsQueryable() + .Where(x => x.TaskId == taskid) + .OrderBy(x => x.CreateTime, OrderByType.Asc) + .ToPageListAsync(page, limit, totalCount); + return new Response>> + { + Result = new PageInfo> { Items = list, Total = totalCount } + }; + } + } #endregion - public async Task>>> GetMediaFile(string flightId,string taskId, string airId, string device, + public async Task>>> GetMediaFile(string flightId, string taskId, string airId, string device, int? type, string picname, DateTime? startTime, DateTime? endTime, int page, int limit, string parentKey, int? objectKeyExist) { RefAsync totalCount = 0; @@ -511,5 +549,47 @@ namespace OpenAuth.App.ServiceApp await _client.Ado.ExecuteCommandAsync(sql); return new Response { Result = "修改成功!" }; } + + + #region 天气阻飞 + //天气预报和是否有人在操作 + //public async Task> IsCanFly(string sn, string dock, string address = "兰山") + //{ + // var rainThreshforecast = 3; // 天气预报雨量 + // var windSpeedThresh = 12;// 机场设定雨量 + // double weatherWindSpeedforecast = 0; // 天气预报风速 + // double weatherWindSpeedThre = 12;// 机场设定风速 + // switch (dock) + // { + // case "Dock": + // weatherWindSpeedThre = 12; + // break; + // case "Dock 2": + // weatherWindSpeedThre = 8; + // break; + // case "Dock 3": + // weatherWindSpeedThre = 8; + // break; + // } + // using (var httpClient = new HttpClient()) + // { + // var response = await httpClient.GetAsync( + // $"http://v1.yiketianqi.com/api?unescape=1&version=v61&appid=84261622&appsecret=k0WPY4Cx&city={address}"); + // if (response.IsSuccessStatusCode) + // { + // var content = await response.Content.ReadAsStringAsync(); + // var weather = Newtonsoft.Json.JsonConvert.DeserializeObject(content); + // var winMeterStr = + // weather.GetValue("win_meter")?.Value(); //风速 + // if (!string.IsNullOrEmpty(winMeterStr)) + // { + // weatherWindSpeedforecast = int.Parse(winMeterStr.Replace("km/h", "")) / 3.6; + // } + // } + // } + + + //} + #endregion } } \ No newline at end of file diff --git a/OpenAuth.App/ServiceApp/Response/MqttClientResp.cs b/OpenAuth.App/ServiceApp/Response/MqttClientResp.cs index 22603af..4a45b5a 100644 --- a/OpenAuth.App/ServiceApp/Response/MqttClientResp.cs +++ b/OpenAuth.App/ServiceApp/Response/MqttClientResp.cs @@ -25,6 +25,10 @@ namespace OpenAuth.App.ServiceApp.Response /// public DateTime ConnectTime { get; set; } /// + /// 设备sn + /// + public string DeviceSn { get; set; } + /// /// 是否控制 /// public bool IsLock { get; set; } diff --git a/OpenAuth.Repository/Domain/LasaControlOperation.cs b/OpenAuth.Repository/Domain/LasaControlOperation.cs new file mode 100644 index 0000000..2a85006 --- /dev/null +++ b/OpenAuth.Repository/Domain/LasaControlOperation.cs @@ -0,0 +1,54 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OpenAuth.Repository.Domain +{ + [SugarTable("lasa_control_operation")] + public class LasaControlOperation + { + /// + /// Desc:标识 + /// Default: + /// Nullable:False + /// + [SugarColumn(IsPrimaryKey = true)] + public string Id { get; set; } + /// + /// Desc:专题 + /// Default: + /// Nullable:False + /// + public string DeviceSn { get; set; } + + /// + /// Desc:任务id + /// Default: + /// Nullable:False + /// + public string TaskId { get; set; } + + /// + /// Desc:创建时间 + /// Default: + /// Nullable:True + /// + public DateTime CreateTime { get; set; } + /// + /// Desc:创建人 + /// Default: + /// Nullable:True + /// + public long CreateId { get; set; } + + /// + /// Desc:数据 + /// Default: + /// Nullable:True + /// + public string Data { get; set; } + } +} diff --git a/OpenAuth.Repository/SugarDbContext.cs b/OpenAuth.Repository/SugarDbContext.cs index 0a2d3ee..c5f36e3 100644 --- a/OpenAuth.Repository/SugarDbContext.cs +++ b/OpenAuth.Repository/SugarDbContext.cs @@ -87,6 +87,7 @@ namespace OpenAuth.Repository public SugarRepositiry LasaAiAchievement { get; set; } public SugarRepositiry LasaAiAchievementDetail { get; set; } public SugarRepositiry Lasa_ChangeDetection { get; set; } + public SugarRepositiry LasaControlOperation { get; set; } public SugarRepositiry DbfineInfo { get; set; } public SugarRepositiry SysOpenJob { get; set; } diff --git a/OpenAuth.WebApi/Controllers/ServiceControllers/AirportMaintenanceController.cs b/OpenAuth.WebApi/Controllers/ServiceControllers/AirportMaintenanceController.cs index 237f1e5..ee71166 100644 --- a/OpenAuth.WebApi/Controllers/ServiceControllers/AirportMaintenanceController.cs +++ b/OpenAuth.WebApi/Controllers/ServiceControllers/AirportMaintenanceController.cs @@ -632,6 +632,115 @@ namespace OpenAuth.WebApi.Controllers.ServiceControllers return result; } + + /// + /// 添加修改mqtt客户端信息 设备为主 + /// + /// + /// + [HttpPost] + [AllowAnonymous] + public async Task> AddOrUpdateRedisDevice(MqttClientResp info) + { + var result = new Response(); + try + { + var clientKey = $"client:{info.DeviceSn}"; + string lockSetKey = "locked_devices"; + + // 查询所有锁定用户 + var existingLocked = await _cache.SetMembersAsync(lockSetKey); + + // 如果设备锁定,并且锁定的用户不是当前用户,则拒绝 + if (existingLocked.Length > 0 && info.IsLock == true) + { + bool isCurrentUserLocked = existingLocked.Any(u => u == info.DeviceSn); + if (isCurrentUserLocked) + { + result.Code = 400; + result.Message = "已有其他用户处于锁定状态,不能添加新的锁定用户。"; + result.Result = false; + return result; + } + } + + // 存客户端信息 + _cache.HashSetAsync(clientKey, new HashEntry[] + { + new("ClientId", info.ClientId), + new("UserId", info.UserId), + new("UserName", info.UserName), + new("ConnectTime", info.ConnectTime.ToString("O")), + new("IsLock", info.IsLock ? "true" : "false") + }); + + if (info.IsLock) + { + await _cache.SetAddAsync(lockSetKey, info.DeviceSn); + } + else + { + await _cache.SetRemoveAsync(lockSetKey, info.DeviceSn); + } + + result.Result = true; + } + catch (Exception ex) + { + result.Code = 500; + result.Message = ex.Message; + } + + return result; + } + /// + /// 获取当前用户mqtt客户端信息 + /// + /// + /// + [HttpGet] + [AllowAnonymous] + public async Task> GetRedisDevice(string id) + { + var result = new Response(); + try + { + result.Result = ParseClient(await _cache.HashGetAllAsync($"client:{id}")); + } + catch (Exception ex) + { + result.Code = 500; + result.Message = ex.Message; + } + + return result; + } + /// + /// 获取所有锁定的用户客户端信息 + /// + /// + [HttpGet] + [AllowAnonymous] + public async Task> GetLockedDeviceClients() + { + var userIds = await _cache.SetMembersAsync("locked_devices"); + + var result = new List(); + + foreach (var userId in userIds) + { + var entries = await _cache.HashGetAllAsync($"client:{userId}"); + if (entries.Length > 0) + result.Add(ParseClient(entries)); + } + + return result; + } + //只有天气阻飞 + //public async Task> IsCanFly(string address,string sn,string dock) + //{ + + //} #endregion #region 获取告警信息 @@ -695,19 +804,42 @@ namespace OpenAuth.WebApi.Controllers.ServiceControllers return result; } + /// + /// 根据任务获取无人机操作日志 + /// + /// 任务id + /// + /// + /// + [HttpGet] + [AllowAnonymous] + public async Task>>> GetOperationLogList(string taskid, int page, int limit) + { + var result = new Response>>(); + try + { + result = await _app.GetOperationLogList(taskid, page, limit); + } + catch (Exception ex) + { + result.Code = 500; + result.Message = ex.Message; + } + return result; + } #endregion [HttpGet] - public async Task>>> GetMediaFile(string flightId,string taskId, string airId, + public async Task>>> GetMediaFile(string flightId, string taskId, string airId, string device, int? type, string picname, DateTime startTime, DateTime endTime, int page, int limit, string parentKey, int? objectKeyExist) { var result = new Response>>(); try { - result = await _app.GetMediaFile(flightId,taskId, airId, device,type, picname, startTime, endTime, page, limit, + result = await _app.GetMediaFile(flightId, taskId, airId, device, type, picname, startTime, endTime, page, limit, parentKey, objectKeyExist); } catch (Exception ex) diff --git a/OpenAuth.WebApi/Model/mqtt/MqttHostedService.cs b/OpenAuth.WebApi/Model/mqtt/MqttHostedService.cs index a1fa26f..d193ce8 100644 --- a/OpenAuth.WebApi/Model/mqtt/MqttHostedService.cs +++ b/OpenAuth.WebApi/Model/mqtt/MqttHostedService.cs @@ -81,7 +81,8 @@ namespace OpenAuth.WebApi.Model.mqtt "thing/product/+/events", "thing/product/+/requests", "thing/product/+/osd", - "sys/product/+/status" + "sys/product/+/status", + "thing/product/+/control-operation" }; await _mqttCenter.SubscribeAsync(topicList); } diff --git a/OpenAuth.WebApi/Model/mqtt/ThingOperationHandler.cs b/OpenAuth.WebApi/Model/mqtt/ThingOperationHandler.cs new file mode 100644 index 0000000..d951ce5 --- /dev/null +++ b/OpenAuth.WebApi/Model/mqtt/ThingOperationHandler.cs @@ -0,0 +1,39 @@ +using Infrastructure.Cache; +using Infrastructure.CloudSdk.mqttmessagecenter; +using OpenAuth.App.ServiceApp; +using OpenAuth.Repository.Domain; +using System.Text.Json.Nodes; + +namespace OpenAuth.WebApi.Model.mqtt +{ + public class ThingOperationHandler : IMqttMessageHandler + { + AirportMaintenanceApp _app; + private readonly ICacheContext _cache; + public ThingOperationHandler(AirportMaintenanceApp app, ICacheContext cache) + { + _app = app; + _cache = cache; + } + public bool CanHandle(string topic) + { + return topic.Contains("/control-operation"); + } + public async Task HandleAsync(string topic, string payload) + { + var root = JsonNode.Parse(payload)?.AsObject(); + await _app.AddOperationLog(new LasaControlOperation + { + Id = Guid.NewGuid().ToString(), + DeviceSn = root["DeviceSn"]?.ToString() ?? "", + TaskId = root["TaskId"]?.ToString() ?? "", + CreateTime = DateTime.Now, + CreateId = long.Parse((root["CreateId"]?.ToString() ?? "0")), + Data = root["Data"]?.ToString() ?? "", + }); + + // 自定义处理逻辑 + //return Task.CompletedTask; + } + } +} diff --git a/OpenAuth.WebApi/Startup.cs b/OpenAuth.WebApi/Startup.cs index 4fa0bf4..bfc1577 100644 --- a/OpenAuth.WebApi/Startup.cs +++ b/OpenAuth.WebApi/Startup.cs @@ -181,7 +181,7 @@ namespace OpenAuth.WebApi services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); - //services.AddSingleton(); + services.AddSingleton(); services.AddSingleton(); services.AddHostedService(); #endregion @@ -344,7 +344,7 @@ namespace OpenAuth.WebApi #endregion #region Quartz - services.AddHostedService(); + //services.AddHostedService(); #endregion