using Mirle.Component.MPLC.DataBlocks;
using Mirle.Component.MPLC.Interfaces;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace Mirle.Component.MPLC.FileData
{
///
///
///
public class FileDataViewer : IMPLCProvider
{
///
/// 建構式
///
/// 檔案讀取器
public FileDataViewer(FileReader fileReader)
{
_fileReader = fileReader;
foreach (FileDataBlock fileReaderDataBlock in _fileReader.GetDataBlocks())
{
_dataBlocks.Add(new FileDataBlock(fileReaderDataBlock.DeviceRange, fileReaderDataBlock.ColumnIndex));
}
}
///
/// 檔案讀取器
///
private readonly FileReader _fileReader;
///
/// 資料區塊
///
private readonly List _dataBlocks = new List();
///
/// 是否連線
///
public bool IsConnected => true;
///
///
///
///
///
///
public IEnumerable Query(DateTime begin, DateTime end)
{
return _fileReader.GetDateTimeIndexes().Where(t => t.TimeOfDay >= begin.TimeOfDay && t.TimeOfDay <= end.TimeOfDay).OrderBy(i => i);
}
///
///
///
///
public void RefreshRawData(DateTime index)
{
foreach (FileDataBlock block in _dataBlocks)
{
try
{
byte[] newByteArray = _fileReader.GetRawDataByDateTimeIndex(index, block.ColumnIndex);
if (newByteArray != null)
block.SetRawData(newByteArray);
}
catch (Exception ex)
{
Debug.WriteLine($"{ex.Message}-{ex.StackTrace}");
}
}
}
///
/// 取得位元
///
/// 位置
/// True/False
public bool GetBit(string address)
{
foreach (var block in _dataBlocks)
{
if (block.TryGetBit(address, out bool value))
return value;
}
return false;
}
///
/// 設置位元開啟
///
/// 位置
public void SetBitOn(string address)
{
return;
}
///
/// 設置位元關閉
///
/// 位置
public void SetBitOff(string address)
{
return;
}
///
/// 讀取字元
///
/// 位置
/// 字元
public int ReadWord(string address)
{
foreach (var block in _dataBlocks)
{
if (block.TryGetWord(address, out int value))
return value;
}
return 0;
}
///
/// 寫入字元
///
/// 位置
/// 字元
public void WriteWord(string address, int data)
{
return;
}
///
/// 讀取多個字元
///
/// 起始位置
/// 長度
/// 字元陣列
public int[] ReadWords(string startAddress, int length)
{
foreach (var block in _dataBlocks)
{
if (block.TryGetWords(startAddress, out int[] data, length))
return data;
}
return new int[length];
}
///
/// 寫入多個字元
///
/// 起始位置
/// 字元陣列
public void WriteWords(string startAddress, int[] data)
{
return;
}
}
}