Beispiel #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));
        }
Beispiel #2
0
        public bool UpdateDeviceProperties(UpdateDeviceProperties device, Guid deviceUid)
        {
            DbDeviceType deviceType    = deviceTypesCache.First(x => string.Equals(x.Key, device.DeviceType, StringComparison.InvariantCultureIgnoreCase)).Value;
            var          currentUTC    = DateTime.UtcNow;
            var          devicePayload = new UpdateDevicePayload
            {
                ActionUTC = currentUTC,
                CellularFirmwarePartnumber = device.CellularFirmwarePartnumber,
                DataLinkType                = device.DataLinkType,
                DeviceSerialNumber          = device.DeviceSerialNumber,
                DeviceType                  = device.DeviceType,
                DeviceUID                   = deviceUid,
                GatewayFirmwarePartNumber   = device.GatewayFirmwarePartNumber,
                MainboardSoftwareVersion    = device.MainboardSoftwareVersion,
                ModuleType                  = device.ModuleType,
                NetworkFirmwarePartnumber   = device.NetworkFirmwarePartnumber,
                RadioFirmwarePartNumber     = device.RadioFirmwarePartNumber,
                ReceivedUTC                 = device.ReceivedUTC,
                SatelliteFirmwarePartnumber = device.SatelliteFirmwarePartnumber,
                Description                 = device.Description
            };


            var existingDeviceProp    = GetExistingDeviceProperties(deviceUid);
            var existingPersonalities = GetExistingDevicePersonalities(devicePayload.DeviceUID);
            var devicePersonality     = GetDevicePersonalities(devicePayload, existingPersonalities, deviceType);

            devicePayload = AppendExistingDeviceProperties(devicePayload, existingDeviceProp);
            devicePayload.OwningCustomerUID = GetExistingOwningCustomerUid(deviceUid);
            var deviceState = (DeviceStateEnum)Enum.Parse(typeof(DeviceStateEnum), existingDeviceProp.DeviceState, true);

            devicePayload.ReceivedUTC = DateTime.UtcNow;

            bool equal = CheckExistingDevicePropertiesForUpdate(devicePayload, deviceState, devicePayload.OwningCustomerUID, 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     = deviceUid.ToString(),
                        Message = new { UpdateDeviceEvent = mapper.Map <UpdateDevicePayload, UpdateDeviceEvent>(devicePayload) },
                        Topic   = topic
                    };
                    kafkaMessageList.Add(kafkaMessage);
                });

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

                var 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_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,
                    fk_DeviceStatusID           = deviceState.GetHashCode()
                };
                actions.Add(() => transactions.Upsert(deviceModel));
                actions.Add(() => transactions.Upsert <DbDevicePersonality>(devicePersonality));
                actions.Add(() => transactions.Publish(kafkaMessageList));
                return(transactions.Execute(actions));
            }
        }
Beispiel #3
0
        /// <summary>
        /// Get device personalities
        /// </summary>
        /// <param name="devicePayLoad"></param>
        /// <param name="personalities"></param>
        /// <returns></returns>
        private List <DbDevicePersonality> GetDevicePersonalities(DevicePayload devicePayLoad, List <DbDevicePersonality> personalities, DbDeviceType devicetype)
        {
            var  supportedPersonalities = GetDeviceSupportedPersonality(devicetype.DefaultValueJson);
            bool isCreateEvent          = personalities == null;  //this will be NULL for create case

            personalities = personalities ?? new List <DbDevicePersonality>();
            var populatedPersonalities = new List <DbDevicePersonality>();

            if (supportedPersonalities != null)
            {
                var personality = new DbDevicePersonality();
                foreach (var item in supportedPersonalities)
                {
                    string desc         = null;
                    var    propertyInfo = typeof(DevicePayload).GetProperty(item.PersonalityTypeName);
                    var    value        = propertyInfo != null?propertyInfo.GetValue(devicePayLoad, null) : null;

                    personality = personalities.FirstOrDefault(x => x.fk_PersonalityTypeID == item.DevicePersonalityTypeID);
                    if (personality != null)
                    {
                        personalities.Remove(personality);
                    }
                    string personalityValue = null;
                    if (value == null)
                    {
                        personalityValue = personality != null ? personality.PersonalityValue : null;
                    }
                    else                     // for create event Empty alone should be treated as NULL
                    {
                        if (isCreateEvent)
                        {
                            personalityValue = value.ToString() == string.Empty ? null : value.ToString();
                        }
                        else
                        {
                            personalityValue = value?.ToString();
                        }
                    }
                    if (personalityValue != null)
                    {
                        desc = !string.IsNullOrEmpty(value?.ToString()) && !string.IsNullOrEmpty(devicePayLoad.Description) ? devicePayLoad.Description : personality?.PersonalityDesc;

                        personalities.Add(
                            new DbDevicePersonality
                        {
                            DevicePersonalityUID = personality == null ? Guid.NewGuid() : personality.DevicePersonalityUID,
                            fk_DeviceUID         = devicePayLoad.DeviceUID,
                            fk_PersonalityTypeID = item.DevicePersonalityTypeID,
                            PersonalityDesc      = desc,
                            PersonalityValue     = value?.ToString() == string.Empty ? null : personalityValue,
                            RowUpdatedUTC        = DateTime.UtcNow
                        });
                    }
                }
            }
            return(personalities);
        }
Beispiel #4
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));
            }
        }