123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- using System;
- using System.Xml;
- using Aitex.Core.Common.DeviceData;
- using Aitex.Core.RT.DataCenter;
- using Aitex.Core.RT.Event;
- using Aitex.Core.RT.IOCore;
- using Aitex.Core.RT.Log;
- using Aitex.Core.RT.OperationCenter;
- using Aitex.Core.RT.SCCore;
- using Aitex.Core.Util;
- using MECF.Framework.Common.Event;
- namespace Aitex.Core.RT.Device.Unit
- {
- /// <summary>
- /// 支持Alarm机制
- ///
- /// DI open
- /// DI close
- ///
- /// DO open
- /// DO close
- ///
- /// </summary>
- ///
- public class IoLift2 : BaseDevice, IDevice
- {
- public LiftStateEnum State
- {
- get
- {
- if (_diUp.Value && _diDown.Value)
- return LiftStateEnum.Error;
- if (_diUp.Value && !_diDown.Value)
- return LiftStateEnum.Up;
- if (!_diUp.Value && _diDown.Value)
- return LiftStateEnum.Down;
- return LiftStateEnum.Unknown;
- }
- }
- enum DeviceState
- {
- Idle,
- MovingUp,
- MovingDown,
- Error,
- }
- private DIAccessor _diUp;
- private DIAccessor _diDown;
-
- private DOAccessor _doUp;
- private DOAccessor _doDown;
- private DeviceState _state = DeviceState.Idle;
- private DeviceTimer _timer = new DeviceTimer();
- private SCConfigItem _scTimeout;
- public bool IsUp { get { return !_diDown.Value && _diUp.Value; } }
- public bool IsDown { get { return _diDown.Value && !_diUp.Value; } }
- public AlarmEventItem AlarmMoveTimeout { get; set; }
- public AlarmEventItem AlarmSignalAbnormal { get; set; }
- private object _lockerState = new object();
- public IoLift2(string module, XmlElement node, string ioModule = "")
- {
- var attrModule = node.GetAttribute("module");
- base.Module = string.IsNullOrEmpty(attrModule) ? module : attrModule;
- base.Name = node.GetAttribute("id");
- base.Display = node.GetAttribute("display");
- base.DeviceID = node.GetAttribute("schematicId");
- string scBasePath = node.GetAttribute("scBasePath");
- if (string.IsNullOrEmpty(scBasePath))
- scBasePath = $"{Module}.{Name}";
- else
- {
- scBasePath = scBasePath.Replace("{module}", Module);
- }
- _diUp = ParseDiNode("diUp", node, ioModule);
- _diDown = ParseDiNode("diDown", node, ioModule);
-
- _doUp = ParseDoNode("doUp", node, ioModule);
- _doDown = ParseDoNode("doDown", node, ioModule);
- _scTimeout = ParseScNode("scTimeout", node, ioModule, $"{scBasePath}.{Name}.MoveTimeout");
- }
- public bool Initialize()
- {
- _state = DeviceState.Idle;
- AlarmMoveTimeout = SubscribeAlarm($"{Module}.{Name}.MoveTimeout", "", null);
- AlarmSignalAbnormal = SubscribeAlarm($"{Module}.{Name}.SignalAbnormal", $"{Name} Up&Down signal trigger at same time", ResetCheckSignalAbnormal);
- DATA.Subscribe($"{Module}.{Name}.State", () => State.ToString());
- DATA.Subscribe($"{Module}.{Name}.UpFeedback", ()=> _diUp.Value && !_diDown.Value);
- DATA.Subscribe($"{Module}.{Name}.DownFeedback", () => _diDown.Value && !_diUp.Value);
-
- OP.Subscribe($"{Module}.{Name}.MoveUp", (out string reason, int time, object[] param) =>
- {
- reason = "";
- return MoveUp(out reason);
- });
- OP.Subscribe($"{Module}.{Name}.MoveDown", (out string reason, int time, object[] param) =>
- {
- reason = "";
- return MoveDown(out reason);
- });
- return true;
- }
- public void Monitor()
- {
- lock (_lockerState)
- {
- switch (_state)
- {
- case DeviceState.MovingUp:
- if (IsUp)
- {
- _state = DeviceState.Idle;
- }
- else if (_timer.IsTimeout())
- {
- if (!_doUp.SetValue(false, out string reason))
- {
- LOG.Error($"{Module} reset DO failed, {reason}");
- }
- AlarmMoveTimeout.Description = $"Can not move {Name} up in {_scTimeout.IntValue} seconds";
- AlarmMoveTimeout.Set();
- _state = DeviceState.Error;
- }
- break;
- case DeviceState.MovingDown:
- if (IsDown)
- {
- _state = DeviceState.Idle;
- }
- else if (_timer.IsTimeout())
- {
- if (!_doUp.SetValue(true, out string reason))
- {
- LOG.Error($"{Module} reset DO failed, {reason}");
- }
- AlarmMoveTimeout.Description = $"Can not move {Name} down in {_scTimeout.IntValue} seconds";
- AlarmMoveTimeout.Set();
- _state = DeviceState.Error;
- }
- break;
- default:
- break;
- }
- }
- if (_diUp.Value && _diDown.Value)
- {
- AlarmSignalAbnormal.Set();
- }
- }
- public void Terminate()
- {
- }
- public bool Move(bool up, out string reason)
- {
- if (up)
- return MoveUp(out reason);
- return MoveDown(out reason);
- }
- public bool MoveUp(out string reason)
- {
- lock (_lockerState)
- {
- if (!_doDown.SetValue(false, out reason))
- {
- return false;
- }
- if (!_doUp.SetValue(true, out reason))
- {
- return false;
- }
- _timer.Start(_scTimeout.IntValue * 1000);
- _state = DeviceState.MovingUp;
- }
- return true;
- }
- public bool MoveDown(out string reason)
- {
- lock (_lockerState)
- {
- if (!_doUp.SetValue(false, out reason))
- {
- return false;
- }
- if (!_doDown.SetValue(true, out reason))
- {
- return false;
- }
- _timer.Start(_scTimeout.IntValue * 1000);
- _state = DeviceState.MovingDown;
- }
- return true;
- }
- public bool ResetCheckSignalAbnormal()
- {
- return !(_diUp.Value && _diDown.Value);
- }
- public void Reset()
- {
- AlarmMoveTimeout.Reset();
- AlarmSignalAbnormal.Reset();
- }
- }
- }
|