using ACS.SPiiPlusNET; using Aurora.Api; using AuroraACS; using AuroraDeviceCommon.Utils; using Hermes; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; namespace AuroraIO.Vendor.ACS { public class ACSIO : IOUnit { private readonly ACSDevice _core; private readonly IOUnitConfig _config; private CancellationTokenSource _cts; private int _failCount = 0; private object _LockInputObject = new object(); private object _LockOutputObject = new object(); public List InputBuffer { get; set; } public List OutputBuffer { get; set; } public ACSIO(Device dev, IOUnitConfig config) { _core = dev as ACSDevice; _config = config; InputBuffer = Enumerable.Repeat(false, _config.InputBoardCount * _config.BoardPortCount).ToList(); OutputBuffer = Enumerable.Repeat(false, _config.OutputBoardCount * _config.BoardPortCount).ToList(); } public override void ClearList() { } public override void Close() { } public override int GetAICount() { return 0; } public override int GetAOCount() { return 0; } public override int GetDoArray(ref bool[] array) { try { if (!_core.IsActivated) return E2.ABORTED; string OutputIOName = _config.OutputIOName; var boardOutput = (int[])_core.ReadVariable(OutputIOName, ProgramBuffer.ACSC_NONE, 0, _config.OutputBoardCount - 1); lock (_LockOutputObject) { for (int i = 0; i < ListDO.Count; i++) OutputBuffer[i] = (boardOutput[ListDO[i].Index / _config.BoardPortCount] & 0x00000001 << (ListDO[i].Index % _config.BoardPortCount)) > 0; array = OutputBuffer.ToArray(); return E1.OK; } } catch (ACSException ex) { Log.I(ex.Message); throw; } return 0; } public override int GetDICount() { return ListDI.Count; } public override int GetDiArray(ref bool[] array) { if (_config.IsDeviceSimul) return E1.OK; try { string InputIOName = _config.InputIOName; var boardInput = (int[])_core.ReadVariable(InputIOName, ProgramBuffer.ACSC_NONE, 0, _config.InputBoardCount - 1); lock (_LockInputObject) { for (int i = 0; i < ListDI.Count; i++) { InputBuffer[i] = (boardInput[ListDI[i].Index / _config.BoardPortCount] & 0x00000001 << (ListDI[i].Index % _config.BoardPortCount)) > 0; } array = InputBuffer.ToArray(); return E1.OK; } } catch (ACSException ex) { Log.I(ex.Message); return E2.ABORTED; } } public override int GetDOCount() { return ListDO.Count; } public override int Initialize() { return 0; } public override void Instance(string mappingFolder) { } public override bool IsConnected() { return _core.IsActivated; } public override int Open(string iP) { //InputBuffer = Enumerable.Repeat(false, _config.InputBoardCount * _config.BoardPortCount).ToList(); //OutputBuffer = Enumerable.Repeat(false, _config.OutputBoardCount * _config.BoardPortCount).ToList(); return _core.IsActivated ? E1.OK : E1.NETWORK; } public override int ReadDiBit(int index, ref bool bit) { bit = InputBuffer[index]; return E1.OK; int ReturnValue; int ArrayIndex = index / _config.BoardPortCount; int BitIndex = index % _config.BoardPortCount; string InputIOName = _config.InputIOName; ReturnValue = (int)_core?.ReadVariable(InputIOName, ProgramBuffer.ACSC_NONE, ArrayIndex, ArrayIndex); bit = (ReturnValue & 0x00000001 << (BitIndex)) > 0; return E1.OK; } public override bool ReadDiBit(int index) { return InputBuffer[index]; int ReturnValue; int ArrayIndex = index / _config.BoardPortCount; int BitIndex = index % _config.BoardPortCount; string InputIOName = _config.InputIOName; try { ReturnValue = (int)_core?.ReadVariable(InputIOName, ProgramBuffer.ACSC_NONE, ArrayIndex, ArrayIndex); return (ReturnValue & 0x00000001 << (BitIndex)) > 0; } catch (Exception) { return false; } } public override int ReadDoBit(int index, ref bool bit) { if (_config.IsDeviceSimul){ bit = false; return E1.OK; } bit = OutputBuffer[index]; return E1.OK; int ReturnValue; int ArrayIndex = index / _config.BoardPortCount; int BitIndex = index % _config.BoardPortCount; string OutputIOName = _config.OutputIOName; try { ReturnValue = (int)_core?.ReadVariable(OutputIOName, ProgramBuffer.ACSC_NONE, ArrayIndex, ArrayIndex); bit = (ReturnValue & 0x00000001 << (BitIndex)) > 0; } catch (Exception) { return E2.UNKNOWN; } return E1.OK; } public override bool ReadDoBit(int index) { if(_config.IsDeviceSimul) return false; return OutputBuffer[index]; int ReturnValue; int ArrayIndex = index / _config.BoardPortCount; int BitIndex = index % _config.BoardPortCount; string OutputIOName = _config.InputIOName; try { ReturnValue = (int)_core?.ReadVariable(OutputIOName, ProgramBuffer.ACSC_NONE, ArrayIndex, ArrayIndex); var ret = (ReturnValue & 0x00000001 << (BitIndex)) > 0; return ret; } catch (Exception) { return false; } } public override int WriteDoBit(int index, bool bit) { if (_config.IsDeviceSimul) { return E1.OK; } int arrayIndex = index / _config.BoardPortCount; int bitIndex = index % _config.BoardPortCount; string outputIOName = _config.OutputIOName; try { int value = (int)_core?.ReadVariable(outputIOName, ProgramBuffer.ACSC_NONE, arrayIndex, arrayIndex); if (bit) value |= (1 << bitIndex); // 해당 비트를 1로 설정 else value &= ~(1 << bitIndex); // 해당 비트를 0으로 설정 if (!_core.WriteVariable(outputIOName, value, ProgramBuffer.ACSC_NONE, arrayIndex)) return E1.GENERIC; return E1.OK; } catch (ACSException ex) { Log.W(ex.Message); return E1.GENERIC; } } } }