Skip to content

Commit

Permalink
Merge pull request #329 from patrickmcquay/parameter-id-duplication-l…
Browse files Browse the repository at this point in the history
…oading-fix

Parameter id duplication loading fix
  • Loading branch information
antuspcm committed Aug 15, 2023
2 parents 43becd7 + 31fea75 commit 5f47e14
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 44 deletions.
7 changes: 7 additions & 0 deletions Apps/PcmLibrary/Devices/MockDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace PcmHacking
Expand Down Expand Up @@ -58,6 +59,8 @@ public override Task<TimeoutScenario> SetTimeout(TimeoutScenario scenario)
/// </summary>
public override Task<bool> SendMessage(Message message)
{
Thread.Sleep(100);

StringBuilder builder = new StringBuilder();
this.Logger.AddDebugMessage("Sending message " + message.GetBytes().ToHex());
this.port.Send(message.GetBytes());
Expand All @@ -70,6 +73,8 @@ public override Task<bool> SendMessage(Message message)
/// <returns></returns>
protected async override Task Receive()
{
Thread.Sleep(100);

//List<byte> incoming = new List<byte>(5000);
byte[] incoming = new byte[5000];
int count = await this.port.Receive(incoming, 0, incoming.Length);
Expand All @@ -91,6 +96,8 @@ protected async override Task Receive()
/// </remarks>
protected override Task<bool> SetVpwSpeedInternal(VpwSpeed newSpeed)
{
Thread.Sleep(100);

if (newSpeed == VpwSpeed.Standard)
{
this.Logger.AddDebugMessage("Setting VPW 1X");
Expand Down
16 changes: 13 additions & 3 deletions Apps/PcmLibrary/Devices/OBDXProDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace PcmHacking
Expand Down Expand Up @@ -210,13 +211,22 @@ async private Task<bool> WaitForSerial(ushort NumBytes, int timeout = 0)
// Wait for bytes to arrive...
while (sw.ElapsedMilliseconds < timeout)
{
if (await this.Port.GetReceiveQueueSize() > TempCount)
var rqSize = await this.Port.GetReceiveQueueSize();

if (rqSize > TempCount)
{
TempCount = await this.Port.GetReceiveQueueSize();
TempCount = rqSize;
sw.Restart();
}
if (await this.Port.GetReceiveQueueSize() >= NumBytes) { return true; }

if (rqSize >= NumBytes)
{
return true;
}

Thread.Sleep(1);
}

return false;
}

Expand Down
22 changes: 13 additions & 9 deletions Apps/PcmLibrary/Logging/LogProfileReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ private void LoadPidParameters(XDocument xml)
foreach (XElement parameterElement in container.Elements("PidParameter"))
{
string id = parameterElement.Attribute("id").Value;
string name = parameterElement.Attribute("name")?.Value;
string units = parameterElement.Attribute("units").Value;
this.AddPidParameter(id, units);
this.AddPidParameter(id, name, units);
}
}
}
Expand All @@ -69,8 +70,9 @@ private void LoadRamParameters(XDocument xml)
foreach (XElement parameterElement in container.Elements("RamParameter"))
{
string id = parameterElement.Attribute("id").Value;
string name = parameterElement.Attribute("name")?.Value;
string units = parameterElement.Attribute("units").Value;
this.AddRamParameter(id, units);
this.AddRamParameter(id, name, units);
}
}
}
Expand All @@ -83,16 +85,18 @@ private void LoadMathParameters(XDocument xml)
foreach (XElement parameterElement in container.Elements("MathParameter"))
{
string id = parameterElement.Attribute("id").Value;
string name = parameterElement.Attribute("name")?.Value;
string units = parameterElement.Attribute("units").Value;
this.AddMathParameter(id, units);
this.AddMathParameter(id, name, units);
}
}
}

private void AddPidParameter(string id, string units)
private void AddPidParameter(string id, string name, string units)
{
PidParameter parameter;
if (!this.database.PidParameters.TryGetParameter(id, out parameter))

if (!this.database.PidParameters.TryGetParameter(id, name, out parameter))
{
return;
}
Expand All @@ -112,10 +116,10 @@ private void AddPidParameter(string id, string units)
this.profile.AddColumn(column);
}

