| | 1 | | using LGDXRobotCloud.API.Exceptions; |
| | 2 | | using LGDXRobotCloud.API.Services.Administration; |
| | 3 | | using LGDXRobotCloud.Data.DbContexts; |
| | 4 | | using LGDXRobotCloud.Data.Entities; |
| | 5 | | using LGDXRobotCloud.Data.Models.Business.Administration; |
| | 6 | | using LGDXRobotCloud.Data.Models.Business.Automation; |
| | 7 | | using LGDXRobotCloud.Data.Models.Business.Navigation; |
| | 8 | | using LGDXRobotCloud.Utilities.Enums; |
| | 9 | | using LGDXRobotCloud.Utilities.Helpers; |
| | 10 | | using Microsoft.EntityFrameworkCore; |
| | 11 | | using Microsoft.Extensions.Caching.Memory; |
| | 12 | |
|
| | 13 | | namespace LGDXRobotCloud.API.Services.Navigation; |
| | 14 | |
|
| | 15 | | public interface IRobotService |
| | 16 | | { |
| | 17 | | Task<(IEnumerable<RobotListBusinessModel>, PaginationHelper)> GetRobotsAsync(int? realmId, string? name, int pageNumbe |
| | 18 | | Task<RobotBusinessModel> GetRobotAsync(Guid robotId); |
| | 19 | | Task<RobotCreateResponseBusinessModel> CreateRobotAsync(RobotCreateBusinessModel robotCreateBusinessModel); |
| | 20 | | Task<bool> UpdateRobotAsync(Guid id, RobotUpdateBusinessModel robotUpdateDtoBusinessModel); |
| | 21 | | Task<RobotChassisInfoBusinessModel?> GetRobotChassisInfoAsync(Guid robotId); |
| | 22 | | Task<bool> UpdateRobotChassisInfoAsync(Guid id, RobotChassisInfoUpdateBusinessModel robotChassisInfoUpdateBusinessMode |
| | 23 | | Task<bool> TestDeleteRobotAsync(Guid id); |
| | 24 | | Task<bool> DeleteRobotAsync(Guid id); |
| | 25 | |
|
| | 26 | | Task<RobotSystemInfoBusinessModel?> GetRobotSystemInfoAsync(Guid robotId); |
| | 27 | | Task<bool> CreateRobotSystemInfoAsync(Guid robotId, RobotSystemInfoCreateBusinessModel robotSystemInfoCreateBusinessMo |
| | 28 | | Task<bool> UpdateRobotSystemInfoAsync(Guid robotId, RobotSystemInfoUpdateBusinessModel robotSystemInfoUpdateBusinessMo |
| | 29 | |
|
| | 30 | | Task<IEnumerable<RobotSearchBusinessModel>> SearchRobotsAsync(int realmId, string? name, Guid? robotId); |
| | 31 | |
|
| | 32 | | Task<int?> GetRobotRealmIdAsync(Guid robotId); |
| | 33 | | Task<bool> GetRobotIsRealtimeExchange(Guid robotId); |
| | 34 | | } |
| | 35 | |
|
| 20 | 36 | | public class RobotService( |
| 20 | 37 | | IActivityLogService activityLogService, |
| 20 | 38 | | IMemoryCache memoryCache, |
| 20 | 39 | | IRobotCertificateService robotCertificateService, |
| 20 | 40 | | LgdxContext context |
| 20 | 41 | | ) : IRobotService |
| | 42 | | { |
| 20 | 43 | | private readonly IActivityLogService _activityLogService = activityLogService ?? throw new ArgumentNullException(nameo |
| 20 | 44 | | private readonly IMemoryCache _memoryCache = memoryCache ?? throw new ArgumentNullException(nameof(memoryCache)); |
| 20 | 45 | | private readonly IRobotCertificateService _robotCertificateService = robotCertificateService ?? throw new ArgumentNull |
| 20 | 46 | | private readonly LgdxContext _context = context ?? throw new ArgumentNullException(nameof(context)); |
| | 47 | |
|
| | 48 | | public async Task<(IEnumerable<RobotListBusinessModel>, PaginationHelper)> GetRobotsAsync(int? realmId, string? name, |
| 4 | 49 | | { |
| 4 | 50 | | var query = _context.Robots as IQueryable<Robot>; |
| 4 | 51 | | if (realmId != null) |
| 4 | 52 | | { |
| 4 | 53 | | query = query.Where(r => r.RealmId == realmId); |
| 4 | 54 | | } |
| 4 | 55 | | if (!string.IsNullOrWhiteSpace(name)) |
| 3 | 56 | | { |
| 3 | 57 | | name = name.Trim(); |
| 3 | 58 | | query = query.Where(r => r.Name.ToLower().Contains(name.ToLower())); |
| 3 | 59 | | } |
| 4 | 60 | | var itemCount = await query.CountAsync(); |
| 4 | 61 | | var PaginationHelper = new PaginationHelper(itemCount, pageNumber, pageSize); |
| 4 | 62 | | var robots = await query.AsNoTracking() |
| 4 | 63 | | .OrderBy(r => r.Id) |
| 4 | 64 | | .Skip(pageSize * (pageNumber - 1)) |
| 4 | 65 | | .Take(pageSize) |
| 4 | 66 | | .Select(r => new RobotListBusinessModel { |
| 4 | 67 | | Id = r.Id, |
| 4 | 68 | | Name = r.Name, |
| 4 | 69 | | RealmId = r.RealmId, |
| 4 | 70 | | RealmName = r.Realm.Name, |
| 4 | 71 | | }) |
| 4 | 72 | | .AsSplitQuery() |
| 4 | 73 | | .ToListAsync(); |
| 4 | 74 | | return (robots, PaginationHelper); |
| 4 | 75 | | } |
| | 76 | |
|
| | 77 | | public async Task<RobotBusinessModel> GetRobotAsync(Guid robotId) |
| 2 | 78 | | { |
| 2 | 79 | | return await _context.Robots.AsNoTracking() |
| 2 | 80 | | .Where(r => r.Id == robotId) |
| 2 | 81 | | .Select(r => new RobotBusinessModel { |
| 2 | 82 | | Id = r.Id, |
| 2 | 83 | | Name = r.Name, |
| 2 | 84 | | RealmId = r.RealmId, |
| 2 | 85 | | RealmName = r.Realm.Name, |
| 2 | 86 | | IsRealtimeExchange = r.IsRealtimeExchange, |
| 2 | 87 | | IsProtectingHardwareSerialNumber = r.IsProtectingHardwareSerialNumber, |
| 2 | 88 | | RobotCertificate = new RobotCertificateBusinessModel { |
| 2 | 89 | | Id = r.RobotCertificate.Id, |
| 2 | 90 | | RobotId = r.Id, |
| 2 | 91 | | RobotName = r.Name, |
| 2 | 92 | | Thumbprint = r.RobotCertificate.Thumbprint, |
| 2 | 93 | | ThumbprintBackup = r.RobotCertificate.ThumbprintBackup, |
| 2 | 94 | | NotBefore = r.RobotCertificate.NotBefore, |
| 2 | 95 | | NotAfter = r.RobotCertificate.NotAfter, |
| 2 | 96 | | }, |
| 2 | 97 | | RobotSystemInfo = r.RobotSystemInfo == null ? null : new RobotSystemInfoBusinessModel { |
| 2 | 98 | | Id = r.RobotSystemInfo.Id, |
| 2 | 99 | | Cpu = r.RobotSystemInfo.Cpu, |
| 2 | 100 | | IsLittleEndian = r.RobotSystemInfo.IsLittleEndian, |
| 2 | 101 | | Motherboard = r.RobotSystemInfo.Motherboard, |
| 2 | 102 | | MotherboardSerialNumber = r.RobotSystemInfo.MotherboardSerialNumber, |
| 2 | 103 | | RamMiB = r.RobotSystemInfo.RamMiB, |
| 2 | 104 | | Gpu = r.RobotSystemInfo.Gpu, |
| 2 | 105 | | Os = r.RobotSystemInfo.Os, |
| 2 | 106 | | Is32Bit = r.RobotSystemInfo.Is32Bit, |
| 2 | 107 | | McuSerialNumber = r.RobotSystemInfo.McuSerialNumber, |
| 2 | 108 | | }, |
| 2 | 109 | | RobotChassisInfo = r.RobotChassisInfo == null ? null : new RobotChassisInfoBusinessModel { |
| 2 | 110 | | Id = r.RobotChassisInfo.Id, |
| 2 | 111 | | RobotTypeId = r.RobotChassisInfo.RobotTypeId, |
| 2 | 112 | | ChassisLengthX = r.RobotChassisInfo.ChassisLengthX, |
| 2 | 113 | | ChassisLengthY = r.RobotChassisInfo.ChassisLengthY, |
| 2 | 114 | | ChassisWheelCount = r.RobotChassisInfo.ChassisWheelCount, |
| 2 | 115 | | ChassisWheelRadius = r.RobotChassisInfo.ChassisWheelRadius, |
| 2 | 116 | | BatteryCount = r.RobotChassisInfo.BatteryCount, |
| 2 | 117 | | BatteryMaxVoltage = r.RobotChassisInfo.BatteryMaxVoltage, |
| 2 | 118 | | BatteryMinVoltage = r.RobotChassisInfo.BatteryMinVoltage, |
| 2 | 119 | | }, |
| 2 | 120 | | AssignedTasks = r.AssignedTasks |
| 2 | 121 | | .Where(t => t.CurrentProgressId != (int)ProgressState.Aborted |
| 2 | 122 | | && t.CurrentProgressId != (int)ProgressState.Completed |
| 2 | 123 | | && t.CurrentProgressId != (int)ProgressState.Template) |
| 2 | 124 | | .OrderByDescending(t => t.CurrentProgressId) |
| 2 | 125 | | .ThenBy(t => t.Id) |
| 2 | 126 | | .Select(t => new AutoTaskListBusinessModel { |
| 2 | 127 | | Id = t.Id, |
| 2 | 128 | | Name = t.Name, |
| 2 | 129 | | Priority = t.Priority, |
| 2 | 130 | | FlowId = t.FlowId, |
| 2 | 131 | | FlowName = t.Flow!.Name, |
| 2 | 132 | | RealmId = r.RealmId, |
| 2 | 133 | | RealmName = r.Realm.Name, |
| 2 | 134 | | AssignedRobotId = r.Id, |
| 2 | 135 | | AssignedRobotName = r.Name, |
| 2 | 136 | | CurrentProgressId = t.CurrentProgressId, |
| 2 | 137 | | CurrentProgressName = t.CurrentProgress.Name, |
| 2 | 138 | | }) |
| 2 | 139 | | .ToList(), |
| 2 | 140 | | }) |
| 2 | 141 | | .FirstOrDefaultAsync() |
| 2 | 142 | | ?? throw new LgdxNotFound404Exception(); |
| 1 | 143 | | } |
| | 144 | |
|
| | 145 | | public async Task<RobotCreateResponseBusinessModel> CreateRobotAsync(RobotCreateBusinessModel robotCreateBusinessModel |
| 2 | 146 | | { |
| 2 | 147 | | var realm = await _context.Realms.Where(r => r.Id == robotCreateBusinessModel.RealmId).AnyAsync(); |
| 2 | 148 | | if (realm == false) |
| 1 | 149 | | { |
| 1 | 150 | | throw new LgdxValidation400Expection(nameof(robotCreateBusinessModel.RealmId), "Realm does not exist."); |
| | 151 | | } |
| | 152 | |
|
| 1 | 153 | | var id = Guid.CreateVersion7(); |
| 1 | 154 | | var robotCertificate = await _robotCertificateService.IssueRobotCertificateAsync(id); |
| 1 | 155 | | var robot = new Robot { |
| 1 | 156 | | Id = id, |
| 1 | 157 | | Name = robotCreateBusinessModel.Name, |
| 1 | 158 | | RealmId = robotCreateBusinessModel.RealmId, |
| 1 | 159 | | IsRealtimeExchange = robotCreateBusinessModel.IsRealtimeExchange, |
| 1 | 160 | | IsProtectingHardwareSerialNumber = robotCreateBusinessModel.IsProtectingHardwareSerialNumber, |
| 1 | 161 | | RobotCertificate = new RobotCertificate { |
| 1 | 162 | | Thumbprint = robotCertificate.RobotCertificateThumbprint, |
| 1 | 163 | | NotBefore = DateTime.SpecifyKind(robotCertificate.RobotCertificateNotBefore, DateTimeKind.Utc), |
| 1 | 164 | | NotAfter = DateTime.SpecifyKind(robotCertificate.RobotCertificateNotAfter, DateTimeKind.Utc) |
| 1 | 165 | | }, |
| 1 | 166 | | RobotChassisInfo = new RobotChassisInfo { |
| 1 | 167 | | RobotTypeId = robotCreateBusinessModel.RobotChassisInfo.RobotTypeId, |
| 1 | 168 | | ChassisLengthX = robotCreateBusinessModel.RobotChassisInfo.ChassisLengthX, |
| 1 | 169 | | ChassisLengthY = robotCreateBusinessModel.RobotChassisInfo.ChassisLengthY, |
| 1 | 170 | | ChassisWheelCount = robotCreateBusinessModel.RobotChassisInfo.ChassisWheelCount, |
| 1 | 171 | | ChassisWheelRadius = robotCreateBusinessModel.RobotChassisInfo.ChassisWheelRadius, |
| 1 | 172 | | BatteryCount = robotCreateBusinessModel.RobotChassisInfo.BatteryCount, |
| 1 | 173 | | BatteryMaxVoltage = robotCreateBusinessModel.RobotChassisInfo.BatteryMaxVoltage, |
| 1 | 174 | | BatteryMinVoltage = robotCreateBusinessModel.RobotChassisInfo.BatteryMinVoltage, |
| 1 | 175 | | } |
| 1 | 176 | | }; |
| 1 | 177 | | await _context.Robots.AddAsync(robot); |
| 1 | 178 | | await _context.SaveChangesAsync(); |
| | 179 | |
|
| 1 | 180 | | await _activityLogService.CreateActivityLogAsync(new ActivityLogCreateBusinessModel |
| 1 | 181 | | { |
| 1 | 182 | | EntityName = nameof(Robot), |
| 1 | 183 | | EntityId = robot.Id.ToString(), |
| 1 | 184 | | Action = ActivityAction.Create, |
| 1 | 185 | | }); |
| | 186 | |
|
| 1 | 187 | | return new RobotCreateResponseBusinessModel |
| 1 | 188 | | { |
| 1 | 189 | | RobotId = robot.Id, |
| 1 | 190 | | RobotName = robot.Name, |
| 1 | 191 | | RootCertificate = robotCertificate.RootCertificate, |
| 1 | 192 | | RobotCertificatePrivateKey = robotCertificate.RobotCertificatePrivateKey, |
| 1 | 193 | | RobotCertificatePublicKey = robotCertificate.RobotCertificatePublicKey |
| 1 | 194 | | }; |
| 1 | 195 | | } |
| | 196 | |
|
| | 197 | | public async Task<bool> UpdateRobotAsync(Guid id, RobotUpdateBusinessModel robotUpdateDtoBusinessModel) |
| 0 | 198 | | { |
| 0 | 199 | | bool result = await _context.Robots |
| 0 | 200 | | .Where(r => r.Id == id) |
| 0 | 201 | | .ExecuteUpdateAsync(setters => setters |
| 0 | 202 | | .SetProperty(r => r.Name, robotUpdateDtoBusinessModel.Name) |
| 0 | 203 | | .SetProperty(r => r.IsRealtimeExchange, robotUpdateDtoBusinessModel.IsRealtimeExchange) |
| 0 | 204 | | .SetProperty(r => r.IsProtectingHardwareSerialNumber, robotUpdateDtoBusinessModel.IsProtectingHardwareSerialNumb |
| 0 | 205 | | ) == 1; |
| | 206 | |
|
| 0 | 207 | | if (result) |
| 0 | 208 | | { |
| 0 | 209 | | await _activityLogService.CreateActivityLogAsync(new ActivityLogCreateBusinessModel |
| 0 | 210 | | { |
| 0 | 211 | | EntityName = nameof(Robot), |
| 0 | 212 | | EntityId = id.ToString(), |
| 0 | 213 | | Action = ActivityAction.Update, |
| 0 | 214 | | }); |
| 0 | 215 | | } |
| 0 | 216 | | return result; |
| 0 | 217 | | } |
| | 218 | |
|
| | 219 | | public async Task<bool> UpdateRobotChassisInfoAsync(Guid id, RobotChassisInfoUpdateBusinessModel robotChassisInfoUpdat |
| 0 | 220 | | { |
| 0 | 221 | | bool result = await _context.RobotChassisInfos |
| 0 | 222 | | .Where(r => r.RobotId == id) |
| 0 | 223 | | .ExecuteUpdateAsync(setters => setters |
| 0 | 224 | | .SetProperty(r => r.RobotTypeId, robotChassisInfoUpdateBusinessModel.RobotTypeId) |
| 0 | 225 | | .SetProperty(r => r.ChassisLengthX, robotChassisInfoUpdateBusinessModel.ChassisLengthX) |
| 0 | 226 | | .SetProperty(r => r.ChassisLengthY, robotChassisInfoUpdateBusinessModel.ChassisLengthY) |
| 0 | 227 | | .SetProperty(r => r.ChassisWheelCount, robotChassisInfoUpdateBusinessModel.ChassisWheelCount) |
| 0 | 228 | | .SetProperty(r => r.ChassisWheelRadius, robotChassisInfoUpdateBusinessModel.ChassisWheelRadius) |
| 0 | 229 | | .SetProperty(r => r.BatteryCount, robotChassisInfoUpdateBusinessModel.BatteryCount) |
| 0 | 230 | | .SetProperty(r => r.BatteryMaxVoltage, robotChassisInfoUpdateBusinessModel.BatteryMaxVoltage) |
| 0 | 231 | | .SetProperty(r => r.BatteryMinVoltage, robotChassisInfoUpdateBusinessModel.BatteryMinVoltage) |
| 0 | 232 | | ) == 1; |
| | 233 | |
|
| 0 | 234 | | if (result) |
| 0 | 235 | | { |
| 0 | 236 | | await _activityLogService.CreateActivityLogAsync(new ActivityLogCreateBusinessModel |
| 0 | 237 | | { |
| 0 | 238 | | EntityName = nameof(Robot), |
| 0 | 239 | | EntityId = id.ToString(), |
| 0 | 240 | | Action = ActivityAction.Update, |
| 0 | 241 | | }); |
| 0 | 242 | | } |
| 0 | 243 | | return result; |
| 0 | 244 | | } |
| | 245 | |
|
| | 246 | | public async Task<RobotChassisInfoBusinessModel?> GetRobotChassisInfoAsync(Guid robotId) |
| 0 | 247 | | { |
| 0 | 248 | | return await _context.RobotChassisInfos.AsNoTracking() |
| 0 | 249 | | .Where(r => r.RobotId == robotId) |
| 0 | 250 | | .Select(r => new RobotChassisInfoBusinessModel { |
| 0 | 251 | | Id = r.Id, |
| 0 | 252 | | RobotTypeId = r.RobotTypeId, |
| 0 | 253 | | ChassisLengthX = r.ChassisLengthX, |
| 0 | 254 | | ChassisLengthY = r.ChassisLengthY, |
| 0 | 255 | | ChassisWheelCount = r.ChassisWheelCount, |
| 0 | 256 | | ChassisWheelRadius = r.ChassisWheelRadius, |
| 0 | 257 | | BatteryCount = r.BatteryCount, |
| 0 | 258 | | BatteryMaxVoltage = r.BatteryMaxVoltage, |
| 0 | 259 | | BatteryMinVoltage = r.BatteryMinVoltage, |
| 0 | 260 | | }).FirstOrDefaultAsync(); |
| 0 | 261 | | } |
| | 262 | |
|
| | 263 | | public async Task<bool> TestDeleteRobotAsync(Guid id) |
| 2 | 264 | | { |
| 2 | 265 | | var dependencies = await _context.AutoTasks |
| 2 | 266 | | .Where(t => t.AssignedRobotId == id) |
| 2 | 267 | | .Where(t => t.CurrentProgressId != (int)ProgressState.Completed && t.CurrentProgressId != (int)ProgressState.Abort |
| 2 | 268 | | .CountAsync(); |
| 2 | 269 | | if (dependencies > 0) |
| 1 | 270 | | { |
| 1 | 271 | | throw new LgdxValidation400Expection(nameof(id), $"This robot has been used by {dependencies} running/waiting/temp |
| | 272 | | } |
| 1 | 273 | | return true; |
| 1 | 274 | | } |
| | 275 | |
|
| | 276 | | public async Task<bool> DeleteRobotAsync(Guid id) |
| 0 | 277 | | { |
| 0 | 278 | | bool result = await _context.Robots.Where(r => r.Id == id) |
| 0 | 279 | | .ExecuteDeleteAsync() == 1; |
| | 280 | |
|
| 0 | 281 | | if (result) |
| 0 | 282 | | { |
| 0 | 283 | | await _activityLogService.CreateActivityLogAsync(new ActivityLogCreateBusinessModel |
| 0 | 284 | | { |
| 0 | 285 | | EntityName = nameof(Robot), |
| 0 | 286 | | EntityId = id.ToString(), |
| 0 | 287 | | Action = ActivityAction.Delete, |
| 0 | 288 | | }); |
| 0 | 289 | | } |
| 0 | 290 | | return result; |
| 0 | 291 | | } |
| | 292 | |
|
| | 293 | | public async Task<RobotSystemInfoBusinessModel?> GetRobotSystemInfoAsync(Guid robotId) |
| 1 | 294 | | { |
| 1 | 295 | | return await _context.RobotSystemInfos.AsNoTracking() |
| 1 | 296 | | .Where(r => r.RobotId == robotId) |
| 1 | 297 | | .Select(r => new RobotSystemInfoBusinessModel { |
| 1 | 298 | | Id = r.Id, |
| 1 | 299 | | Cpu = r.Cpu, |
| 1 | 300 | | IsLittleEndian = r.IsLittleEndian, |
| 1 | 301 | | Motherboard = r.Motherboard, |
| 1 | 302 | | MotherboardSerialNumber = r.MotherboardSerialNumber, |
| 1 | 303 | | RamMiB = r.RamMiB, |
| 1 | 304 | | Gpu = r.Gpu, |
| 1 | 305 | | Os = r.Os, |
| 1 | 306 | | Is32Bit = r.Is32Bit, |
| 1 | 307 | | McuSerialNumber = r.McuSerialNumber, |
| 1 | 308 | | }) |
| 1 | 309 | | .FirstOrDefaultAsync(); |
| 1 | 310 | | } |
| | 311 | |
|
| | 312 | | public async Task<bool> CreateRobotSystemInfoAsync(Guid robotId, RobotSystemInfoCreateBusinessModel robotSystemInfoCre |
| 1 | 313 | | { |
| 1 | 314 | | var robotSystemInfo = new RobotSystemInfo { |
| 1 | 315 | | Cpu = robotSystemInfoCreateBusinessModel.Cpu, |
| 1 | 316 | | IsLittleEndian = robotSystemInfoCreateBusinessModel.IsLittleEndian, |
| 1 | 317 | | Motherboard = robotSystemInfoCreateBusinessModel.Motherboard, |
| 1 | 318 | | MotherboardSerialNumber = robotSystemInfoCreateBusinessModel.MotherboardSerialNumber, |
| 1 | 319 | | RamMiB = robotSystemInfoCreateBusinessModel.RamMiB, |
| 1 | 320 | | Gpu = robotSystemInfoCreateBusinessModel.Gpu, |
| 1 | 321 | | Os = robotSystemInfoCreateBusinessModel.Os, |
| 1 | 322 | | Is32Bit = robotSystemInfoCreateBusinessModel.Is32Bit, |
| 1 | 323 | | McuSerialNumber = robotSystemInfoCreateBusinessModel.McuSerialNumber, |
| 1 | 324 | | RobotId = robotId |
| 1 | 325 | | }; |
| 1 | 326 | | _context.RobotSystemInfos.Add(robotSystemInfo); |
| 1 | 327 | | return await _context.SaveChangesAsync() >= 1; |
| 1 | 328 | | } |
| | 329 | |
|
| | 330 | | public async Task<bool> UpdateRobotSystemInfoAsync(Guid robotId, RobotSystemInfoUpdateBusinessModel robotSystemInfoUpd |
| 0 | 331 | | { |
| 0 | 332 | | return await _context.RobotSystemInfos |
| 0 | 333 | | .Where(r => r.RobotId == robotId) |
| 0 | 334 | | .ExecuteUpdateAsync(setters => setters |
| 0 | 335 | | .SetProperty(r => r.Cpu, robotSystemInfoUpdateBusinessModel.Cpu) |
| 0 | 336 | | .SetProperty(r => r.IsLittleEndian, robotSystemInfoUpdateBusinessModel.IsLittleEndian) |
| 0 | 337 | | .SetProperty(r => r.Motherboard, robotSystemInfoUpdateBusinessModel.Motherboard) |
| 0 | 338 | | .SetProperty(r => r.MotherboardSerialNumber, robotSystemInfoUpdateBusinessModel.MotherboardSerialNumber) |
| 0 | 339 | | .SetProperty(r => r.RamMiB, robotSystemInfoUpdateBusinessModel.RamMiB) |
| 0 | 340 | | .SetProperty(r => r.Gpu, robotSystemInfoUpdateBusinessModel.Gpu) |
| 0 | 341 | | .SetProperty(r => r.Os, robotSystemInfoUpdateBusinessModel.Os) |
| 0 | 342 | | .SetProperty(r => r.Is32Bit, robotSystemInfoUpdateBusinessModel.Is32Bit) |
| 0 | 343 | | .SetProperty(r => r.McuSerialNumber, robotSystemInfoUpdateBusinessModel.McuSerialNumber) |
| 0 | 344 | | ) == 1; |
| 0 | 345 | | } |
| | 346 | |
|
| | 347 | | public async Task<IEnumerable<RobotSearchBusinessModel>> SearchRobotsAsync(int realmId, string? name, Guid? robotId) |
| 4 | 348 | | { |
| 4 | 349 | | var n = name ?? string.Empty; |
| 4 | 350 | | return await _context.Robots.AsNoTracking() |
| 4 | 351 | | .Where(r => r.RealmId == realmId) |
| 4 | 352 | | .Where(t => t.Name.ToLower().Contains(n.ToLower())) |
| 4 | 353 | | .Where(r => robotId == null || r.Id == robotId) |
| 4 | 354 | | .Take(10) |
| 4 | 355 | | .Select(m => new RobotSearchBusinessModel { |
| 4 | 356 | | Id = m.Id, |
| 4 | 357 | | Name = m.Name, |
| 4 | 358 | | }) |
| 4 | 359 | | .ToListAsync(); |
| 4 | 360 | | } |
| | 361 | |
|
| | 362 | | public async Task<int?> GetRobotRealmIdAsync(Guid robotId) |
| 3 | 363 | | { |
| 3 | 364 | | if (_memoryCache.TryGetValue<int>($"RobotService_GetRobotRealmIdAsync_{robotId}", out var RealmId)) |
| 1 | 365 | | { |
| 1 | 366 | | return RealmId; |
| | 367 | | } |
| | 368 | |
|
| 2 | 369 | | var robot = await _context.Robots.AsNoTracking() |
| 2 | 370 | | .Where(m => m.Id == robotId) |
| 2 | 371 | | .Select(m => new { m.Id, m.RealmId }) |
| 2 | 372 | | .FirstOrDefaultAsync(); |
| 2 | 373 | | if (robot == null) |
| 1 | 374 | | return null; |
| | 375 | |
|
| 1 | 376 | | _memoryCache.Set($"RobotService_GetRobotRealmIdAsync_{robotId}", robot!.RealmId, TimeSpan.FromDays(1)); |
| 1 | 377 | | return robot.RealmId; |
| 3 | 378 | | } |
| | 379 | |
|
| | 380 | | public async Task<bool> GetRobotIsRealtimeExchange(Guid robotId) |
| 1 | 381 | | { |
| 1 | 382 | | return await _context.Robots.AsNoTracking() |
| 1 | 383 | | .Where(r => r.Id == robotId) |
| 1 | 384 | | .Select(r => r.IsRealtimeExchange) |
| 1 | 385 | | .FirstOrDefaultAsync(); |
| 1 | 386 | | } |
| | 387 | | } |