Skip to content

Commit

Permalink
Fix DbContext issues
Browse files Browse the repository at this point in the history
  • Loading branch information
gpproton committed Jan 26, 2022
1 parent 2481c23 commit 0352186
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 101 deletions.
9 changes: 0 additions & 9 deletions src/Frequency.Device/Data/CaptureContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,6 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
case "Sqlite":
optionsBuilder.UseSqlite(_config.DATA_STORE);
break;
case "Mysql":
optionsBuilder.UseSqlite(_config.DATA_STORE);
break;
case "Postgres":
optionsBuilder.UseSqlite(_config.DATA_STORE);
break;
case "Mssql":
optionsBuilder.UseSqlite(_config.DATA_STORE);
break;
default:
optionsBuilder.UseSqlite(_config.DATA_STORE);
break;
Expand Down
85 changes: 38 additions & 47 deletions src/Frequency.Device/Data/CapturePersist.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,66 +49,57 @@ CaptureContext context
_context = context;
}

public void OldestDelete()
{

}

public async Task<bool> Save(Scan scn)
public async Task<bool> Save(Scan scan)
{
var returnBool = false;

using (var context = _context)
if (await _filter.EarlyFilter(_context, scan))
{
if (await _filter.EarlyFilter(context, scn))
{
context.Add(scn);

var tagUpdate = context.Tags
.FirstOrDefaultAsync(e => e.Id == scn.TagId);
Task.WaitAll(tagUpdate);
await tagUpdate;

if (scn.Tag != null && tagUpdate.Result != null)
{
tagUpdate.Result.LastUpdated = DateTime.Now;
}

var antennaUpdate = context.Antennae
.FirstOrDefaultAsync(e => e.Id == scn.AntennaId);
Task.WaitAll(antennaUpdate);
await antennaUpdate;

if (scn.Antenna != null && antennaUpdate.Result != null)
{
antennaUpdate.Result.LastUpdated = DateTime.Now;
}

var readerUpdate = context.Readers
.FirstOrDefaultAsync(e => e.Id == scn.ReaderId);
Task.WaitAll(readerUpdate);
await readerUpdate;
_context.Add(scan);

if (scn.Reader != null && readerUpdate.Result != null)
{
readerUpdate.Result.LastUpdated = DateTime.Now;
}
var tagUpdate = _context.Tags
.FirstOrDefaultAsync(e => e.Id == scan.TagId);
Task.WaitAll(tagUpdate);
await tagUpdate;

returnBool = true;
if (scan.Tag != null && tagUpdate.Result != null)
{
tagUpdate.Result.LastUpdated = DateTime.Now;
}

try
var antennaUpdate = _context.Antennae
.FirstOrDefaultAsync(e => e.Id == scan.AntennaId);
Task.WaitAll(antennaUpdate);
await antennaUpdate;

if (scan.Antenna != null && antennaUpdate.Result != null)
{
await context.SaveChangesAsync();
antennaUpdate.Result.LastUpdated = DateTime.Now;
}
catch (Exception e)

var readerUpdate = _context.Readers
.FirstOrDefaultAsync(e => e.Id == scan.ReaderId);
Task.WaitAll(readerUpdate);
await readerUpdate;

if (scan.Reader != null && readerUpdate.Result != null)
{
returnBool = false;
_logger.LogCritical(e.ToString().Remove(0, e.ToString().Length) + "Unique Ignored..");
readerUpdate.Result.LastUpdated = DateTime.Now;
}

return returnBool;
returnBool = true;
}

try
{
await _context.SaveChangesAsync();
}
catch (Exception e)
{
returnBool = false;
_logger.LogCritical(e.ToString().Remove(0, e.ToString().Length) + "Unique Ignored..");
}

return returnBool;
}
}
}
13 changes: 8 additions & 5 deletions src/Frequency.Device/Data/PersistRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@ namespace Proton.Frequency.Device.Data
public class PersistRequest
{
public readonly ConfigKey _config;
private readonly CaptureContext _context;
public PersistRequest(
ConfigKey config
ConfigKey config,
CaptureContext context
)
{
_config = config;
_context = context;
}

public void OldestData()
Expand All @@ -47,7 +50,7 @@ public void OldestData()
}


public async Task<Scan> GetScanById(CaptureContext _context, Guid getbyid)
public async Task<Scan> GetScanById(Guid getbyid)
{
var ScanData = _context.Scans
.AsNoTracking()
Expand Down Expand Up @@ -78,7 +81,7 @@ public async Task<Scan> GetScanById(CaptureContext _context, Guid getbyid)
return ScanData.Result;
}

