public Entity.BaseResponse <Entity.Device> Manage([FromBody] Entity.Device request) { if (request == null || !request.ParentDeviceGuid.HasValue) { return(new Entity.BaseResponse <Entity.Device>(false, "Invalid Request")); } Entity.BaseResponse <Entity.Device> response = new Entity.BaseResponse <Entity.Device>(true); try { if ((request.ParentDeviceGuid.HasValue && request.ParentDeviceGuid != Guid.Empty && (request.TemplateGuid == null || request.TemplateGuid == Guid.Empty))) { var parentDevice = _service.Get(request.ParentDeviceGuid.Value); request.TemplateGuid = parentDevice.TemplateGuid; request.GreenHouseGuid = parentDevice.GreenHouseGuid; request.CompanyGuid = parentDevice.CompanyGuid; } var status = _service.Manage(request); response.IsSuccess = status.Success; response.Message = status.Message; response.Data = status.Data; } catch (Exception ex) { base.LogException(ex); return(new Entity.BaseResponse <Entity.Device>(false, ex.Message)); } return(response); }
public Entity.BaseResponse <Entity.Device> Manage([FromBody] Entity.Device request) { Entity.BaseResponse <Entity.Device> response = new Entity.BaseResponse <Entity.Device>(true); try { var status = _service.Manage(request); response.IsSuccess = status.Success; response.Message = status.Message; response.Data = status.Data; } catch (Exception ex) { base.LogException(ex); return(new Entity.BaseResponse <Entity.Device>(false, ex.Message)); } return(response); }
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.ActionStatus Manage(Entity.Device request) { Entity.ActionStatus actionStatus = new Entity.ActionStatus(true); try { var dbDevice = Mapper.Configuration.Mapper.Map <Entity.Device, Model.Device>(request); if (request.Guid == null || request.Guid == Guid.Empty) { var addDeviceResult = _iotConnectClient.Device.Add(Mapper.Configuration.Mapper.Map <IOT.AddDeviceModel>(request)).Result; if (addDeviceResult != null && addDeviceResult.status && addDeviceResult.data != null) { request.Guid = Guid.Parse(addDeviceResult.data.newid.ToUpper()); IOT.DataResponse <IOT.AcquireDeviceResult> acquireResult = _iotConnectClient.Device.AcquireDevice(request.UniqueId, new IOT.AcquireDeviceModel()).Result; dbDevice.IsProvisioned = acquireResult.status; dbDevice.IsActive = true; dbDevice.Guid = request.Guid; dbDevice.CompanyGuid = SolutionConfiguration.CompanyId; dbDevice.CreatedDate = DateTime.Now; dbDevice.CreatedBy = SolutionConfiguration.CurrentUserId; actionStatus = _deviceRepository.Manage(dbDevice); actionStatus.Data = Mapper.Configuration.Mapper.Map <Model.Device, Entity.Device>(actionStatus.Data); 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(request.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!"; } } } else { _logger.ErrorLog(new Exception($"Device is not added in iotconnect, Error: {addDeviceResult.message}"), this.GetType().Name, MethodBase.GetCurrentMethod().Name); actionStatus.Success = false; actionStatus.Message = new UtilityHelper().IOTResultMessage(addDeviceResult.errorMessages); } } else { var olddbDevice = _deviceRepository.FindBy(x => x.Guid.Equals(request.Guid)).FirstOrDefault(); if (olddbDevice == null) { throw new NotFoundCustomException($"{CommonException.Name.NoRecordsFound} : Device"); } var updateEntityResult = _iotConnectClient.Device.Update(request.Guid.ToString(), Mapper.Configuration.Mapper.Map <IOT.UpdateDeviceModel>(request)).Result; if (updateEntityResult != null && updateEntityResult.status && updateEntityResult.data != null) { dbDevice.CreatedDate = olddbDevice.CreatedDate; dbDevice.CreatedBy = olddbDevice.CreatedBy; dbDevice.UpdatedDate = DateTime.Now; dbDevice.UpdatedBy = SolutionConfiguration.CurrentUserId; dbDevice.CompanyGuid = SolutionConfiguration.CompanyId; actionStatus = _deviceRepository.Manage(dbDevice); actionStatus.Data = Mapper.Configuration.Mapper.Map <Model.Device, Entity.Device>(actionStatus.Data); if (!actionStatus.Success) { _logger.ErrorLog(new Exception($"Device is not updated in solution database, Error: {actionStatus.Message}"), this.GetType().Name, MethodBase.GetCurrentMethod().Name); actionStatus.Success = false; actionStatus.Message = "Something Went Wrong!"; } } else { _logger.ErrorLog(new Exception($"Device is not added in iotconnect, Error: {updateEntityResult.message}"), this.GetType().Name, MethodBase.GetCurrentMethod().Name); actionStatus.Success = false; actionStatus.Message = new UtilityHelper().IOTResultMessage(updateEntityResult.errorMessages); } } } catch (Exception ex) { _logger.ErrorLog(ex, this.GetType().Name, MethodBase.GetCurrentMethod().Name); return(new Entity.ActionStatus { Success = false, Message = ex.Message }); } return(actionStatus); }