init
This commit is contained in:
@@ -0,0 +1,130 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using DTS.Common.Classes.Locking;
|
||||
using DTS.Common.Import.Interfaces;
|
||||
using DTS.Common.SharedResource.Strings;
|
||||
using DTS.Common.Storage;
|
||||
using DTS.Common.Utilities.Logging;
|
||||
using DTS.SensorDB;
|
||||
using DTS.Slice.Users;
|
||||
|
||||
//FB 36740
|
||||
namespace DTS.Common.Import.DatabaseLocks
|
||||
{
|
||||
public class LockImportSensors : ILockImport
|
||||
{
|
||||
private readonly User _currentUser;
|
||||
private readonly double _strandedLockTimeoutMinutes;
|
||||
|
||||
/// <summary>
|
||||
/// list of all sensors the user has locked
|
||||
/// </summary>
|
||||
private readonly List<LockRecord> _lockedSensors = new List<LockRecord>();
|
||||
/// <summary>
|
||||
/// list of all sensors the user attempted to lock but another user has the lock
|
||||
/// </summary>
|
||||
private readonly List<LockRecord> _contendedSensors = new List<LockRecord>();
|
||||
public bool Contended { get => _contendedSensors.Any(); }
|
||||
|
||||
public LockImportSensors(User currentUser, double strandedLockTimeoutMinutes)
|
||||
{
|
||||
_currentUser = currentUser;
|
||||
_strandedLockTimeoutMinutes = strandedLockTimeoutMinutes;
|
||||
}
|
||||
|
||||
public void FreeLock(ref ImportObject importObject)
|
||||
{
|
||||
if (!_lockedSensors.Any())
|
||||
{
|
||||
return;
|
||||
}
|
||||
foreach (var record in _lockedSensors)
|
||||
{
|
||||
LockManager.FreeLock(record.ItemId, _currentUser.UserName, _currentUser.Id, out var lockError, record.ItemCategory);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetLock(ref ImportObject importObject, ref StringBuilder message)
|
||||
{
|
||||
var sensorLookup = SensorsCollection.SensorsList.GetAllSensors(true).ToDictionary(s => s.SerialNumber);
|
||||
|
||||
_lockedSensors.Clear();
|
||||
_contendedSensors.Clear();
|
||||
|
||||
foreach (var sensorSerialNumber in importObject.Sensors().Select(s => s.SerialNumber))
|
||||
{
|
||||
if (!sensorLookup.ContainsKey(sensorSerialNumber)) { continue; } //no lock needed
|
||||
try
|
||||
{
|
||||
var wasAbleToLock = LockManager.LockItem(sensorSerialNumber, sensorLookup[sensorSerialNumber].DatabaseId, LockManager.ItemCategories.Sensor, _currentUser.UserName,
|
||||
_currentUser.Id, out var existingLock, out var lockError);
|
||||
if (!wasAbleToLock)
|
||||
{
|
||||
var lockOutOfDate = existingLock.LastUpdated.AddMinutes(_strandedLockTimeoutMinutes) < DateTime.Now;
|
||||
var IHaveTheLock = existingLock.LockingUserName == _currentUser.UserName && existingLock.LockingMachineName == Environment.MachineName;
|
||||
|
||||
if (lockOutOfDate || IHaveTheLock)
|
||||
{
|
||||
//lock is expired (or already belongs to us), either way we can claim it
|
||||
LockManager.FreeLock(existingLock.ItemId, _currentUser.UserName, _currentUser.Id, out lockError, existingLock.ItemCategory);
|
||||
LockManager.LockItem(existingLock.ItemKey, existingLock.ItemId, LockManager.ItemCategories.Sensor, _currentUser.UserName,
|
||||
_currentUser.Id, out existingLock, out lockError);
|
||||
_lockedSensors.Add(existingLock);
|
||||
}
|
||||
else
|
||||
{
|
||||
//lock is not expired, we need admin + confirmation to steal the lock
|
||||
_contendedSensors.Add(existingLock);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_lockedSensors.Add(existingLock); //successfully locked
|
||||
}
|
||||
}
|
||||
catch (Exception ex) { APILogger.Log(ex); }
|
||||
}
|
||||
if (!_contendedSensors.Any())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
message.AppendLine(StringResources.ImportTestSetup_TestsLocked);
|
||||
foreach (var contended in _contendedSensors)
|
||||
{
|
||||
message.AppendLine($"{contended.ItemKey} by {contended.LockingUserName} on {contended.LockingMachineName}");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public bool StealLock(bool proceed)
|
||||
{
|
||||
if (!_contendedSensors.Any())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!_currentUser.IsAdmin)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
//steal locks if user oks it
|
||||
if (!proceed)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (var contendedSensor in _contendedSensors)
|
||||
{
|
||||
LockManager.FreeLock(contendedSensor.ItemId, _currentUser.UserName, _currentUser.Id, out var lockError, contendedSensor.ItemCategory);
|
||||
LockManager.LockItem(contendedSensor.ItemKey, contendedSensor.ItemId, LockManager.ItemCategories.Sensor, _currentUser.UserName, _currentUser.Id, out var newLockRecord, out lockError);
|
||||
_lockedSensors.Add(newLockRecord);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
using DTS.Common.Classes;
|
||||
using DTS.Common.Enums.Sensors;
|
||||
using DTS.Common.Import.Interfaces;
|
||||
using Microsoft.VisualBasic.FileIO;
|
||||
|
||||
namespace DTS.Common.Import.Parsers.CSV
|
||||
{
|
||||
public class Version6CSVTestParser : IParseCSVTest
|
||||
{
|
||||
public int Version => 6;
|
||||
public void ParseVersion(TextFieldParser parser, TestSetupImportData tsid)
|
||||
{
|
||||
var foundHeader = false;
|
||||
string[] tokens = null;
|
||||
while (!foundHeader && !parser.EndOfData)
|
||||
{
|
||||
tokens = parser.ReadFields();
|
||||
if (null == tokens) { return; }
|
||||
if (0 == tokens.Length || string.IsNullOrEmpty(tokens[0])) { continue; }
|
||||
var field = CSVImportTags.GetTagForString(tokens[0]);
|
||||
var version = CSVImportTags.GetVersionForTag(field);
|
||||
if (version != 6) { return; } //no version 5 tokens in here ...
|
||||
foundHeader = true;
|
||||
}
|
||||
|
||||
while (!parser.EndOfData)
|
||||
{
|
||||
var tokens2 = parser.ReadFields();
|
||||
if (null == tokens2 || 0 == tokens2.Length || string.IsNullOrWhiteSpace(tokens2[1]))
|
||||
{
|
||||
//we found a new line, we are done with this table
|
||||
break;
|
||||
}
|
||||
var dasSerial = string.Empty;
|
||||
for (var i = 0; i < tokens.Length && i < tokens2.Length; i++)
|
||||
{
|
||||
var field = CSVImportTags.GetTagForString(tokens[i]);
|
||||
var val = tokens2[i];
|
||||
|
||||
switch (field)
|
||||
{
|
||||
case CSVImportTags.Tags.DASSerial:
|
||||
dasSerial = val;
|
||||
break;
|
||||
case CSVImportTags.Tags.DASSampleRate:
|
||||
{
|
||||
if (int.TryParse(val, out var iTemp))
|
||||
{
|
||||
tsid.SampleRateForDAS[dasSerial] = iTemp;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case CSVImportTags.Tags.PTPDomainId:
|
||||
{
|
||||
if (uint.TryParse(val, out var iTemp))
|
||||
{
|
||||
tsid.DomainIdForDAS[dasSerial] = iTemp;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case CSVImportTags.Tags.ClockMaster:
|
||||
{
|
||||
if (bool.TryParse(val, out var bTemp))
|
||||
{
|
||||
tsid.IsClockMaster[dasSerial] = bTemp;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user