Untitled
unknown
csharp
a year ago
2.2 kB
1
Indexable
Never
using Boltun.PLC.Enums; using DATABUILDERAXLibLB; using System.Collections.Generic; namespace Keyence.PLC.Models { public class SimpleMonitorTrigger { public delegate void Triggered(DBPlcDevice type, string number); public event Triggered OnTriggered; private readonly DBTriggerManager _triggerMgr; private readonly DBCommManager _commMgr; private Dictionary<MonitorIndex, MonitorSite> _monitors { get; set; } public SimpleMonitorTrigger(DBCommManager commMgr) { _triggerMgr = new DBTriggerManagerClass(); _triggerMgr.Fire += PlcTrigger; _commMgr = commMgr; _monitors = new Dictionary<MonitorIndex, MonitorSite>(); } public void SetMonitor<TReg>(TReg reg, string number, MonitorRegMode mode) { var index = MonitorIndex.Construct(reg, number); lock (_monitors) { if (_monitors.TryGetValue(index, out var site)) { // exist, check monitor mode if (site.MonitorMode != mode) { // if it's different, change it. _triggerMgr.Triggers.Remove(site.Trigger); } else { // skip, if same return; } } // add trigger var trigger = _triggerMgr.Triggers.Add(DBAxTriggerType.ttDevice); var triggerDevice = trigger.AsDevice; triggerDevice.DeviceCondition = (int)mode; triggerDevice.DeviceKind = index.Type; triggerDevice.DeviceNo = number; triggerDevice.CommManager = _commMgr; _monitors[index] = new MonitorSite(trigger, mode); } } public void SwitchActive(bool active) { _triggerMgr.Active = active; } private void PlcTrigger(DBTrigger pTrigger) { var device = pTrigger.AsDevice; OnTriggered?.Invoke(device.DeviceKind, device.DeviceNo); } } }