e-register changes

This commit is contained in:
Stoyan Zlatev
2025-04-14 14:10:58 +02:00
parent d67bdd1fc5
commit cd41d87d4c
9 changed files with 216 additions and 195 deletions
@@ -1,7 +1,5 @@
namespace Common.Hardware.SIRT.Parameters namespace Common.Hardware.SIRT.Parameters
{ {
using System.Text;
public class PAMStatus public class PAMStatus
{ {
private readonly byte[] bits; private readonly byte[] bits;
@@ -21,39 +19,34 @@
public override string ToString() public override string ToString()
{ {
var statusBuilder = new StringBuilder(); if (this.bits.GetByteLE() == 0)
if (this.CMD_UNKNOWN)
{ {
statusBuilder.AppendLine(nameof(this.CMD_UNKNOWN)); return nameof(OK);
} }
if (this.WRONG_AUTH)
{
statusBuilder.AppendLine(nameof(this.WRONG_AUTH));
}
if (this.EXPIRED_AUTH)
{
statusBuilder.AppendLine(nameof(this.EXPIRED_AUTH));
}
if (this.MIXED_TYPES)
{
statusBuilder.AppendLine(nameof(this.MIXED_TYPES));
}
if (this.OUT_OF_RANGE) if (this.OUT_OF_RANGE)
{ {
statusBuilder.AppendLine(nameof(this.OUT_OF_RANGE)); return nameof(this.OUT_OF_RANGE);
} }
else if (this.MIXED_TYPES)
if (statusBuilder.Length == 0)
{ {
statusBuilder.AppendLine(nameof(OK)); return nameof(this.MIXED_TYPES);
}
else if (this.CMD_UNKNOWN)
{
return nameof(this.CMD_UNKNOWN);
}
else if (this.EXPIRED_AUTH)
{
return nameof(this.EXPIRED_AUTH);
}
else if (this.WRONG_AUTH)
{
return nameof(this.WRONG_AUTH);
}
else
{
return string.Join(string.Empty, this.bits);
} }
return statusBuilder.ToString().Trim('\n', '\r');
} }
public static byte OK = 0; public static byte OK = 0;
@@ -84,8 +84,6 @@
void SIRTBroadcaster_Disconnected(SIRTBroadcaster _) void SIRTBroadcaster_Disconnected(SIRTBroadcaster _)
{ {
// broadcasters.TryRemove(portName, out sirtBroadcaster);
connectedEvent.Set(); connectedEvent.Set();
this.Disconnected?.Invoke(sirtBroadcaster); this.Disconnected?.Invoke(sirtBroadcaster);
@@ -1 +1 @@
8cb55c312de586b683d72cf58c13e2801d10e14b767777d98338d3e1473a1213 89102b0142e574de38a9f47d370f3334e5e6c268
@@ -39,6 +39,7 @@
public void Clear() public void Clear()
{ {
// Just clear the queue
while (this.runningTasks.TryDequeue(out var _)) ; while (this.runningTasks.TryDequeue(out var _)) ;
} }
@@ -105,16 +106,13 @@
void messageReceived(SIRTMessage message) void messageReceived(SIRTMessage message)
{ {
if (this.runningTasks.TryPeek(out var task)) if (this.runningTasks.TryPeek(out var eregisterTask))
{ {
if (task.State != SIRTTaskState.Cancelled && task.State != SIRTTaskState.Completed) eregisterTask.Push(message);
{
task.Push(message);
if (task.State == SIRTTaskState.Cancelled || task.State == SIRTTaskState.Completed) if (eregisterTask.State == SIRTTaskState.Cancelled || eregisterTask.State == SIRTTaskState.Completed)
{ {
readEvent.Set(); readEvent.Set();
}
} }
this.history.Push(message); this.history.Push(message);
@@ -122,7 +120,8 @@
} }
this.sirtChannel.MessageReceived += messageReceived; this.sirtChannel.MessageReceived += messageReceived;
var stopProgramming = false;
var lastTask = default(EregisterTask); var lastTask = default(EregisterTask);
var firstTask = true; var firstTask = true;
@@ -139,10 +138,11 @@
if (firstTask && !task.AnyTelegramReceived) if (firstTask && !task.AnyTelegramReceived)
{ {
var result = MessageBox.Show($"Das Rechenwerk an Ep: {task.SlotNr} mit Funkadresse: {task.RequestAddress}|{task.ResponseAddress} sendet nicht. " _ = MessageBox.Show($"Das Rechenwerk an Ep: {task.SlotNr} mit Funkadresse: {task.RequestAddress}|{task.ResponseAddress} sendet nicht. "
+ $"Überprüfen Sie, ob alle SIRT's sind und oder die Funkfrequenz von dem Rechenwerk ist nicht verwechselt." + $"Überprüfen Sie, ob alle SIRT's sind und oder die Funkfrequenz von dem Rechenwerk ist nicht verwechselt."
, "Achtung" , "Achtung"
, MessageBoxButtons.OK); , MessageBoxButtons.OK);
stopProgramming = true;
break; break;
} }
@@ -155,69 +155,38 @@
if (result == DialogResult.No) if (result == DialogResult.No)
{ {
task.State = SIRTTaskState.Cancelled; task.State = SIRTTaskState.Cancelled;
stopProgramming = true;
break; break;
} }
} }
if (task.State != SIRTTaskState.Completed) this.RemoveFromPamPoolIfNotCompleted(readEvent, task);
{ }
var dtask = new DeleteFromPamPoolTask
{
Frequency = task.Frequency,
RequestAddress = task.RequestAddress,
ResponseAddress = task.ResponseAddress,
};
this.sirtChannel.WriteHandler(task); if (stopProgramming)
readEvent.Wait(500); {
readEvent.Reset(); break;
}
} }
firstTask = false; firstTask = false;
if (task.FactoryState == FactoryState.Shipment) if (task.FactoryState == FactoryState.Shipment && !this.runningTasks.IsEmpty)
{ {
this.state.GeschlossenDatum = DateTime.Now;
break; break;
} }
var cultureInfo = CultureInfo.InvariantCulture; this.UpdateTasksStateInTheQueue(task);
var numberStyles = NumberStyles.HexNumber | NumberStyles.AllowHexSpecifier;
foreach (var rtask in this.runningTasks.AsEnumerable())
{
if (rtask.State == SIRTTaskState.Completed)
{
continue;
}
else if (task.State == SIRTTaskState.Cancelled)
{
rtask.State = SIRTTaskState.Cancelled;
}
rtask.RequestAddress = task.RequestAddress;
rtask.ResponseAddress = task.ResponseAddress;
rtask.FactoryState = task.FactoryState;
rtask.IsEncrypted = task.IsEncrypted;
if (task.DEBUGMessage != null)
{
rtask.RadioFWHex = task.DEBUGMessage.RadioFWVersion;
if (int.TryParse($"{rtask.RadioFWHex}", numberStyles, cultureInfo, out var radioFWVersion))
{
rtask.RadioFWVersion = radioFWVersion;
}
}
}
if (task.State == SIRTTaskState.Completed) if (task.State == SIRTTaskState.Completed)
{ {
this.runningTasks.TryDequeue(out task); if (!this.runningTasks.TryDequeue(out _))
this.runningTasks.TryPeek(out task); {
MessageBox.Show($"Task an Ep: {task.SlotNr} mit Funkadresse: {task.RequestAddress}|{task.ResponseAddress} könnte nicht gelöscht werden."
, "Achtung"
, MessageBoxButtons.OK);
break;
}
} }
else if (task.State == SIRTTaskState.Cancelled) else if (task.State == SIRTTaskState.Cancelled)
{ {
@@ -248,6 +217,56 @@
} }
} }
private void RemoveFromPamPoolIfNotCompleted(ManualResetEventSlim readEvent, EregisterTask task)
{
if (task.State != SIRTTaskState.Completed)
{
var deleteFromPamPoolTask = new DeleteFromPamPoolTask
{
Frequency = task.Frequency,
RequestAddress = task.RequestAddress,
ResponseAddress = task.ResponseAddress,
};
this.sirtChannel.WriteHandler(deleteFromPamPoolTask);
readEvent.Wait(500);
readEvent.Reset();
}
}
private void UpdateTasksStateInTheQueue(EregisterTask task)
{
var cultureInfo = CultureInfo.InvariantCulture;
var numberStyles = NumberStyles.HexNumber | NumberStyles.AllowHexSpecifier;
foreach (var eregisterTask in this.runningTasks.AsEnumerable())
{
if (eregisterTask.State == SIRTTaskState.Completed)
{
continue;
}
else if (task.State == SIRTTaskState.Cancelled)
{
eregisterTask.State = SIRTTaskState.Cancelled;
}
eregisterTask.RequestAddress = task.RequestAddress;
eregisterTask.ResponseAddress = task.ResponseAddress;
eregisterTask.FactoryState = task.FactoryState;
eregisterTask.IsEncrypted = task.IsEncrypted;
if (task.DEBUGMessage != null)
{
eregisterTask.RadioFWHex = task.DEBUGMessage.RadioFWVersion;
if (int.TryParse($"{eregisterTask.RadioFWHex}", numberStyles, cultureInfo, out var radioFWVersion))
{
eregisterTask.RadioFWVersion = radioFWVersion;
}
}
}
}
private void LoadProgrammingParameters(EregisterFeatures features, [CallerMemberName] string caller = "") private void LoadProgrammingParameters(EregisterFeatures features, [CallerMemberName] string caller = "")
{ {
this.Clear(); this.Clear();
@@ -266,13 +285,14 @@
EncryptionKey = features.EncryptionKey, EncryptionKey = features.EncryptionKey,
}); });
if (this.executionType == nameof(LoadInspectionInitializationTasks)) // if (this.executionType == nameof(LoadInspectionInitializationTasks))
{ {
this.runningTasks.Enqueue(new EregisterCheckInitializationTask(this.parameters, frequency) this.runningTasks.Enqueue(new EregisterCheckInitializationTask(this.parameters, frequency)
{ {
SlotNr = features.SlotNr, SlotNr = features.SlotNr,
PoNr = features.PoNr, PoNr = features.PoNr,
SerialNr = features.SerialNr, SerialNr = features.SerialNr,
WakeUpCmd = true,
DelPamSemi = true, DelPamSemi = true,
RequestAddress = features.RequestAddress, RequestAddress = features.RequestAddress,
ResponseAddress = features.ResponseAddress, ResponseAddress = features.ResponseAddress,
@@ -114,7 +114,7 @@
} }
else if (this.State != SIRTTaskState.Pending) else if (this.State != SIRTTaskState.Pending)
{ {
if (response.Address != this.RequestAddress) if (response.Address == this.ResponseAddress)
{ {
var requestAddress = this.RequestAddress; var requestAddress = this.RequestAddress;
this.RequestAddress = this.ResponseAddress; this.RequestAddress = this.ResponseAddress;
@@ -135,7 +135,7 @@
{ {
var type = data[0]; var type = data[0];
if (type == SIRTConstants.BUP) // 13 if (type == SIRTConstants.BUP) // always: 13
{ {
this.Label = nameof(SIRTConstants.BUP); this.Label = nameof(SIRTConstants.BUP);
this.BUPMessage = new BUP(data, this.P11); this.BUPMessage = new BUP(data, this.P11);
@@ -147,7 +147,7 @@
this.IsEncrypted = true; this.IsEncrypted = true;
} }
} }
else if (type == SIRTConstants.LAT) // 13 else if (type == SIRTConstants.LAT) // always: 13
{ {
this.Label = nameof(SIRTConstants.LAT); this.Label = nameof(SIRTConstants.LAT);
this.LATMessage = new LAT(data, this.P11); this.LATMessage = new LAT(data, this.P11);
@@ -159,7 +159,7 @@
this.IsEncrypted = true; this.IsEncrypted = true;
} }
} }
else if (type == SIRTConstants.DEBUG) // 52 else if (type == SIRTConstants.DEBUG) // plain: 52 - enc: 54
{ {
if (data.Length > DEBUG.MIN_LENGTH) if (data.Length > DEBUG.MIN_LENGTH)
{ {
@@ -171,17 +171,17 @@
this.DEBUGMessage = new DEBUG(data, this.P11); this.DEBUGMessage = new DEBUG(data, this.P11);
this.FactoryState = this.DEBUGMessage.FactoryState; this.FactoryState = this.DEBUGMessage.FactoryState;
} }
else if (type == SIRTConstants.SEMI) // 93 else if (type == SIRTConstants.SEMI) // plain: 91 - enc: 93
{ {
if (data.Length > SEMI.MIN_LENGTH) if (data.Length > SEMI.MIN_LENGTH)
{ {
data = data.DecryptResponse(this.EncryptionKey); data = data.DecryptResponse(this.EncryptionKey);
this.IsEncrypted = true; this.IsEncrypted = true;
} }
this.Label = default(string);
this.SEMIMessage = new SEMI(data, this.Frequency, this.P11); this.SEMIMessage = new SEMI(data, this.Frequency, this.P11);
var pamStatus = this.SEMIMessage.PAMStatus; var pamStatus = this.SEMIMessage.PAMStatus;
this.Label = pamStatus.ToString();
if (pamStatus == PAMStatus.OK) if (pamStatus == PAMStatus.OK)
{ {
@@ -196,15 +196,13 @@
else else
{ {
this.State = SIRTTaskState.Completed; this.State = SIRTTaskState.Completed;
this.Label = this.SEMIMessage.PAMStatus.ToString();
} }
} }
else else
{ {
this.Label = "!SEMI"; if (pamStatus.EXPIRED_AUTH && ++this.countFailures > 3)
if (!pamStatus.EXPIRED_AUTH && ++this.countFailures > 3)
{ {
this.countFailures = 0;
this.State = SIRTTaskState.Cancelled; this.State = SIRTTaskState.Cancelled;
} }
} }
@@ -232,7 +230,34 @@
protected void NotifyChanged() => this.Changed?.Invoke(this); protected void NotifyChanged() => this.Changed?.Invoke(this);
public static implicit operator byte[](EregisterTask task) public static implicit operator byte[] (EregisterTask task)
=> task.payload ?? Array.Empty<byte>(); {
var payloadLength = task.request.Payload.Length;
task.payload = new byte[payloadLength];
Array.Copy(task.request.Payload, 0, task.payload, 0, payloadLength);
if (task.FactoryState == FactoryState.Shipment)
{
task.payload = new EregisterAvailableParametersParser(task.payload);
}
if (task.IsEncrypted)
{
task.payload = task.payload.EncryptRequest(task.EncryptionKey, task.RequestAddress);
}
return new SIRTMessage
{
Direction = task.request.Direction,
Address = task.request.Address,
Command = task.request.Command,
Frequency = task.Frequency,
Payload = task.payload,
P1 = task.request.P1,
P2 = task.request.P2,
}
.GetBytes();
}
} }
} }
+3 -3
View File
@@ -4,10 +4,10 @@
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8.1" /> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8.1" />
</startup> </startup>
<appSettings> <appSettings>
<!--<add key="ConnectionString" value="Server=SLASQL01.emea.sensus.net; Database=Testreplikation; User ID=sa; Password=sqlserver;" />--> <add key="ConnectionString" value="Server=SLASQL01.emea.sensus.net; Database=Testreplikation; User ID=sa; Password=sqlserver;" />
<add key="ConnectionString" value="Server=SLASQL01.emea.sensus.net; Database=Auftrag; User ID=sa; Password=sqlserver;" /> <!--<add key="ConnectionString" value="Server=SLASQL01.emea.sensus.net; Database=Auftrag; User ID=sa; Password=sqlserver;" />-->
<add key="EncryptionKeisUrl" value="http://sla12iis01.emea.sensus.net/LaaProductionWeb/api/eregister/radiokey/byserialno/{0}" /> <add key="EncryptionKeisUrl" value="http://sla12iis01.emea.sensus.net/LaaProductionWeb/api/eregister/radiokey/byserialno/{0}" />
<add key="SIRTComPorts" value="COM6,COM8" /> <add key="SIRTComPorts" value="COM6, COM8" />
<add key="PSNR" value="2001" /> <add key="PSNR" value="2001" />
</appSettings> </appSettings>
<runtime> <runtime>
+17 -21
View File
@@ -6,37 +6,33 @@
WindowTitle="Eregister-History"> WindowTitle="Eregister-History">
<Grid> <Grid>
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" /> <RowDefinition Height="Auto" />
<RowDefinition Height="*" /> <RowDefinition Height="*" />
</Grid.RowDefinitions> </Grid.RowDefinitions>
<Controls:MainMenu Grid.Row="0" /> <Controls:MainMenu Grid.Row="0" />
<Grid Grid.Row="1"> <Controls:SIRTBroadcasters Grid.Row="1" />
<Grid Grid.Row="2">
<Grid.ColumnDefinitions> <Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" /> <ColumnDefinition Width="*" />
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<Grid Grid.Column="0"> <ScrollViewer Grid.Column="0" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Auto">
<Grid.RowDefinitions> <ItemsControl ItemsSource="{Binding Addresses}" Padding="0 5">
<RowDefinition Height="Auto" /> <ItemsControl.ItemTemplate>
<RowDefinition Height="*" /> <DataTemplate>
</Grid.RowDefinitions> <CheckBox Command="{Binding CheckedCommand}"
<TextBox Grid.Row="0" Text="{Binding AddressFilter, UpdateSourceTrigger=PropertyChanged}" Margin="10" /> Content="{Binding Address}"
<ScrollViewer Grid.Row="1" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Auto"> Cursor="Hand"
<ItemsControl ItemsSource="{Binding Addresses}" Padding="0 5"> FontFamily="Verdana"
<ItemsControl.ItemTemplate> IsChecked="{Binding IsChecked}"
<DataTemplate> Margin="10 5 15 0" />
<CheckBox Command="" </DataTemplate>
Content="{Binding}" </ItemsControl.ItemTemplate>
Cursor="Hand" </ItemsControl>
FontFamily="Verdana" </ScrollViewer>
Margin="10 5 10 0" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</Grid>
</Grid> </Grid>
</Grid> </Grid>
</Page> </Page>
@@ -7,13 +7,5 @@
public partial class EregisterHistory : Page public partial class EregisterHistory : Page
{ {
public EregisterHistory() => this.InitializeComponent(); public EregisterHistory() => this.InitializeComponent();
private void ComboBox_ContextMenuOpening(object sender, ContextMenuEventArgs args)
{
if (this.DataContext is EregisterHistoryVM vm)
{
vm.LoadAddresses();
}
}
} }
} }
@@ -1,100 +1,97 @@
namespace Common.UI.ViewModels namespace Common.UI.ViewModels
{ {
using Common.Hardware.SIRT;
using Common.UI.Infrastructure; using Common.UI.Infrastructure;
using LaaPackages.SqlClient; using LaaPackages.SqlClient;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Linq; using System.Windows.Input;
using System.Threading.Tasks;
public class EregisterHistoryVM : VM<EregisterHistoryVM> public class EregisterHistoryVM : VM<EregisterHistoryVM>
{ {
private readonly HashSet<uint> selectedAddresses;
private readonly SqlConnection sqlConnection; private readonly SqlConnection sqlConnection;
public EregisterHistoryVM() public EregisterHistoryVM()
{ {
var connectionString = Appsettings.GetSetting("ConnectionString"); var connectionString = Appsettings.GetSetting("ConnectionString");
this.sqlConnection = SqlConnection.CreateSqlConnection(connectionString); this.sqlConnection = SqlConnection.CreateSqlConnection(connectionString);
this.selectedAddresses = new HashSet<uint>(); this.Addresses = new ObservableCollection<AddressVM>();
this.Addresses = new ObservableCollection<uint>();
this.LoadAddresses(); AppHost.SIRTHub.MessageProcessed += SIRTHub_MessageProcessed;
} }
private uint? addressFilter; public ObservableCollection<AddressVM> Addresses { get; }
public uint? AddressFilter
private void SIRTHub_MessageProcessed(SIRTMessage message)
{ {
get => this.addressFilter; var address = message.Address;
set
if (address > 0)
{ {
this.SetValue(x => x.addressFilter = value); this.InvokeAsync(() =>
Task.Run((Action)this.UpdateSelectedAddressesHistory);
}
}
public ObservableCollection<uint> Addresses { get; }
public void LoadAddresses()
{
Task.Run(() =>
{
this.InvokeAsync(this.Addresses.Clear);
var filter = $"{this.AddressFilter}%";
var addresses = this.sqlConnection
.CreateCommand($@"
SELECT DISTINCT TOP 50 Adresse
FROM eRegister_History
WHERE Adresse LIKE @{nameof(filter)}
AND Adresse BETWEEN 1 AND 10000000000")
.SetParameter(nameof(filter), filter)
.ExecuteReader(x => x.GetValue<uint>(0))
.ToList();
var addressesToRemove = this.Addresses.Except(addresses).ToList();
foreach (var addressToRemove in addressesToRemove)
{ {
this.Addresses.Remove(addressToRemove); var inserted = false;
} var addressesCount = this.Addresses.Count;
var addressesToAdd = addresses.Except(this.Addresses).ToList(); for (var addressIX = 0; addressIX < addressesCount; addressIX++)
foreach (var addressToAdd in addressesToAdd)
{
var insertIX = -1;
foreach (var address in this.Addresses)
{ {
insertIX++; var vm = this.Addresses[addressIX];
if (addressToAdd >= address) if (vm.Address == address)
{ {
this.InvokeAsync(() => inserted = true;
{
this.Addresses.Insert(insertIX, addressToAdd); break;
}); }
else if (vm.Address > address)
{
this.Addresses.Insert(addressIX, address);
inserted = true;
break; break;
} }
} }
if (insertIX < 0) if (!inserted)
{ {
this.InvokeAsync(() => this.Addresses.Add(address);
{
this.Addresses.Add(addressToAdd);
});
} }
} });
}); }
}
}
public class AddressVM : VM<AddressVM>
{
public AddressVM()
=> this.CheckedCommand = new Command(this.CheckedCommandHandler);
private uint address;
public uint Address
{
get => this.address;
set => this.SetValue(vm => vm.address = value);
} }
private void UpdateSelectedAddressesHistory() private bool isChecked;
public bool IsChecked
{
get => this.isChecked;
set => this.SetValue(vm => vm.isChecked = value);
}
public ICommand CheckedCommand { get; }
private void CheckedCommandHandler()
{ {
} }
public static implicit operator AddressVM(uint address) => new AddressVM
{
Address = address
};
} }
} }