using Mirle.Component.MPLC.DataBlocks.DeviceRange.Interfaces;
using Mirle.Component.MPLC.DataBlocks.Interfaces;
using System;
using System.Collections;
using System.Diagnostics;
using System.Threading;
namespace Mirle.Component.MPLC.DataBlocks
{
///
/// 資料區塊
///
public class DataBlock : IDataBlock, IDisposable
{
///
/// 建構式
///
/// 設備範圍介面
public DataBlock(ITypeDeviceRange deviceRange)
{
DeviceRange = deviceRange;
_rawData = new byte[deviceRange.ByteArrayLength];
}
///
/// 原始資料
///
private readonly byte[] _rawData = new byte[1];
///
/// 原始資料物件鎖定
///
private readonly ReaderWriterLockSlim _rwLock = new ReaderWriterLockSlim();
///
/// 設備範圍介面
///
public ITypeDeviceRange DeviceRange { get; }
///
/// 設置原始資料
///
/// 原始資料
public void SetRawData(byte[] newRawData)
{
try
{
_rwLock.EnterWriteLock();
Array.Resize(ref newRawData, _rawData.Length);
Array.Copy(newRawData, _rawData, _rawData.Length);
}
finally
{
_rwLock.ExitWriteLock();
}
}
///
/// 取得原始資料
///
/// 原始資料
public byte[] GetRawData()
{
byte[] data = new byte[_rawData.Length];
try
{
_rwLock.EnterReadLock();
Array.Copy(_rawData, data, _rawData.Length);
}
finally
{
_rwLock.ExitReadLock();
}
return data;
}
///
/// 取得位元
///
/// 位置
/// 是否成功取得
/// True/False
public bool TryGetBit(string address, out bool value)
{
value = false;
try
{
_rwLock.EnterReadLock();
if (DeviceRange.TryGetByteArrayOffset(address, out int offset) && DeviceRange.TryGetByteArrayBitIndex(address, out int index))
{
ushort word = BitConverter.ToUInt16(_rawData, offset);
BitArray bitArray = new BitArray(BitConverter.GetBytes(word));
value = bitArray.Get(index);
return true;
}
}
catch (Exception ex)
{
Debug.WriteLine($"{ex}");
}
finally
{
_rwLock.ExitReadLock();
}
return false;
}
///
/// 設置位元開啟
///
///
/// True/False
public bool TrySetBitOn(string address)
{
return SetBit(address, true);
}
///
/// 設置位元
///
/// 位元
/// 是否開啟
/// True/False
private bool SetBit(string address, bool IsOn)
{
try
{
_rwLock.EnterWriteLock();
if (DeviceRange.TryGetByteArrayOffset(address, out int offset) && DeviceRange.TryGetByteArrayBitIndex(address, out int index))
{
ushort word = BitConverter.ToUInt16(_rawData, offset);
BitArray bitArray = new BitArray(BitConverter.GetBytes(word));
bitArray.Set(index, IsOn);
byte[] tmpBytes = new byte[2];
bitArray.CopyTo(tmpBytes, 0);
_rawData[offset] = tmpBytes[0];
_rawData[offset + 1] = tmpBytes[1];
return true;
}
}
catch (Exception ex)
{
Debug.WriteLine($"{ex}");
}
finally
{
_rwLock.ExitWriteLock();
}
return false;
}
///
/// 設置位元關閉
///
/// 位置
/// True/False
public bool TrySetBitOff(string address)
{
return SetBit(address, false);
}
///
/// 取得字元
///
/// 位置
/// 值
/// True/False
public bool TryGetWord(string address, out int value)
{
value = 0;
try
{
_rwLock.EnterReadLock();
if (DeviceRange.TryGetByteArrayOffset(address, out int offset))
{
value = BitConverter.ToUInt16(_rawData, offset);
return true;
}
}
catch (Exception ex)
{
Debug.WriteLine($"{ex}");
}
finally
{
_rwLock.ExitReadLock();
}
return false;
}
///
/// 設置字元
///
/// 位置
/// 值
/// True/False
public bool TrySetWord(string address, int value)
{
try
{
_rwLock.EnterWriteLock();
if (DeviceRange.TryGetByteArrayOffset(address, out int offset))
{
byte[] tmpBytes = BitConverter.GetBytes(value);
_rawData[offset] = tmpBytes[0];
_rawData[offset + 1] = tmpBytes[1];
return true;
}
}
catch (Exception ex)
{
Debug.WriteLine($"{ex}");
}
finally
{
_rwLock.ExitWriteLock();
}
return false;
}
///
/// 取得多個字元
///
/// 位置
/// 資料集
/// 長度
/// True/False
public bool TryGetWords(string address, out int[] data, int length)
{
data = new int[length];
try
{
_rwLock.EnterReadLock();
if (DeviceRange.TryGetByteArrayOffset(address, out int offset))
{
for (int i = 0; i < length; i++)
{
data[i] = BitConverter.ToUInt16(_rawData, offset + i * 2);
}
return true;
}
}
catch (Exception ex)
{
Debug.WriteLine($"{ex}");
}
finally
{
_rwLock.ExitReadLock();
}
return false;
}
///
/// 設置多個字元
///
/// 位置
/// 資料集
/// True/False
public bool TrySetWords(string address, int[] data)
{
try
{
_rwLock.EnterWriteLock();
if (DeviceRange.TryGetByteArrayOffset(address, out int offset))
{
for (int i = 0; i < data.Length; i++)
{
byte[] tmpBytes = BitConverter.GetBytes(data[i]);
_rawData[offset + i * 2] = tmpBytes[0];
_rawData[offset + i * 2 + 1] = tmpBytes[1];
}
return true;
}
}
catch (Exception ex)
{
Debug.WriteLine($"{ex}");
}
finally
{
_rwLock.ExitWriteLock();
}
return false;
}
#region IDisposable Support
private bool disposedValue = false;
///
/// 釋放資源
///
///
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
_rwLock?.Dispose();
}
disposedValue = true;
}
}
///
/// 解構式
///
~DataBlock()
{
Dispose(false);
}
///
/// 釋放資源
///
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
}