Example #1
0
        public IActionResult CreateDevice([FromBody] CreateDeviceEvent device)
        {
            try
            {
                logger.LogInformation($"CreateDevice called for Device {device.DeviceUID}");

                if (device.DeviceSerialNumber == null)
                {
                    logger.LogInformation(
                        $"Received CreateDevicePayload with DeviceSerialNumber as null for DeviceType {device.DeviceType} and DeviceUID {device.DeviceUID} ");
                    return(BadRequest("Device SerialNumber can't be null"));
                }

                if (!deviceTypesCache.TryGetValue(device.DeviceType, out DbDeviceType deviceType))
                {
                    logger.LogWarning($"DeviceType {device.DeviceType} is not exists");
                    return(BadRequest("Make sure request contains valid DeviceType"));
                }

                if (!Enum.TryParse <DeviceStateEnum>(device.DeviceState?.Trim(), true, out DeviceStateEnum deviceState))
                {
                    return(BadRequest("Invalid Device state. Supported Device states are Installed/Provisioned/Subscribed/DeregisteredTechnician/DeregisteredStore"));
                }

                if (deviceService.CheckExistingDevice(device.DeviceUID.Value))
                {
                    return(BadRequest("Device already exists!"));
                }

                device.ReceivedUTC = DateTime.UtcNow;
                if (deviceService.CreateDevice(device, deviceState))
                {
                    return(Ok());
                }
                else
                {
                    return(BadRequest("Create Request is not processed."));
                }
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message + ex.StackTrace);
                return(StatusCode((int)HttpStatusCode.InternalServerError, ex.Message));
            }
        }
Example #2
0
        public bool CreateDevice(CreateDeviceEvent device, DeviceStateEnum deviceState)
        {
            DbDeviceType deviceType = deviceTypesCache.First(x => string.Equals(x.Key, device.DeviceType, StringComparison.InvariantCultureIgnoreCase)).Value;

            var currentUTC    = DateTime.UtcNow;
            var devicePayload = new CreateDevicePayload
            {
                ActionUTC     = currentUTC,
                CellModemIMEI = device.CellModemIMEI,
                CellularFirmwarePartnumber = device.CellularFirmwarePartnumber,
                DataLinkType                = device.DataLinkType,
                DeviceState                 = deviceState.ToString(),
                DeregisteredUTC             = device.DeregisteredUTC,
                DevicePartNumber            = device.DevicePartNumber,
                DeviceSerialNumber          = device.DeviceSerialNumber,
                DeviceType                  = device.DeviceType,
                DeviceUID                   = device.DeviceUID.Value,
                FirmwarePartNumber          = device.FirmwarePartNumber,
                GatewayFirmwarePartNumber   = device.GatewayFirmwarePartNumber,
                MainboardSoftwareVersion    = device.MainboardSoftwareVersion,
                ModuleType                  = device.ModuleType,
                NetworkFirmwarePartnumber   = device.NetworkFirmwarePartnumber,
                RadioFirmwarePartNumber     = device.RadioFirmwarePartNumber,
                ReceivedUTC                 = device.ReceivedUTC,
                SatelliteFirmwarePartnumber = device.SatelliteFirmwarePartnumber
            };

            var devicePersonality = GetDevicePersonalities(devicePayload, null, deviceType);

            devicePayload = SetNullIfPropertyEmpty(devicePayload);
            //DeviceSerialNumber will be persisited as NULL in masterdata and send it as Empty to Kafka
            devicePayload.DeviceSerialNumber = string.IsNullOrEmpty(devicePayload.DeviceSerialNumber) ? string.Empty : devicePayload.DeviceSerialNumber;
            var deviceModel = new DbDevice
            {
                SerialNumber              = devicePayload.DeviceSerialNumber,
                DeregisteredUTC           = devicePayload.DeregisteredUTC,
                ModuleType                = devicePayload.ModuleType,
                fk_DeviceStatusID         = deviceState.GetHashCode(),
                MainboardSoftwareVersion  = devicePayload.MainboardSoftwareVersion,
                FirmwarePartNumber        = devicePayload.RadioFirmwarePartNumber == null ? devicePayload.FirmwarePartNumber : devicePayload.RadioFirmwarePartNumber,
                GatewayFirmwarePartNumber = devicePayload.GatewayFirmwarePartNumber,
                DataLinkType              = devicePayload.DataLinkType,
                InsertUTC                   = currentUTC,
                UpdateUTC                   = currentUTC,
                fk_DeviceTypeID             = deviceType.DeviceTypeID,
                CellModemIMEI               = devicePayload.CellModemIMEI,
                DevicePartNumber            = devicePayload.DevicePartNumber,
                CellularFirmwarePartnumber  = devicePayload.CellularFirmwarePartnumber,
                NetworkFirmwarePartnumber   = devicePayload.NetworkFirmwarePartnumber,
                SatelliteFirmwarePartnumber = devicePayload.SatelliteFirmwarePartnumber,
                DeviceUID                   = devicePayload.DeviceUID
            };

            var kafkaMessageList = new List <KafkaMessage>();

            kafkaTopicNames.ForEach(topic =>
            {
                var kafkaMessage = new KafkaMessage()
                {
                    Key     = device.DeviceUID.ToString(),
                    Message = new { CreateDeviceEvent = mapper.Map <CreateDevicePayload, CreateDeviceEvent>(devicePayload) },
                    Topic   = topic
                };
                kafkaMessageList.Add(kafkaMessage);
            });

            kafkaTopicNamesV2.ForEach(topic =>
            {
                var kafkaMessage = new KafkaMessage()
                {
                    Key     = device.DeviceUID.ToString(),
                    Message = ToDevicePayloadV2(devicePayload, devicePersonality),
                    Topic   = topic
                };
                kafkaMessageList.Add(kafkaMessage);
            });


            var actions = new List <Action>();

            actions.Add(() => transactions.Upsert(deviceModel));
            actions.Add(() => transactions.Upsert <DbDevicePersonality>(devicePersonality));
            actions.Add(() => transactions.Publish(kafkaMessageList));

            return(transactions.Execute(actions));
        }