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 FileDataBlock : IDataBlock, IDisposable { /// /// 建構式 /// /// 設備範圍介面 /// 欄位索引 public FileDataBlock(ITypeDeviceRange deviceRange, int columnIndex) { DeviceRange = deviceRange; _rawData = new byte[deviceRange.ByteArrayLength]; ColumnIndex = columnIndex; } /// /// 原始資料 /// private byte[] _rawData = new byte[1]; /// /// 原始資料物件鎖定 /// private readonly ReaderWriterLockSlim _rwLock = new ReaderWriterLockSlim(); /// /// 設備範圍介面 /// public ITypeDeviceRange DeviceRange { get; } /// /// 欄位索引 /// public int ColumnIndex { get; } /// /// 設置原始資料 /// /// 原始資料 public void SetRawData(byte[] newRawData) { try { _rwLock.EnterWriteLock(); _rawData = newRawData; } catch (Exception ex) { Debug.WriteLine($"{ex}"); } finally { _rwLock.ExitWriteLock(); } } /// /// 取得原始資料 /// /// 原始資料 public byte[] GetRawData() { byte[] data = new byte[DeviceRange.ByteArrayLength]; try { _rwLock.EnterReadLock(); if (_rawData != null) Array.Copy(_rawData, data, Math.Min(_rawData.Length, data.Length)); } catch (Exception ex) { Debug.WriteLine($"{ex}"); } 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)) { if (_rawData != null && _rawData.Length > offset) { 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)) { if (_rawData != null && _rawData.Length > offset + 1) { 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)) { if (_rawData != null && _rawData.Length > 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)) { if (_rawData != null && _rawData.Length > offset + 1) { 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)) { if (_rawData != null && _rawData.Length > offset + length) { 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)) { if (_rawData != null && _rawData.Length > offset + data.Length) { 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; } } /// /// 解構式 /// ~FileDataBlock() { Dispose(false); } /// /// 釋放資源 /// public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } #endregion } }