public Entity.BaseResponse <bool> ProvisionKit(Entity.ProvisionKitRequest request) { Entity.BaseResponse <bool> result = new Entity.BaseResponse <bool>(true); try { var repoResult = _deviceRepository.ProvisionKit(request); if (repoResult != null && repoResult.Data != null && repoResult.Data.Any()) { Entity.HardwareKit hardwareKit = repoResult.Data.FirstOrDefault(); string templateGuid = _lookupService.GetIotTemplateGuidByName(hardwareKit.KitTypeName); IOT.AddDeviceModel iotDeviceDetail = new IOT.AddDeviceModel() { DisplayName = hardwareKit.Name, entityGuid = request.WingGuid.ToString(), uniqueId = hardwareKit.UniqueId, deviceTemplateGuid = templateGuid, note = hardwareKit.Note, properties = new List <IOT.AddProperties>() }; var addDeviceResult = _iotConnectClient.Device.Add(iotDeviceDetail).Result; if (addDeviceResult != null && addDeviceResult.status && addDeviceResult.data != null) { IOT.DataResponse <IOT.AcquireDeviceResult> acquireResult = _iotConnectClient.Device.AcquireDevice(hardwareKit.UniqueId, new IOT.AcquireDeviceModel()).Result; Model.Elevator dbDevice = new Model.Elevator() { Guid = Guid.Parse(addDeviceResult.data.newid.ToUpper()), CompanyGuid = SolutionConfiguration.CompanyId, EntityGuid = request.WingGuid, TemplateGuid = Guid.Parse(templateGuid), UniqueId = hardwareKit.UniqueId, Name = hardwareKit.Name, Note = hardwareKit.Note, Description = request.Description, Specification = request.Specification, IsProvisioned = acquireResult.status, IsActive = true, IsDeleted = false, CreatedDate = DateTime.UtcNow, CreatedBy = SolutionConfiguration.CurrentUserId }; if (request.ImageFile != null) { dbDevice.Image = SaveElevatorImage(dbDevice.Guid, request.ImageFile); } Entity.ActionStatus actionStatus = _deviceRepository.Manage(dbDevice); if (!actionStatus.Success) { _logger.ErrorLog(new Exception($"Device is not added in solution database, Error: {actionStatus.Message}"), this.GetType().Name, MethodBase.GetCurrentMethod().Name); var deleteEntityResult = _iotConnectClient.Device.Delete(dbDevice.Guid.ToString()).Result; if (deleteEntityResult != null && deleteEntityResult.status) { _logger.ErrorLog(new Exception($"Device is not deleted from iotconnect, Error: {actionStatus.Message}"), this.GetType().Name, MethodBase.GetCurrentMethod().Name); actionStatus.Success = false; actionStatus.Message = "Something Went Wrong!"; } } var dbhardwareKit = _hardwareKitRepository.GetByUniqueId(t => t.KitCode == request.KitCode); if (dbhardwareKit != null) { dbhardwareKit.CompanyGuid = SolutionConfiguration.CompanyId; _hardwareKitRepository.Update(dbhardwareKit); } } else { _logger.ErrorLog(new Exception($"Device is not deleted from iotconnect, Error: {addDeviceResult.message}"), this.GetType().Name, MethodBase.GetCurrentMethod().Name); result.IsSuccess = false; result.Message = "Something Went Wrong!"; } } else { return(new Entity.BaseResponse <bool>(false, repoResult.Message)); } } catch (Exception ex) { _logger.ErrorLog(ex, $"Device.GetDeviceStatus, Error: {ex.Message}"); return(null); } return(result); }
public Entity.BaseResponse <bool> ProvisionKit(Entity.Device request) { Entity.BaseResponse <bool> result = new Entity.BaseResponse <bool>(true); try { var repoResult = _deviceRepository.ProvisionKit(new ProvisionKitRequest { DeviceGuid = new Guid(), KitCode = request.KitCode, UniqueId = request.UniqueId }); if (repoResult != null && repoResult.Data != null && repoResult.Data.Any()) { Entity.HardwareKit device = repoResult.Data.OrderBy(d => d.KitCode == request.KitCode && d.UniqueId == request.UniqueId).FirstOrDefault(); IOT.AddDeviceModel iotDeviceDetail = new IOT.AddDeviceModel() { DisplayName = device.Name, //entityGuid = request.DeviceGuid.ToString(), uniqueId = device.UniqueId, deviceTemplateGuid = device.TemplateGuid.ToString(), note = device.Note, tag = device.Tag, properties = new List <IOT.AddProperties>() }; var addDeviceResult = _iotConnectClient.Device.Add(iotDeviceDetail).Result; if (addDeviceResult != null && addDeviceResult.status && addDeviceResult.data != null) { Guid newDeviceId = Guid.Parse(addDeviceResult.data.newid.ToUpper()); IOT.DataResponse <IOT.AcquireDeviceResult> acquireResult = _iotConnectClient.Device.AcquireDevice(request.UniqueId, new IOT.AcquireDeviceModel()).Result; Entity.ActionStatus actionStatus = _deviceRepository.Manage(new Model.Device() { Guid = newDeviceId, CompanyGuid = SolutionConfiguration.CompanyId, Description = request.Description, EntityGuid = new Guid(request.EntityGuid.ToString()), Specification = request.Specification, TemplateGuid = device.TemplateGuid.Value, ParentDeviceGuid = null, TypeGuid = request.TypeGuid, UniqueId = request.UniqueId, Name = request.Name, Note = request.Note, Tag = request.Tag, IsProvisioned = acquireResult.status, IsActive = request.IsActive, IsDeleted = false, CreatedDate = DateTime.UtcNow, CreatedBy = SolutionConfiguration.CurrentUserId }); if (!actionStatus.Success) { _logger.ErrorLog(new Exception($"Device is not added in solution database, Error: {actionStatus.Message}"), this.GetType().Name, MethodBase.GetCurrentMethod().Name); var deleteEntityResult = _iotConnectClient.Device.Delete(newDeviceId.ToString()).Result; if (deleteEntityResult != null && deleteEntityResult.status) { _logger.ErrorLog(new Exception($"Device is not deleted from iotconnect"), this.GetType().Name, MethodBase.GetCurrentMethod().Name); actionStatus.Success = false; actionStatus.Message = new UtilityHelper().IOTResultMessage(deleteEntityResult.errorMessages); } } else { //Update companyid in hardware kit var hardwareKit = _hardwareKitRepository.GetByUniqueId(t => t.KitCode == request.KitCode && t.UniqueId == request.UniqueId); if (hardwareKit != null) { hardwareKit.CompanyGuid = SolutionConfiguration.CompanyId; _hardwareKitRepository.Update(hardwareKit); } result.IsSuccess = true; } } else { _logger.ErrorLog(new Exception($"Kit is not added in iotconnect, Error: {addDeviceResult.message}"), this.GetType().Name, MethodBase.GetCurrentMethod().Name); result.IsSuccess = false; result.Message = new UtilityHelper().IOTResultMessage(addDeviceResult.errorMessages); } } else { return(new Entity.BaseResponse <bool>(false, repoResult.Message)); } } catch (Exception ex) { _logger.ErrorLog(ex, this.GetType().Name, MethodBase.GetCurrentMethod().Name); return(null); } return(result); }
public Entity.BaseResponse <bool> ProvisionKit(Entity.ProvisionKitRequest request) { Entity.BaseResponse <bool> result = new Entity.BaseResponse <bool>(true); try { Entity.BaseResponse <List <Entity.KitDevice> > kitDeviceList = _deviceRepository.ProvisionKit(request); if (kitDeviceList != null && kitDeviceList.Data != null && kitDeviceList.Data.Any()) { List <Entity.KitDevice> deviceList = kitDeviceList.Data.OrderBy(d => d.ParentDeviceGuid.HasValue).ToList(); string templateGuid = _lookupService.GetIotTemplateGuidByName(deviceList.FirstOrDefault().TemplateName); if (!string.IsNullOrEmpty(templateGuid)) { List <Model.Device> lstAddedDevice = new List <Model.Device>(); bool IsDeviceAdded = true; foreach (var device in deviceList) { string tagName = string.Empty; if (device.ParentDeviceGuid.HasValue) { var kitType = _kitTypeAttributeRepository.FindBy(t => t.Guid == device.TagGuid).FirstOrDefault(); if (kitType != null) { tagName = kitType.Tag.ToString(); } else { throw new Exception("Device tag is not exists in solution."); } } IOT.AddDeviceModel iotDeviceDetail = new IOT.AddDeviceModel() { DisplayName = device.Name, entityGuid = request.GreenHouseGuid.ToString(), uniqueId = device.UniqueId, deviceTemplateGuid = templateGuid,// device.TemplateGuid.ToString(), parentDeviceGuid = device.ParentDeviceGuid.ToString(), note = device.Note, tag = tagName,//device.Tag, properties = new List <IOT.AddProperties>() }; var addDeviceResult = _iotConnectClient.Device.Add(iotDeviceDetail).Result; if (addDeviceResult != null && addDeviceResult.status && addDeviceResult.data != null) { Guid newDeviceId = Guid.Parse(addDeviceResult.data.newid.ToUpper()); IOT.DataResponse <IOT.AcquireDeviceResult> acquireResult = _iotConnectClient.Device.AcquireDevice(device.UniqueId, new IOT.AcquireDeviceModel()).Result; var intUpdated = deviceList.Where(d => d.ParentDeviceGuid == device.Guid).Select(d => d.ParentDeviceGuid = newDeviceId).Count(); lstAddedDevice.Add(new Model.Device() { Guid = newDeviceId, CompanyGuid = SolutionConfiguration.CompanyId, GreenHouseGuid = request.GreenHouseGuid, TemplateGuid = device.TemplateGuid, ParentDeviceGuid = device.ParentDeviceGuid, Type = null, UniqueId = device.UniqueId, Name = device.Name, Note = device.Note, Tag = tagName,//device.Tag, IsProvisioned = acquireResult.status, IsActive = true, IsDeleted = false, CreatedDate = DateTime.UtcNow, CreatedBy = SolutionConfiguration.CurrentUserId }); } else { IsDeviceAdded = false; _logger.ErrorLog(new Exception($"Kit is not added in iotconnect, Error: {addDeviceResult.message}"), this.GetType().Name, MethodBase.GetCurrentMethod().Name); result.IsSuccess = false; result.Message = new UtilityHelper().IOTResultMessage(addDeviceResult.errorMessages); break; } } if (IsDeviceAdded && lstAddedDevice != null && lstAddedDevice.Any()) { foreach (var device in lstAddedDevice) { Entity.ActionStatus actionStatus = _deviceRepository.Manage(device); if (!actionStatus.Success) { _logger.ErrorLog(new Exception($"Device is not added in solution database, Error: {actionStatus.Message}"), this.GetType().Name, MethodBase.GetCurrentMethod().Name); var deleteEntityResult = _iotConnectClient.Device.Delete(device.Guid.ToString()).Result; if (deleteEntityResult != null && deleteEntityResult.status) { _logger.ErrorLog(new Exception($"Device is not deleted from iotconnect"), this.GetType().Name, MethodBase.GetCurrentMethod().Name); actionStatus.Success = false; actionStatus.Message = "Something Went Wrong!"; } } } } if (result.IsSuccess) { result.Message = "Kit Added Successfully!"; //Update companyid in hardware kit var hardwareKit = _hardwareKitRepository.GetByUniqueId(t => t.KitCode == request.KitCode); if (hardwareKit != null) { hardwareKit.CompanyGuid = SolutionConfiguration.CompanyId; _hardwareKitRepository.Update(hardwareKit); } } } else { result.IsSuccess = false; result.Data = false; result.Message = "Unable To Locate Kit Type."; } } else { result.IsSuccess = false; result.Data = false; result.Message = "Invalid Kit Details.Please Correct It!"; } } catch (Exception ex) { _logger.ErrorLog(ex, this.GetType().Name, MethodBase.GetCurrentMethod().Name); return(null); } return(result); }