omni changes
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
using System;
|
||||
using OmniPlus.Connection.EventHandlers;
|
||||
|
||||
using System;
|
||||
|
||||
namespace OmniPlus.Connection.Contracts
|
||||
{
|
||||
@@ -9,6 +11,6 @@ namespace OmniPlus.Connection.Contracts
|
||||
|
||||
void OpenConnection();
|
||||
|
||||
void Write(byte[] data);
|
||||
void Send(IRequest request);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
namespace OmniPlus.Connection.Contracts
|
||||
{
|
||||
internal interface IData
|
||||
{
|
||||
byte[] Data { get; }
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
using System.IO.Ports;
|
||||
using OmniPlus.Connection.Enums;
|
||||
|
||||
using System.IO.Ports;
|
||||
|
||||
namespace OmniPlus.Connection.Contracts
|
||||
{
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace OmniPlus.Connection.Contracts
|
||||
{
|
||||
internal interface IRequest
|
||||
{
|
||||
byte[] ToByteArray();
|
||||
}
|
||||
}
|
||||
+3
-1
@@ -1,7 +1,9 @@
|
||||
namespace OmniPlus.Connection.Contracts
|
||||
{
|
||||
internal interface IDataSettings : IData
|
||||
internal interface IResponse
|
||||
{
|
||||
byte[] Bytes { get; }
|
||||
|
||||
bool ReadByte(byte data);
|
||||
|
||||
void StartReading();
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
namespace OmniPlus.Connection
|
||||
namespace OmniPlus.Connection.Enums
|
||||
{
|
||||
public enum BaudRate : int
|
||||
{
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
namespace OmniPlus.Connection
|
||||
namespace OmniPlus.Connection.Enums
|
||||
{
|
||||
public enum DataBits : int
|
||||
{
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace OmniPlus.Connection
|
||||
namespace OmniPlus.Connection.Enums
|
||||
{
|
||||
internal enum SPState
|
||||
{
|
||||
@@ -0,0 +1,6 @@
|
||||
using OmniPlus.Connection.Contracts;
|
||||
|
||||
namespace OmniPlus.Connection.EventHandlers
|
||||
{
|
||||
internal delegate void ReadEventHandler(IResponse data);
|
||||
}
|
||||
+3
-1
@@ -1,4 +1,6 @@
|
||||
namespace OmniPlus.Connection
|
||||
using OmniPlus.Connection.Enums;
|
||||
|
||||
namespace OmniPlus.Connection.EventHandlers
|
||||
{
|
||||
internal delegate void StateEventHandler(SPState state, string message);
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
using OmniPlus.Connection.Contracts;
|
||||
|
||||
namespace OmniPlus.Connection
|
||||
{
|
||||
internal delegate void ReadEventHandler(IData data);
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
using OmniPlus.Connection.Contracts;
|
||||
using OmniPlus.Connection.Enums;
|
||||
using OmniPlus.Connection.EventHandlers;
|
||||
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
@@ -10,18 +12,18 @@ namespace OmniPlus.Connection
|
||||
{
|
||||
private readonly string portName;
|
||||
private readonly IPortSettings portSettings;
|
||||
private readonly IDataSettings dataSettings;
|
||||
private readonly IResponse response;
|
||||
|
||||
private event Action bytesReceived;
|
||||
private ConcurrentQueue<byte> data;
|
||||
private SerialPort serialPort;
|
||||
private bool parsingData;
|
||||
|
||||
public SPConnection(string portName, IPortSettings portSettings, IDataSettings dataSettings)
|
||||
public SPConnection(string portName, IPortSettings portSettings, IResponse dataSettings)
|
||||
{
|
||||
this.portName = portName;
|
||||
this.portSettings = portSettings;
|
||||
this.dataSettings = dataSettings;
|
||||
this.response = dataSettings;
|
||||
this.bytesReceived += SPConnection_BytesReceived;
|
||||
}
|
||||
|
||||
@@ -44,17 +46,6 @@ namespace OmniPlus.Connection
|
||||
this.CloseConnection();
|
||||
}
|
||||
|
||||
public void Write(byte[] data)
|
||||
{
|
||||
if (data?.Length > 0 && this.serialPort.IsOpen)
|
||||
{
|
||||
lock (this.serialPort)
|
||||
{
|
||||
this.serialPort.Write(data, 0, data.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void OpenConnection()
|
||||
{
|
||||
this.CloseConnection();
|
||||
@@ -101,6 +92,19 @@ namespace OmniPlus.Connection
|
||||
}
|
||||
}
|
||||
|
||||
public void Send(IRequest request)
|
||||
{
|
||||
var data = request.ToByteArray();
|
||||
|
||||
if (data?.Length > 0 && this.serialPort.IsOpen)
|
||||
{
|
||||
lock (this.serialPort)
|
||||
{
|
||||
this.serialPort.Write(data, 0, data.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void CloseConnection()
|
||||
{
|
||||
if (this.serialPort != null)
|
||||
@@ -123,14 +127,14 @@ namespace OmniPlus.Connection
|
||||
{
|
||||
this.parsingData = true;
|
||||
|
||||
this.dataSettings.StartReading();
|
||||
this.response.StartReading();
|
||||
|
||||
while (this.parsingData)
|
||||
{
|
||||
this.parsingData = this.data.TryDequeue(out byte current) && this.dataSettings.ReadByte(current);
|
||||
this.parsingData = this.data.TryDequeue(out byte current) && this.response.ReadByte(current);
|
||||
}
|
||||
|
||||
this.DataReceived?.Invoke(this.dataSettings);
|
||||
this.DataReceived?.Invoke(this.response);
|
||||
}
|
||||
|
||||
private void SerialPort_DataReceived(Object sender, SerialDataReceivedEventArgs args)
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
using OmniPlus.Connection.Contracts;
|
||||
|
||||
namespace OmniPlus.Connection
|
||||
{
|
||||
internal abstract class SPRequest : IRequest
|
||||
{
|
||||
public abstract byte[] ToByteArray();
|
||||
}
|
||||
}
|
||||
@@ -1,22 +1,24 @@
|
||||
using OmniPlus.Connection.Contracts;
|
||||
|
||||
namespace OmniPlus
|
||||
namespace OmniPlus.Connection
|
||||
{
|
||||
class OmniDataSettings : IDataSettings
|
||||
internal abstract class SPResponse : IResponse
|
||||
{
|
||||
private readonly byte[][] template;
|
||||
private byte[][] messageTemplate;
|
||||
private byte[] messageContent;
|
||||
private readonly byte[][] messageTemplate;
|
||||
private int rowLength;
|
||||
private int row;
|
||||
private int col;
|
||||
private bool ok;
|
||||
private bool reading;
|
||||
|
||||
public OmniDataSettings(byte[][] template)
|
||||
=> this.template = template;
|
||||
public SPResponse(byte[][] template)
|
||||
{
|
||||
this.template = template;
|
||||
this.messageTemplate = template;
|
||||
}
|
||||
|
||||
public byte[] Data => this.messageContent;
|
||||
public byte[] Bytes { get; private set; }
|
||||
|
||||
public bool ReadByte(byte data)
|
||||
{
|
||||
@@ -30,7 +32,7 @@ namespace OmniPlus
|
||||
|
||||
if (this.ok)
|
||||
{
|
||||
this.messageContent[this.col] = data;
|
||||
this.Bytes[this.col] = data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,12 +43,12 @@ namespace OmniPlus
|
||||
{
|
||||
this.template.CopyTo(this.messageTemplate, 0);
|
||||
|
||||
this.messageContent = new byte[8];
|
||||
this.Bytes = new byte[8];
|
||||
this.row = 0;
|
||||
this.rowLength = this.messageTemplate[this.row][0];
|
||||
this.col = -1;
|
||||
this.reading = false;
|
||||
this.ok = this.rowLength > 0 && this.col < this.messageContent.Length;
|
||||
this.ok = this.rowLength > 0 && this.col < this.Bytes.Length;
|
||||
}
|
||||
|
||||
private bool PrepareReading()
|
||||
@@ -65,6 +67,7 @@ namespace OmniPlus
|
||||
return false;
|
||||
}
|
||||
|
||||
var bytesLength = this.Bytes.Length;
|
||||
var nextLength = this.messageTemplate[this.row][0];
|
||||
|
||||
if (nextLength == 0)
|
||||
@@ -76,16 +79,16 @@ namespace OmniPlus
|
||||
this.messageTemplate[i][1] += nextLength;
|
||||
}
|
||||
|
||||
var messageContent = new byte[this.messageContent.Length + nextLength];
|
||||
var messageContent = new byte[bytesLength + nextLength];
|
||||
|
||||
this.messageContent.CopyTo(messageContent, 0);
|
||||
this.Bytes.CopyTo(messageContent, 0);
|
||||
|
||||
this.messageContent = messageContent;
|
||||
this.Bytes = messageContent;
|
||||
}
|
||||
|
||||
this.rowLength += nextLength;
|
||||
|
||||
return this.row < this.messageTemplate.Length && this.col < this.messageContent.Length;
|
||||
return this.row < this.messageTemplate.Length && this.col < bytesLength;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using OmniPlus.Connection;
|
||||
using OmniPlus.Connection.Contracts;
|
||||
using OmniPlus.Connection.EventHandlers;
|
||||
|
||||
using System;
|
||||
|
||||
@@ -12,6 +13,6 @@ namespace OmniPlus.Contracts
|
||||
|
||||
void OpenConnection();
|
||||
|
||||
byte[] Send(byte[] request);
|
||||
IResponse Send(IRequest request);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
using System;
|
||||
|
||||
namespace OmniPlus.Contracts
|
||||
{
|
||||
public interface IOmniRegister : IDisposable
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using OmniPlus.Connection.Contracts;
|
||||
|
||||
namespace OmniPlus.Contracts
|
||||
{
|
||||
internal interface IOmniRequest : IRequest
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using OmniPlus.Connection.Contracts;
|
||||
|
||||
namespace OmniPlus.Contracts
|
||||
{
|
||||
internal interface IOmniResponse : IResponse
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
using OmniPlus.Connection;
|
||||
using OmniPlus.Connection.Contracts;
|
||||
using OmniPlus.Connection.EventHandlers;
|
||||
using OmniPlus.Contracts;
|
||||
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace OmniPlus
|
||||
@@ -13,7 +13,7 @@ namespace OmniPlus
|
||||
private readonly int timeoutSeconds;
|
||||
private readonly int sleeMilliseconds;
|
||||
|
||||
private byte[] data;
|
||||
private IResponse response;
|
||||
private bool waiting;
|
||||
|
||||
public OmniConnection(string portName, int timeoutSeconds = 3000, int sleeMilliseconds = 300)
|
||||
@@ -21,16 +21,7 @@ namespace OmniPlus
|
||||
this.connection = new SPConnection(
|
||||
portName: portName,
|
||||
portSettings: new OmniPortSettings(),
|
||||
dataSettings: new OmniDataSettings(new byte[][]
|
||||
{
|
||||
// length, length index
|
||||
new byte[] { 1, 0 }, // Start byte
|
||||
new byte[] { 3, 0 }, // Control bytes
|
||||
new byte[] { 1, 0 }, // Data length byte
|
||||
new byte[] { 0, 4 }, // Data bytes
|
||||
new byte[] { 1, 0 }, // Alignment byte
|
||||
new byte[] { 2, 0 } // CRC 16 bytes
|
||||
}));
|
||||
dataSettings: new OmniResponse());
|
||||
this.timeoutSeconds = timeoutSeconds;
|
||||
this.sleeMilliseconds = sleeMilliseconds;
|
||||
}
|
||||
@@ -50,13 +41,13 @@ namespace OmniPlus
|
||||
public void OpenConnection()
|
||||
=> this.connection.OpenConnection();
|
||||
|
||||
public byte[] Send(byte[] request)
|
||||
public IResponse Send(IRequest request)
|
||||
{
|
||||
this.waiting = true;
|
||||
|
||||
this.connection.DataReceived += this.Connection_DataReceived;
|
||||
|
||||
this.connection.Write(request);
|
||||
this.connection.Send(request);
|
||||
|
||||
var timeoutSeconds = this.timeoutSeconds;
|
||||
|
||||
@@ -69,13 +60,13 @@ namespace OmniPlus
|
||||
|
||||
this.connection.DataReceived -= this.Connection_DataReceived;
|
||||
|
||||
return this.data ?? Array.Empty<byte>();
|
||||
return this.response ?? default(IResponse);
|
||||
}
|
||||
|
||||
private void Connection_DataReceived(IData data)
|
||||
private void Connection_DataReceived(IResponse response)
|
||||
{
|
||||
this.response = response;
|
||||
this.waiting = false;
|
||||
this.data = data.Data;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,25 +44,32 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Connection\Contracts\IConnection.cs" />
|
||||
<Compile Include="Connection\Contracts\IData.cs" />
|
||||
<Compile Include="Connection\Contracts\IDataSettings.cs" />
|
||||
<Compile Include="Connection\Contracts\IRequest.cs" />
|
||||
<Compile Include="Connection\Contracts\IResponse.cs" />
|
||||
<Compile Include="Connection\Contracts\IPortSettings.cs" />
|
||||
<Compile Include="Connection\ReadEventHandler.cs" />
|
||||
<Compile Include="Connection\EventHandlers\ReadEventHandler.cs" />
|
||||
<Compile Include="Connection\SPConnection.cs" />
|
||||
<Compile Include="Connection\BaudRate.cs" />
|
||||
<Compile Include="Connection\Enums\BaudRate.cs" />
|
||||
<Compile Include="Connection\SPRequest.cs" />
|
||||
<Compile Include="Connection\SPResponse.cs" />
|
||||
<Compile Include="Contracts\IOmniConnection.cs" />
|
||||
<Compile Include="Connection\DataBits.cs" />
|
||||
<Compile Include="Connection\StateEventHandler.cs" />
|
||||
<Compile Include="OmniDataSettings.cs" />
|
||||
<Compile Include="Connection\Enums\DataBits.cs" />
|
||||
<Compile Include="Connection\EventHandlers\StateEventHandler.cs" />
|
||||
<Compile Include="Contracts\IOmniRegister.cs" />
|
||||
<Compile Include="Contracts\IOmniRequest.cs" />
|
||||
<Compile Include="Contracts\IOmniResponse.cs" />
|
||||
<Compile Include="OmniRequest.cs" />
|
||||
<Compile Include="OmniResponse.cs" />
|
||||
<Compile Include="Messages\AggregateSystemParameters.cs" />
|
||||
<Compile Include="Messages\FixedMessages.cs" />
|
||||
<Compile Include="Messages\SetInputVolume.cs" />
|
||||
<Compile Include="OmniConnection.cs" />
|
||||
<Compile Include="OmniPortSettings.cs" />
|
||||
<Compile Include="OmniRegister.cs" />
|
||||
<Compile Include="OmniTemplates.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Connection\SPState.cs" />
|
||||
<Compile Include="Connection\Enums\SPState.cs" />
|
||||
<Compile Include="Types\ASCIIString.cs" />
|
||||
<Compile Include="Types\Battery.cs" />
|
||||
<Compile Include="Types\Bytes.cs" />
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
using OmniPlus.Connection;
|
||||
using OmniPlus.Connection.Contracts;
|
||||
using OmniPlus.Connection.Contracts;
|
||||
using OmniPlus.Connection.Enums;
|
||||
|
||||
using System.IO.Ports;
|
||||
|
||||
|
||||
@@ -1,38 +1,53 @@
|
||||
using OmniPlus.Connection;
|
||||
using OmniPlus.Connection.Contracts;
|
||||
using OmniPlus.Connection.Enums;
|
||||
using OmniPlus.Contracts;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace OmniPlus
|
||||
{
|
||||
public class OmniRegister
|
||||
public class OmniRegister : IOmniRegister
|
||||
{
|
||||
private readonly string loggingFile;
|
||||
private readonly IOmniConnection omniConnection;
|
||||
private readonly ICollection<IRequest> requests;
|
||||
|
||||
private bool connectionAvailable;
|
||||
|
||||
public OmniRegister(string portName)
|
||||
{
|
||||
this.loggingFile = this.GetOrCreateLoggingFile(portName);
|
||||
this.requests = new List<IRequest>();
|
||||
this.omniConnection = new OmniConnection(portName);
|
||||
this.omniConnection.StateChanged += OmniConnection_StateChanged;
|
||||
this.omniConnection.OpenConnection();
|
||||
this.omniConnection.StateChanged += this.OmniConnection_StateChanged;
|
||||
}
|
||||
|
||||
public bool EnsureRegisterInitialized()
|
||||
public void Dispose()
|
||||
{
|
||||
return true;
|
||||
this.omniConnection.StateChanged -= this.OmniConnection_StateChanged;
|
||||
|
||||
this.omniConnection.Dispose();
|
||||
}
|
||||
|
||||
public bool EnsureRegisterSealed()
|
||||
private string GetOrCreateLoggingFile(string portName)
|
||||
{
|
||||
return true;
|
||||
var directory = Directory.CreateDirectory(portName);
|
||||
var fileName = $"{DateTime.Now:yyyy_MM_dd}.log";
|
||||
var loggingFile = Path.Combine(directory.FullName, fileName);
|
||||
|
||||
if (!File.Exists(loggingFile))
|
||||
{
|
||||
File.WriteAllText(loggingFile, string.Empty);
|
||||
}
|
||||
|
||||
return loggingFile;
|
||||
}
|
||||
|
||||
private void OmniConnection_StateChanged(SPState state, string message)
|
||||
{
|
||||
// TODO: handle, log - changes
|
||||
|
||||
Console.WriteLine($"[{state}] {message}");
|
||||
File.AppendAllText(this.loggingFile, $"[{DateTime.Now:hh:mm:ss}][{state}] {message}{Environment.NewLine}");
|
||||
|
||||
this.connectionAvailable = state == SPState.Opened;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
using OmniPlus.Connection;
|
||||
using OmniPlus.Contracts;
|
||||
|
||||
namespace OmniPlus
|
||||
{
|
||||
internal class OmniRequest : SPRequest, IOmniRequest
|
||||
{
|
||||
public override System.Byte[] ToByteArray()
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using OmniPlus.Connection;
|
||||
using OmniPlus.Contracts;
|
||||
|
||||
namespace OmniPlus
|
||||
{
|
||||
internal class OmniResponse : SPResponse, IOmniResponse
|
||||
{
|
||||
public OmniResponse() : base(OmniTemplates.IrdaResponse)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
namespace OmniPlus
|
||||
{
|
||||
internal class OmniTemplates
|
||||
{
|
||||
public static byte[][] IrdaResponse => new byte[][]
|
||||
{
|
||||
new byte[] { 1, 0x9B }, // Start byte
|
||||
new byte[] { 3, 0x00 }, // Control bytes
|
||||
new byte[] { 1, 0x00 }, // Data length byte
|
||||
new byte[] { 0, 0x04 }, // Data bytes
|
||||
new byte[] { 1, 0x00 }, // Alignment byte
|
||||
new byte[] { 2, 0x00 } // CRC 16 bytes
|
||||
};
|
||||
}
|
||||
}
|
||||
+20
-24
@@ -1,36 +1,32 @@
|
||||
using OmniPlus.Connection;
|
||||
|
||||
using OmniPlus.Contracts;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Text;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace OmniPlus
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static IOmniRegister omniRegister = new OmniRegister("COM1");
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
//var queue = new ConcurrentQueue<byte>();
|
||||
//var dataSettings = new OmniIrdaSettings();
|
||||
//var parsingData = true;
|
||||
|
||||
//dataSettings.StartReading();
|
||||
|
||||
//for (byte i = (byte)'a'; i < (byte)'a' + 26; i++)
|
||||
//{
|
||||
// queue.Enqueue(i);
|
||||
//}
|
||||
|
||||
//while (parsingData)
|
||||
//{
|
||||
// parsingData = queue.TryDequeue(out byte current) && dataSettings.ReadByte(current);
|
||||
//}
|
||||
|
||||
//parsingData = false;
|
||||
|
||||
//Console.WriteLine(Encoding.ASCII.GetString(dataSettings.Data));
|
||||
|
||||
//var test = new SPConnection(null, null, null);
|
||||
}
|
||||
}
|
||||
|
||||
public class Command
|
||||
{
|
||||
public Command(int index, int delay, params byte[] bytes)
|
||||
{
|
||||
this.Order = index;
|
||||
this.Delay = delay;
|
||||
this.Bytes = bytes;
|
||||
}
|
||||
|
||||
public int Order { get; }
|
||||
|
||||
public int Delay { get; }
|
||||
|
||||
public byte[] Bytes { get; }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user