init
This commit is contained in:
36
Common/DTS.Common/Classes/Locking/LockError.cs
Normal file
36
Common/DTS.Common/Classes/Locking/LockError.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
|
||||
namespace DTS.Common.Classes.Locking
|
||||
{
|
||||
public class LockError
|
||||
{
|
||||
public int ErrorCode { get; private set; }
|
||||
public string Message { get; private set; }
|
||||
public string LockingUser { get; private set; }
|
||||
public string LockingMachine { get; private set; }
|
||||
|
||||
public bool LockStolen => ErrorCode == LOCKSTOLEN_ERROR;
|
||||
public bool LockLost => ErrorCode == LOCKDOESNTEXIST_ERROR;
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{ErrorCode} - {Message}";
|
||||
}
|
||||
|
||||
public LockError(int error, string message, string user = null, string machine = null)
|
||||
{
|
||||
ErrorCode = error;
|
||||
Message = message;
|
||||
LockingUser = user ?? string.Empty;
|
||||
LockingMachine = machine ?? string.Empty;
|
||||
}
|
||||
//14782 Improve lost remote db connection modal dialogs
|
||||
//this can apparently be returned by sqlclient on failed lock maintenance issues
|
||||
public const int BAD_NETPATH_ERROR = 994;
|
||||
//14782 Improve lost remote db connection modal dialogs
|
||||
//system error of semaphore timeout, returned by sqlclient on failed to connect issues
|
||||
public const int SEM_TIMEOUT_ERROR = 995;
|
||||
public const int ITEM_NOT_FOUND = 996;
|
||||
public const int LOCKDOESNTEXIST_ERROR = 997;
|
||||
public const int LOCKSTOLEN_ERROR = 998;
|
||||
public const int UNKNOWN_ERROR = 999;
|
||||
}
|
||||
}
|
||||
30
Common/DTS.Common/Classes/Locking/LockRecord.cs
Normal file
30
Common/DTS.Common/Classes/Locking/LockRecord.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
|
||||
namespace DTS.Common.Classes.Locking
|
||||
{
|
||||
public class LockRecord
|
||||
{
|
||||
public string LockingUserName { get; }
|
||||
public string LockingMachineName { get; }
|
||||
public DateTime CreationTime { get; }
|
||||
public DateTime LastUpdated { get; }
|
||||
public string ItemKey { get; }
|
||||
/// <summary>
|
||||
/// this is the item category (as a value in the db - LockCategories)
|
||||
/// </summary>
|
||||
public int ItemCategory { get; }
|
||||
public int ItemId { get; }
|
||||
|
||||
public LockRecord(string user, string machine, DateTime createTime, DateTime lastUpdate, string itemKey, int itemCategory, int itemId)
|
||||
{
|
||||
LockingUserName = user;
|
||||
LockingMachineName = machine;
|
||||
CreationTime = createTime;
|
||||
LastUpdated = lastUpdate;
|
||||
ItemKey = itemKey;
|
||||
ItemCategory = itemCategory;
|
||||
ItemId = itemId;
|
||||
}
|
||||
}
|
||||
}
|
||||
191
Common/DTS.Common/Classes/Locking/SensorsLocking.cs
Normal file
191
Common/DTS.Common/Classes/Locking/SensorsLocking.cs
Normal file
@@ -0,0 +1,191 @@
|
||||
using DTS.Common.Events;
|
||||
using DTS.Common.SharedResource.Strings;
|
||||
using DTS.Common.Utilities.Logging;
|
||||
using Prism.Events;
|
||||
using Prism.Events;
|
||||
using Prism.Ioc;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing.Text;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Interop;
|
||||
|
||||
namespace DTS.Common.Classes.Locking
|
||||
{
|
||||
public class SensorsLocking
|
||||
{
|
||||
private LockRecord[] _existingLocks = null;
|
||||
private static readonly object MyLock = new object();
|
||||
|
||||
private CancellationTokenSource _cancellationTokenSource = null;
|
||||
private readonly Task _updateTask = null;
|
||||
private const int MAX_WAIT_TIME_MS = 10000;
|
||||
/// <summary>
|
||||
/// returns false if stop was not needed or failed
|
||||
/// returns true if stop was completed
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private bool StopUpdating()
|
||||
{
|
||||
if (null == _cancellationTokenSource || null == _existingLocks) { return false; }
|
||||
lock (MyLock)
|
||||
{
|
||||
_cancellationTokenSource.Cancel();
|
||||
if (!_updateTask.Wait(MAX_WAIT_TIME_MS))
|
||||
{
|
||||
APILogger.Log("Failed to stop updating locks");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public void Unlock(bool bCheckLock, string userName, int userId)
|
||||
{
|
||||
if (!StopUpdating()) { return; }
|
||||
|
||||
lock (MyLock)
|
||||
{
|
||||
if (null == _existingLocks || 0 == _existingLocks.Length) { return; }
|
||||
}
|
||||
try
|
||||
{
|
||||
FreeLocks();
|
||||
}
|
||||
catch (Exception ex) { APILogger.Log(ex); }
|
||||
}
|
||||
private void FreeLocks()
|
||||
{
|
||||
LockRecord[] locks;
|
||||
lock (MyLock)
|
||||
{
|
||||
if (null == _existingLocks || 0 == _existingLocks.Length) { return; }
|
||||
locks = _existingLocks.ToArray();
|
||||
}
|
||||
foreach( var record in locks)
|
||||
{
|
||||
try
|
||||
{
|
||||
LockManager.FreeLoc
|
||||
}
|
||||
catch(Exception ex) { APILogger.Log(ex); }
|
||||
}
|
||||
}
|
||||
private bool CheckLock(bool publishErrors, string userName, int userId, LockRecord lockRecord, out LockError lockError)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
/// <summary>
|
||||
/// performs an immediate update of locks, publishes an error if the lock could not be updated
|
||||
/// </summary>
|
||||
/// <param name="publishErrors">whether to publish when there's an error or not</param>
|
||||
/// <returns></returns>
|
||||
private bool CheckLocks(bool publishErrors, string userName, int userId)
|
||||
{
|
||||
try
|
||||
{
|
||||
LockRecord[] records;
|
||||
lock (MyLock)
|
||||
{
|
||||
//FB 18006 Prevent null reference exception
|
||||
if (_existingLocks == null || 0 == _existingLocks.Length)
|
||||
{
|
||||
APILogger.Log("_existingLock is null");
|
||||
return false;
|
||||
}
|
||||
//create a local copy of the locks so we can update them without the lock
|
||||
records = _existingLocks.ToArray();
|
||||
}
|
||||
var failed = new List<LockRecord>();
|
||||
var errors = new List<LockError>();
|
||||
foreach (var record in records)
|
||||
{
|
||||
LockError lockError;
|
||||
if (!CheckLock(publishErrors, userName, userId, record, out lockError))
|
||||
{
|
||||
failed.Add(record);
|
||||
errors.Add(lockError);
|
||||
}
|
||||
}
|
||||
if (failed.Any() && errors.Any())
|
||||
{
|
||||
if (publishErrors)
|
||||
{
|
||||
var msg = GetErrorMessage(errors.ToArray(), records.ToArray());
|
||||
var eventAggregator = ContainerLocator.Container.Resolve<IEventAggregator>();
|
||||
eventAggregator.GetEvent<PageErrorEvent>().Publish(new PageErrorArg(new[] { msg }, null));
|
||||
}
|
||||
//if we failed to update ANY of them we need to wipe out our selection
|
||||
lock (MyLock)
|
||||
{
|
||||
_existingLocks = new LockRecord[0];
|
||||
}
|
||||
WipeOutChanges();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
APILogger.Log(ex);
|
||||
if (publishErrors)
|
||||
{
|
||||
var eventAggregator = ContainerLocator.Container.Resolve<IEventAggregator>();
|
||||
eventAggregator.GetEvent<PageErrorEvent>().Publish(new PageErrorArg(
|
||||
new[] { $"{StringResources.FailedToUpdateLock}, {ex.Message}" }, null));
|
||||
}
|
||||
_cancellationTokenSource?.Cancel();
|
||||
lock (MyLock)
|
||||
{
|
||||
_existingLocks = new LockRecord[0];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
private void WipeOutChanges()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
private static string GetErrorMessage(LockError[] errors, LockRecord[] lockRecords )
|
||||
{
|
||||
StringBuilder sb = new StringBuilder(0);
|
||||
|
||||
for( var i = 0; i < errors.Length && i < lockRecords.Length; i ++)
|
||||
{
|
||||
var error = errors[i];
|
||||
var record = lockRecords[i];
|
||||
if (error.LockStolen)
|
||||
{
|
||||
sb.AppendLine(string.Format(StringResources.LockStolen_Sensors, record.ItemKey, error.LockingUser, error.LockingMachine));
|
||||
}
|
||||
else if(error.LockLost)
|
||||
{
|
||||
sb.AppendLine(string.Format(StringResources.LockLost_Sensors, record.ItemKey));
|
||||
}
|
||||
else
|
||||
{
|
||||
sb.AppendLine($"{StringResources.FailedToUpdateLock} - {record.ItemKey}");
|
||||
}
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
/// <summary>
|
||||
/// returns true if the lockrecord matches the sensor currently locked in this control
|
||||
/// 14340 sensor lost/locked - unlocked on sensor import
|
||||
/// </summary>
|
||||
/// <param name="lockRecord"></param>
|
||||
/// <returns></returns>
|
||||
public bool IsCurrentlyLocked(LockRecord lockRecord)
|
||||
{
|
||||
lock (MyLock)
|
||||
{
|
||||
if (null == _existingLocks) { return false; }
|
||||
|
||||
return Array.Exists(_existingLocks, x => x.ItemId == lockRecord.ItemId && x.ItemCategory == lockRecord.ItemCategory);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user