Skip to content

Commit

Permalink
Sort out com port usage in different instances of the loggers! Rename…
Browse files Browse the repository at this point in the history
… the Navigate tool bar to Properties (after all these years). Tidy up the SQL log experiment.
  • Loading branch information
Kirk Goddard committed May 12, 2021
1 parent e5c7a84 commit 1d1c013
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 78 deletions.
2 changes: 1 addition & 1 deletion Mjolnir/Content/config.phree
Expand Up @@ -84,7 +84,7 @@
<dock tool="find" visible="false" edge="bottom" solo="true" icon="icon_find.gif" display="Find" />
<dock tool="matches" visible="false" edge="bottom" solo="true" icon="icon_match.gif" display="Results" />
<dock tool="outline" visible="true" edge="left" solo="false" icon="icon_outline.gif" display="Outline" decor="{64EC31FE-F28E-49A6-A12A-9194214DD0D6}" />
<dock tool="navigate" visible="false" edge="left" solo="false" icon="icon_navigate.gif" display="Properties" decor="{1509A246-5CB0-41B1-A6D2-572D38EEC9C5}" />
<dock tool="properties" visible="false" edge="left" solo="false" icon="icon_navigate.gif" display="Properties" decor="{1509A246-5CB0-41B1-A6D2-572D38EEC9C5}" />
<dock tool="menu" visible="true" edge="top" solo="true" icon="icon_menu.gif" display="Menu" />
<dock tool="views" visible="false" edge="bottom" solo="true" icon="icon_windows.gif" display="Views" />
<dock tool="alerts" visible="false" edge="bottom" solo="true" icon="icon_output.gif" display="Alerts" />
Expand Down
10 changes: 5 additions & 5 deletions MorsePractice/Controller.cs
Expand Up @@ -78,7 +78,7 @@ public class MorseController2 :

public override IDisposable CreateDocument( IPgBaseSite oSite, string strExtension ) {
if( strExtension.ToLower() == ".netlog" ) {
return( new DocNotes( oSite ) );
return new DocNotes( oSite ) { FlagSComsOn = true };
}

return null;
Expand All @@ -95,8 +95,8 @@ public class MorseController2 :
case Guid r when r == ViewNotes._guidViewCategory:
return new ViewNotes(oBaseSite, oMorsePractice);

case Guid r when r == ViewLog.ViewLogger:
return new ViewLog( oBaseSite, oMorsePractice );
//case Guid r when r == ViewLog.ViewLogger:
// return new ViewLog( oBaseSite, oMorsePractice );

case Guid r when r == _guidSchedule:
return new EditWindow2( oBaseSite, oMorsePractice.Calls, fReadOnly:true, fSingleLine:false );
Expand Down Expand Up @@ -128,7 +128,7 @@ public class MorseController2 :
yield return new ViewType( "Qrz", ViewQrz._guidViewCategory );
yield return new ViewType( "Qrz Raw Bio", _guidRawBio );
yield return new ViewType( "Qrz Raw Page", _guidRawPage );
yield return new ViewType( "Logger", ViewLog.ViewLogger );
//yield return new ViewType( "Logger", ViewLog.ViewLogger ); broken for now.
}
}

Expand All @@ -141,7 +141,7 @@ public class MorseController3 :

public override IDisposable CreateDocument( IPgBaseSite oSite, string strExtension ) {
if( strExtension.ToLower() == ".stdlog" ) {
return new DocNotes( oSite ) { ScanCallsFlag = false };
return new DocNotes( oSite ) { FlagScanCalls = false };
}

return null;
Expand Down
163 changes: 102 additions & 61 deletions MorsePractice/DocMorse.cs
Expand Up @@ -43,7 +43,6 @@ public class MorseDoc:
IPgParent,
IPgLoad<TextReader>,
IPgSave<TextWriter>,
IPgTableDocument,
IDisposable
{
readonly IPgScheduler _oScheduler;
Expand Down Expand Up @@ -124,8 +123,6 @@ protected class MorseDocSlot :
public IPgParent Parentage => _oSiteBase.Host;
public IPgParent Services => Parentage.Services;

public ICollection<ICollection<Line>> Rows { get; } = new List<ICollection<Line>>();

/// <summary>
/// Load Morse code reference text.
/// </summary>
Expand Down Expand Up @@ -163,15 +160,6 @@ protected class MorseDocSlot :
if( !Stats.InitNew() )
return false;

for( int i=0; i<3; ++i ) {
List<Line> rgRow = new List<Line>(7);

for( int j=0; j<7; ++j ) {
rgRow.Add( new TextLine( j, j.ToString() ) );
}
Rows.Add( rgRow );
}

LoadMorse();
// LoadStats(); We'll load the stats table here, it'll double as an audio reference.

Expand Down Expand Up @@ -199,12 +187,6 @@ protected class MorseDocSlot :

return true;
}

public void TableListenerAdd(IPgTableEvents oEvent) {
}

public void TableListenerRemove(IPgTableEvents oEvent) {
}
}