private void AddRamParameter(string id, string units)
private void AddRamParameter(string id, string name, string units)
{
RamParameter parameter;
if (!this.database.RamParameters.TryGetParameter(id, out parameter))
if (!this.database.RamParameters.TryGetParameter(id, name, out parameter))
{
return;
}
Expand All @@ -135,10 +139,10 @@ private void AddRamParameter(string id, string units)
this.profile.AddColumn(column);
}

private void AddMathParameter(string id, string units)
private void AddMathParameter(string id, string name, string units)
{
MathParameter parameter;
if (!this.database.MathParameters.TryGetParameter(id, out parameter))
if (!this.database.MathParameters.TryGetParameter(id, name, out parameter))
{
return;
}
Expand Down
3 changes: 3 additions & 0 deletions Apps/PcmLibrary/Logging/LogProfileWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public static void Write(LogProfile profile, string path)
{
XElement element = new XElement("PidParameter");
element.SetAttributeValue("id", column.Parameter.Id);
element.SetAttributeValue("name", column.Parameter.Name);
element.SetAttributeValue("units", column.Conversion.Units);
pidParameters.Add(element);
}
Expand All @@ -50,6 +51,7 @@ public static void Write(LogProfile profile, string path)
{
XElement element = new XElement("RamParameter");
element.SetAttributeValue("id", column.Parameter.Id);
element.SetAttributeValue("name", column.Parameter.Name);
element.SetAttributeValue("units", column.Conversion.Units);
ramParameters.Add(element);
}
Expand All @@ -64,6 +66,7 @@ public static void Write(LogProfile profile, string path)
{
XElement element = new XElement("MathParameter");
element.SetAttributeValue("id", column.Parameter.Id);
element.SetAttributeValue("name", column.Parameter.Name);
element.SetAttributeValue("units", column.Conversion.Units);
mathParameters.Add(element);
}
Expand Down
12 changes: 11 additions & 1 deletion Apps/PcmLibrary/Logging/Parameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private string Sanitize(string input)
/// <summary>
/// Base class for various parameter types (PID, RAM, Math)
/// </summary>
public abstract class Parameter
public abstract class Parameter : IEqualityComparer<Parameter>
{
public string Id { get; protected set; }
public string Name { get; protected set; }
Expand Down Expand Up @@ -126,6 +126,16 @@ public bool TryGetConversion(string units, out Conversion conversion)
}

public abstract bool IsSupported(uint osid);

public bool Equals(Parameter x, Parameter y)
{
return x.Id == y.Id && x.Name == y.Name;
}

public int GetHashCode(Parameter obj)
{
return HashCode.Combine(obj.Id, obj.Name);
}
}

/// <summary>
Expand Down
12 changes: 8 additions & 4 deletions Apps/PcmLibrary/Logging/ParameterDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@ public class ParameterDatabase
{
public class ParameterTable<T> where T : Parameter
{
private Dictionary<string, T> dictionary = new Dictionary<string, T>();
private List<T> table = new List<T>();

public void Add(string id, T parameter)
{
this.dictionary[id] = parameter;
this.table.Add(parameter);
}

public bool TryGetParameter(string id, out T parameter)
public bool TryGetParameter(string id, string name, out T parameter)
{
return this.dictionary.TryGetValue(id, out parameter);
//String.IsNullOrEmpty(name) is to handle cases where old profiles are being loaded, because we didnt test for that before.
parameter = this.table.FirstOrDefault(x => x.Id == id && (x.Name == name || String.IsNullOrEmpty(name)));

return parameter != null;
}
}

Expand Down
1 change: 1 addition & 0 deletions Apps/PcmLibrary/PcmLibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

<ItemGroup>
<PackageReference Include="DynamicExpresso.Core" Version="2.3.1" />
<PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.Collections.Immutable" Version="1.6.0" />
<PackageReference Include="System.Xml.XmlSerializer" Version="4.3.0" />
Expand Down
19 changes: 14 additions & 5 deletions Apps/PcmLibraryWindowsForms/DialogBoxes/DevicePicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,10 @@ private void FillPortList()
this.serialPortList.Items.Add(portInfo);
}

// This is useful for testing without an actual PCM.
// You'll need to uncomment a line in FillSerialDeviceList as well as this one.
// this.serialPortList.Items.Add(MockPort.PortName);
// This is useful for testing without an actual PCM.
#if DEBUG

This comment has been minimized.

Copy link
@Gampyg28

Gampyg28 Aug 18, 2023

Collaborator

