Fix: Duplicate Device Models
Stop future duplicate device models and repair any existing duplicates SEE: http://discoict.com.au/forum/support/2013/2/duplicate-device-models.aspx
This commit is contained in:
@@ -0,0 +1,61 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Data.Entity;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Disco.Data.Repository;
|
||||||
|
using Disco.Models.Repository;
|
||||||
|
|
||||||
|
namespace Disco.BI
|
||||||
|
{
|
||||||
|
public static class DeviceModelBI
|
||||||
|
{
|
||||||
|
|
||||||
|
// Added: 2013-02-07 G#
|
||||||
|
// Ensure Duplicate Device Models are not created by creating only one Device Model at a time
|
||||||
|
// http://www.discoict.com.au/forum/support/2013/2/duplicate-device-models.aspx
|
||||||
|
// Thanks to Michael Vorster for reporting this problem.
|
||||||
|
private static object _CreateDeviceModelLock = new object();
|
||||||
|
public static Tuple<DeviceModel, bool> GetOrCreateDeviceModel(this DbSet<DeviceModel> DeviceModelsSet, string Manufacturer, string Model, string ModelType)
|
||||||
|
{
|
||||||
|
// Already Exists?
|
||||||
|
var deviceModel = DeviceModelsSet.FirstOrDefault(dm => dm.Manufacturer == Manufacturer && dm.Model == Model);
|
||||||
|
if (deviceModel == null)
|
||||||
|
{
|
||||||
|
// Ensure only one thread/request at a time
|
||||||
|
lock (_CreateDeviceModelLock)
|
||||||
|
{
|
||||||
|
// Check again now that lock is enforced
|
||||||
|
deviceModel = DeviceModelsSet.FirstOrDefault(dm => dm.Manufacturer == Manufacturer && dm.Model == Model);
|
||||||
|
|
||||||
|
if (deviceModel == null)
|
||||||
|
{
|
||||||
|
// Create the Device Model in a different DataContext so we don't have to commit unrelated changes
|
||||||
|
using (DiscoDataContext dbContext = new DiscoDataContext())
|
||||||
|
{
|
||||||
|
var addDeviceModel = new DeviceModel
|
||||||
|
{
|
||||||
|
Manufacturer = Manufacturer,
|
||||||
|
Model = Model,
|
||||||
|
ModelType = ModelType,
|
||||||
|
Description = string.Format("{0} {1}", Manufacturer, Model)
|
||||||
|
};
|
||||||
|
dbContext.DeviceModels.Add(addDeviceModel);
|
||||||
|
dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtain the Device Model with the in-scope DataContext
|
||||||
|
// - Overhead acknowledged, but reasonable given the infrequency of occurrence
|
||||||
|
deviceModel = DeviceModelsSet.FirstOrDefault(dm => dm.Manufacturer == Manufacturer && dm.Model == Model);
|
||||||
|
return new Tuple<DeviceModel, bool>(deviceModel, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Tuple<DeviceModel,bool>(deviceModel, false);
|
||||||
|
}
|
||||||
|
// Added: 2013-02-07 G#
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -241,23 +241,14 @@ namespace Disco.BI.DeviceBI
|
|||||||
EnrolmentLog.LogSessionProgress(sessionId, 50, "New Device, Building Disco Instance");
|
EnrolmentLog.LogSessionProgress(sessionId, 50, "New Device, Building Disco Instance");
|
||||||
EnrolmentLog.LogSessionTaskAddedDevice(sessionId, Request.DeviceSerialNumber);
|
EnrolmentLog.LogSessionTaskAddedDevice(sessionId, Request.DeviceSerialNumber);
|
||||||
DeviceProfile deviceProfile = dbContext.DeviceProfiles.Find(dbContext.DiscoConfiguration.DeviceProfiles.DefaultDeviceProfileId);
|
DeviceProfile deviceProfile = dbContext.DeviceProfiles.Find(dbContext.DiscoConfiguration.DeviceProfiles.DefaultDeviceProfileId);
|
||||||
DeviceModel deviceModel = dbContext.DeviceModels.Where(dm => dm.Manufacturer == Request.DeviceManufacturer.Trim() && dm.Model == Request.DeviceModel.Trim()).FirstOrDefault();
|
|
||||||
if (deviceModel == null)
|
var deviceModelResult = dbContext.DeviceModels.GetOrCreateDeviceModel(Request.DeviceManufacturer.Trim(), Request.DeviceModel.Trim(), Request.DeviceModel.Trim());
|
||||||
{
|
DeviceModel deviceModel = deviceModelResult.Item1;
|
||||||
deviceModel = new DeviceModel
|
if (deviceModelResult.Item2)
|
||||||
{
|
EnrolmentLog.LogSessionTaskCreatedDeviceModel(sessionId, Request.DeviceSerialNumber, deviceModelResult.Item1.Manufacturer, deviceModelResult.Item1.Model);
|
||||||
Manufacturer = Request.DeviceManufacturer.Trim(),
|
|
||||||
Model = Request.DeviceModel.Trim(),
|
|
||||||
ModelType = Request.DeviceModelType.Trim(),
|
|
||||||
Description = string.Format("{0} {1}", Request.DeviceManufacturer.Trim(), Request.DeviceModel)
|
|
||||||
};
|
|
||||||
dbContext.DeviceModels.Add(deviceModel);
|
|
||||||
EnrolmentLog.LogSessionTaskCreatedDeviceModel(sessionId, Request.DeviceSerialNumber, Request.DeviceManufacturer.Trim(), Request.DeviceModel.Trim());
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
|
||||||
EnrolmentLog.LogSessionDevice(sessionId, Request.DeviceSerialNumber, deviceModel.Id);
|
EnrolmentLog.LogSessionDevice(sessionId, Request.DeviceSerialNumber, deviceModel.Id);
|
||||||
}
|
|
||||||
RepoDevice = new Device
|
RepoDevice = new Device
|
||||||
{
|
{
|
||||||
SerialNumber = Request.DeviceSerialNumber,
|
SerialNumber = Request.DeviceSerialNumber,
|
||||||
@@ -277,23 +268,13 @@ namespace Disco.BI.DeviceBI
|
|||||||
EnrolmentLog.LogSessionTaskUpdatingDevice(sessionId, Request.DeviceSerialNumber);
|
EnrolmentLog.LogSessionTaskUpdatingDevice(sessionId, Request.DeviceSerialNumber);
|
||||||
if (!RepoDevice.DeviceModelId.HasValue || RepoDevice.DeviceModelId.Value == 1)
|
if (!RepoDevice.DeviceModelId.HasValue || RepoDevice.DeviceModelId.Value == 1)
|
||||||
{
|
{
|
||||||
DeviceModel deviceModel = dbContext.DeviceModels.Where(dm => dm.Manufacturer == Request.DeviceManufacturer.Trim() && dm.Model == Request.DeviceModel.Trim()).FirstOrDefault();
|
var deviceModelResult = dbContext.DeviceModels.GetOrCreateDeviceModel(Request.DeviceManufacturer.Trim(), Request.DeviceModel.Trim(), Request.DeviceModel.Trim());
|
||||||
if (deviceModel == null)
|
DeviceModel deviceModel = deviceModelResult.Item1;
|
||||||
{
|
if (deviceModelResult.Item2)
|
||||||
deviceModel = new DeviceModel
|
EnrolmentLog.LogSessionTaskCreatedDeviceModel(sessionId, Request.DeviceSerialNumber, deviceModelResult.Item1.Manufacturer, deviceModelResult.Item1.Model);
|
||||||
{
|
|
||||||
Manufacturer = Request.DeviceManufacturer.Trim(),
|
|
||||||
Model = Request.DeviceModel.Trim(),
|
|
||||||
ModelType = Request.DeviceModelType.Trim(),
|
|
||||||
Description = string.Format("{0} {1}", Request.DeviceManufacturer.Trim(), Request.DeviceModel.Trim())
|
|
||||||
};
|
|
||||||
dbContext.DeviceModels.Add(deviceModel);
|
|
||||||
EnrolmentLog.LogSessionTaskCreatedDeviceModel(sessionId, Request.DeviceSerialNumber, Request.DeviceManufacturer.Trim(), Request.DeviceModel.Trim());
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
|
||||||
EnrolmentLog.LogSessionDevice(sessionId, Request.DeviceSerialNumber, deviceModel.Id);
|
EnrolmentLog.LogSessionDevice(sessionId, Request.DeviceSerialNumber, deviceModel.Id);
|
||||||
}
|
|
||||||
RepoDevice.DeviceModel = deviceModel;
|
RepoDevice.DeviceModel = deviceModel;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -396,23 +377,15 @@ namespace Disco.BI.DeviceBI
|
|||||||
EnrolmentLog.LogSessionProgress(sessionId, 30, "New Device, Creating Disco Instance");
|
EnrolmentLog.LogSessionProgress(sessionId, 30, "New Device, Creating Disco Instance");
|
||||||
EnrolmentLog.LogSessionTaskAddedDevice(sessionId, Request.DeviceSerialNumber);
|
EnrolmentLog.LogSessionTaskAddedDevice(sessionId, Request.DeviceSerialNumber);
|
||||||
DeviceProfile deviceProfile = dbContext.DeviceProfiles.Find(dbContext.DiscoConfiguration.DeviceProfiles.DefaultDeviceProfileId);
|
DeviceProfile deviceProfile = dbContext.DeviceProfiles.Find(dbContext.DiscoConfiguration.DeviceProfiles.DefaultDeviceProfileId);
|
||||||
DeviceModel deviceModel = dbContext.DeviceModels.Where(dm => dm.Manufacturer == Request.DeviceManufacturer.Trim() && dm.Model == Request.DeviceModel.Trim()).FirstOrDefault();
|
|
||||||
if (deviceModel == null)
|
|
||||||
{
|
var deviceModelResult = dbContext.DeviceModels.GetOrCreateDeviceModel(Request.DeviceManufacturer.Trim(), Request.DeviceModel.Trim(), Request.DeviceModel.Trim());
|
||||||
deviceModel = new DeviceModel
|
DeviceModel deviceModel = deviceModelResult.Item1;
|
||||||
{
|
if (deviceModelResult.Item2)
|
||||||
Manufacturer = Request.DeviceManufacturer.Trim(),
|
EnrolmentLog.LogSessionTaskCreatedDeviceModel(sessionId, Request.DeviceSerialNumber, deviceModelResult.Item1.Manufacturer, deviceModelResult.Item1.Model);
|
||||||
Model = Request.DeviceModel.Trim(),
|
|
||||||
ModelType = Request.DeviceModelType.Trim(),
|
|
||||||
Description = string.Format("{0} {1}", Request.DeviceManufacturer.Trim(), Request.DeviceModel.Trim())
|
|
||||||
};
|
|
||||||
dbContext.DeviceModels.Add(deviceModel);
|
|
||||||
EnrolmentLog.LogSessionTaskCreatedDeviceModel(sessionId, Request.DeviceSerialNumber, Request.DeviceManufacturer.Trim(), Request.DeviceModel.Trim());
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
|
||||||
EnrolmentLog.LogSessionDevice(sessionId, Request.DeviceSerialNumber, deviceModel.Id);
|
EnrolmentLog.LogSessionDevice(sessionId, Request.DeviceSerialNumber, deviceModel.Id);
|
||||||
}
|
|
||||||
RepoDevice = new Device
|
RepoDevice = new Device
|
||||||
{
|
{
|
||||||
SerialNumber = Request.DeviceSerialNumber,
|
SerialNumber = Request.DeviceSerialNumber,
|
||||||
@@ -432,28 +405,14 @@ namespace Disco.BI.DeviceBI
|
|||||||
EnrolmentLog.LogSessionProgress(sessionId, 30, "Existing Device, Updating Disco Instance");
|
EnrolmentLog.LogSessionProgress(sessionId, 30, "Existing Device, Updating Disco Instance");
|
||||||
EnrolmentLog.LogSessionTaskUpdatingDevice(sessionId, Request.DeviceSerialNumber);
|
EnrolmentLog.LogSessionTaskUpdatingDevice(sessionId, Request.DeviceSerialNumber);
|
||||||
|
|
||||||
DeviceModel deviceModel = dbContext.DeviceModels.Where(dm => dm.Manufacturer == Request.DeviceManufacturer.Trim() && dm.Model == Request.DeviceModel.Trim()).FirstOrDefault();
|
var deviceModelResult = dbContext.DeviceModels.GetOrCreateDeviceModel(Request.DeviceManufacturer.Trim(), Request.DeviceModel.Trim(), Request.DeviceModel.Trim());
|
||||||
if (deviceModel == null)
|
DeviceModel deviceModel = deviceModelResult.Item1;
|
||||||
{
|
if (deviceModelResult.Item2)
|
||||||
deviceModel = new DeviceModel
|
EnrolmentLog.LogSessionTaskCreatedDeviceModel(sessionId, Request.DeviceSerialNumber, deviceModelResult.Item1.Manufacturer, deviceModelResult.Item1.Model);
|
||||||
{
|
|
||||||
Manufacturer = Request.DeviceManufacturer.Trim(),
|
|
||||||
Model = Request.DeviceModel.Trim(),
|
|
||||||
ModelType = Request.DeviceModelType.Trim(),
|
|
||||||
Description = string.Format("{0} {1}", Request.DeviceManufacturer.Trim(), Request.DeviceModel)
|
|
||||||
};
|
|
||||||
dbContext.DeviceModels.Add(deviceModel);
|
|
||||||
RepoDevice.DeviceModel = deviceModel;
|
|
||||||
EnrolmentLog.LogSessionTaskCreatedDeviceModel(sessionId, Request.DeviceSerialNumber, Request.DeviceManufacturer.Trim(), Request.DeviceModel.Trim());
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
EnrolmentLog.LogSessionDevice(sessionId, Request.DeviceSerialNumber, deviceModel.Id);
|
||||||
if (!RepoDevice.DeviceModelId.HasValue || RepoDevice.DeviceModelId.Value != deviceModel.Id)
|
|
||||||
{
|
RepoDevice.DeviceModel = deviceModel;
|
||||||
RepoDevice.DeviceModel = deviceModel;
|
|
||||||
}
|
|
||||||
EnrolmentLog.LogSessionDevice(sessionId, Request.DeviceSerialNumber, RepoDevice.DeviceModelId);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!RepoDevice.EnrolledDate.HasValue)
|
if (!RepoDevice.EnrolledDate.HasValue)
|
||||||
RepoDevice.EnrolledDate = DateTime.Now;
|
RepoDevice.EnrolledDate = DateTime.Now;
|
||||||
|
|||||||
@@ -52,6 +52,9 @@ namespace Disco.Data.Repository
|
|||||||
UpdateDeviceModelConfiguration(context);
|
UpdateDeviceModelConfiguration(context);
|
||||||
// Removed: 2013-01-14 G#
|
// Removed: 2013-01-14 G#
|
||||||
//UpdateDeviceModelImageStorage(context);
|
//UpdateDeviceModelImageStorage(context);
|
||||||
|
|
||||||
|
// Added: 2013-02-07 G#
|
||||||
|
UpdateDeviceModelDuplicates(context);
|
||||||
}
|
}
|
||||||
public static void SeedDeviceProfiles(this DiscoDataContext context)
|
public static void SeedDeviceProfiles(this DiscoDataContext context)
|
||||||
{
|
{
|
||||||
@@ -240,31 +243,61 @@ namespace Disco.Data.Repository
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Removed: 2013-01-14 G#
|
// Removed: 2013-01-14 G#
|
||||||
// private static void UpdateDeviceModelImageStorage(this DiscoDataContext dbContext)
|
// private static void UpdateDeviceModelImageStorage(this DiscoDataContext dbContext)
|
||||||
// {
|
// {
|
||||||
//#pragma warning disable 0618
|
//#pragma warning disable 0618
|
||||||
// var updateModels = dbContext.DeviceModels.Where(dm => dm.Image != null);
|
// var updateModels = dbContext.DeviceModels.Where(dm => dm.Image != null);
|
||||||
// if (updateModels.Count() > 0)
|
// if (updateModels.Count() > 0)
|
||||||
// {
|
// {
|
||||||
// var dataStoreLocation = dbContext.ConfigurationItems.Where(ci => ci.Scope == "System" && ci.Key == "DataStoreLocation").Select(ci => ci.Value).FirstOrDefault();
|
// var dataStoreLocation = dbContext.ConfigurationItems.Where(ci => ci.Scope == "System" && ci.Key == "DataStoreLocation").Select(ci => ci.Value).FirstOrDefault();
|
||||||
// if (!string.IsNullOrEmpty(dataStoreLocation) && System.IO.Directory.Exists(dataStoreLocation))
|
// if (!string.IsNullOrEmpty(dataStoreLocation) && System.IO.Directory.Exists(dataStoreLocation))
|
||||||
// {
|
// {
|
||||||
// var deviceModelImagesLocation = System.IO.Path.Combine(dataStoreLocation, "DeviceModelImages");
|
// var deviceModelImagesLocation = System.IO.Path.Combine(dataStoreLocation, "DeviceModelImages");
|
||||||
// if (!System.IO.Directory.Exists(deviceModelImagesLocation))
|
// if (!System.IO.Directory.Exists(deviceModelImagesLocation))
|
||||||
// System.IO.Directory.CreateDirectory(deviceModelImagesLocation);
|
// System.IO.Directory.CreateDirectory(deviceModelImagesLocation);
|
||||||
// foreach (var model in updateModels)
|
// foreach (var model in updateModels)
|
||||||
// {
|
// {
|
||||||
// var modelpath = System.IO.Path.Combine(deviceModelImagesLocation, string.Format("{0}.png", model.Id));
|
// var modelpath = System.IO.Path.Combine(deviceModelImagesLocation, string.Format("{0}.png", model.Id));
|
||||||
|
|
||||||
// if (model.Image != null && model.Image.Length > 0)
|
// if (model.Image != null && model.Image.Length > 0)
|
||||||
// {
|
// {
|
||||||
// System.IO.File.WriteAllBytes(modelpath, model.Image);
|
// System.IO.File.WriteAllBytes(modelpath, model.Image);
|
||||||
// }
|
// }
|
||||||
// model.Image = null;
|
// model.Image = null;
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
//#pragma warning restore 0618
|
//#pragma warning restore 0618
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
// Added: 2013-02-07 G#
|
||||||
|
// Fix previous problem with duplicate device models being created if new devices enrol at the same time
|
||||||
|
// http://www.discoict.com.au/forum/support/2013/2/duplicate-device-models.aspx
|
||||||
|
// Thanks to Michael Vorster for reporting this problem.
|
||||||
|
private static void UpdateDeviceModelDuplicates(this DiscoDataContext dbContext)
|
||||||
|
{
|
||||||
|
var deviceModels = dbContext.DeviceModels.ToList();
|
||||||
|
var duplicateModels = deviceModels.GroupBy(g => string.Format("{0}|{1}", g.Manufacturer, g.Model)).Where(g => g.Count() > 1);
|
||||||
|
foreach (var duplicateModel in duplicateModels)
|
||||||
|
{
|
||||||
|
var primaryModel = duplicateModel.OrderBy(dm => dm.Id).First();
|
||||||
|
|
||||||
|
foreach (var redundantModel in duplicateModel.Where(m => m != primaryModel))
|
||||||
|
{
|
||||||
|
foreach (var affectedDevice in dbContext.Devices.Where(d => d.DeviceModelId == redundantModel.Id))
|
||||||
|
{
|
||||||
|
affectedDevice.DeviceModelId = primaryModel.Id;
|
||||||
|
}
|
||||||
|
if (!redundantModel.Description.EndsWith("** REDUNDANT **"))
|
||||||
|
{
|
||||||
|
if (redundantModel.Description.Length > 484)
|
||||||
|
redundantModel.Description = string.Format("{0} ** REDUNDANT **", redundantModel.Description.Substring(0, 484));
|
||||||
|
else
|
||||||
|
redundantModel.Description = string.Format("{0} ** REDUNDANT **", redundantModel.Description);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// End Added: 2013-02-07 G#
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user