public async Task<Scan> ResolveReader(CaptureContext _context, Scan _Scan, Reader _Reader)
public async Task<Scan> ResolveReader(Scan _Scan, Reader _Reader)
{
var reader = _context.Readers
.AsNoTracking()
Expand All @@ -100,7 +103,7 @@ public async Task<Scan> ResolveReader(CaptureContext _context, Scan _Scan, Reade
return _Scan;
}

public async Task<Scan> ResolveAntenna(CaptureContext _context, Scan _Scan, Antenna _Antenna, string atnData)
public async Task<Scan> ResolveAntenna(Scan _Scan, Antenna _Antenna, string atnData)
{
var antenna = _context.Antennae
.AsNoTracking()
Expand All @@ -120,7 +123,7 @@ public async Task<Scan> ResolveAntenna(CaptureContext _context, Scan _Scan, Ante
return _Scan;
}

public async Task<Scan> ResolveTag(CaptureContext _context, Scan _Scan, Tag _Tag, string _tagData)
public async Task<Scan> ResolveTag(Scan _Scan, Tag _Tag, string _tagData)
{
var tag = _context.Tags
.AsNoTracking()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public SerialPort BuildConnection()

public string SuggestPort()
{
var selectedPort = "/dev/tty.usb_serial";
var selectedPort = "/dev/ttyUSB0";
var ListConnections = ListConnection();
_logger.LogWarning("Incorrect port specified, Suggesting port...");
foreach (string portName in ListConnections)
Expand Down
16 changes: 11 additions & 5 deletions src/Frequency.Device/Handlers/ReaderProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public ReaderProcess(
ConfigKey config,
SerialConnection serialConnection,
WebSync webSync,
KingJoinProtocol selectedProtocol
IReaderProtocol selectedProtocol
)
{
_config = config;
Expand Down Expand Up @@ -216,17 +216,23 @@ void OnDevTest(Object source, System.Timers.ElapsedEventArgs e)
Task.Factory.StartNew(() => _serial.ManuallyReadData(_serialProfile, _selectedProtocol));
}
}
else if (!DevMode && !_serialProfile.IsOpen)
{
int maxRetries = _config.IOT_SERIAL_CONN_RETRY;
while (maxRetries > 0)
{
maxRetries--;
serialOpen();
}
}
}

public void NetworkConnection()
{

}
{ }

public async Task Run()
{
await Task.CompletedTask;

}

}
Expand Down
3 changes: 2 additions & 1 deletion src/Frequency.Device/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Proton.Frequency.Device.Helpers;
using Proton.Frequency.Device.Handlers;
using Proton.Frequency.Device.Handlers.ReaderConnections;
using Proton.Frequency.Device.Protocols;
using Proton.Frequency.Device.Protocols.Readers;

namespace Proton.Frequency.Device
Expand Down Expand Up @@ -53,7 +54,7 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
services.AddSingleton<WebSync>();

/** Extra DI Registration **/
services.AddSingleton<KingJoinProtocol>();
services.AddSingleton<IReaderProtocol, KingJoinProtocol>();
services.AddSingleton<ReaderProcess>();

/** Worker service Registration **/
Expand Down
38 changes: 17 additions & 21 deletions src/Frequency.Device/Protocols/Readers/BaseProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,37 +91,33 @@ PersistRequest persistRequest
public virtual bool AutoRead { get; set; } = true;

// Specify data type before processing response, in case conversion is required.
public virtual string DataType { get; set; } = "hex";
public virtual string DataType { get; set; } = "Hex";

public virtual async Task Log()
{
_logger.LogInformation($"Received {DataType} data: {BitConverter.ToString(ReceivedData).Replace("-", string.Empty).ToLower()}");

_logger.LogInformation($"{DataType} data: {BitConverter.ToString(ReceivedData).Replace("-", string.Empty).ToLower()}");
var decData = await DecodeData();
if (await Persist(decData))
{
await using (var xcix = _context)
try
{
try
{
var getFullScan = await _request.GetScanById(xcix, decData.Id);
if (getFullScan.Reader != null
&& getFullScan.Tag != null
&& getFullScan.Antenna != null)
{
var logVal = $"Reader-UID: {getFullScan.Reader.UniqueId} | " +
$"Type: {getFullScan.Tag.Type} | " +
$"Mode: {getFullScan.Reader.Mode} | " +
$"Tag-UID: {getFullScan.Tag.UniqueId} | " +
$"Ant-UID: {getFullScan.Antenna.UniqueId}";
_logger.LogInformation(logVal);
}
}
catch (Exception e)
var getFullScan = await _request.GetScanById(decData.Id);
if (getFullScan.Reader != null
&& getFullScan.Tag != null
&& getFullScan.Antenna != null)
{
_logger.LogCritical(e.ToString().Remove(0, e.ToString().Length) + "Unique Ignored..");
var logVal = $"Reader-UID: {getFullScan.Reader.UniqueId} | " +
$"Type: {getFullScan.Tag.Type} | " +
$"Mode: {getFullScan.Reader.Mode} | " +
$"Tag-UID: {getFullScan.Tag.UniqueId} | " +
$"Ant-UID: {getFullScan.Antenna.UniqueId}";
_logger.LogInformation(logVal);
}
}
catch (Exception e)
{
_logger.LogCritical(e.ToString().Remove(0, e.ToString().Length) + "Unique Ignored..");
}
}
}

Expand Down
15 changes: 4 additions & 11 deletions src/Frequency.Device/Protocols/Readers/KingJoinProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ PersistRequest persistRequest
)
{
DataLength = 20;
_context = context;
}

public new const byte START_RESPONSE_BYTE = 0xCC;
Expand Down Expand Up @@ -160,16 +159,10 @@ public override async Task<Scan> DecodeData()
LastMode = "Unknown"
};

await using (var context = _context)
{
context.Database.EnsureCreated();

_Scan = await _request.ResolveReader(context, _Scan, _Reader);
_Scan = await _request.ResolveAntenna(context, _Scan, _Antenna, "ant01-" + _config.IOT_UNIQUE_ID);
_Scan = await _request.ResolveTag(context, _Scan, _Tag, _tagData);

_context = context;
}
_context.Database.EnsureCreated();
_Scan = await _request.ResolveReader(_Scan, _Reader);
_Scan = await _request.ResolveAntenna(_Scan, _Antenna, "ant01-" + _config.IOT_UNIQUE_ID);
_Scan = await _request.ResolveTag(_Scan, _Tag, _tagData);

return _Scan;

Expand Down
2 changes: 1 addition & 1 deletion src/Frequency.Device/defaults.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ IOTMode:
Connections:
Serial:
Enable: true
PortName: "null"
PortName: "/dev/ttyUSB0"
BaudRate: 9600
DataBits: 8
ConnectionRetries: 5
Expand Down

0 comments on commit 0352186

Please sign in to comment.