Example #1
0
 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);
 }
Example #2
0
        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);
        }