Skip to content

Commit faabbfd

Browse files
OATControl V0.9.9.01 - Updates
- Added lots of logging around the communication between the app and the OAT - Moved logfiles to AppData/Roaming/OpenAstroTracker - Only keep the last 6 lo files around
1 parent b51091e commit faabbfd

File tree

4 files changed

+67
-10
lines changed

4 files changed

+67
-10
lines changed

Software/OATMobile/OATCommunications/CommunicationHandlers/TcpCommunicationHandler.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public class TcpCommunicationHandler : ICommunicationHandler
1616

1717
public TcpCommunicationHandler(string spec)
1818
{
19+
Log.WriteLine($"COMMFACTORY: Creating Wifi handler at {spec} ...");
20+
1921
string ip = string.Empty;
2022
string port = string.Empty;
2123

@@ -26,13 +28,16 @@ public TcpCommunicationHandler(string spec)
2628
{
2729
ip = spec.Substring(0, colon);
2830
port = spec.Substring(colon + 1);
31+
32+
Log.WriteLine($"COMMFACTORY: Wifi handler will monitor at {ip}:{port} ...");
33+
2934
_ip = IPAddress.Parse(ip);
3035
_port = int.Parse(port);
3136
_client = new TcpClient();
3237
}
33-
catch
38+
catch (Exception ex)
3439
{
35-
40+
Log.WriteLine($"COMMFACTORY: Failed to create TCP client. {ex.Message}");
3641
}
3742
}
3843
}
@@ -63,6 +68,7 @@ private async Task<CommandResponse> SendCommand(string command, ResponseType nee
6368
{
6469
if (_client == null)
6570
{
71+
Log.WriteLine($"TCP: Configuration error, IP [{_ip}] or port [{_port}] is invalid.");
6672
return new CommandResponse(string.Empty, false, $"Configuration error, IP [{_ip}] or port [{_port}] is invalid.");
6773
}
6874

@@ -116,6 +122,7 @@ private async Task<CommandResponse> SendCommand(string command, ResponseType nee
116122
case ResponseType.DigitResponse:
117123
case ResponseType.FullResponse:
118124
{
125+
Log.WriteLine("TCP: [{0}] Expecting a reply needed to command, waiting...", command);
119126
var response = new byte[256];
120127
var respCount = await stream.ReadAsync(response, 0, response.Length);
121128
respString = Encoding.ASCII.GetString(response, 0, respCount).TrimEnd("#".ToCharArray());
@@ -153,6 +160,7 @@ public void Disconnect()
153160
{
154161
if (_client != null && _client.Connected)
155162
{
163+
Log.WriteLine("TCP: Closing port.");
156164
_client.Close();
157165
_client = null;
158166
}

Software/OATMobile/OATCommunications/Log.cs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ public class Log
1313
{
1414
private static DateTime appStartTime = DateTime.UtcNow;
1515
private static object oLock = new object();
16-
private static string sPath = string.Format("{0}\\oat_{1}-{2}.log", Environment.GetFolderPath(Environment.SpecialFolder.Personal), DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"), Environment.UserName);
16+
private static string sFolder;
17+
private static string sPath;
1718

1819
private static List<string> lstBuffer = new List<string>();
1920
private static DateTime dtLastUpdate = DateTime.Now.AddSeconds(5.0);
@@ -29,6 +30,31 @@ public static string Filename
2930

3031
public static void Init(string sTitle)
3132
{
33+
// Create our logfile folder in AppData/Roaming
34+
sFolder = string.Format("{0}\\OpenAstroTracker", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
35+
Directory.CreateDirectory(sFolder);
36+
37+
// Create this session logfile
38+
sPath = string.Format("{0}\\OATControl_{1}-{2}.log", sFolder, DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"), Environment.UserName);
39+
40+
// Find old logfiles and keep the latest 5 around.
41+
var oldLogFiles = Directory.GetFiles(sFolder, "OATControl*.log").OrderByDescending(s => s).Skip(5).ToList();
42+
43+
// Probably should run this by the user.... for now they can jhust manually delete them
44+
// oldLogFiles.AddRange(Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "oat_*.log"));
45+
46+
foreach (var logFile in oldLogFiles)
47+
{
48+
try
49+
{
50+
File.Delete(logFile);
51+
}
52+
catch
53+
{
54+
// Oh well....
55+
}
56+
}
57+
3258
Log.WriteLine("*********************************");
3359
Log.WriteLine(string.Format("* {0} *", sTitle.PadRight(28)));
3460
Log.WriteLine("*********************************");
@@ -42,7 +68,7 @@ private static string FormatMessage(string message, object[] args)
4268
var sb = new StringBuilder(message.Length + 64);
4369

4470
TimeSpan elapsed = DateTime.UtcNow - Log.appStartTime;
45-
sb.AppendFormat("[{0}] [{1}]: ", elapsed.ToString("hh\\:mm\\:ss\\.ffff"), Thread.CurrentThread.ManagedThreadId);
71+
sb.AppendFormat("[{0}] [{1:00}]: ", elapsed.ToString("hh\\:mm\\:ss\\.fff"), Thread.CurrentThread.ManagedThreadId);
4672

4773
if (args != null && args.Length > 0)
4874
{
@@ -67,7 +93,6 @@ public static void WriteLine(string message, params object[] args)
6793
Log.lstBuffer.Clear();
6894
}
6995
Log.dtLastUpdate = DateTime.UtcNow;
70-
return;
7196
}
7297

7398
string sLine = FormatMessage(message, args);

Software/OpenAstroTracker ASCOM/OATCommuncations.WPF/CommunicationHandlers/CommunicationHandlerFactory.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using OATCommunications.CommunicationHandlers;
99
using System.Collections.ObjectModel;
1010
using OATCommuncations.WPF;
11+
using OATCommunications.Utilities;
1112

1213
namespace OATCommunications.WPF.CommunicationHandlers
1314
{
@@ -16,12 +17,17 @@ public static class CommunicationHandlerFactory
1617
static ObservableCollection<string> _available = new ObservableCollection<string>();
1718
public static void DiscoverDevices()
1819
{
20+
Log.WriteLine("COMMFACTORY: Device Discovery initiated.");
21+
Log.WriteLine("COMMFACTORY: Checking Serial ports....");
22+
1923
_available.Clear();
2024
foreach (var port in SerialPort.GetPortNames())
2125
{
26+
Log.WriteLine("COMMFACTORY: Found Serial port [{0}]", port);
2227
_available.Add("Serial : " + port);
2328
}
2429

30+
Log.WriteLine("COMMFACTORY: Starting Wifi search by Broadcastign OAT on port 4031");
2531
var searcher = new UdpClientAdapter("OAT", 4031);
2632
searcher.ClientFound += OnWifiClientFound;
2733
searcher.StartClientSearch();
@@ -30,15 +36,16 @@ public static void DiscoverDevices()
3036
private static void OnWifiClientFound(object sender, ClientFoundEventArgs e) => WpfUtilities.RunOnUiThread(
3137
() =>
3238
{
39+
Log.WriteLine($"COMMFACTORY: Wifi device {e.Name} replied from {e.Address}:4030!");
3340
_available.Insert(0, $"WiFi : {e.Name} ({e.Address}:4030)");
34-
3541
},
3642
System.Windows.Application.Current.Dispatcher);
3743

3844
public static ObservableCollection<String> AvailableDevices { get { return _available; } }
3945

4046
public static ICommunicationHandler ConnectToDevice(string device)
4147
{
48+
Log.WriteLine($"COMMFACTORY: Attempting to connect to device {device}...");
4249
if (device.StartsWith("Serial : "))
4350
{
4451
string comPort = device.Substring("Serial : ".Length);

Software/OpenAstroTracker ASCOM/OATCommuncations.WPF/CommunicationHandlers/SerialCommunicationHandler.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using OATCommunications.CommunicationHandlers;
2+
using OATCommunications.Utilities;
23
using System;
34
using System.Collections.Generic;
45
using System.IO.Ports;
@@ -15,6 +16,8 @@ public class SerialCommunicationHandler : ICommunicationHandler
1516

1617
public SerialCommunicationHandler(string comPort)
1718
{
19+
Log.WriteLine($"COMMFACTORY: Creating Serial handler on {comPort} at 57600 baud...");
20+
1821
_portName = comPort;
1922
_port = new SerialPort(comPort);
2023
_port.BaudRate = 57600;
@@ -46,11 +49,13 @@ private async Task<CommandResponse> SendCommand(string command, ResponseType nee
4649
{
4750
try
4851
{
52+
Log.WriteLine("SERIAL: [{0}] Sending command", command);
4953
_port.Write(command);
5054
}
51-
catch
55+
catch (Exception ex)
5256
{
53-
return new CommandResponse(string.Empty, false, $"Unable to write to {_portName}");
57+
Log.WriteLine("SERIAL: [{0}] Failed to send command. {1}", command, ex.Message);
58+
return new CommandResponse(string.Empty, false, $"Unable to write to {_portName}. " + ex.Message);
5459
}
5560

5661
try
@@ -59,31 +64,38 @@ private async Task<CommandResponse> SendCommand(string command, ResponseType nee
5964
{
6065
case ResponseType.NoResponse:
6166
{
67+
Log.WriteLine("SERIAL: [{0}] No response needed for command", command);
6268
return new CommandResponse(string.Empty, true);
6369
}
6470

6571
case ResponseType.DigitResponse:
6672
{
73+
Log.WriteLine("SERIAL: [{0}] Expecting single digit response for command, waiting...", command);
6774
string response = new string((char)_port.ReadChar(), 1);
75+
Log.WriteLine("SERIAL: [{0}] Received single digit response '{1}' for command", command, response);
6876
return new CommandResponse(response, true);
6977
}
7078

7179
case ResponseType.FullResponse:
7280
{
81+
Log.WriteLine("SERIAL: [{0}] Expecting #-delimited response for Command, waiting...", command);
7382
string response = _port.ReadTo("#");
83+
Log.WriteLine("SERIAL: [{0}] Received response '{1}' for command", command, response);
7484
return new CommandResponse(response, true);
7585
}
7686
}
7787
}
7888
catch (Exception ex)
7989
{
90+
Log.WriteLine("SERIAL: [{0}] Failed to receive response to command. {1}", command, ex.Message);
8091
return new CommandResponse(string.Empty, false, $"Unable to read response to {command} from {_portName}. {ex.Message}");
8192
}
8293

8394
return new CommandResponse(string.Empty, false, "Something weird going on...");
8495
}
8596
else
8697
{
98+
Log.WriteLine("SERIAL: Failed to open port {0}", _portName);
8799
return new CommandResponse(string.Empty, false, $"Unable to open {_portName}");
88100
}
89101
}
@@ -94,13 +106,16 @@ private async Task<bool> EnsurePortIsOpen()
94106
{
95107
try
96108
{
109+
Log.WriteLine("SERIAL: Port {0} is not open, attempting to open...", _portName);
97110
_port.Open();
98111
await Task.Delay(750); // Arduino resets on connection. Give it time to start up.
112+
Log.WriteLine("SERIAL: Port is open, sending initial [:I#] command..");
99113
_port.Write(":I#");
100114
return true;
101115
}
102-
catch
116+
catch (Exception ex)
103117
{
118+
Log.WriteLine("SERIAL: Failed to open the port. {0}", ex.Message);
104119
return false;
105120
}
106121
}
@@ -111,11 +126,13 @@ public void Disconnect()
111126
{
112127
if (_port.IsOpen)
113128
{
129+
Log.WriteLine("SERIAL: Port is open, sending shutdown command [:Qq#]");
114130
_port.Write(":Qq#");
131+
Log.WriteLine("SERIAL: Closing port...");
115132
_port.Close();
116133
_port = null;
134+
Log.WriteLine("SERIAL: Disconnected...");
117135
}
118136
}
119-
120137
}
121138
}

0 commit comments

Comments
 (0)