Sorry, I have not had the ability to test this request, I do not believe the directive constant DEBUG can be used!
PCMHammer is released as DEBUG, thus this would be enabled all the time.
I do not believe this to be the correct state of MockPort for normal use.

I would recommend MOCKPORT.

This comment has been minimized.

Copy link
@patrickmcquay

patrickmcquay Aug 18, 2023

Contributor

I can certainly open another PR to fix that. Why is PCMHammer released as debug?

this.serialPortList.Items.Add(MockPort.PortName);
#endif
}

/// <summary>
Expand All @@ -171,8 +172,9 @@ private void FillSerialDeviceList()
this.serialDeviceList.Items.Add(OBDXProDevice.DeviceType);

// This is useful for testing without an actual PCM.
// You'll need to uncomment a line in FillPortList as well as this one.
// this.serialDeviceList.Items.Add(MockDevice.DeviceType);
#if DEBUG
this.serialDeviceList.Items.Add(MockDevice.DeviceType);
#endif
}

/// <summary>
Expand Down Expand Up @@ -249,6 +251,13 @@ private void j2534RadioButton_CheckedChanged(object sender, EventArgs e)
/// </summary>
private void serialPortList_SelectedIndexChanged(object sender, EventArgs e)
{
//mock port isnt a SerialPortInfo
if (this.serialPortList.SelectedItem is String)
{
this.SerialPort = this.serialPortList.SelectedItem as String;
return;
}

this.SerialPort = (this.serialPortList.SelectedItem as SerialPortInfo)?.PortName;
}

Expand Down
15 changes: 9 additions & 6 deletions Apps/PcmLogger/MainForm.ParameterGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace PcmHacking
{
public partial class MainForm
{
private Dictionary<string, DataGridViewRow> parameterIdsToRows;
//private Dictionary<string, DataGridViewRow> parameterIdsToRows;
private ParameterDatabase database;
private bool suspendSelectionEvents = true;

Expand All @@ -28,8 +28,6 @@ private void FillParameterGrid()
throw new InvalidDataException("Unable to load parameters from XML: " + errorMessage);
}

this.parameterIdsToRows = new Dictionary<string, DataGridViewRow>();

foreach (Parameter parameter in this.database.Parameters)
{
if (!parameter.IsSupported(osid))
Expand All @@ -38,20 +36,24 @@ private void FillParameterGrid()
}

DataGridViewRow row = new DataGridViewRow();

row.CreateCells(this.parameterGrid);

row.Cells[0].Value = false; // enabled
row.Cells[1].Value = parameter;

DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)row.Cells[2];

cell.DisplayMember = "Units";
cell.ValueMember = "Units";

foreach (Conversion conversion in parameter.Conversions)
{
cell.Items.Add(conversion);
}

row.Cells[2].Value = parameter.Conversions.First();

this.parameterIdsToRows[parameter.Id] = row;
this.parameterGrid.Rows.Add(row);
}

Expand All @@ -76,8 +78,9 @@ private void UpdateGridFromProfile()

foreach (LogColumn column in this.currentProfile.Columns)
{
DataGridViewRow row;
if (this.parameterIdsToRows.TryGetValue(column.Parameter.Id, out row))
DataGridViewRow row = this.parameterGrid.Rows.Cast<DataGridViewRow>().FirstOrDefault(r => r.Cells[1].Value == column.Parameter);

if (row != null)
{
row.Cells[0].Value = true;

Expand Down
27 changes: 11 additions & 16 deletions Apps/PcmLogger/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,18 @@ public override void AddUserMessage(string message)
/// </summary>
public override void AddDebugMessage(string message)
{
string timestamp = DateTime.Now.ToString("hh:mm:ss:fff");
if(this.debugLog.InvokeRequired)
{
var self = new Action<string>(AddDebugMessage);
this.BeginInvoke(self, new[] { message });
return;
}

Task foreground = Task.Factory.StartNew(
delegate ()
{
try
{
this.debugLog.AppendText("[" + timestamp + "] " + message + Environment.NewLine);
}
catch (ObjectDisposedException)
{
// This will happen if the window is closing. Just ignore it.
}
},
CancellationToken.None,
TaskCreationOptions.None,
uiThreadScheduler);
lock (this)
{
string timestamp = DateTime.Now.ToString("hh:mm:ss:fff");
this.debugLog.AppendText("[" + timestamp + "] " + message + Environment.NewLine);
}
}

public override void ResetLogs()
Expand Down

0 comments on commit 5f47e14

Please sign in to comment.