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