/// <summary>
Expand All @@ -214,7 +196,6 @@ public class DocNotes:
IPgParent,
IPgLoad<TextReader>,
IPgSave<TextWriter>,
IPgTableDocument,
IPgCiVEvents,
IDisposable
{
Expand Down Expand Up @@ -264,18 +245,32 @@ protected class MorseDocSlot :
public Editor CallSignBioHtml { get; } // base 64 converted HTML streaml;
public Editor CallSignPageHtml{ get; } // This is the main page returned by qrz.

SerialPort _spCiV; // Good cantidate for "init once"
SerialPort _oCiV; // Good cantidate for "init once"
readonly ConcurrentQueue<byte[]> _oMsgQueue = new ConcurrentQueue<byte[]>();
readonly Line _oDataGram = new TextLine( 0, string.Empty );
readonly Grammer<char> _oCiVGrammar;
readonly DatagramParser _oParse;
public bool ScanCallsFlag { get; set; } = true;

int _iFrequencyLast = -1;
public bool FlagScanCalls { get; set; } = true; // Set up call back to buffer to parse calls.
public bool FlagSComsOn { get; set; } = false; // Turn on the com ports.

Dictionary <int, RepeaterDir> _rgRepeaters = new Dictionary<int, RepeaterDir>();

protected static readonly HttpClient _oHttpClient = new HttpClient();

protected static Type[] CiVErrorList {
get {
Type[] rgErrors = { typeof( UnauthorizedAccessException ),
typeof( IOException ),
typeof( ArgumentOutOfRangeException ),
typeof( ArgumentException ),
typeof( InvalidOperationException ),
typeof( NullReferenceException ),
typeof( FileNotFoundException ) };
return rgErrors;
}
}

/// <summary>
/// Document object for a little Morse Practice document.
/// </summary>
Expand Down Expand Up @@ -325,7 +320,7 @@ protected class MorseDocSlot :

public void Dispose() {
if( !_fDisposed ) {
_spCiV ?.Close();
_oCiV ?.Close();
_oParse?.Dispose();

Notes .Dispose();
Expand All @@ -349,7 +344,7 @@ protected class MorseDocSlot :
public IPgParent Parentage => _oSiteBase.Host;
public IPgParent Services => Parentage.Services;

public ICollection<ICollection<Line>> Rows { get; } = new List<ICollection<Line>>();
//public ICollection<ICollection<Line>> Rows { get; } = new List<ICollection<Line>>();

public void CiVError( string strError ) {
LogError( "Ci-V", strError );
Expand Down Expand Up @@ -390,7 +385,6 @@ public struct RepeaterDir {
Properties.UpdateValue( 0, "Stopped." );
_oTaskTimer.Stop(); // stopped talking most likely.
}
_iFrequencyLast = iFrequency;
}

public void CiVModeChange( string strMode, string strFilter ) {
Expand Down Expand Up @@ -503,44 +497,48 @@ public struct RepeaterDir {
}
}

/// <summary>
/// set up the COM port i/o.
/// </summary>
/// <remarks>
/// Turns out we can always set ourselves up to listen to a COM port, BUT
/// we might not be able to open it, or we don't want to open it right away.
/// </remarks>
protected void InitSerial() {
// this is crude since we're polling even if no messages.
// This is crude since we're polling even if no messages.
// we'll fix this up later.
_oTaskCiv.Queue( ListenToCom(), 100 );

SerialPort oCiV;
try {
oCiV = new SerialPort("COM4");
_oCiV = new SerialPort("COM4");
_oCiV.BaudRate = 9600;
_oCiV.Parity = Parity.None;
_oCiV.StopBits = StopBits.One;
_oCiV.DataBits = 8;
_oCiV.Handshake = Handshake.None;

_oCiV.DataReceived += CiV_DataReceived;
} catch( IOException ) {
LogError( "Morse COM access", "Unable to open COM port. Timer disabled" );
Properties.UpdateValue( 1, "Failed Alloc" );
return;
}

try {
oCiV.BaudRate = 9600;
oCiV.Parity = Parity.None;
oCiV.StopBits = StopBits.One;
oCiV.DataBits = 8;
oCiV.Handshake = Handshake.None;

oCiV.DataReceived += CiV_DataReceived;
oCiV.Open();

_spCiV = oCiV;
if( FlagSComsOn ) {
_oCiV.Open();
Properties.UpdateValue( 1, "On" );
} else {
Properties.UpdateValue( 1, "Off" );
}
} catch( Exception oEx ) {
Type[] rgErrors = { typeof( UnauthorizedAccessException ),
typeof( IOException ),
typeof( ArgumentOutOfRangeException ),
typeof( ArgumentException ),
typeof( InvalidOperationException ),
typeof( NullReferenceException ),
typeof( FileNotFoundException ) };
if( rgErrors.IsUnhandled( oEx ) )
if( CiVErrorList.IsUnhandled( oEx ) )
throw;

oCiV.Dispose();
_oCiV.Dispose();

LogError( "Morse COM access", "Unable to open COM port. Timer disabled" );
Properties.UpdateValue( 1, "Failed Open" );
}
}

Expand All @@ -557,8 +555,14 @@ public struct RepeaterDir {
}
}

/// <summary>
/// Properties are initialized first... BUT the values and where they
/// come from might not be initialized yet. So those will have to come
/// come later.
/// </summary>
protected void InitProperties() {
Properties.AddLabel( "Timer" );
Properties.AddLabel( "Timer Enable" );
}

/// <summary>
Expand All @@ -568,11 +572,11 @@ public struct RepeaterDir {
/// <remarks>Would be nice if we could post a message to the forground so that
/// it spins up a task that can terminate once all the data has been pulled.</remarks>
private void CiV_DataReceived( object sender, SerialDataReceivedEventArgs e ) {
int iBytesWaiting = _spCiV.BytesToRead;
int iBytesWaiting = _oCiV.BytesToRead;
if( iBytesWaiting > 0 ) {
byte[] rgMsg = new byte[iBytesWaiting];

_spCiV.Read( rgMsg, 0, iBytesWaiting );
_oCiV?.Read( rgMsg, 0, iBytesWaiting );
_oMsgQueue.Enqueue( rgMsg );
}
}
Expand All @@ -590,7 +594,7 @@ public struct RepeaterDir {

// Note: Change this to trigger after a notes parse.
//_oTaskSched.Queue( EnumCallsScanTask(), 3000 );
if( ScanCallsFlag ) {
if( FlagScanCalls ) {
Notes.BufferEvent += Notes_BufferEvent;
}

Expand All @@ -605,18 +609,18 @@ public struct RepeaterDir {
if( !CallSignAddress.InitNew() )
return false;

InitProperties();
InitSerial ();
InitRepeaters ();
InitProperties();

for( int i=0; i<3; ++i ) {
List<Line> rgRow = new List<Line>(7);
//for( int i=0; i<3; ++i ) {
// List<Line> rgRow = new List<Line>(7);

for( int j=0; j<7; ++j ) {
rgRow.Add( new TextLine( j, j.ToString() ) );
}
Rows.Add( rgRow );
}
// for( int j=0; j<7; ++j ) {
// rgRow.Add( new TextLine( j, j.ToString() ) );
// }
// Rows.Add( rgRow );
//}

return true;
}
Expand Down Expand Up @@ -911,6 +915,43 @@ public IEnumerator<int> EnumWatchTask(Task<HttpResponseMessage> oTask)

public void TableListenerRemove(IPgTableEvents oEvent) {
}

public bool Execute( Guid guidCmnd ) {
if( guidCmnd == GlobalCommands.Play ) {
try {
if( !_oCiV.IsOpen ) {
_oCiV.Open();
Properties.UpdateValue( 1, "On" );
} else {
Properties.UpdateValue( 1, "Already Opened" );
}
} catch( Exception oEx ) {
if( CiVErrorList.IsUnhandled( oEx ) )
throw;

Properties.UpdateValue( 1, "Open Error" );
}
return true; // Handled, even if ended in error.
}
if( guidCmnd == GlobalCommands.Stop ) {
try {
if( _oCiV.IsOpen ) {
_oCiV.Close();
Properties.UpdateValue( 1, "Off" );
} else {
Properties.UpdateValue( 1, "Already Closed" );
}
} catch( Exception oEx ) {
if( CiVErrorList.IsUnhandled( oEx ) )
throw;

Properties.UpdateValue( 1, "Close Error" );
}
return true; // Handled, even if ended in error.
}

return false;
}
}

public class DocProperties : IPgParent, IPgLoad {
Expand Down Expand Up @@ -959,9 +1000,9 @@ protected class DocSlot :
return true;
}

public void AddLabel( string strLabel ) {
Property_Labels.LineAppend( strLabel, fUndoable:false );
Property_Values.LineAppend( string.Empty, fUndoable:false );
public void AddLabel( string strLabel, string strValue = "" ) {
Property_Labels.LineAppend( strLabel, fUndoable:false );
Property_Values.LineAppend( strValue, fUndoable:false );
}

public Line this[int iIndex] {
Expand Down

0 comments on commit 1d1c013

Please sign in to comment.