Esempio n. 1
0
        public IActionResult DissociateDeviceAsset([FromBody] DissociateDeviceAssetEvent dissociateDeviceAsset)
        {
            try
            {
                logger.LogInformation($"DissociateDeviceAsset called for Asset {dissociateDeviceAsset.AssetUID} Device {dissociateDeviceAsset.DeviceUID}");

                var associateDeviceDetail = deviceService.GetAssetDevice(dissociateDeviceAsset.AssetUID, dissociateDeviceAsset.DeviceUID);
                if (associateDeviceDetail == null)
                {
                    return(BadRequest("No AssetDevice Association exists for the given Asset-Device."));
                }
                else if (associateDeviceDetail.ActionUTC >= dissociateDeviceAsset.ActionUTC)
                {
                    return(BadRequest("The DissociateDeviceAssetEvent does not have the latest data to be updated."));
                }

                if (deviceService.DissociateAssetDevice(dissociateDeviceAsset))
                {
                    return(Ok());
                }
                else
                {
                    return(StatusCode((int)HttpStatusCode.InternalServerError, "DissociateDeviceAsset is not processed."));
                }
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message + ex.StackTrace);
                return(StatusCode((int)HttpStatusCode.InternalServerError, ex.Message));
            }
        }
Esempio n. 2
0
        public IActionResult AssociateDeviceAsset([FromBody] AssociateDeviceAssetEvent associateDeviceAsset)
        {
            try
            {
                logger.LogInformation($"AssociateDeviceAsset called for Asset {associateDeviceAsset.AssetUID} Device {associateDeviceAsset.DeviceUID}");

                var associateDeviceDetail = deviceService.GetAssetDevice(associateDeviceAsset.AssetUID, associateDeviceAsset.DeviceUID);
                if (associateDeviceDetail != null)
                {
                    if (associateDeviceDetail.ActionUTC >= associateDeviceAsset.ActionUTC)
                    {
                        return(BadRequest("The AssociateDeviceAsset does not have the latest data to be updated."));
                    }
                    else
                    {
                        return(BadRequest("The Device is already Associated with this Asset."));
                    }
                }
                //To check any device are dissociated for this asset
                var associateDevice = deviceService.GetAssociatedDevicesByAsset(associateDeviceAsset.AssetUID);
                if (associateDevice != null)
                {
                    if (associateDevice.DeviceStatusID == DeviceStateEnum.Subscribed.GetHashCode())
                    {
                        logger.LogInformation(string.Format("AssociateDeviceAsset ignored. Already Asset ({0}) associated with Device ({1})"
                                                            , associateDeviceAsset.AssetUID, associateDevice.DeviceUID));
                        return(Ok("AssociateDeviceAsset ignored as another device is subscribed already"));
                    }
                    else
                    {
                        var dissociateDeviceAsset = new DissociateDeviceAssetEvent()
                        {
                            AssetUID    = new Guid(associateDeviceAsset.AssetUID.ToString()),
                            DeviceUID   = associateDevice.DeviceUID,
                            ActionUTC   = associateDeviceAsset.ActionUTC.Value.AddMilliseconds(-1),
                            ReceivedUTC = DateTime.UtcNow
                        };

                        deviceService.DissociateAssetDevice(dissociateDeviceAsset);
                    }
                }


                if (deviceService.AssociateAssetDevice(associateDeviceAsset))
                {
                    return(Ok());
                }
                else
                {
                    return(StatusCode((int)HttpStatusCode.InternalServerError, "AssociateDeviceAsset is not published."));
                }
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message + ex.StackTrace);
                return(StatusCode((int)HttpStatusCode.InternalServerError, ex.Message));
            }
        }
Esempio n. 3
0
        public bool DissociateAssetDevice(DissociateDeviceAssetEvent dissociateDeviceAsset)
        {
            var kafkaMessageList = new List <KafkaMessage>();

            dissociateDeviceAsset.ReceivedUTC = DateTime.UtcNow;
            kafkaTopicNames.ForEach(topic =>
            {
                KafkaMessage kafkaMessage = new KafkaMessage()
                {
                    Key     = dissociateDeviceAsset.DeviceUID.ToString(),
                    Message = new { DissociateDeviceAssetEvent = dissociateDeviceAsset },
                    Topic   = topic
                };
                kafkaMessageList.Add(kafkaMessage);
            });

            var actions = new List <Action>();

            actions.Add(() => transactions.Delete($"DELETE FROM md_asset_AssetDevice WHERE fk_AssetUID={dissociateDeviceAsset.AssetUID.ToStringWithoutHyphens().WrapWithUnhex()} AND fk_DeviceUID={dissociateDeviceAsset.DeviceUID.ToStringWithoutHyphens().WrapWithUnhex()}"));
            actions.Add(() => transactions.Publish(kafkaMessageList));
            return(transactions.Execute(actions));
        }