Skip to content

Commit

Permalink
Merge branch 'regex'
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwahl committed Mar 17, 2018
2 parents a280ee7 + 1fdf269 commit 61c3fc5
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 51 deletions.
Binary file added SDRSharp.GpredictConnector/GlobalSuppressions.cs
Binary file not shown.
58 changes: 14 additions & 44 deletions SDRSharp.GpredictConnector/Rigctrld.cs
Original file line number Diff line number Diff line change
@@ -1,59 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SDRSharp.GpredictConnector
{
enum Errcode
{
RIG_OK = 0, /*!< No error, operation completed successfully */
RIG_EINVAL, /*!< invalid parameter */
RIG_ECONF, /*!< invalid configuration (serial,..) */
RIG_ENOMEM, /*!< memory shortage */
RIG_ENIMPL, /*!< function not implemented, but will be */
RIG_ETIMEOUT, /*!< communication timed out */
RIG_EIO, /*!< IO error, including open failed */
RIG_EINTERNAL, /*!< Internal Hamlib error, huh! */
RIG_EPROTO, /*!< Protocol error */
RIG_ERJCTED, /*!< Command rejected by the rig */
RIG_ETRUNC, /*!< Command performed, but arg truncated */
RIG_ENAVAIL, /*!< function not available */
RIG_ENTARGET, /*!< VFO not targetable */
RIG_BUSERROR, /*!< Error talking on the bus */
RIG_BUSBUSY, /*!< Collision on the bus */
RIG_EARG, /*!< NULL RIG handle or any invalid pointer parameter in get arg */
RIG_EVFO, /*!< Invalid VFO */
RIG_EDOM /*!< Argument out of domain of func */
};

class Rigctrld
{

private string GenAnswer(Errcode code)
{
return "RPRT " + ((int)code).ToString() + "\n";
}

public string ExecCommand(string command)
{
var answer = GenAnswer(Errcode.RIG_OK);
if (command.StartsWith("F"))
{
answer = SetFrequency(command);
Regex regex = new Regex("^F[ ]*([0-9]{1,})$");
var matches = regex.Matches(command);
if (matches.Count > 0)
{
if (matches[0].Groups.Count == 2)
{
var f_string = matches[0].Groups[1].Value;
frequency_set_thread_ = new Thread(() => FrequencyInHzString = f_string);
frequency_set_thread_.Start();
}
}
}
return answer;
}

private string SetFrequency(string command)
{
var f_string = command.Substring(3, 9);
frequency_set_thread_ = new Thread(() => FrequencyInHzString = f_string);
frequency_set_thread_.Start();
return GenAnswer(Errcode.RIG_OK);
return "RPRT 0\n";
}

public long FrequencyInHz
Expand All @@ -72,6 +41,7 @@ private set

}
}

public string FrequencyInHzString
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<DependentUpon>Controlpanel.cs</DependentUpon>
</Compile>
<Compile Include="FrequencyManager.cs" />
<Compile Include="GlobalSuppressions.cs" />
<Compile Include="GpredictConnectorPlugin.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Rigctrld.cs" />
Expand Down
51 changes: 44 additions & 7 deletions UnitTestGpredictConnector/Test_rigctrld_set_frequency.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SDRSharp.GpredictConnector;
using System.Threading;

namespace UnitTestGpredictConnector
{
Expand Down Expand Up @@ -62,15 +58,56 @@ public void Initialize()
[TestMethod]
public void SetFrequency_F__144800123()
{
Rigctrld cut = new Rigctrld();
TestHelper_SetFrequency(expected_freq: 144800123, command: "F 144800123");
}
[TestMethod]
public void SetFrequency_F__1GHZ()
{
TestHelper_SetFrequency(expected_freq: 1234567890, command: "F 1234567890");
}
[TestMethod]
public void SetFrequency_F__10m()
{
TestHelper_SetFrequency(expected_freq: 29420877, command: "F 29420877");
}

[TestMethod]
public void SetFrequency_F_144800123()
{
TestHelper_SetFrequency(expected_freq: 144800123, command: "F 144800123");
}

[TestMethod]
public void SetFrequency_F_00144800123()
{
TestHelper_SetFrequency(expected_freq: 144800123, command: "F 00144800123");
}

[TestMethod]
public void SetFrequency_F_1GHZ()
{
TestHelper_SetFrequency(expected_freq: 1234567890, command: "F 1234567890");
}
[TestMethod]
public void SetFrequency_F_10m()
{
TestHelper_SetFrequency(expected_freq: 29420877, command: "F 29420877");
}

[TestMethod]
public void SetFrequency_unparseableFreq()
{
Assert.AreEqual("RPRT 0\n", class_under_test_.ExecCommand("F 1234d567890"));
Assert.IsNull(class_under_test_.FrequencySetThread);
Assert.AreEqual(0, class_under_test_.FrequencyInHz);
}

private void TestHelper_SetFrequency(long expected_freq, string command)
{
long result_freq = 0;

class_under_test_.FrequencyInHzChanged += x => result_freq = x;
class_under_test_.ExecCommand(command);
Assert.AreEqual("RPRT 0\n", class_under_test_.ExecCommand(command));
Assert.IsNotNull(class_under_test_.FrequencySetThread); // should be running
class_under_test_.FrequencySetThread.Join(); // wait for the freqeuncy set thread to finish his work
Assert.AreEqual(expected_freq, class_under_test_.FrequencyInHz);
Expand Down

0 comments on commit 61c3fc5

Please sign in to comment.