diff --git a/src/Frequency.Device/Data/CaptureContext.cs b/src/Frequency.Device/Data/CaptureContext.cs index 9879036..1316f83 100644 --- a/src/Frequency.Device/Data/CaptureContext.cs +++ b/src/Frequency.Device/Data/CaptureContext.cs @@ -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; diff --git a/src/Frequency.Device/Data/CapturePersist.cs b/src/Frequency.Device/Data/CapturePersist.cs index 3e73c15..64c22c1 100644 --- a/src/Frequency.Device/Data/CapturePersist.cs +++ b/src/Frequency.Device/Data/CapturePersist.cs @@ -49,66 +49,57 @@ CaptureContext context _context = context; } - public void OldestDelete() - { - - } - - public async Task Save(Scan scn) + public async Task 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; } } } diff --git a/src/Frequency.Device/Data/PersistRequest.cs b/src/Frequency.Device/Data/PersistRequest.cs index d1e317c..e59c70c 100644 --- a/src/Frequency.Device/Data/PersistRequest.cs +++ b/src/Frequency.Device/Data/PersistRequest.cs @@ -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() @@ -47,7 +50,7 @@ public void OldestData() } - public async Task GetScanById(CaptureContext _context, Guid getbyid) + public async Task GetScanById(Guid getbyid) { var ScanData = _context.Scans .AsNoTracking() @@ -78,7 +81,7 @@ public async Task GetScanById(CaptureContext _context, Guid getbyid) return ScanData.Result; } - public async Task ResolveReader(CaptureContext _context, Scan _Scan, Reader _Reader) + public async Task ResolveReader(Scan _Scan, Reader _Reader) { var reader = _context.Readers .AsNoTracking() @@ -100,7 +103,7 @@ public async Task ResolveReader(CaptureContext _context, Scan _Scan, Reade return _Scan; } - public async Task ResolveAntenna(CaptureContext _context, Scan _Scan, Antenna _Antenna, string atnData) + public async Task ResolveAntenna(Scan _Scan, Antenna _Antenna, string atnData) { var antenna = _context.Antennae .AsNoTracking() @@ -120,7 +123,7 @@ public async Task ResolveAntenna(CaptureContext _context, Scan _Scan, Ante return _Scan; } - public async Task ResolveTag(CaptureContext _context, Scan _Scan, Tag _Tag, string _tagData) + public async Task ResolveTag(Scan _Scan, Tag _Tag, string _tagData) { var tag = _context.Tags .AsNoTracking() diff --git a/src/Frequency.Device/Handlers/ReaderConnections/SerialConnection.cs b/src/Frequency.Device/Handlers/ReaderConnections/SerialConnection.cs index 969e703..2dabeb2 100644 --- a/src/Frequency.Device/Handlers/ReaderConnections/SerialConnection.cs +++ b/src/Frequency.Device/Handlers/ReaderConnections/SerialConnection.cs @@ -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) diff --git a/src/Frequency.Device/Handlers/ReaderProcess.cs b/src/Frequency.Device/Handlers/ReaderProcess.cs index 970e4c2..42ce4c1 100644 --- a/src/Frequency.Device/Handlers/ReaderProcess.cs +++ b/src/Frequency.Device/Handlers/ReaderProcess.cs @@ -55,7 +55,7 @@ public ReaderProcess( ConfigKey config, SerialConnection serialConnection, WebSync webSync, - KingJoinProtocol selectedProtocol + IReaderProtocol selectedProtocol ) { _config = config; @@ -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; - } } diff --git a/src/Frequency.Device/Program.cs b/src/Frequency.Device/Program.cs index c38a3df..33d0195 100644 --- a/src/Frequency.Device/Program.cs +++ b/src/Frequency.Device/Program.cs @@ -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 @@ -53,7 +54,7 @@ public static IHostBuilder CreateHostBuilder(string[] args) => services.AddSingleton(); /** Extra DI Registration **/ - services.AddSingleton(); + services.AddSingleton(); services.AddSingleton(); /** Worker service Registration **/ diff --git a/src/Frequency.Device/Protocols/Readers/BaseProtocol.cs b/src/Frequency.Device/Protocols/Readers/BaseProtocol.cs index e741d4d..2243663 100644 --- a/src/Frequency.Device/Protocols/Readers/BaseProtocol.cs +++ b/src/Frequency.Device/Protocols/Readers/BaseProtocol.cs @@ -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.."); + } } } diff --git a/src/Frequency.Device/Protocols/Readers/KingJoinProtocol.cs b/src/Frequency.Device/Protocols/Readers/KingJoinProtocol.cs index 3a5dd19..6890e11 100644 --- a/src/Frequency.Device/Protocols/Readers/KingJoinProtocol.cs +++ b/src/Frequency.Device/Protocols/Readers/KingJoinProtocol.cs @@ -53,7 +53,6 @@ PersistRequest persistRequest ) { DataLength = 20; - _context = context; } public new const byte START_RESPONSE_BYTE = 0xCC; @@ -160,16 +159,10 @@ public override async Task 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; diff --git a/src/Frequency.Device/defaults.yaml b/src/Frequency.Device/defaults.yaml index 297691f..914818e 100644 --- a/src/Frequency.Device/defaults.yaml +++ b/src/Frequency.Device/defaults.yaml @@ -34,7 +34,7 @@ IOTMode: Connections: Serial: Enable: true - PortName: "null" + PortName: "/dev/ttyUSB0" BaudRate: 9600 DataBits: 8 ConnectionRetries: 5