Exemplo n.º 1
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));
        }
Exemplo n.º 2
0
        public bool UpdateDevice(UpdateDeviceEvent device, DeviceStateEnum deviceState)
        {
            var currentUTC    = DateTime.UtcNow;
            var devicePayload = new UpdateDevicePayload
            {
                ActionUTC     = currentUTC,
                CellModemIMEI = device.CellModemIMEI,
                CellularFirmwarePartnumber = device.CellularFirmwarePartnumber,
                DataLinkType                = device.DataLinkType,
                DeregisteredUTC             = device.DeregisteredUTC,
                DevicePartNumber            = device.DevicePartNumber,
                DeviceSerialNumber          = device.DeviceSerialNumber,
                DeviceState                 = deviceState == DeviceStateEnum.None?null: deviceState.ToString(),
                DeviceType                  = device.DeviceType,
                DeviceUID                   = device.DeviceUID.Value,
                FirmwarePartNumber          = device.FirmwarePartNumber,
                GatewayFirmwarePartNumber   = device.GatewayFirmwarePartNumber,
                MainboardSoftwareVersion    = device.MainboardSoftwareVersion,
                ModuleType                  = device.ModuleType,
                NetworkFirmwarePartnumber   = device.NetworkFirmwarePartnumber,
                OwningCustomerUID           = device.OwningCustomerUID,
                RadioFirmwarePartNumber     = device.RadioFirmwarePartNumber,
                ReceivedUTC                 = device.ReceivedUTC,
                SatelliteFirmwarePartnumber = device.SatelliteFirmwarePartnumber
            };

            var existingDeviceProp = GetExistingDeviceProperties(device.DeviceUID.Value);

            if (PLDeviceTypes.Contains(device.DeviceType.ToLower(), StringComparer.InvariantCultureIgnoreCase))
            {
                existingDeviceProp.RadioFirmwarePartNumber = existingDeviceProp.FirmwarePartNumber;
                existingDeviceProp.FirmwarePartNumber      = null;
            }

            var          existingPersonalities = GetExistingDevicePersonalities(device.DeviceUID.Value);
            DbDeviceType deviceType            = string.IsNullOrEmpty(devicePayload.DeviceType) ? deviceTypesCache.First(x => string.Equals(x.Key, existingDeviceProp.DeviceType, StringComparison.InvariantCultureIgnoreCase)).Value : deviceTypesCache.First(x => string.Equals(x.Key, devicePayload.DeviceType, StringComparison.InvariantCultureIgnoreCase)).Value;
            var          devicePersonality     = GetDevicePersonalities(devicePayload, existingPersonalities, deviceType);

            devicePayload = AppendExistingDeviceProperties(devicePayload, existingDeviceProp);
            Guid?currentOwningCustomerUID = GetExistingOwningCustomerUid(device.DeviceUID.Value);

            devicePayload.OwningCustomerUID = devicePayload.OwningCustomerUID ?? currentOwningCustomerUID;
            //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;
            bool equal = CheckExistingDevicePropertiesForUpdate(devicePayload, (DeviceStateEnum)Enum.Parse(typeof(DeviceStateEnum), devicePayload.DeviceState), currentOwningCustomerUID, existingDeviceProp);

            if (equal)
            {
                logger.LogInformation("No properties to update");
                return(false);
            }
            else
            {
                var kafkaMessageList = new List <KafkaMessage>();
                kafkaTopicNames.ForEach(topic =>
                {
                    var kafkaMessage = new KafkaMessage()
                    {
                        Key     = device.DeviceUID.ToString(),
                        Message = new { UpdateDeviceEvent = mapper.Map <UpdateDevicePayload, UpdateDeviceEvent>(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);
                });

                List <Action> actions = new List <Action>();

                var deviceModel = new DbDevice
                {
                    CellModemIMEI = devicePayload.CellModemIMEI,
                    CellularFirmwarePartnumber = devicePayload.CellularFirmwarePartnumber,
                    DataLinkType                = devicePayload.DataLinkType,
                    DeregisteredUTC             = devicePayload.DeregisteredUTC,
                    DevicePartNumber            = devicePayload.DevicePartNumber,
                    SerialNumber                = devicePayload.DeviceSerialNumber,
                    fk_DeviceStatusID           = deviceState.GetHashCode(),
                    fk_DeviceTypeID             = deviceType.DeviceTypeID,
                    DeviceUID                   = devicePayload.DeviceUID,
                    FirmwarePartNumber          = devicePayload.RadioFirmwarePartNumber == null ? devicePayload.FirmwarePartNumber : devicePayload.RadioFirmwarePartNumber,
                    GatewayFirmwarePartNumber   = devicePayload.GatewayFirmwarePartNumber,
                    MainboardSoftwareVersion    = devicePayload.MainboardSoftwareVersion,
                    ModuleType                  = devicePayload.ModuleType,
                    NetworkFirmwarePartnumber   = devicePayload.NetworkFirmwarePartnumber,
                    SatelliteFirmwarePartnumber = devicePayload.SatelliteFirmwarePartnumber,
                    UpdateUTC                   = currentUTC
                };
                var asset = GetOwningCustomerUID(device.DeviceUID.Value);

                if (device.OwningCustomerUID.HasValue && asset != null && (asset.OwningCustomerUID == Guid.Empty || !device.OwningCustomerUID.Equals(asset.OwningCustomerUID)))
                {
                    var updateAsset = new UpdateAssetEvent
                    {
                        AssetName         = asset.AssetName,
                        AssetType         = asset.AssetType,
                        EquipmentVIN      = asset.EquipmentVIN,
                        IconKey           = asset.IconKey,
                        Model             = asset.Model,
                        ModelYear         = asset.ModelYear,
                        LegacyAssetID     = asset.LegacyAssetID,
                        AssetUID          = asset.AssetUID,
                        OwningCustomerUID = device.OwningCustomerUID.Value,
                        ActionUTC         = DateTime.UtcNow,
                        ReceivedUTC       = DateTime.UtcNow
                    };

                    actions.Add(() => assetServices.UpdateAsset(updateAsset));
                }
                actions.Add(() => transactions.Upsert(deviceModel));
                actions.Add(() => transactions.Upsert <DbDevicePersonality>(devicePersonality));
                actions.Add(() => transactions.Publish(kafkaMessageList));
                return(transactions.Execute(actions));
            }
        }