private void _onSuccess(GetSubscriberInfoResult o) { if (o == null) { Log.Warn("Empty reply recieved for GetDriverInfo."); Reply(null); return; } var result = o.DriverInfo; Log.Trace("Add driver {0} to DB", result.DriverId); using (var dbConn = DatabaseManager.DbConn()) { var di = o.DriverInfo; if (di.DataSource == null) { Log.Warn("Driver datasource is NULL, cloning and setting 'Backend'"); di = o.DriverInfo.Clone<DriverInfo>(); di.DataSource = DataSources.Backend; } var exDriver = dbConn.ExecuteBpl(new DriverGetById { DriverId = di.DriverId }); if (exDriver != null) { if ((exDriver.DataSource == DataSources.Backend) || (OscarServer.UseBackendMasterData)) { // TK: we are using OpCo settings from SD (periodic) di.Operator = exDriver.Operator; dbConn.ExecuteBpl(new DriverUpdate { i = di }); } } else { dbConn.ExecuteBpl(new DriverAdd { i = di }); } if ((exDriver == null) || ((exDriver.DataSource == DataSources.Backend) || (OscarServer.UseBackendMasterData))) { dbConn.ExecuteBpl(new ContactRemoveById { ContactId = o.DriverInfo.DriverId }); foreach (var ct in o.DriverInfo.Contacts) { if (ct.Value.IsEmpty() == true) { continue; } var ci = new ContactInfo(); ci.ContactId = o.DriverInfo.DriverId; ci.Type = ct.Type; ci.Value = ct.Value; dbConn.ExecuteBpl(new ContactAdd { i = ci }); } dbConn.ExecuteBpl(new SubscriptionDeleteByDriverId { DriverId = o.DriverInfo.DriverId }); if (o.DriverVehicles != null) { foreach (var vh in o.DriverVehicles) { var dbvi = BackendHelpers.VehicleSave(dbConn, vh); if (dbvi != null) { var subi = new DbSubscriptionInfo(); subi.SubscriptionInfo.SubscriptionId = BplIdentity.Get(Guid.NewGuid().ToString()); subi.SubscriptionInfo.Operator = dbvi.VehicleInfo.Operator; subi.SubscriptionInfo.Type = SubscriptionType.Get("EV"); subi.SubscriptionInfo.Status = SubscriptionStatus.Active; subi.SubscriptionInfo.DriverId = o.DriverInfo.DriverId; subi.SubscriptionInfo.DataSource = DataSources.Backend; subi.VehicleKey = dbvi.VehicleKey; dbConn.ExecuteBpl(new SubscriptionAdd { i = subi }); } } } } } if (_isReplyRequired) { Log.Trace("Reply to node with DriverInfo {0}", result); Reply(result); } }
internal void Handle(PeriodicUpdateRequest request) { var resp = new PeriodicUpdateResponse(); using (var dbConn = DatabaseManager.DbConn()) { var dataSource = DataSources.Vehicle; var opco = _detectOperator(request.OperatorName); var platformId = (request.PlatformVersion == null) ? BplIdentity.Empty : BplIdentity.Get(request.PlatformVersion.Revision.ToString("X08")); var deviceId = MasterDataHelpers.DeviceResolveId(dbConn, request.AutosSerialNumber, request.HardwareId); var codeName = ""; if (platformId.IsEmpty || deviceId.IsEmpty) { DiagnosticsHelpers.ReportIntegrityError(dbConn, "PeriodicUpdateRequest", DumpRequest(), "No device information found (Device Id: '{0}') or no device type (Platform Id: '{1}'). Record skipped.", deviceId, platformId); Reply(resp); return; } var imageInstalls = dbConn.ExecuteBpl(new InstallGetByPlatformIdType { PlatformId = platformId, Type = Mpcr.Services.Oscar.Logistics.SoftwareUpdateType.Image }); if (imageInstalls == null || imageInstalls.Count == 0) { Log.Warn("No installations found for platform {0}. Record skipped.", platformId.LocalId.ToString()); Reply(resp); return; } var deviceType = DeviceType.Get(request.PlatformVersion.Revision); foreach (var cv in request.ClientVersions) { if (cv.Version == null || cv.UpdateType != SoftwareUpdateType.Image) { continue; } var vn = SwlVersion.FromVersion(cv.Version).ToInt32(); var ii = dbConn.ExecuteBpl(new InstallGetByPlatformIdTypeVersionNumber(platformId, (Mpcr.Services.Oscar.Logistics.SoftwareUpdateType)cv.UpdateType, vn)); if (ii == null) { continue; } codeName = ii.CodeName; } if (codeName.IsEmpty()) { DiagnosticsHelpers.ReportIntegrityError(dbConn, "PeriodicUpdateRequest", DumpRequest(), "No code name found for device '{0}'. Record skipped.", deviceId); Reply(resp); return; } var dei = new DeviceInfo(); dei.DeviceId = deviceId; dei.Operator = opco; dei.Type = deviceType; dei.DeviceSN = request.AutosSerialNumber; dei.DialupPhone = PhoneStruct.ToPhone(request.AutOSClient.ModemNumber); dei.SimIMEI = request.AutOSClient.SimIMEI; dei.SimIMSI = request.AutOSClient.SimIMSI; dei.SecretCode = request.AutOSClient.SecretCode; dei.HardwareId = request.HardwareId; dei.DataSource = dataSource; var exDevice = dbConn.ExecuteBpl(new DeviceGetById { DeviceId = dei.DeviceId }); if (exDevice != null) { if (dei.Operator == null) { dei.Operator = exDevice.Operator; } dbConn.ExecuteBpl(new DeviceUpdate { Device = dei }); } else { dbConn.ExecuteBpl(new DeviceAdd { Device = dei }); } // // Master Data update // if (codeName.StartsWith("SP4")) { Log.Trace("Processing old master data update (SP4 path)"); dbConn.BeginTransaction(this); var dbbi = new DbBatteryInfo(); if (request.Battery != null) { dbbi.BatteryInfo = new BatteryInfo(); dbbi.BatteryInfo.BatteryId = (request.Battery.IsFactory) ? BplIdentity.Empty : request.Battery.Id; dbbi.BatteryInfo.Operator = opco; dbbi.BatteryInfo.Type = request.Battery.Type; dbbi.BatteryInfo.BatterySN = request.Battery.SerialNumber; dbbi.BatteryInfo.DataSource = dataSource; dbbi = MasterDataHelpers.BatterySave(dbConn, deviceType, dbbi); } else { // no battery in request - is normal, e.g. while battery is being switched, so nothing to log here dbbi = null; } var dbvi = new DbVehicleInfo(); if (request.Vehicle != null) { dbvi.VehicleInfo = new VehicleInfo(); dbvi.VehicleInfo.VehicleId = (request.Vehicle.IsFactory) ? BplIdentity.Empty : request.Vehicle.Id; dbvi.VehicleInfo.Operator = opco; dbvi.VehicleInfo.Type = request.Vehicle.Type; dbvi.VehicleInfo.VehicleSN = _decodeLegacyVehicleSN(request.Vehicle.SerialNumber); dbvi.VehicleInfo.VehicleVIN = request.Vehicle.VIN; dbvi.VehicleInfo.LicensePlate = request.Vehicle.LicensePlate; dbvi.VehicleInfo.ModelColor = request.Vehicle.Color; dbvi.VehicleInfo.ModelName = request.Vehicle.ModelName; dbvi.VehicleInfo.ModelYear = request.Vehicle.ModelYear; dbvi.VehicleInfo.DeviceId = dei.DeviceId; dbvi.VehicleInfo.DataSource = dataSource; dbvi.BatteryKey = (dbbi != null) ? dbbi.BatteryKey : null; dbvi = MasterDataHelpers.VehicleSave(dbConn, deviceType, dbvi); } else { Log.Warn("No vehicle in request"); dbvi = null; } var dri = new DriverInfo(); if (request.PrimaryDriver != null && request.PrimaryDriver.Type != DriverTypes.Factory) { dri.DriverId = request.PrimaryDriver.Id; dri.Operator = opco; dri.Type = request.PrimaryDriver.Type; if (request.PrimaryDriver.LocalizedName.IsEmpty) { var dict = new Dictionary<Locale, string>(); dict[request.PrimaryDriver.PrimaryLocale] = "{0}, {1}".Substitute(request.PrimaryDriver.FirstName, request.PrimaryDriver.LastName); dri.Name = MultiString.FromDictionary(dict); } else { dri.Name = request.PrimaryDriver.LocalizedName; } dri.Gender = request.PrimaryDriver.Gender; dri.BirthDate = Date.Undefined; dri.DriverSsn = ""; dri.DefaultLocale = request.PrimaryDriver.PrimaryLocale; dri.DefaultVehicleId = (dbvi != null) ? dbvi.VehicleInfo.VehicleId : BplIdentity.Empty; dri.DataSource = dataSource; dri = MasterDataHelpers.DriverSave(dbConn, dri); if ((dbvi != null) && (dri != null)) { var subi = new DbSubscriptionInfo(); subi.SubscriptionInfo = new SubscriptionInfo(); subi.SubscriptionInfo.SubscriptionId = BplIdentity.Get(Guid.NewGuid().ToString()); subi.SubscriptionInfo.Operator = opco; subi.SubscriptionInfo.Type = SubscriptionType.Get("EV"); subi.SubscriptionInfo.Status = SubscriptionStatus.Active; subi.SubscriptionInfo.DriverId = dri.DriverId; subi.SubscriptionInfo.DataSource = dataSource; subi.VehicleKey = dbvi.VehicleKey; dbConn.ExecuteBpl(new SubscriptionAdd { i = subi }); } } else { dri = null; } dbConn.CommitTransaction(); } // // Logistics Data update // if (!deviceId.IsEmpty) { dbConn.BeginTransaction(this); dei = dbConn.ExecuteBpl(new DeviceGetById { DeviceId = deviceId }); if (dei != null) { var devPlatformId = dbConn.ExecuteBpl(new DeviceGetPlatformId { DeviceId = deviceId }); if (platformId != devPlatformId) { if (!platformId.IsEmpty) { dbConn.ExecuteBpl(new DeviceSetPlatformId { DeviceId = dei.DeviceId, PlatformId = platformId }); } else { platformId = devPlatformId; } } if (!platformId.IsEmpty) { dbConn.ExecuteBpl(new DeviceInstallDeleteByDeviceId(deviceId)); foreach (var cv in request.ClientVersions) { if (cv.Version == null) { Log.Warn("Version NULL in type {0}", cv.UpdateType); continue; } var vn = SwlVersion.FromVersion(cv.Version).ToInt32(); Log.Trace("Type {0} Version {1} ({2})", cv.UpdateType, vn, cv.Version); var ii = dbConn.ExecuteBpl(new InstallGetByPlatformIdTypeVersionNumber(platformId, (Mpcr.Services.Oscar.Logistics.SoftwareUpdateType)cv.UpdateType, vn)); if (ii == null) { Log.Warn("No installation PLID {0}, Type {1}, Version {2}", platformId, (Mpcr.Services.Oscar.Logistics.SoftwareUpdateType)cv.UpdateType, vn); continue; } dbConn.ExecuteBpl(new DeviceInstallAdd(deviceId, (Mpcr.Services.Oscar.Logistics.SoftwareUpdateType)cv.UpdateType, ii.InstallId)); } } } dbConn.CommitTransaction(); } var target = LogisticsHelpers.CampaignTargetCheckForUpdate(dbConn, deviceId); if (target != null) { PeriodicUpdate pu = new PeriodicUpdate(); pu.UpdateType = (Mpcr.Messages.AutOS_SW.SoftwareUpdateType)target.CampaignType; pu.UpdateId = new Guid(target.TargetId.LocalId.ToString()); resp.AvailableUpdates.Add(pu); } } Reply(resp); }
internal void Handle(UpdateActivationInfo input) { // guarranteed delivery - must reply true, even in case of exception try { using (var dbConn = DatabaseManager.DbConn()) { dbConn.BeginTransaction(this); var dei = (input.Device != null) ? input.Device.Clone() : null; if (dei == null) { Log.Warn("No device information found or no device type. Record skipped."); Reply(true); return; } dei.DeviceId = MasterDataHelpers.DeviceResolveId(dbConn, dei.DeviceId.LocalId.ToString(), dei.HardwareId); if (dei.DeviceId == null) { Log.Warn("Can not resolve DeviceId. Record skipped."); Reply(true); return; } var exDevice = dbConn.ExecuteBpl(new DeviceGetById { DeviceId = dei.DeviceId }); if (exDevice != null) { dbConn.ExecuteBpl(new DeviceUpdate { Device = dei }); } else { dbConn.ExecuteBpl(new DeviceAdd { Device = dei }); } if (dei.Type == null) { Log.Warn("No device type. Record skipped."); dbConn.CommitTransaction(); Reply(true); return; } var bi = (input.Battery != null) ? new DbBatteryInfo { BatteryInfo = input.Battery.Clone() } : null; if (bi != null) { bi = MasterDataHelpers.BatterySave(dbConn, dei.Type, bi); } var vi = (input.Vehicle != null) ? new DbVehicleInfo { VehicleInfo = input.Vehicle.Clone() } : null; if (vi != null) { vi.VehicleInfo.DeviceId = (dei == null) ? BplIdentity.Empty : dei.DeviceId; vi.BatteryKey = (bi == null) ? null : bi.BatteryKey; vi = MasterDataHelpers.VehicleSave(dbConn, dei.Type, vi); } foreach (var driver in input.Drivers.Clone()) { var dri = MasterDataHelpers.DriverSave(dbConn, driver); if ((vi != null) && (dri != null)) { var subi = new DbSubscriptionInfo(); subi.SubscriptionInfo = new SubscriptionInfo(); subi.SubscriptionInfo.SubscriptionId = BplClass.CreateGuid<SubscriptionInfo>(); subi.SubscriptionInfo.Operator = vi.VehicleInfo.Operator; subi.SubscriptionInfo.Type = SubscriptionType.Get("EV"); subi.SubscriptionInfo.Status = SubscriptionStatus.Active; subi.SubscriptionInfo.DriverId = dri.DriverId; subi.SubscriptionInfo.DataSource = vi.VehicleInfo.DataSource; subi.VehicleKey = vi.VehicleKey; dbConn.ExecuteBpl(new SubscriptionAdd { i = subi }); } } dbConn.CommitTransaction(); } } catch (Exception ex) { DiagnosticsHelpers.ReportIntegrityError(RequestType.CanonicName, DumpRequest(), "Exception: {0}", Log.FormatException(ex)); } Reply(true); }